some formatting

This commit is contained in:
André Fiedler 2025-02-12 09:38:47 +01:00
parent d0eada7a40
commit 7006b39724
9 changed files with 141 additions and 139 deletions

View File

@ -9,49 +9,49 @@ Display::Display(int sda, int scl, int readerid)
display->setTextAlignment(TEXT_ALIGN_CENTER); display->setTextAlignment(TEXT_ALIGN_CENTER);
display->flipScreenVertically(); display->flipScreenVertically();
ReaderInfo[0] = '\0'; // Empty string ReaderInfo[0] = '\0'; // Empty string
strcpy(Title, "Boot ..."); // Set initial title strcpy(Title, "Boot ..."); // Set initial title
Info[0] = '\0'; // Empty string Info[0] = '\0'; // Empty string
} }
void Display::clearReaderInfo() void Display::clearReaderInfo()
{ {
char buffer [DISPLAY_BUFFER_SIZE]; char buffer[DISPLAY_BUFFER_SIZE];
sprintf(buffer, "%05d", readerid); sprintf(buffer, "%05d", readerid);
writeReaderInfo(buffer); 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); sprintf(buffer, "%s %05d", state, readerid);
writeReaderInfo(buffer); 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); strcpy(ReaderInfo, text);
} }
updateDisplay(); 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); strcpy(Title, text);
} }
updateDisplay(); 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); strcpy(Info, text);
} }
@ -75,7 +75,7 @@ void Display::updateDisplay()
display->display(); 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"; char topic_displayTitle[] = "fabreader/00000/display/title";
sprintf(topic_displayTitle, "fabreader/%05d/display/title", readerid); 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"; char topic_stopOTA[] = "fabreader/00000/stopOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", readerid); sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", readerid);
char* buffer = new char[length + 1]; char *buffer = new char[length + 1];
memcpy(buffer, payload, length); memcpy(buffer, payload, length);
if(!strcmp(topic, topic_displayTitle)) if (!strcmp(topic, topic_displayTitle))
{ {
writeTitle(buffer); writeTitle(buffer);
} }
else if(!strcmp(topic, topic_displayInfo)) else if (!strcmp(topic, topic_displayInfo))
{ {
writeInfo(buffer); writeInfo(buffer);
} }
else if(!strcmp(topic, topic_stopOTA)) else if (!strcmp(topic, topic_stopOTA))
{ {
clearReaderInfo(); clearReaderInfo();
} }

View File

@ -6,21 +6,21 @@
class Display class Display
{ {
private: private:
SSD1306Wire* display; SSD1306Wire *display;
int readerid; int readerid;
char ReaderInfo[DISPLAY_BUFFER_SIZE]; char ReaderInfo[DISPLAY_BUFFER_SIZE];
char Title[DISPLAY_BUFFER_SIZE]; char Title[DISPLAY_BUFFER_SIZE];
char Info[DISPLAY_BUFFER_SIZE]; char Info[DISPLAY_BUFFER_SIZE];
public: public:
Display(int sda, int scl, int readerid); Display(int sda, int scl, int readerid);
void createReaderInfo(const char* state); void createReaderInfo(const char *state);
void clearReaderInfo(); void clearReaderInfo();
void writeReaderInfo(const char* text); void writeReaderInfo(const char *text);
void writeTitle(const char* text); void writeTitle(const char *text);
void writeInfo(const char* text); void writeInfo(const char *text);
void updateDisplay(); void updateDisplay();
void updateByMQTT(const char* topic, byte* payload, unsigned int length); void updateByMQTT(const char *topic, byte *payload, unsigned int length);
}; };
#endif #endif

View File

