From c45ef8ff6b12db8420eeb07907a7ce76c919d434 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Fri, 21 Oct 2022 22:51:00 +0200 Subject: [PATCH] Added: new NFC --- src/FabReader_v2/.vscode/settings.json | 3 +- src/FabReader_v2/src/NFC.cpp | 206 +++++++++++------------ src/FabReader_v2/src/NFC.h | 13 +- src/FabReader_v2/src/OTAProxy.cpp | 186 ++++++++++----------- src/FabReader_v2/src/helpers.cpp | 1 + src/FabReader_v2/src/main.cpp | 216 +++++++++++++++---------- src/FabReader_v2/src/main.cpp.2 | 49 ++++++ src/FabReader_v2/src/main.cpp.desfire | 145 ----------------- 8 files changed, 375 insertions(+), 444 deletions(-) create mode 100644 src/FabReader_v2/src/main.cpp.2 delete mode 100644 src/FabReader_v2/src/main.cpp.desfire diff --git a/src/FabReader_v2/.vscode/settings.json b/src/FabReader_v2/.vscode/settings.json index 8c81608..c51607b 100644 --- a/src/FabReader_v2/.vscode/settings.json +++ b/src/FabReader_v2/.vscode/settings.json @@ -9,6 +9,7 @@ "utility": "cpp", "string": "cpp", "functional": "cpp", - "*.desfire": "cpp" + "*.desfire": "cpp", + "*.old": "cpp" } } \ No newline at end of file diff --git a/src/FabReader_v2/src/NFC.cpp b/src/FabReader_v2/src/NFC.cpp index 37617fd..0fecbc8 100644 --- a/src/FabReader_v2/src/NFC.cpp +++ b/src/FabReader_v2/src/NFC.cpp @@ -1,4 +1,4 @@ -#include "NFC.h" +#include "nfc.h" #include #include #include @@ -13,140 +13,120 @@ NFC::NFC(int pin_ss, int pin_rst) rfid->PCD_DumpVersionToSerial(); } -bool NFC::requestCard() -{ - byte bufferATQA[2]; - byte bufferSize = sizeof(bufferATQA); - rfid->PCD_WriteRegister(rfid->TxModeReg, 0x00); - rfid->PCD_WriteRegister(rfid->RxModeReg, 0x00); - rfid->PCD_WriteRegister(rfid->ModWidthReg, 0x26); - - MFRC522::StatusCode status = rfid->PICC_RequestA(bufferATQA, &bufferSize); - Serial.printf("PICC_RequestA: 0x%02x\n", status); - return status == MFRC522::STATUS_OK; -} - MFRC522::Uid NFC::getUID() { return uid; } -bool NFC::selectCard() -{ - MFRC522::StatusCode status = rfid->PICC_Select(&uid); - Serial.printf("PICC_Select: 0x%02x\n", status); - if(status != MFRC522::STATUS_OK) - { - return false; - } - - if(rfid->uid.sak != 0x20) - { - Serial.printf("SAK Incorret: 0x%02x\n", rfid->uid.sak); - } - - - - - - byte ats_buffer[16]; - byte ats_buffer_len = 16; - - - - - status = rfid->PICC_RequestATS(ats_buffer, &ats_buffer_len); - if (status != MFRC522::STATUS_OK) - { - Serial.printf("PICC_RequestATS: 0x%02x\n", status); - return false; - } - - status = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11); - if (status != MFRC522::STATUS_OK) - { - Serial.printf("PICC_ProtocolAndParameterSelection: 0x%02x\n", status); - return false; - } - - cardSelected = true; - uid = rfid->uid; - Serial.println("Card Selected"); - return true; -} - -bool NFC::deselectCard() -{ - cardSelected = false; - Serial.println("Card Deselected"); - - return true; -} - bool NFC::hasCardSelected() { return cardSelected; } -bool NFC::isCardLost() +bool NFC::testNFC() { - uint8_t control = 0x00; - for(int i=0; i<3; i++) - { - if( !rfid->PICC_IsNewCardPresent() ) - { - if(rfid->PICC_ReadCardSerial()) - { - control |= 0x16; - } - if(rfid->PICC_ReadCardSerial()) - { - control |= 0x16; - } - control += 0x1; - } - control += 0x4; - } + return rfid->PCD_PerformSelfTest(); +} - Serial.printf("Control: %d\n", control); - - if(!(control == 13 || control == 14)) - { - Serial.println("Card Lost"); - return true; - } - else +bool NFC::checkforCard() +{ + // RequestA + if ( ! rfid->PICC_IsNewCardPresent()) { return false; } + + // PICC_Select ??? + if ( ! rfid->PICC_ReadCardSerial()) + { + return false; + } + + // Check for DESFire + if (rfid->uid.sak != 0x20) + { + return false; + } + + return true; +} + +bool NFC::connecttoCard() +{ + // RATS + byte ats[16]; + byte atsLength = 16; + MFRC522::StatusCode state = rfid->PICC_RequestATS(ats, &atsLength); + if (state != MFRC522::STATUS_OK) { + Serial.println(F("Failed ATS")); + Serial.println(state); + rfid->PICC_HaltA(); + return false; + } + + // PPS + state = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11, 0x00); + if (state != MFRC522::STATUS_OK) + { + Serial.println("Failed PPS"); + Serial.println(state); + rfid->PICC_HaltA(); + return false; + } + + cardSelected = true; + uid = rfid->uid; + + return true; +} + +bool NFC::disconnectCard() +{ + MFRC522::StatusCode state = rfid->PICC_HaltA(); + if (state != MFRC522::STATUS_OK) + { + Serial.println(F("Failed PICC_HaltA")); + Serial.println(state); + return false; + } + + return true; +} + +bool NFC::testCard() +{ + return true; } MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* response, byte* response_len) { - byte buffer[63 + 4]; - byte buffer_len = command_len + 4; - MFRC522::StatusCode status; + MFRC522::StatusCode state; + byte request_buffer[APDU_BUFFER_SIZE + 4]; // APDU Size + PCB + CID + 2x CRC + byte request_buffer_size = command_len + 4; - buffer[0] = 0x0A; - buffer[1] = 0x00; - memcpy(&buffer[2], command, command_len); - - status = rfid->PCD_CalculateCRC(buffer, buffer_len - 2, &buffer[buffer_len - 2]); - if (status != MFRC522::STATUS_OK) - { - Serial.printf("PCD_CalculateCRC: 0x%02x\n", status); - return status; - } - - Serial.println(buffer_len); - printbytes(buffer, buffer_len); + request_buffer[0] = pcb; + request_buffer[1] = cid; - status = rfid->PCD_TransceiveData(buffer, buffer_len, response, response_len, NULL, 0, true); - if (status != MFRC522::STATUS_OK) + memcpy(&request_buffer[2], command, command_len); + + // Update the PCB + if (pcb == 0x0A) + pcb = 0x0B; + else + pcb = 0x0A; + + // Calculate CRC_A + state = rfid->PCD_CalculateCRC(request_buffer, request_buffer_size, &request_buffer[request_buffer_size - 2]); + if (state != MFRC522::STATUS_OK) { - Serial.printf("PCD_TransceiveData: 0x%02x\n", status); - return status; + return state; } - return status; + state = rfid->PCD_TransceiveData(request_buffer, request_buffer_size, response, response_len); + if (state != MFRC522::STATUS_OK) + { + return state; + } + + return state; } \ No newline at end of file diff --git a/src/FabReader_v2/src/NFC.h b/src/FabReader_v2/src/NFC.h index 747e304..31dbb90 100644 --- a/src/FabReader_v2/src/NFC.h +++ b/src/FabReader_v2/src/NFC.h @@ -2,7 +2,7 @@ #define NFC_H #include -#include +#include #define APDU_BUFFER_SIZE 256 class NFC @@ -10,15 +10,18 @@ class NFC private: MFRC522::Uid uid; + byte pcb = 0x0A; + byte cid = 0x00; bool cardSelected = false; public: DESFire* rfid; NFC(int pin_ss, int pin_rst); - bool requestCard(); - bool selectCard(); - bool deselectCard(); - bool isCardLost(); + bool testNFC(); + bool checkforCard(); + bool connecttoCard(); + bool disconnectCard(); + bool testCard(); bool hasCardSelected(); MFRC522::Uid getUID(); diff --git a/src/FabReader_v2/src/OTAProxy.cpp b/src/FabReader_v2/src/OTAProxy.cpp index 8af8ebe..c7be739 100644 --- a/src/FabReader_v2/src/OTAProxy.cpp +++ b/src/FabReader_v2/src/OTAProxy.cpp @@ -1,110 +1,110 @@ -#include "OTAProxy.h" -#include "NFC.h" -#include +// #include "OTAProxy.h" +// #include "NFC.h" +// #include -OTAProxy::OTAProxy(PubSubClient* mqttClient, NFC* nfc, int id) -{ - this->mqtt = mqttClient; - this->nfc = nfc; - this->id = id; -} +// OTAProxy::OTAProxy(PubSubClient* mqttClient, NFC* nfc, int id) +// { +// this->mqtt = mqttClient; +// this->nfc = nfc; +// this->id = id; +// } -bool OTAProxy::hasActiveOTA() -{ - return activeOTA; -} +// bool OTAProxy::hasActiveOTA() +// { +// return activeOTA; +// } -void OTAProxy::startOTA() -{ - if(!(nfc->selectCard())) - { - return; - } - activeOTA = true; +// void OTAProxy::startOTA() +// { +// if(!(nfc->selectCard())) +// { +// return; +// } +// activeOTA = true; - char topic[] = "fabreader/00000/startOTA"; - sprintf(topic, "fabreader/%05d/startOTA", id); - MFRC522::Uid uid = nfc->getUID(); - mqtt->publish(topic, uid.uidByte, uid.size); +// char topic[] = "fabreader/00000/startOTA"; +// sprintf(topic, "fabreader/%05d/startOTA", id); +// MFRC522::Uid uid = nfc->getUID(); +// mqtt->publish(topic, uid.uidByte, uid.size); - Serial.println("Start OTA"); -} +// Serial.println("Start OTA"); +// } -void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length) -{ - char topic_requestOTA[] = "fabreader/00000/requestOTA"; - sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id); +// void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length) +// { +// char topic_requestOTA[] = "fabreader/00000/requestOTA"; +// sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id); - char topic_responseOTA[] = "fabreader/00000/responseOTA"; - sprintf(topic_responseOTA, "fabreader/%05d/responseOTA", id); +// char topic_responseOTA[] = "fabreader/00000/responseOTA"; +// sprintf(topic_responseOTA, "fabreader/%05d/responseOTA", id); - char topic_stopOTA[] = "fabreader/00000/stopOTA"; - sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", id); +// char topic_stopOTA[] = "fabreader/00000/stopOTA"; +// sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", id); - char topic_restartOTA[] = "fabreader/00000/restartOTA"; - sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id); +// char topic_restartOTA[] = "fabreader/00000/restartOTA"; +// sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id); - if(!strcmp(topic, topic_requestOTA)) - { - Serial.println("Request OTA"); - byte response[APDU_BUFFER_SIZE] = {0}; - byte response_len; +// if(!strcmp(topic, topic_requestOTA)) +// { +// Serial.println("Request OTA"); +// byte response[APDU_BUFFER_SIZE] = {0}; +// byte response_len; - MFRC522::StatusCode status; - while (true) - { - if(nfc->isCardLost()) - { - Serial.println("Card Lost"); - return; - } - Serial.println("Run Transceive"); - status = nfc->Transceive(payload, length, response, &response_len); - Serial.printf("PICC_Tranceive: 0x%02x\n", status); - if(status == MFRC522::STATUS_OK) - { - break; - } - } +// MFRC522::StatusCode status; +// while (true) +// { +// if(nfc->isCardLost()) +// { +// Serial.println("Card Lost"); +// return; +// } +// Serial.println("Run Transceive"); +// status = nfc->Transceive(payload, length, response, &response_len); +// Serial.printf("PICC_Tranceive: 0x%02x\n", status); +// if(status == MFRC522::STATUS_OK) +// { +// break; +// } +// } - for(int i = 0; i < response_len; i++) - { - char hexCar[2]; +// for(int i = 0; i < response_len; i++) +// { +// char hexCar[2]; - sprintf(hexCar, "%02X", response[i]); - Serial.print(hexCar); - } - Serial.println(); +// sprintf(hexCar, "%02X", response[i]); +// Serial.print(hexCar); +// } +// Serial.println(); - mqtt->publish(topic_responseOTA, response, response_len); - Serial.println("Response OTA"); - } - else if(!strcmp(topic, topic_stopOTA)) - { - Serial.println("Stop OTA"); - while(!(nfc->deselectCard())); - activeOTA = false; - } - else if(!strcmp(topic, topic_restartOTA)) - { - Serial.println("Restart OTA"); - while(!(nfc->deselectCard())); - // if(nfc->hasNewCard()) - // { - // startOTA(); - // } - } -} +// mqtt->publish(topic_responseOTA, response, response_len); +// Serial.println("Response OTA"); +// } +// else if(!strcmp(topic, topic_stopOTA)) +// { +// Serial.println("Stop OTA"); +// while(!(nfc->deselectCard())); +// activeOTA = false; +// } +// else if(!strcmp(topic, topic_restartOTA)) +// { +// Serial.println("Restart OTA"); +// while(!(nfc->deselectCard())); +// // if(nfc->hasNewCard()) +// // { +// // startOTA(); +// // } +// } +// } -void OTAProxy::cancelOTA() -{ - char topic_cancelOTA[] = "fabreader/00000/cancelOTA"; - sprintf(topic_cancelOTA, "fabreader/%05d/cancelOTA", id); - MFRC522::Uid uid = nfc->getUID(); - mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size); +// void OTAProxy::cancelOTA() +// { +// char topic_cancelOTA[] = "fabreader/00000/cancelOTA"; +// sprintf(topic_cancelOTA, "fabreader/%05d/cancelOTA", id); +// MFRC522::Uid uid = nfc->getUID(); +// mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size); - while(!(nfc->deselectCard())); - activeOTA = false; +// while(!(nfc->deselectCard())); +// activeOTA = false; - Serial.println("Cancel OTA"); -} \ No newline at end of file +// Serial.println("Cancel OTA"); +// } \ No newline at end of file diff --git a/src/FabReader_v2/src/helpers.cpp b/src/FabReader_v2/src/helpers.cpp index 6df7bd3..ae7a7fc 100644 --- a/src/FabReader_v2/src/helpers.cpp +++ b/src/FabReader_v2/src/helpers.cpp @@ -62,6 +62,7 @@ void bytes2chars(byte* array, byte array_len, char* str, bool msb) void printbytes(byte* array, byte array_len) { + Serial.print("0x"); for(int i = 0; i < array_len; i++) { Serial.printf("%02x", array[i]); diff --git a/src/FabReader_v2/src/main.cpp b/src/FabReader_v2/src/main.cpp index fcb1086..3b82050 100644 --- a/src/FabReader_v2/src/main.cpp +++ b/src/FabReader_v2/src/main.cpp @@ -1,104 +1,146 @@ +/* + * -------------------------------------------------------------------------------------------------------------------- + * Example sketch/program showing how to read data from a PICC to serial. + * -------------------------------------------------------------------------------------------------------------------- + * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid + * + * Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID + * Reader on the Arduino SPI interface. + * + * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE + * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When + * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output + * will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages + * when removing the PICC from reading distance too early. + * + * If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading). + * So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all + * details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so + * keep the PICCs at reading distance until complete. + * + * @license Released into the public domain. + * + * Typical pin layout used: + * ----------------------------------------------------------------------------------------- + * MFRC522 Arduino Arduino Arduino Arduino Arduino + * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro + * Signal Pin Pin Pin Pin Pin Pin + * ----------------------------------------------------------------------------------------- + * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST + * SPI SS SDA(SS) 10 53 D10 10 10 + * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 + * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 + * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 + */ + #include #include #include +#define RST_PIN 9 // Configurable, see typical pin layout above +#define SS_PIN 10 // Configurable, see typical pin layout above + #include "Pins.h" DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance -void setup() -{ - Serial.begin(115200); // Initialize serial communications with the PC - while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) - SPI.begin(); // Init SPI bus - mfrc522.PCD_Init(); // Init MFRC522 - mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details - Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); +void setup() { + Serial.begin(115200); // Initialize serial communications with the PC + while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) + SPI.begin(); // Init SPI bus + mfrc522.PCD_Init(); // Init MFRC522 + mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details + mfrc522.PCD_PerformSelfTest(); + Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); } -void printBuffer(byte* buffer, byte buffer_len) -{ - Serial.printf("Len: %02d Buffer: 0x", buffer_len); - for(int i = 0; i < buffer_len; i++) - { - Serial.printf("%02x", buffer[i]); - } - Serial.println(); -} +void loop() { + // Look for new cards + if ( ! mfrc522.PICC_IsNewCardPresent()) { + return; + } -void loop() -{ - // RequestA - if ( ! mfrc522.PICC_IsNewCardPresent()) - { - return; - } + // Select one of the cards + if ( ! mfrc522.PICC_ReadCardSerial()) { + return; + } - // PICC_Select ??? - if ( ! mfrc522.PICC_ReadCardSerial()) - { - return; - } + if (mfrc522.uid.sak != 0x20) { + // Dump debug info about the card; PICC_HaltA() is automatically called + mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); + return; + } - // Check for DESFire - if (mfrc522.uid.sak != 0x20) - { - // Dump debug info about the card; PICC_HaltA() is automatically called - mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); - return; - } + // Show an extra line + Serial.println(); - Serial.println("New Card"); - MFRC522::StatusCode state; + DESFire::mifare_desfire_tag tag; + DESFire::StatusCode response; + + tag.pcb = 0x0A; + tag.cid = 0x00; + memset(tag.selected_application, 0, 3); + + // Make sure none DESFire status codes have DESFireStatus code to OK + response.desfire = DESFire::MF_OPERATION_OK; + + byte ats[16]; + byte atsLength = 16; + response.mfrc522 = mfrc522.PICC_RequestATS(ats, &atsLength); + if ( ! mfrc522.IsStatusCodeOK(response)) { + Serial.println(F("Failed to get ATS!")); + Serial.println(mfrc522.GetStatusCodeName(response)); + Serial.println(response.mfrc522); - // RATS - byte ats[16]; - byte atsLength = 16; - state = mfrc522.PICC_RequestATS(ats, &atsLength); - if (state != MFRC522::STATUS_OK) { - Serial.println(F("Failed to get ATS!")); - Serial.println(state); - mfrc522.PICC_HaltA(); - return; - } - - // PPS - state = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11, 0x00); - if (state != MFRC522::STATUS_OK) - { - Serial.println(F("Failed to perform protocol and parameter selection (PPS)!")); - Serial.println(state); - mfrc522.PICC_HaltA(); - return; - } - - Serial.println("Complete ISO 14443 Activation"); - - DESFire::mifare_desfire_tag tag; - tag.pcb = 0x0A; - tag.cid = 0x00; - - DESFire::StatusCode des_state; - byte buffer[64] = {0x5A, 0x00, 0x00, 0x03, 0x42, 0x41, 0x46, 0x00}; - byte bufferSize = 8; - - byte response_buffer[64]; - byte response_len; - - des_state = mfrc522.MIFARE_BlockExchangeWithData(&tag, 0x90, buffer, &bufferSize, response_buffer, &response_len); - state = des_state.mfrc522; - if (state != MFRC522::STATUS_OK) - { - Serial.println("Data Exchange failed"); - Serial.println(state); - } - Serial.println("Data Exchange complete"); - printBuffer(response_buffer, response_len); - - // Deselect - // Missing - - // HaltA mfrc522.PICC_HaltA(); - Serial.println("Stop Connection"); + return; + } + + // TODO: Should do checks but since I know my DESFire allows and requires PPS... + // PPS1 is ommitted and, therefore, 0x00 is used (106kBd) + response.mfrc522 = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11); + if ( ! mfrc522.IsStatusCodeOK(response)) { + Serial.println(F("Failed to perform protocol and parameter selection (PPS)!")); + Serial.println(mfrc522.GetStatusCodeName(response)); + mfrc522.PICC_HaltA(); + return; + } + + // MIFARE DESFire should respond to a GetVersion command + DESFire::MIFARE_DESFIRE_Version_t desfireVersion; + response = mfrc522.MIFARE_DESFIRE_GetVersion(&tag, &desfireVersion); + if ( ! mfrc522.IsStatusCodeOK(response)) { + Serial.println(F("Failed to get a response for GetVersion!")); + Serial.println(mfrc522.GetStatusCodeName(response)); + mfrc522.PICC_HaltA(); + return; + } + + // Dump MIFARE DESFire version information. + // NOTE: KEEP YOUR CARD CLOSE TO THE READER! + // This method takes some time and the card will be read + // once output ends! If you remove the card too fast + // a timeout will occur! + mfrc522.PICC_DumpMifareDesfireVersion(&tag, &desfireVersion); + + mfrc522.PICC_DumpMifareDesfireMasterKey(&tag); + + DESFire::mifare_desfire_aid_t aids[MIFARE_MAX_APPLICATION_COUNT]; + byte applicationCount = 0; + response = mfrc522.MIFARE_DESFIRE_GetApplicationIds(&tag, aids, &applicationCount); + if ( ! mfrc522.IsStatusCodeOK(response)) { + Serial.println(F("Failed to get application IDs!")); + Serial.println(mfrc522.GetStatusCodeName(response)); + mfrc522.PICC_HaltA(); + return; + } + + // Dump all applications + for (byte aidIndex = 0; aidIndex < applicationCount; aidIndex++) { + mfrc522.PICC_DumpMifareDesfireApplication(&tag, &(aids[aidIndex])); + } + + // Call PICC_HaltA() + mfrc522.PICC_HaltA(); + Serial.println(); } \ No newline at end of file diff --git a/src/FabReader_v2/src/main.cpp.2 b/src/FabReader_v2/src/main.cpp.2 new file mode 100644 index 0000000..de83929 --- /dev/null +++ b/src/FabReader_v2/src/main.cpp.2 @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "nfc.h" +#include "pins.h" +#include "helpers.h" + +NFC* nfc; + +void setup() +{ + Serial.begin(115200); // Initialize serial communications with the PC + while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) + + nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST); +} + +void loop() +{ + if(nfc->checkforCard()) + { + Serial.println("Card detected"); + if(nfc->connecttoCard()) + { + Serial.println("Card connected"); + + MFRC522::StatusCode state; + + byte request_buffer[APDU_BUFFER_SIZE] = {0x5A, 0x00, 0x00, 0x03, 0x42, 0x41, 0x46, 0x00}; + byte request_buffer_size = 8; + byte response_buffer[APDU_BUFFER_SIZE]; + byte response_buffer_size; + printbytes(request_buffer, request_buffer_size); + + state = nfc->Transceive(request_buffer, request_buffer_size, response_buffer, &response_buffer_size); + if (state != MFRC522::STATUS_OK) + { + Serial.println("Data Exchange failed"); + Serial.println(state); + } + Serial.println("Data Exchange complete"); + printbytes(response_buffer, response_buffer_size); + + nfc->disconnectCard(); + Serial.println("Card disconnected"); + } + } +} \ No newline at end of file diff --git a/src/FabReader_v2/src/main.cpp.desfire b/src/FabReader_v2/src/main.cpp.desfire deleted file mode 100644 index a157253..0000000 --- a/src/FabReader_v2/src/main.cpp.desfire +++ /dev/null @@ -1,145 +0,0 @@ -/* - * -------------------------------------------------------------------------------------------------------------------- - * Example sketch/program showing how to read data from a PICC to serial. - * -------------------------------------------------------------------------------------------------------------------- - * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid - * - * Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID - * Reader on the Arduino SPI interface. - * - * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE - * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When - * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output - * will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages - * when removing the PICC from reading distance too early. - * - * If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading). - * So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all - * details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so - * keep the PICCs at reading distance until complete. - * - * @license Released into the public domain. - * - * Typical pin layout used: - * ----------------------------------------------------------------------------------------- - * MFRC522 Arduino Arduino Arduino Arduino Arduino - * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro - * Signal Pin Pin Pin Pin Pin Pin - * ----------------------------------------------------------------------------------------- - * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST - * SPI SS SDA(SS) 10 53 D10 10 10 - * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 - * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 - * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 - */ - -#include -#include -#include - -#define RST_PIN 9 // Configurable, see typical pin layout above -#define SS_PIN 10 // Configurable, see typical pin layout above - -#include "Pins.h" - -DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance - -void setup() { - Serial.begin(115200); // Initialize serial communications with the PC - while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) - SPI.begin(); // Init SPI bus - mfrc522.PCD_Init(); // Init MFRC522 - mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details - Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); -} - -void loop() { - // Look for new cards - if ( ! mfrc522.PICC_IsNewCardPresent()) { - return; - } - - // Select one of the cards - if ( ! mfrc522.PICC_ReadCardSerial()) { - return; - } - - if (mfrc522.uid.sak != 0x20) { - // Dump debug info about the card; PICC_HaltA() is automatically called - mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); - return; - } - - // Show an extra line - Serial.println(); - - DESFire::mifare_desfire_tag tag; - DESFire::StatusCode response; - - tag.pcb = 0x0A; - tag.cid = 0x00; - memset(tag.selected_application, 0, 3); - - // Make sure none DESFire status codes have DESFireStatus code to OK - response.desfire = DESFire::MF_OPERATION_OK; - - byte ats[16]; - byte atsLength = 16; - response.mfrc522 = mfrc522.PICC_RequestATS(ats, &atsLength); - if ( ! mfrc522.IsStatusCodeOK(response)) { - Serial.println(F("Failed to get ATS!")); - Serial.println(mfrc522.GetStatusCodeName(response)); - Serial.println(response.mfrc522); - - mfrc522.PICC_HaltA(); - return; - } - - // TODO: Should do checks but since I know my DESFire allows and requires PPS... - // PPS1 is ommitted and, therefore, 0x00 is used (106kBd) - response.mfrc522 = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11); - if ( ! mfrc522.IsStatusCodeOK(response)) { - Serial.println(F("Failed to perform protocol and parameter selection (PPS)!")); - Serial.println(mfrc522.GetStatusCodeName(response)); - mfrc522.PICC_HaltA(); - return; - } - - // MIFARE DESFire should respond to a GetVersion command - DESFire::MIFARE_DESFIRE_Version_t desfireVersion; - response = mfrc522.MIFARE_DESFIRE_GetVersion(&tag, &desfireVersion); - if ( ! mfrc522.IsStatusCodeOK(response)) { - Serial.println(F("Failed to get a response for GetVersion!")); - Serial.println(mfrc522.GetStatusCodeName(response)); - mfrc522.PICC_HaltA(); - return; - } - - // Dump MIFARE DESFire version information. - // NOTE: KEEP YOUR CARD CLOSE TO THE READER! - // This method takes some time and the card will be read - // once output ends! If you remove the card too fast - // a timeout will occur! - mfrc522.PICC_DumpMifareDesfireVersion(&tag, &desfireVersion); - - mfrc522.PICC_DumpMifareDesfireMasterKey(&tag); - - DESFire::mifare_desfire_aid_t aids[MIFARE_MAX_APPLICATION_COUNT]; - byte applicationCount = 0; - response = mfrc522.MIFARE_DESFIRE_GetApplicationIds(&tag, aids, &applicationCount); - if ( ! mfrc522.IsStatusCodeOK(response)) { - Serial.println(F("Failed to get application IDs!")); - Serial.println(mfrc522.GetStatusCodeName(response)); - mfrc522.PICC_HaltA(); - return; - } - - // Dump all applications - for (byte aidIndex = 0; aidIndex < applicationCount; aidIndex++) { - mfrc522.PICC_DumpMifareDesfireApplication(&tag, &(aids[aidIndex])); - } - - // Call PICC_HaltA() - mfrc522.PICC_HaltA(); - Serial.println(); -} \ No newline at end of file