Revision 1b054655 toolbox/rfid.c

View differences:

toolbox/rfid.c
16 16
  }
17 17
}
18 18

  
19
static void read_serno() {
20
  int i;
21
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
22
    serno[i] = serial_read_blocking();
23
  }
24
}
25

  
26
static char check_serno() {
27
  int i;
28
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
29
    if (serno[i] != serial_read_blocking()) {
30
      return 0;
31
    }
32
  }
33
  return 1;
34
}
35

  
36 19
void rfid_init() {
37 20
  serial_init();
38 21
}
39 22

  
40
void rfid_read_safe() {
41
  char n_err = 0, n_ok = 0;
42
  while (1) {
43
    serial_write(read_cmd, sizeof(read_cmd));
44
    if (serial_read_blocking() == RFID_OK) {
45
      if (n_ok == 0) {
46
        read_serno();
47
      } else if (!check_serno()) {
48
        zero_serno();
49
        return;
50
      }
51
      n_ok++;
52
      if (n_ok >= RFID_MIN_OK) {
53
        return;
54
      }
55
    } else {
56
      n_err++;
57
      if (n_err >= RFID_MAX_ERRS) {
58
        zero_serno();
59
        return;
60
      }
61
    }
62
    _delay_ms(10);
63
  }
64
}
65

  
66 23
void rfid_start_read() {
67 24
  serno_idx = -1;
68
  zero_serno(); // TODO temporary
69 25
  serial_flush();
70 26
  serial_write(read_cmd, sizeof(read_cmd));
71 27
}
......
97 53
void rfid_get_serno(uint8_t *buf) {
98 54
  memcpy(buf, serno, sizeof(serno));
99 55
}
100

  
101
char rfid_check_serno(uint8_t *buf) {
102
  return memcmp(buf, serno, sizeof(serno)) == 0;
103
}
104

  
105
char rfid_nonzero() {
106
  int i;
107
  for (i = 0; i < sizeof(serno); i++) {
108
    if (serno[i]) {
109
      return 1;
110
    }
111
  }
112
  return 0;
113
}

Also available in: Unified diff