Experimental code to read RFID card data.

This commit is contained in:
Fabian Schlenz 2019-11-13 20:14:09 +01:00
parent 45fef23bad
commit b32f7d1228
1 changed files with 47 additions and 0 deletions

View File

@ -72,6 +72,53 @@ void Controller::_check_rfid() {
s_uid.concat(temp);
INFO("New RFID card uid: %s\n", s_uid.c_str());
_player->play_id(s_uid);
DEBUG("Trying to read RFID data...");
_spi->select_rc522();
String data = "";
MFRC522::MIFARE_Key key;
for (int i=0; i<6; i++) key.keyByte[i]=0xFF;
MFRC522::PICC_Type type = _rfid->PICC_GetType(_rfid->uid.sak);
MFRC522::StatusCode status;
uint8_t sectors = 0;
switch(type) {
case MFRC522::PICC_TYPE_MIFARE_MINI: sectors = 5; break;
case MFRC522::PICC_TYPE_MIFARE_1K: sectors = 16; break;
case MFRC522::PICC_TYPE_MIFARE_4K: sectors = 40; break;
default: INFO("Unknown PICC type %s\n", String(MFRC522::PICC_GetTypeName(type)).c_str());
}
for (uint8_t sector=0; sector<sectors; sector++) {
uint8_t blocks = (sector < 32) ? 4 : 16;
uint8_t block_offset = (sector < 32) ? sector * 4 : 128 + (sector - 32) * 16;
status = _rfid->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block_offset, &key, &_rfid->uid);
if (status != MFRC522::STATUS_OK) {
DEBUG("PCD_Authenticate() failed: %s\n", String(_rfid->GetStatusCodeName(status)).c_str());
continue;
}
for (uint8_t block=0; block<blocks-1; block++) {
byte buffer[16];
uint8_t byte_count = 16;
status = _rfid->MIFARE_Read(block_offset + block, buffer, &byte_count);
if (status != MFRC522::STATUS_OK) {
DEBUG("MIFARE_Read() failed: %s\n", String(_rfid->GetStatusCodeName(status)).c_str());
continue;
}
for (int i=0; i<16; i++) {
if (buffer[i]!=0x00) data.concat(buffer[i]);
}
}
}
_rfid->PICC_HaltA();
_rfid->PCD_StopCrypto1();
_spi->select_rc522(false);
DEBUG("Data from RFID: %s", data.c_str());
} else {
if (_no_rfid_card_count >= 1) {
INFO("No more RFID card.\n");