From 7006b397241cf4800e7f40631c4cff83cd02be3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Fiedler?= Date: Wed, 12 Feb 2025 09:38:47 +0100 Subject: [PATCH] some formatting --- Software/src/Display.cpp | 36 ++++++++++++++-------------- Software/src/Display.h | 32 ++++++++++++------------- Software/src/NFC.cpp | 49 ++++++++++++++++++++------------------- Software/src/NFC.h | 33 +++++++++++++------------- Software/src/OTAProxy.cpp | 19 ++++++++------- Software/src/OTAProxy.h | 27 ++++++++++----------- Software/src/helpers.cpp | 36 ++++++++++++++-------------- Software/src/helpers.h | 10 ++++---- Software/src/main.cpp | 38 +++++++++++++++--------------- 9 files changed, 141 insertions(+), 139 deletions(-) diff --git a/Software/src/Display.cpp b/Software/src/Display.cpp index 5178aed..552482b 100644 --- a/Software/src/Display.cpp +++ b/Software/src/Display.cpp @@ -9,49 +9,49 @@ Display::Display(int sda, int scl, int readerid) display->setTextAlignment(TEXT_ALIGN_CENTER); display->flipScreenVertically(); - ReaderInfo[0] = '\0'; // Empty string - strcpy(Title, "Boot ..."); // Set initial title - Info[0] = '\0'; // Empty string + ReaderInfo[0] = '\0'; // Empty string + strcpy(Title, "Boot ..."); // Set initial title + Info[0] = '\0'; // Empty string } void Display::clearReaderInfo() { - char buffer [DISPLAY_BUFFER_SIZE]; + char buffer[DISPLAY_BUFFER_SIZE]; sprintf(buffer, "%05d", readerid); writeReaderInfo(buffer); } -void Display::createReaderInfo(const char* state) +void Display::createReaderInfo(const char *state) { - if(strlen(state) < DISPLAY_BUFFER_SIZE - 1 - 6) + if (strlen(state) < DISPLAY_BUFFER_SIZE - 1 - 6) { - char buffer [DISPLAY_BUFFER_SIZE]; + char buffer[DISPLAY_BUFFER_SIZE]; sprintf(buffer, "%s %05d", state, readerid); writeReaderInfo(buffer); } } -void Display::writeReaderInfo(const char* text) +void Display::writeReaderInfo(const char *text) { - if(strlen(text) < DISPLAY_BUFFER_SIZE - 1) + if (strlen(text) < DISPLAY_BUFFER_SIZE - 1) { strcpy(ReaderInfo, text); } updateDisplay(); } -void Display::writeTitle(const char* text) +void Display::writeTitle(const char *text) { - if(strlen(text) < DISPLAY_BUFFER_SIZE - 1) + if (strlen(text) < DISPLAY_BUFFER_SIZE - 1) { strcpy(Title, text); } updateDisplay(); } -void Display::writeInfo(const char* text) +void Display::writeInfo(const char *text) { - if(strlen(text) < DISPLAY_BUFFER_SIZE - 1) + if (strlen(text) < DISPLAY_BUFFER_SIZE - 1) { strcpy(Info, text); } @@ -75,7 +75,7 @@ void Display::updateDisplay() display->display(); } -void Display::updateByMQTT(const char* topic, byte* payload, unsigned int length) +void Display::updateByMQTT(const char *topic, byte *payload, unsigned int length) { char topic_displayTitle[] = "fabreader/00000/display/title"; sprintf(topic_displayTitle, "fabreader/%05d/display/title", readerid); @@ -86,18 +86,18 @@ void Display::updateByMQTT(const char* topic, byte* payload, unsigned int length char topic_stopOTA[] = "fabreader/00000/stopOTA"; sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", readerid); - char* buffer = new char[length + 1]; + char *buffer = new char[length + 1]; memcpy(buffer, payload, length); - if(!strcmp(topic, topic_displayTitle)) + if (!strcmp(topic, topic_displayTitle)) { writeTitle(buffer); } - else if(!strcmp(topic, topic_displayInfo)) + else if (!strcmp(topic, topic_displayInfo)) { writeInfo(buffer); } - else if(!strcmp(topic, topic_stopOTA)) + else if (!strcmp(topic, topic_stopOTA)) { clearReaderInfo(); } diff --git a/Software/src/Display.h b/Software/src/Display.h index 02c76aa..17b318f 100644 --- a/Software/src/Display.h +++ b/Software/src/Display.h @@ -6,21 +6,21 @@ class Display { - private: - SSD1306Wire* display; - int readerid; - char ReaderInfo[DISPLAY_BUFFER_SIZE]; - char Title[DISPLAY_BUFFER_SIZE]; - char Info[DISPLAY_BUFFER_SIZE]; - - public: - Display(int sda, int scl, int readerid); - void createReaderInfo(const char* state); - void clearReaderInfo(); - void writeReaderInfo(const char* text); - void writeTitle(const char* text); - void writeInfo(const char* text); - void updateDisplay(); - void updateByMQTT(const char* topic, byte* payload, unsigned int length); +private: + SSD1306Wire *display; + int readerid; + char ReaderInfo[DISPLAY_BUFFER_SIZE]; + char Title[DISPLAY_BUFFER_SIZE]; + char Info[DISPLAY_BUFFER_SIZE]; + +public: + Display(int sda, int scl, int readerid); + void createReaderInfo(const char *state); + void clearReaderInfo(); + void writeReaderInfo(const char *text); + void writeTitle(const char *text); + void writeInfo(const char *text); + void updateDisplay(); + void updateByMQTT(const char *topic, byte *payload, unsigned int length); }; #endif \ No newline at end of file diff --git a/Software/src/NFC.cpp b/Software/src/NFC.cpp index 63324cd..90747f3 100644 --- a/Software/src/NFC.cpp +++ b/Software/src/NFC.cpp @@ -31,19 +31,19 @@ bool NFC::testNFC() bool NFC::checkforCard() { // RequestA - if ( ! rfid->PICC_IsNewCardPresent()) + if (!rfid->PICC_IsNewCardPresent()) { return false; } // PICC_Select ??? - if ( ! rfid->PICC_ReadCardSerial()) + if (!rfid->PICC_ReadCardSerial()) { return false; } // Check for DESFire - if (rfid->uid.sak != 0x20) + if (rfid->uid.sak != 0x20) { return false; } @@ -57,7 +57,8 @@ bool NFC::connecttoCard() byte ats[16]; byte atsLength = 16; MFRC522::StatusCode state = rfid->PICC_RequestATS(ats, &atsLength); - if (state != MFRC522::STATUS_OK) { + if (state != MFRC522::STATUS_OK) + { Serial.println(F("Failed ATS")); Serial.println(state); rfid->PICC_HaltA(); @@ -66,7 +67,7 @@ bool NFC::connecttoCard() // PPS state = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11, 0x00); - if (state != MFRC522::STATUS_OK) + if (state != MFRC522::STATUS_OK) { Serial.println("Failed PPS"); Serial.println(state); @@ -83,13 +84,13 @@ bool NFC::connecttoCard() bool NFC::disconnectCard() { MFRC522::StatusCode state = rfid->PICC_HaltA(); - if (state != MFRC522::STATUS_OK) + if (state != MFRC522::STATUS_OK) { Serial.println(F("Failed PICC_HaltA")); Serial.println(state); return false; } - + return true; } @@ -98,9 +99,9 @@ bool NFC::testCard() return true; } -MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* response, byte* response_len) +MFRC522::StatusCode NFC::Transceive(byte *command, byte command_len, byte *response, byte *response_len) { - if(command_len >= 0xFF - 4) + if (command_len >= 0xFF - 4) { return MFRC522::STATUS_NO_ROOM; } @@ -110,32 +111,32 @@ MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* respo byte request_buffer_size = command_len + 4; request_buffer[0] = pcb; - request_buffer[1] = cid; - + request_buffer[1] = cid; + memcpy(&request_buffer[2], command, command_len); // Update the PCB - if (pcb == 0x0A) - pcb = 0x0B; - else - pcb = 0x0A; + if (pcb == 0x0A) + pcb = 0x0B; + else + pcb = 0x0A; // Calculate CRC_A - state = rfid->PCD_CalculateCRC(request_buffer, request_buffer_size - 2, &request_buffer[request_buffer_size - 2]); - if (state != MFRC522::STATUS_OK) + state = rfid->PCD_CalculateCRC(request_buffer, request_buffer_size - 2, &request_buffer[request_buffer_size - 2]); + if (state != MFRC522::STATUS_OK) { - return state; - } + return state; + } byte response_buffer[APDU_BUFFER_SIZE + 4] = {0}; // APDU Size + PCB + CID + 2x CRC byte response_buffer_size = 0xFF; - + printbytes(request_buffer, request_buffer_size); - state = rfid->PCD_TransceiveData(request_buffer, request_buffer_size, response_buffer, &response_buffer_size); - if (state != MFRC522::STATUS_OK) + state = rfid->PCD_TransceiveData(request_buffer, request_buffer_size, response_buffer, &response_buffer_size); + if (state != MFRC522::STATUS_OK) { - return state; - } + return state; + } memcpy(response, response_buffer + 2, response_buffer_size - 4); *response_len = response_buffer_size - 4; diff --git a/Software/src/NFC.h b/Software/src/NFC.h index 45b07a4..31b2183 100644 --- a/Software/src/NFC.h +++ b/Software/src/NFC.h @@ -7,25 +7,24 @@ class NFC { - private: - - MFRC522::Uid uid; - byte pcb = 0x0A; - byte cid = 0x00; - bool cardSelected = false; +private: + MFRC522::Uid uid; + byte pcb = 0x0A; + byte cid = 0x00; + bool cardSelected = false; - public: - DESFire* rfid; - NFC(int pin_ss, int pin_rst); - bool testNFC(); - bool checkforCard(); - bool connecttoCard(); - bool disconnectCard(); - bool testCard(); - bool hasCardSelected(); - MFRC522::Uid getUID(); +public: + DESFire *rfid; + NFC(int pin_ss, int pin_rst); + bool testNFC(); + bool checkforCard(); + bool connecttoCard(); + bool disconnectCard(); + bool testCard(); + bool hasCardSelected(); + MFRC522::Uid getUID(); - MFRC522::StatusCode Transceive(byte* command, byte command_len, byte* response, byte* response_len); + MFRC522::StatusCode Transceive(byte *command, byte command_len, byte *response, byte *response_len); }; #endif diff --git a/Software/src/OTAProxy.cpp b/Software/src/OTAProxy.cpp index 2498b2b..88cfc7f 100644 --- a/Software/src/OTAProxy.cpp +++ b/Software/src/OTAProxy.cpp @@ -2,7 +2,7 @@ #include "nfc.h" #include -OTAProxy::OTAProxy(PubSubClient* mqttClient, NFC* nfc, int id) +OTAProxy::OTAProxy(PubSubClient *mqttClient, NFC *nfc, int id) { this->mqtt = mqttClient; this->nfc = nfc; @@ -16,7 +16,7 @@ bool OTAProxy::hasActiveOTA() void OTAProxy::startOTA() { - if(!(nfc->hasCardSelected())) + if (!(nfc->hasCardSelected())) { return; } @@ -30,7 +30,7 @@ void OTAProxy::startOTA() Serial.println("Start OTA"); } -void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length) +void OTAProxy::continueOTA(char *topic, byte *payload, unsigned int length) { char topic_requestOTA[] = "fabreader/00000/requestOTA"; sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id); @@ -44,19 +44,19 @@ void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length) char topic_restartOTA[] = "fabreader/00000/restartOTA"; sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id); - if(!strcmp(topic, topic_requestOTA)) + if (!strcmp(topic, topic_requestOTA)) { Serial.println("Request OTA"); byte response[APDU_BUFFER_SIZE] = {0}; byte response_len; MFRC522::StatusCode status; - + 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) + + if (status != MFRC522::STATUS_OK) { cancelOTA(); return; @@ -65,7 +65,7 @@ void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length) mqtt->publish(topic_responseOTA, response, response_len); Serial.println("Response OTA"); } - else if(!strcmp(topic, topic_stopOTA)) + else if (!strcmp(topic, topic_stopOTA)) { Serial.println("Stop OTA"); nfc->disconnectCard(); @@ -89,7 +89,8 @@ void OTAProxy::cancelOTA() MFRC522::Uid uid = nfc->getUID(); mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size); - while(!(nfc->disconnectCard())); + while (!(nfc->disconnectCard())) + ; activeOTA = false; Serial.println("Cancel OTA"); diff --git a/Software/src/OTAProxy.h b/Software/src/OTAProxy.h index 8245b6d..0095d05 100644 --- a/Software/src/OTAProxy.h +++ b/Software/src/OTAProxy.h @@ -3,23 +3,24 @@ #include #include "nfc.h" -#define MSG_BUFFER_SIZE 50 +#define MSG_BUFFER_SIZE 50 class OTAProxy { - private: - bool activeOTA = false; - PubSubClient* mqtt; - NFC* nfc; - int id; - char msg[MSG_BUFFER_SIZE]; - public: - OTAProxy(PubSubClient* mqtt, NFC* nfc, int id); - bool hasActiveOTA(); +private: + bool activeOTA = false; + PubSubClient *mqtt; + NFC *nfc; + int id; + char msg[MSG_BUFFER_SIZE]; - void startOTA(); - void continueOTA(char* topic, byte* payload, unsigned int length); - void cancelOTA(); +public: + OTAProxy(PubSubClient *mqtt, NFC *nfc, int id); + bool hasActiveOTA(); + + void startOTA(); + void continueOTA(char *topic, byte *payload, unsigned int length); + void cancelOTA(); }; #endif diff --git a/Software/src/helpers.cpp b/Software/src/helpers.cpp index ae7a7fc..9b680a0 100644 --- a/Software/src/helpers.cpp +++ b/Software/src/helpers.cpp @@ -1,21 +1,21 @@ #include "helpers.h" -void char2byte(char* str, byte* array) +void char2byte(char *str, byte *array) { - for(int i=0; i <2 ; i++) + for (int i = 0; i < 2; i++) { char c = *str; - if (c >= '0' && c <= '9') + if (c >= '0' && c <= '9') { *array *= 16; *array += c - '0'; } - else if (c >= 'A' && c <= 'F') + else if (c >= 'A' && c <= 'F') { *array *= 16; *array += (c - 'A') + 10; } - else if (c >= 'a' && c <= 'f') + else if (c >= 'a' && c <= 'f') { *array *= 16; *array += (c - 'a') + 10; @@ -24,46 +24,46 @@ void char2byte(char* str, byte* array) } } -void chars2bytes(char* str, byte* array, bool msb) +void chars2bytes(char *str, byte *array, bool msb) { int len = strlen(str); - for(int i = 0; i < len; i += 2) + for (int i = 0; i < len; i += 2) { - if(msb) + if (msb) { - char2byte(&str[i], &array[i/2]); + char2byte(&str[i], &array[i / 2]); } else { - char2byte(&str[i], &array[len-2 - i/2]); + char2byte(&str[i], &array[len - 2 - i / 2]); } } } -void byte2char(byte* array, char* str) +void byte2char(byte *array, char *str) { sprintf(str, "%02x", *array); } -void bytes2chars(byte* array, byte array_len, char* str, bool msb) +void bytes2chars(byte *array, byte array_len, char *str, bool msb) { - for(int i = 0; i < array_len; i++) + for (int i = 0; i < array_len; i++) { - if(msb) + if (msb) { - byte2char(&array[i], &str[i*2]); + byte2char(&array[i], &str[i * 2]); } else { - byte2char(&array[array_len-2 - i], &str[i*2]); + byte2char(&array[array_len - 2 - i], &str[i * 2]); } } } -void printbytes(byte* array, byte array_len) +void printbytes(byte *array, byte array_len) { Serial.print("0x"); - for(int i = 0; i < array_len; i++) + for (int i = 0; i < array_len; i++) { Serial.printf("%02x", array[i]); } diff --git a/Software/src/helpers.h b/Software/src/helpers.h index c6e3b42..cd100ff 100644 --- a/Software/src/helpers.h +++ b/Software/src/helpers.h @@ -2,12 +2,12 @@ #define HELPERS_H #include -void char2byte(char* str, byte* array); -void chars2bytes(char* str, byte* array, bool msb); +void char2byte(char *str, byte *array); +void chars2bytes(char *str, byte *array, bool msb); -void byte2char(byte* array, char* str); -void bytes2chars(byte* array, byte array_len, char* str, bool msb); +void byte2char(byte *array, char *str); +void bytes2chars(byte *array, byte array_len, char *str, bool msb); -void printbytes(byte* array, byte array_len); +void printbytes(byte *array, byte array_len); #endif \ No newline at end of file diff --git a/Software/src/main.cpp b/Software/src/main.cpp index b96739c..8bcc158 100644 --- a/Software/src/main.cpp +++ b/Software/src/main.cpp @@ -17,17 +17,17 @@ #include WiFiClient espClient; -//Config_Data config; +// Config_Data config; -PubSubClient* mqtt; -NFC* nfc; -OTAProxy* ota; -Display* display; +PubSubClient *mqtt; +NFC *nfc; +OTAProxy *ota; +Display *display; unsigned long otatimeout = 3000; unsigned long lastotatime; -void setup_wifi() +void setup_wifi() { delay(10); Serial.println("Connecting Wifi ..."); @@ -44,7 +44,7 @@ void setup_wifi() Serial.println("WiFi connected"); } -void reconnect() +void reconnect() { while (!mqtt->connected()) { @@ -83,8 +83,8 @@ void reconnect() display->writeTitle("Connected"); display->writeInfo(""); - } - else + } + else { display->writeTitle("Reconnect"); display->writeInfo("MQTT"); @@ -97,11 +97,11 @@ void reconnect() } } -void callback(char* topic, byte* payload, unsigned int length) +void callback(char *topic, byte *payload, unsigned int length) { Serial.println("Receive Message"); Serial.println(topic); - if(ota->hasActiveOTA()) + if (ota->hasActiveOTA()) { ota->continueOTA(topic, payload, length); } @@ -113,7 +113,7 @@ 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); @@ -125,7 +125,7 @@ void setup() display->writeInfo("Start NFC ..."); Serial.println("Connecting NFC ..."); nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST); - if(!(nfc->rfid->PCD_PerformSelfTest())) + if (!(nfc->rfid->PCD_PerformSelfTest())) { Serial.println("NFC Test failed"); } @@ -144,19 +144,19 @@ void setup() } void loop() -{ +{ if (!mqtt->connected()) { reconnect(); } mqtt->loop(); - if(!ota->hasActiveOTA()) + if (!ota->hasActiveOTA()) { - if(nfc->checkforCard()) + if (nfc->checkforCard()) { Serial.println("Card detected"); - if(nfc->connecttoCard()) + if (nfc->connecttoCard()) { Serial.println("Card connected"); lastotatime = millis(); @@ -170,9 +170,9 @@ void loop() } } } - if(ota->hasActiveOTA()) + if (ota->hasActiveOTA()) { - if(millis() - lastotatime > otatimeout) + if (millis() - lastotatime > otatimeout) { ota->cancelOTA(); display->clearReaderInfo();