bool loop_rfid_read() {
if (!mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial()) {
return false;
}
Debug.PrintDebug(F("**Card Detected:**"));
DA.initClient();
for (int i=0; i<4; i++) {
readcard[i] = mfrc522.uid.uidByte[i];
array_to_string(readcard, 4, str);
memcpy(DA.client.cardUID, str, sizeof(str));
}
byte buffer[18];
byte size = sizeof(buffer);
//Read Name --->
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, BLOCK_NAME_AUTH, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Debug.PrintDebug(F("auth failed name..."));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay(1000);
Debug.PrintDebug(F("**Wait New Card OK!**"));
return false;
}
buffer[18] = "";
size = sizeof(buffer);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(BLOCK_NAME, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Debug.PrintDebug(F("read failed name..."));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay(1000);
Debug.PrintDebug(F("**Wait New Card OK!**"));
return false;
}
memcpy(DA.client.name, buffer, sizeof(DA.client.name));
Debug.PrintDebug(DA.client.name);
//<--- Read Name
//Read Surname --->
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, BLOCK_SURNAME_AUTH, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Debug.PrintDebug(F("auth failed suname..."));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay(1000);
Debug.PrintDebug(F("**Wait New Card OK!**"));
return false;
}
buffer[18] = "";
size = sizeof(buffer);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(BLOCK_SURNAME, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Debug.PrintDebug(F("read failed suname..."));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay(1000);
Debug.PrintDebug(F("**Wait New Card OK!**"));
return false;
}
memcpy(DA.client.surname, buffer, sizeof(DA.client.surname));
Debug.PrintDebug(DA.client.surname);
//<--- Read Surname
//Read HASH --->
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, BLOCK_USER_HASH_AUTH, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Debug.PrintDebug(F("auth failed hash..."));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay(1000);
Debug.PrintDebug(F("**Wait New Card OK!**"));
return false;
}
buffer[18] = "";
size = sizeof(buffer);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(BLOCK_USER_HASH, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Debug.PrintDebug(F("read failed hash..."));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay(1000);
Debug.PrintDebug(F("**Wait New Card OK!**"));
return false;
}
array_to_string(buffer, 16, DA.client.cardHash);
Debug.PrintDebug(DA.client.cardHash);
//<--- Read HASH
Debug.PrintDebug(F("**Card Read OK!**"));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
if (DA.getClientByCard()) {
DA.checkClientCard();
} else {
DSTLed.DSLChangeStatus("error");
DLCD.DLCDWrite("Problem With Server!",1);
DLCD.DLCDClearLine(2);
delay(2000);
DLCD.DLCDInit();
DSTLed.DSLChangeStatus("none");
}
Debug.PrintDebug(F("**Wait New Card OK!**"));
return true;
}