From b32f7d1228cf9fe6b9fdb5d146a746c9b336d6e1 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 13 Nov 2019 20:14:09 +0100 Subject: [PATCH] Experimental code to read RFID card data. --- src/controller.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/controller.cpp b/src/controller.cpp index ed3e040..84bcad4 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -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; sectorPCD_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; blockMIFARE_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");