5 typedef uint8_t byte_t;
7 static const char * hex = "0123456789ABCDEF";
12 if (c >= '0' && c <='9') return c - '0';
13 if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
14 if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
17 static const char * cardid(const byte_t uid[], byte_t len)
20 if (len > 10) return NULL;
23 *(p++) = hex[ len ]; //DEBUG
25 for (int i=0; i<len; i++) {
26 *(p++) = hex[ uid[i] >> 4 ];
27 *(p++) = hex[ uid[i] & 15 ];
33 const char * uid_print(const MFRC522::Uid uid)
35 return cardid(uid.uidByte, uid.size);
38 void uid_copy(MFRC522::Uid &a, const MFRC522::Uid b)
41 memcpy(a.uidByte, b.uidByte, b.size);
44 bool uid_compare(const MFRC522::Uid a, const MFRC522::Uid b)
46 if (a.size != b.size) return 0;
47 if (a.size == 0 || b.size == 0) return 0;
48 return memcmp(a.uidByte, b.uidByte, a.size)==0?true:false;
51 void uid_zero(MFRC522::Uid &uid)
56 /* is this card id permitted */
57 bool uid_auth(const MFRC522::Uid uid)
60 // return card_auth(cardid(uid.uidByte, uid.size));
64 bool uid_isset(const MFRC522::Uid uid)
69 bool uid_visible(MFRC522 &reader, const MFRC522::Uid master)
74 if (master.size == 0) return false;
77 byte bufferSize = sizeof(bufferATQA);
82 // no tags in range to wakeup
83 if (reader.PICC_WakeupA(bufferATQA, &bufferSize) == MFRC522::STATUS_TIMEOUT) {
89 uid_copy(uid, master);
90 if (reader.PICC_Select(&uid, uid.size * 8) == MFRC522::STATUS_OK) {
91 // something answered, but was it the right one
92 if (uid_compare(uid, master)) {
106 bool uid_fetch(MFRC522 &reader, MFRC522::Uid &uid, int count)
111 byte bufferSize = sizeof(bufferATQA);
115 status = reader.PICC_WakeupA(bufferATQA, &bufferSize);
117 status = reader.PICC_RequestA(bufferATQA, &bufferSize);
121 if (status != MFRC522::STATUS_OK && status != MFRC522::STATUS_COLLISION) {
122 if (status == MFRC522::STATUS_TIMEOUT) {
129 if ( (status = reader.PICC_Select(&uid, 0)) == MFRC522::STATUS_OK ) {
138 bool uid_read(File &f, MFRC522::Uid &uid)
142 int len = f.readBytesUntil('\n', line, 40);
143 if (len < 0 || len > 24) {
144 Serial.print("Invalid read: ");
156 // minimum length is 4 bytes == 10 chars
158 Serial.print(F("Read Card: bad line length="));
167 if (line[1] != ':') {
168 Serial.print(F("Read Card: bad format '"));
174 int bytes = hexto(line[0]);
176 // invalid lengths: 4, 7, and 10 are the standards
177 if (bytes != 4 && bytes != 7 && bytes != 10) {
178 Serial.print(F("Read Card: bad size="));
186 if (len < (bytes*2)+2) {
187 Serial.print(F("Read card: line too short. len="));
189 Serial.print(F(" size="));
198 byte * p = uid.uidByte;
201 for (;bytes > 0;bytes--) {
203 a = hexto(line[in++]);
204 b = hexto(line[in++]);
205 if (a == -1 || b == -1) {
208 Serial.print(F("Read Card: Non Hex char in line '"));
217 Serial.print(F("Loaded card '"));
219 Serial.print("' as ");
220 Serial.println(uid_print(uid));
226 bool uid_valid(MFRC522::Uid &uid)
229 Serial.print(F("Opening file "));
230 Serial.println(KEYS_FILE);
233 File f = SPIFFS.open(KEYS_FILE, "r");
234 // cant open the file, so card isnt there
236 Serial.println(F("Cannot open cards file"));
241 while ( uid_read(f, entry) ) {
242 if (uid_compare(uid, entry)) {
245 Serial.println(F("Found card"));
252 Serial.println(F("Card not found"));