@ -31,13 +31,13 @@ bool NFC::testNFC()
bool NFC::checkforCard() bool NFC::checkforCard()
{ {
// RequestA // RequestA
if ( ! rfid->PICC_IsNewCardPresent()) if (!rfid->PICC_IsNewCardPresent())
{ {
return false; return false;
} }
// PICC_Select ??? // PICC_Select ???
if ( ! rfid->PICC_ReadCardSerial()) if (!rfid->PICC_ReadCardSerial())
{ {
return false; return false;
} }
@ -57,7 +57,8 @@ bool NFC::connecttoCard()
byte ats[16]; byte ats[16];
byte atsLength = 16; byte atsLength = 16;
MFRC522::StatusCode state = rfid->PICC_RequestATS(ats, &atsLength); 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(F("Failed ATS"));
Serial.println(state); Serial.println(state);
rfid->PICC_HaltA(); rfid->PICC_HaltA();
@ -98,9 +99,9 @@ bool NFC::testCard()
return true; 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; 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; byte request_buffer_size = command_len + 4;
request_buffer[0] = pcb; request_buffer[0] = pcb;
request_buffer[1] = cid; request_buffer[1] = cid;
memcpy(&request_buffer[2], command, command_len); memcpy(&request_buffer[2], command, command_len);
// Update the PCB // Update the PCB
if (pcb == 0x0A) if (pcb == 0x0A)
pcb = 0x0B; pcb = 0x0B;
else else
pcb = 0x0A; pcb = 0x0A;
// Calculate CRC_A // Calculate CRC_A
state = rfid->PCD_CalculateCRC(request_buffer, request_buffer_size - 2, &request_buffer[request_buffer_size - 2]); state = rfid->PCD_CalculateCRC(request_buffer, request_buffer_size - 2, &request_buffer[request_buffer_size - 2]);
if (state != MFRC522::STATUS_OK) 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[APDU_BUFFER_SIZE + 4] = {0}; // APDU Size + PCB + CID + 2x CRC
byte response_buffer_size = 0xFF; byte response_buffer_size = 0xFF;
printbytes(request_buffer, request_buffer_size); printbytes(request_buffer, request_buffer_size);
state = rfid->PCD_TransceiveData(request_buffer, request_buffer_size, response_buffer, &response_buffer_size); state = rfid->PCD_TransceiveData(request_buffer, request_buffer_size, response_buffer, &response_buffer_size);
if (state != MFRC522::STATUS_OK) if (state != MFRC522::STATUS_OK)
{ {
return state; return state;
} }
memcpy(response, response_buffer + 2, response_buffer_size - 4); memcpy(response, response_buffer + 2, response_buffer_size - 4);
*response_len = response_buffer_size - 4; *response_len = response_buffer_size - 4;

View File

@ -7,25 +7,24 @@
class NFC class NFC
{ {
private: private:
MFRC522::Uid uid;
byte pcb = 0x0A;
byte cid = 0x00;
bool cardSelected = false;
MFRC522::Uid uid; public:
byte pcb = 0x0A; DESFire *rfid;
byte cid = 0x00; NFC(int pin_ss, int pin_rst);
bool cardSelected = false; bool testNFC();
bool checkforCard();
bool connecttoCard();
bool disconnectCard();
bool testCard();
bool hasCardSelected();
MFRC522::Uid getUID();
public: MFRC522::StatusCode Transceive(byte *command, byte command_len, byte *response, byte *response_len);
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);
}; };
#endif #endif

View File

@ -2,7 +2,7 @@
#include "nfc.h" #include "nfc.h"
#include <PubSubClient.h> #include <PubSubClient.h>
OTAProxy::OTAProxy(PubSubClient* mqttClient, NFC* nfc, int id) OTAProxy::OTAProxy(PubSubClient *mqttClient, NFC *nfc, int id)
{ {
this->mqtt = mqttClient; this->mqtt = mqttClient;
this->nfc = nfc; this->nfc = nfc;
@ -16,7 +16,7 @@ bool OTAProxy::hasActiveOTA()
void OTAProxy::startOTA() void OTAProxy::startOTA()
{ {
if(!(nfc->hasCardSelected())) if (!(nfc->hasCardSelected()))
{ {
return; return;
} }
@ -30,7 +30,7 @@ void OTAProxy::startOTA()
Serial.println("Start OTA"); 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"; char topic_requestOTA[] = "fabreader/00000/requestOTA";
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id); sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id);
@ -44,7 +44,7 @@ void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
char topic_restartOTA[] = "fabreader/00000/restartOTA"; char topic_restartOTA[] = "fabreader/00000/restartOTA";
sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id); sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id);
if(!strcmp(topic, topic_requestOTA)) if (!strcmp(topic, topic_requestOTA))
{ {
Serial.println("Request OTA"); Serial.println("Request OTA");
byte response[APDU_BUFFER_SIZE] = {0}; byte response[APDU_BUFFER_SIZE] = {0};
@ -56,7 +56,7 @@ void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
status = nfc->Transceive(payload, length, response, &response_len); status = nfc->Transceive(payload, length, response, &response_len);
Serial.printf("PICC_Tranceive: 0x%02x\n", status); Serial.printf("PICC_Tranceive: 0x%02x\n", status);
if(status != MFRC522::STATUS_OK) if (status != MFRC522::STATUS_OK)
{ {
cancelOTA(); cancelOTA();
return; return;
@ -65,7 +65,7 @@ void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
mqtt->publish(topic_responseOTA, response, response_len); mqtt->publish(topic_responseOTA, response, response_len);
Serial.println("Response OTA"); Serial.println("Response OTA");
} }
else if(!strcmp(topic, topic_stopOTA)) else if (!strcmp(topic, topic_stopOTA))
{ {
Serial.println("Stop OTA"); Serial.println("Stop OTA");
nfc->disconnectCard(); nfc->disconnectCard();
@ -89,7 +89,8 @@ void OTAProxy::cancelOTA()
MFRC522::Uid uid = nfc->getUID(); MFRC522::Uid uid = nfc->getUID();
mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size); mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size);
while(!(nfc->disconnectCard())); while (!(nfc->disconnectCard()))
;
activeOTA = false; activeOTA = false;
Serial.println("Cancel OTA"); Serial.println("Cancel OTA");

View File

@ -3,23 +3,24 @@
#include <PubSubClient.h> #include <PubSubClient.h>
#include "nfc.h" #include "nfc.h"
#define MSG_BUFFER_SIZE 50 #define MSG_BUFFER_SIZE 50
class OTAProxy class OTAProxy
{ {
private: private:
bool activeOTA = false; bool activeOTA = false;
PubSubClient* mqtt; PubSubClient *mqtt;
NFC* nfc; NFC *nfc;
int id; int id;
char msg[MSG_BUFFER_SIZE]; char msg[MSG_BUFFER_SIZE];
public:
OTAProxy(PubSubClient* mqtt, NFC* nfc, int id);
bool hasActiveOTA();
void startOTA(); public:
void continueOTA(char* topic, byte* payload, unsigned int length); OTAProxy(PubSubClient *mqtt, NFC *nfc, int id);
void cancelOTA(); bool hasActiveOTA();
void startOTA();
void continueOTA(char *topic, byte *payload, unsigned int length);
void cancelOTA();
}; };
#endif #endif

View File

@ -1,8 +1,8 @@
#include "helpers.h" #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; char c = *str;
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
@ -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); 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 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); 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 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"); Serial.print("0x");
for(int i = 0; i < array_len; i++) for (int i = 0; i < array_len; i++)
{ {
Serial.printf("%02x", array[i]); Serial.printf("%02x", array[i]);
} }

View File

@ -2,12 +2,12 @@
#define HELPERS_H #define HELPERS_H
#include <Arduino.h> #include <Arduino.h>
void char2byte(char* str, byte* array); void char2byte(char *str, byte *array);
void chars2bytes(char* str, byte* array, bool msb); void chars2bytes(char *str, byte *array, bool msb);
void byte2char(byte* array, char* str); void byte2char(byte *array, char *str);
void bytes2chars(byte* array, byte array_len, char* str, bool msb); 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 #endif

View File

@ -17,12 +17,12 @@
#include <SPI.h> #include <SPI.h>
WiFiClient espClient; WiFiClient espClient;
//Config_Data config; // Config_Data config;
PubSubClient* mqtt; PubSubClient *mqtt;
NFC* nfc; NFC *nfc;
OTAProxy* ota; OTAProxy *ota;
Display* display; Display *display;
unsigned long otatimeout = 3000; unsigned long otatimeout = 3000;
unsigned long lastotatime; unsigned long lastotatime;
@ -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("Receive Message");
Serial.println(topic); Serial.println(topic);
if(ota->hasActiveOTA()) if (ota->hasActiveOTA())
{ {
ota->continueOTA(topic, payload, length); ota->continueOTA(topic, payload, length);
} }
@ -125,7 +125,7 @@ void setup()
display->writeInfo("Start NFC ..."); display->writeInfo("Start NFC ...");
Serial.println("Connecting NFC ..."); Serial.println("Connecting NFC ...");
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST); 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"); Serial.println("NFC Test failed");
} }
@ -151,12 +151,12 @@ void loop()
} }
mqtt->loop(); mqtt->loop();
if(!ota->hasActiveOTA()) if (!ota->hasActiveOTA())
{ {
if(nfc->checkforCard()) if (nfc->checkforCard())
{ {
Serial.println("Card detected"); Serial.println("Card detected");
if(nfc->connecttoCard()) if (nfc->connecttoCard())
{ {
Serial.println("Card connected"); Serial.println("Card connected");
lastotatime = millis(); lastotatime = millis();
@ -170,9 +170,9 @@ void loop()
} }
} }
} }
if(ota->hasActiveOTA()) if (ota->hasActiveOTA())
{ {
if(millis() - lastotatime > otatimeout) if (millis() - lastotatime > otatimeout)
{ {
ota->cancelOTA(); ota->cancelOTA();
display->clearReaderInfo(); display->clearReaderInfo();