From a8e4d70401717f6f74557186aec8afb2f2f562a9 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Thu, 20 Oct 2022 13:18:38 +0200 Subject: [PATCH] First working APDU Communication --- src/FabReader_v2/.vscode/settings.json | 3 +- src/FabReader_v2/src/OTAProxy.cpp | 8 +- src/FabReader_v2/src/main.cpp | 251 ++++++++----------------- src/FabReader_v2/src/main.cpp.old | 197 +++++++++++++++++++ 4 files changed, 282 insertions(+), 177 deletions(-) create mode 100644 src/FabReader_v2/src/main.cpp.old diff --git a/src/FabReader_v2/.vscode/settings.json b/src/FabReader_v2/.vscode/settings.json index 355a413..8c81608 100644 --- a/src/FabReader_v2/.vscode/settings.json +++ b/src/FabReader_v2/.vscode/settings.json @@ -8,6 +8,7 @@ "ranges": "cpp", "utility": "cpp", "string": "cpp", - "functional": "cpp" + "functional": "cpp", + "*.desfire": "cpp" } } \ No newline at end of file diff --git a/src/FabReader_v2/src/OTAProxy.cpp b/src/FabReader_v2/src/OTAProxy.cpp index c1bbb18..8af8ebe 100644 --- a/src/FabReader_v2/src/OTAProxy.cpp +++ b/src/FabReader_v2/src/OTAProxy.cpp @@ -89,10 +89,10 @@ void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length) { Serial.println("Restart OTA"); while(!(nfc->deselectCard())); - if(nfc->hasNewCard()) - { - startOTA(); - } + // if(nfc->hasNewCard()) + // { + // startOTA(); + // } } } diff --git a/src/FabReader_v2/src/main.cpp b/src/FabReader_v2/src/main.cpp index 812d2a4..fcb1086 100644 --- a/src/FabReader_v2/src/main.cpp +++ b/src/FabReader_v2/src/main.cpp @@ -1,197 +1,104 @@ -#include -#include - -#include "config.h" -#include "pins.h" -#include "nfc.h" -#include "otaproxy.h" -#include "helpers.h" -#include "Desfire.h" - -#include -#include - -#include -#include #include -#include +#include +#include -WiFiClient espClient; -//Config_Data config; +#include "Pins.h" -PubSubClient* mqtt; -NFC* nfc; -OTAProxy* ota; +DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance -void setup_wifi() +void setup() { - delay(10); - Serial.println("Connecting Wifi ..."); - - WiFi.mode(WIFI_STA); - WiFi.begin(WLAN_SSID, WLAN_PASS); - - while (WiFi.status() != WL_CONNECTED) - { - delay(500); - } - - randomSeed(micros()); - Serial.println("WiFi connected"); + 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 reconnect() +void printBuffer(byte* buffer, byte buffer_len) { - while (!mqtt->connected()) + Serial.printf("Len: %02d Buffer: 0x", buffer_len); + for(int i = 0; i < buffer_len; i++) { - String clientId = "FabReader_"; - clientId += String(FABREADERID); - - Serial.println("Connecting MQTT ..."); - if (mqtt->connect(clientId.c_str())) - { - Serial.println("MQTT connected"); - char id[6] = "00000"; - sprintf(id, "%05d", FABREADERID); - mqtt->publish("fabreader", id); - - char topic_requestOTA[] = "fabreader/00000/requestOTA"; - sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", FABREADERID); - mqtt->subscribe(topic_requestOTA); - - char topic_stopOTA[] = "fabreader/00000/stopOTA"; - sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", FABREADERID); - mqtt->subscribe(topic_stopOTA); - } else { - Serial.print("failed, rc="); - Serial.print(mqtt->state()); - Serial.println(" try again in 5 seconds"); - delay(5000); - } + Serial.printf("%02x", buffer[i]); } + Serial.println(); } -void callback(char* topic, byte* payload, unsigned int length) +void loop() { - Serial.println("Receive Message"); - Serial.println(topic); - if(ota->hasActiveOTA()) + // RequestA + if ( ! mfrc522.PICC_IsNewCardPresent()) { - ota->continueOTA(topic, payload, length); + return; } -} -void setup() -{ - Serial.begin(115200); - Serial.print("\n\n\n"); - Serial.println("Booting ..."); + // PICC_Select ??? + if ( ! mfrc522.PICC_ReadCardSerial()) + { + 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; + } + + Serial.println("New Card"); + MFRC522::StatusCode state; - pinMode(PIN_BUZZER, OUTPUT); - pinMode(PIN_BUTTON, INPUT); - pinMode(PIN_ETH_SPI_SS, OUTPUT); - digitalWrite(PIN_ETH_SPI_SS, HIGH); + // 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; + } - Serial.println("Connecting NFC ..."); - nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST); - if(!(nfc->rfid->PCD_PerformSelfTest())) + // PPS + state = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11, 0x00); + if (state != MFRC522::STATUS_OK) { - Serial.println("NFC Test failed"); - } - Serial.println("NFC connected"); - - // setup_wifi(); - - // mqtt = new PubSubClient(espClient); - // mqtt->setServer(MQTT_BROKER, 1883); - // mqtt->setCallback(callback); - - // ota = new OTAProxy(mqtt, nfc, FABREADERID); -} - -void loop() -{ - // if(!(nfc->hasCardSelected()) && nfc->hasNewCard() && nfc->selectCard()) - // { - // // DESFire::mifare_desfire_tag tag; - // // tag.pcb = 0x0A; - // // tag.cid = 0x00; - // // DESFire::mifare_desfire_aid_t aid; - - // // aid.data[0] = 0x42; - // // aid.data[1] = 0x41; - // // aid.data[2] = 0x46; - - // // DESFire::StatusCode status = nfc->rfid->MIFARE_DESFIRE_SelectApplication(&tag, &aid); - // // Serial.printf("%02x\n", status.mfrc522); - - // // char command_h[63*2] = "905a00000342414600"; - // // byte command[63] = { 0 }; - // // byte command_len = strlen(command_h)/2; - - - // // byte response[63] = { 0 }; - // // byte response_len = 0; - // // char response_h[63*2] = { 0 }; - - // // Serial.print("Command: "); - // // Serial.println(command_h); - - // // chars2bytes(command_h, command, true); - // // MFRC522::StatusCode status = nfc->Transceive(command, command_len, response, &response_len); - // // Serial.printf("PICC_Tranceive: 0x%02x\n", status); - // // bytes2chars(response, response_len, response_h, true); - - // // Serial.print("Response: "); - // // Serial.println(response_h); - // } - // if(nfc->hasCardSelected() && nfc->isCardLost()) - // { - // nfc->deselectCard(); - // } - - if( nfc->hasCardSelected() ) { - if( nfc->isCardLost() ) { - nfc->deselectCard(); - Serial.println("Stop"); - } - } - else { - if( nfc->hasNewCard() ) { - if( nfc->selectCard() ) { - Serial.println("Run"); - } - } + Serial.println(F("Failed to perform protocol and parameter selection (PPS)!")); + Serial.println(state); + mfrc522.PICC_HaltA(); + return; } - /* - if(!(nfc->hasCardSelected()) && nfc->hasNewCard()) + 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) { - if(nfc->selectCard()) - { - Serial.println("Run"); - } - } + Serial.println("Data Exchange failed"); + Serial.println(state); + } + Serial.println("Data Exchange complete"); + printBuffer(response_buffer, response_len); - if(nfc->hasCardSelected() && nfc->isCardLost()) - { - Serial.println("Stop"); - } - */ + // Deselect + // Missing - // if (!mqtt->connected()) - // { - // reconnect(); - // } - // mqtt->loop(); - - // if(ota->hasActiveOTA() && nfc->isCardLost()) - // { - // ota->cancelOTA(); - // } - - // if(!ota->hasActiveOTA() && nfc->hasNewCard()) - // { - // ota->startOTA(); - // } + // HaltA + mfrc522.PICC_HaltA(); + Serial.println("Stop Connection"); } \ No newline at end of file diff --git a/src/FabReader_v2/src/main.cpp.old b/src/FabReader_v2/src/main.cpp.old new file mode 100644 index 0000000..812d2a4 --- /dev/null +++ b/src/FabReader_v2/src/main.cpp.old @@ -0,0 +1,197 @@ +#include +#include + +#include "config.h" +#include "pins.h" +#include "nfc.h" +#include "otaproxy.h" +#include "helpers.h" +#include "Desfire.h" + +#include +#include + +#include +#include +#include +#include + +WiFiClient espClient; +//Config_Data config; + +PubSubClient* mqtt; +NFC* nfc; +OTAProxy* ota; + +void setup_wifi() +{ + delay(10); + Serial.println("Connecting Wifi ..."); + + WiFi.mode(WIFI_STA); + WiFi.begin(WLAN_SSID, WLAN_PASS); + + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + } + + randomSeed(micros()); + Serial.println("WiFi connected"); +} + +void reconnect() +{ + while (!mqtt->connected()) + { + String clientId = "FabReader_"; + clientId += String(FABREADERID); + + Serial.println("Connecting MQTT ..."); + if (mqtt->connect(clientId.c_str())) + { + Serial.println("MQTT connected"); + char id[6] = "00000"; + sprintf(id, "%05d", FABREADERID); + mqtt->publish("fabreader", id); + + char topic_requestOTA[] = "fabreader/00000/requestOTA"; + sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", FABREADERID); + mqtt->subscribe(topic_requestOTA); + + char topic_stopOTA[] = "fabreader/00000/stopOTA"; + sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", FABREADERID); + mqtt->subscribe(topic_stopOTA); + } else { + Serial.print("failed, rc="); + Serial.print(mqtt->state()); + Serial.println(" try again in 5 seconds"); + delay(5000); + } + } +} + +void callback(char* topic, byte* payload, unsigned int length) +{ + Serial.println("Receive Message"); + Serial.println(topic); + if(ota->hasActiveOTA()) + { + ota->continueOTA(topic, payload, length); + } +} + +void setup() +{ + Serial.begin(115200); + Serial.print("\n\n\n"); + Serial.println("Booting ..."); + + pinMode(PIN_BUZZER, OUTPUT); + pinMode(PIN_BUTTON, INPUT); + pinMode(PIN_ETH_SPI_SS, OUTPUT); + digitalWrite(PIN_ETH_SPI_SS, HIGH); + + Serial.println("Connecting NFC ..."); + nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST); + if(!(nfc->rfid->PCD_PerformSelfTest())) + { + Serial.println("NFC Test failed"); + } + Serial.println("NFC connected"); + + // setup_wifi(); + + // mqtt = new PubSubClient(espClient); + // mqtt->setServer(MQTT_BROKER, 1883); + // mqtt->setCallback(callback); + + // ota = new OTAProxy(mqtt, nfc, FABREADERID); +} + +void loop() +{ + // if(!(nfc->hasCardSelected()) && nfc->hasNewCard() && nfc->selectCard()) + // { + // // DESFire::mifare_desfire_tag tag; + // // tag.pcb = 0x0A; + // // tag.cid = 0x00; + // // DESFire::mifare_desfire_aid_t aid; + + // // aid.data[0] = 0x42; + // // aid.data[1] = 0x41; + // // aid.data[2] = 0x46; + + // // DESFire::StatusCode status = nfc->rfid->MIFARE_DESFIRE_SelectApplication(&tag, &aid); + // // Serial.printf("%02x\n", status.mfrc522); + + // // char command_h[63*2] = "905a00000342414600"; + // // byte command[63] = { 0 }; + // // byte command_len = strlen(command_h)/2; + + + // // byte response[63] = { 0 }; + // // byte response_len = 0; + // // char response_h[63*2] = { 0 }; + + // // Serial.print("Command: "); + // // Serial.println(command_h); + + // // chars2bytes(command_h, command, true); + // // MFRC522::StatusCode status = nfc->Transceive(command, command_len, response, &response_len); + // // Serial.printf("PICC_Tranceive: 0x%02x\n", status); + // // bytes2chars(response, response_len, response_h, true); + + // // Serial.print("Response: "); + // // Serial.println(response_h); + // } + // if(nfc->hasCardSelected() && nfc->isCardLost()) + // { + // nfc->deselectCard(); + // } + + if( nfc->hasCardSelected() ) { + if( nfc->isCardLost() ) { + nfc->deselectCard(); + Serial.println("Stop"); + } + } + else { + if( nfc->hasNewCard() ) { + if( nfc->selectCard() ) { + Serial.println("Run"); + } + } + } + + /* + if(!(nfc->hasCardSelected()) && nfc->hasNewCard()) + { + if(nfc->selectCard()) + { + Serial.println("Run"); + } + } + + if(nfc->hasCardSelected() && nfc->isCardLost()) + { + Serial.println("Stop"); + } + */ + + // if (!mqtt->connected()) + // { + // reconnect(); + // } + // mqtt->loop(); + + // if(ota->hasActiveOTA() && nfc->isCardLost()) + // { + // ota->cancelOTA(); + // } + + // if(!ota->hasActiveOTA() && nfc->hasNewCard()) + // { + // ota->startOTA(); + // } +} \ No newline at end of file