diff --git a/ESP32_AP-Flasher/include/contentmanager.h b/ESP32_AP-Flasher/include/contentmanager.h index 64e99979..6639b84a 100644 --- a/ESP32_AP-Flasher/include/contentmanager.h +++ b/ESP32_AP-Flasher/include/contentmanager.h @@ -36,7 +36,7 @@ int getJsonTemplateUrl(String &filename, String URL, time_t fetched, String MAC, void drawJsonStream(Stream &stream, String &filename, tagRecord *&taginfo, imgParam &imageParams); void drawElement(const JsonObject &element, TFT_eSprite &spr); uint16_t getColor(const String &color); -char *formatHttpDate(time_t t); +char *formatHttpDate(const time_t t); String urlEncode(const char *msg); int windSpeedToBeaufort(const float windSpeed); String windDirectionIcon(const int degrees); diff --git a/ESP32_AP-Flasher/include/language.h b/ESP32_AP-Flasher/include/language.h index a26b1dba..a30a1ebd 100644 --- a/ESP32_AP-Flasher/include/language.h +++ b/ESP32_AP-Flasher/include/language.h @@ -2,32 +2,32 @@ #include -static int defaultLanguage = 0; +extern int defaultLanguage; -static String languageList[] = {"EN - English", "NL - Nederlands", "DE - Deutsch"}; +extern String languageList[]; /*EN English language section*/ -static String languageEnDaysShort[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; -static String languageEnDays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; -static String languageEnMonth[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; +extern String languageEnDaysShort[]; +extern String languageEnDays[]; +extern String languageEnMonth[]; /*END English language section END*/ /*NL Dutch language section*/ -static String languageNlDaysShort[] = {"ZO", "MA", "DI", "WO", "DO", "VR", "ZA"}; -static String languageNlDays[] = {"zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag"}; -static String languageNlMonth[] = {"januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"}; +extern String languageNlDaysShort[]; +extern String languageNlDays[]; +extern String languageNlMonth[]; /*END Dutch language section END*/ /*DE German language section*/ -static String languageDeDaysShort[] = {"SO", "MO", "DI", "MI", "DO", "FR", "SA"}; -static String languageDeDays[] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"}; -static String languageDeMonth[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"}; +extern String languageDeDaysShort[]; +extern String languageDeDays[]; +extern String languageDeMonth[]; /*END German language section END*/ -static String* languageDaysShort[] = {languageEnDaysShort, languageNlDaysShort, languageDeDaysShort}; -static String* languageDays[] = {languageEnDays, languageNlDays, languageDeDays}; -static String* languageMonth[] = {languageEnMonth, languageNlMonth, languageDeMonth}; +extern String* languageDaysShort[]; +extern String* languageDays[]; +extern String* languageMonth[]; -void updateLanguageFromConfig(); -int getDefaultLanguage(); -int getCurrentLanguage(); +extern void updateLanguageFromConfig(); +extern int getDefaultLanguage(); +extern int getCurrentLanguage(); diff --git a/ESP32_AP-Flasher/include/newproto.h b/ESP32_AP-Flasher/include/newproto.h index f3a290a5..7746a2db 100644 --- a/ESP32_AP-Flasher/include/newproto.h +++ b/ESP32_AP-Flasher/include/newproto.h @@ -7,7 +7,7 @@ extern void processBlockRequest(struct espBlockRequest* br); extern void prepareCancelPending(const uint8_t dst[8]); extern void prepareIdleReq(const uint8_t* dst, uint16_t nextCheckin); extern void prepareDataAvail(uint8_t* data, uint16_t len, uint8_t dataType, const uint8_t* dst); -extern bool prepareDataAvail(String* filename, uint8_t dataType, const uint8_t* dst, uint16_t nextCheckin); +extern bool prepareDataAvail(String& filename, uint8_t dataType, const uint8_t* dst, uint16_t nextCheckin); extern void prepareExternalDataAvail(struct pendingData* pending, IPAddress remoteIP); extern void processXferComplete(struct espXferComplete* xfc, bool local); extern void processXferTimeout(struct espXferComplete* xfc, bool local); diff --git a/ESP32_AP-Flasher/include/tag_db.h b/ESP32_AP-Flasher/include/tag_db.h index f978432f..2125dc08 100644 --- a/ESP32_AP-Flasher/include/tag_db.h +++ b/ESP32_AP-Flasher/include/tag_db.h @@ -86,20 +86,20 @@ extern std::vector tagDB; extern std::unordered_map hwtype; extern std::unordered_map varDB; extern DynamicJsonDocument APconfig; -String tagDBtoJson(const uint8_t mac[8] = nullptr, uint8_t startPos = 0); -bool deleteRecord(const uint8_t mac[8]); -void fillNode(JsonObject& tag, tagRecord*& taginfo); -void saveDB(String filename); -void loadDB(String filename); -void destroyDB(); -uint32_t getTagCount(); -uint32_t getTagCount(uint32_t& timeoutcount); -void mac2hex(const uint8_t* mac, char* hexBuffer); -bool hex2mac(const String& hexString, uint8_t* mac); -void clearPending(tagRecord* taginfo); -void initAPconfig(); -void saveAPconfig(); -HwType getHwType(uint8_t id); -bool setVarDB(const std::string& key, const String& value); +extern String tagDBtoJson(const uint8_t mac[8] = nullptr, uint8_t startPos = 0); +extern bool deleteRecord(const uint8_t mac[8]); +extern void fillNode(JsonObject& tag, const tagRecord* taginfo); +extern void saveDB(const String& filename); +extern void loadDB(const String& filename); +extern void destroyDB(); +extern uint32_t getTagCount(); +extern uint32_t getTagCount(uint32_t& timeoutcount); +extern void mac2hex(const uint8_t* mac, char* hexBuffer); +extern bool hex2mac(const String& hexString, uint8_t* mac); +extern void clearPending(tagRecord* taginfo); +extern void initAPconfig(); +extern void saveAPconfig(); +extern HwType getHwType(const uint8_t id); +extern bool setVarDB(const std::string& key, const String& value); #pragma pack(pop) diff --git a/ESP32_AP-Flasher/src/contentmanager.cpp b/ESP32_AP-Flasher/src/contentmanager.cpp index 3eeeff7e..82b2635d 100644 --- a/ESP32_AP-Flasher/src/contentmanager.cpp +++ b/ESP32_AP-Flasher/src/contentmanager.cpp @@ -193,7 +193,7 @@ void drawNew(const uint8_t mac[8], const bool buttonPressed, tagRecord *&taginfo if (imageParams.hasRed) { imageParams.dataType = DATATYPE_IMG_RAW_2BPP; } - if (prepareDataAvail(&filename, imageParams.dataType, mac, cfgobj["timetolive"].as())) { + if (prepareDataAvail(filename, imageParams.dataType, mac, cfgobj["timetolive"].as())) { cfgobj["#fetched"] = true; if (cfgobj["delete"].as() == "1") { contentFS->remove("/" + configFilename); @@ -243,7 +243,7 @@ void drawNew(const uint8_t mac[8], const bool buttonPressed, tagRecord *&taginfo filename = cfgobj["filename"].as(); if (!util::isEmptyOrNull(filename) && !cfgobj["#fetched"].as()) { - if (prepareDataAvail(&filename, DATATYPE_FW_UPDATE, mac, cfgobj["timetolive"].as())) { + if (prepareDataAvail(filename, DATATYPE_FW_UPDATE, mac, cfgobj["timetolive"].as())) { cfgobj["#fetched"] = true; } else { wsErr("Error accessing " + filename); @@ -395,7 +395,7 @@ bool updateTagImage(String &filename, const uint8_t *dst, uint16_t nextCheckin, sendAPSegmentedData(dst, (String)imageParams.segments, imageParams.symbols, imageParams.invert, (taginfo->isExternal == false)); } else { if (imageParams.hasRed) imageParams.dataType = DATATYPE_IMG_RAW_2BPP; - prepareDataAvail(&filename, imageParams.dataType, dst, nextCheckin); + prepareDataAvail(filename, imageParams.dataType, dst, nextCheckin); } return true; } @@ -792,8 +792,8 @@ bool getRssFeed(String &filename, String URL, String title, tagRecord *&taginfo, // https://www.nu.nl/rss/Algemeen const char *url = URL.c_str(); - const char *tag = "title"; - const int rssArticleSize = 128; + constexpr const char *tag = "title"; + constexpr const int rssArticleSize = 128; TFT_eSprite spr = TFT_eSprite(&tft); U8g2_for_TFT_eSPI u8f; @@ -856,7 +856,7 @@ bool getCalFeed(String &filename, String URL, String title, tagRecord *&taginfo, time(&now); struct tm timeinfo; localtime_r(&now, &timeinfo); - static char dateString[40]; + char dateString[40]; strftime(dateString, sizeof(dateString), "%d.%m.%Y", &timeinfo); HTTPClient http; @@ -1143,7 +1143,7 @@ uint16_t getColor(const String &color) { return TFT_WHITE; } -char *formatHttpDate(time_t t) { +char *formatHttpDate(const time_t t) { static char buf[40]; struct tm *timeinfo; timeinfo = localtime(&t); // Get the local time @@ -1154,7 +1154,7 @@ char *formatHttpDate(time_t t) { } String urlEncode(const char *msg) { - static const char *hex = "0123456789ABCDEF"; + constexpr const char *hex = "0123456789ABCDEF"; String encodedMsg = ""; while (*msg != '\0') { @@ -1172,8 +1172,8 @@ String urlEncode(const char *msg) { } int windSpeedToBeaufort(const float windSpeed) { - constexpr static const float speeds[] = {0.3, 1.5, 3.3, 5.5, 8, 10.8, 13.9, 17.2, 20.8, 24.5, 28.5, 32.7}; - constexpr static const int numSpeeds = sizeof(speeds) / sizeof(speeds[0]); + constexpr const float speeds[] = {0.3, 1.5, 3.3, 5.5, 8, 10.8, 13.9, 17.2, 20.8, 24.5, 28.5, 32.7}; + constexpr const int numSpeeds = sizeof(speeds) / sizeof(speeds[0]); int beaufort = 0; for (int i = 0; i < numSpeeds; i++) { if (windSpeed >= speeds[i]) { @@ -1184,7 +1184,7 @@ int windSpeedToBeaufort(const float windSpeed) { } String windDirectionIcon(const int degrees) { - static const String directions[] = {"\uf044", "\uf043", "\uf048", "\uf087", "\uf058", "\uf057", "\uf04d", "\uf088"}; + const String directions[] = {"\uf044", "\uf043", "\uf048", "\uf087", "\uf058", "\uf057", "\uf04d", "\uf088"}; int index = (degrees + 22) / 45; if (index >= 8) { index = 0; @@ -1234,8 +1234,8 @@ void prepareNFCReq(const uint8_t *dst, const char *url) { } void prepareLUTreq(const uint8_t *dst, const String &input) { - const char *delimiters = ", \t"; - const int maxValues = 76; + constexpr const char *delimiters = ", \t"; + constexpr const int maxValues = 76; uint8_t waveform[maxValues]; char *ptr = strtok(const_cast(input.c_str()), delimiters); int i = 0; @@ -1269,7 +1269,7 @@ void getTemplate(JsonDocument &json, const uint8_t id, const uint8_t hwtype) { StaticJsonDocument<2048> doc; const String idstr = String(id); - const char *templateKey = "template"; + constexpr const char *templateKey = "template"; char filename[20]; snprintf(filename, sizeof(filename), "/tagtypes/%02X.json", hwtype); diff --git a/ESP32_AP-Flasher/src/ips_display.cpp b/ESP32_AP-Flasher/src/ips_display.cpp index 7d5043f4..4478c9bc 100644 --- a/ESP32_AP-Flasher/src/ips_display.cpp +++ b/ESP32_AP-Flasher/src/ips_display.cpp @@ -17,8 +17,7 @@ int32_t tftid = -1; int32_t findId(uint8_t mac[8]) { for (uint32_t c = 0; c < tagDB.size(); c++) { - tagRecord* tag = nullptr; - tag = tagDB.at(c); + tagRecord* tag = tagDB.at(c); if (memcmp(tag->mac, mac, 8) == 0) { return c; } @@ -65,8 +64,7 @@ void yellow_ap_display_loop(void) { // if ((uint32_t)WiFi.localIP() == (uint32_t)0) {} - tagRecord* tag = nullptr; - tag = tagDB.at(tftid); + tagRecord* tag = tagDB.at(tftid); if (tag->pending) { String filename = tag->filename; fs::File file = contentFS->open(filename); @@ -83,7 +81,7 @@ void yellow_ap_display_loop(void) { void* spriteData = spr.getPointer(); size_t bytesRead = file.readBytes((char*)spriteData, spr.width() * spr.height() * 2); file.close(); - spr.pushSprite(0,0); + spr.pushSprite(0, 0); struct espXferComplete xfc = {0}; memcpy(xfc.src, tag->mac, 8); diff --git a/ESP32_AP-Flasher/src/language.cpp b/ESP32_AP-Flasher/src/language.cpp index e639c2cf..88efff8c 100644 --- a/ESP32_AP-Flasher/src/language.cpp +++ b/ESP32_AP-Flasher/src/language.cpp @@ -5,6 +5,32 @@ #include "settings.h" #include "tag_db.h" +int defaultLanguage = 0; + +String languageList[] = {"EN - English", "NL - Nederlands", "DE - Deutsch"}; + +/*EN English language section*/ +String languageEnDaysShort[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; +String languageEnDays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; +String languageEnMonth[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; +/*END English language section END*/ + +/*NL Dutch language section*/ +String languageNlDaysShort[] = {"ZO", "MA", "DI", "WO", "DO", "VR", "ZA"}; +String languageNlDays[] = {"zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag"}; +String languageNlMonth[] = {"januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"}; +/*END Dutch language section END*/ + +/*DE German language section*/ +String languageDeDaysShort[] = {"SO", "MO", "DI", "MI", "DO", "FR", "SA"}; +String languageDeDays[] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"}; +String languageDeMonth[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"}; +/*END German language section END*/ + +String* languageDaysShort[] = {languageEnDaysShort, languageNlDaysShort, languageDeDaysShort}; +String* languageDays[] = {languageEnDays, languageNlDays, languageDeDays}; +String* languageMonth[] = {languageEnMonth, languageNlMonth, languageDeMonth}; + int currentLanguage = defaultLanguage; void updateLanguageFromConfig() { diff --git a/ESP32_AP-Flasher/src/newproto.cpp b/ESP32_AP-Flasher/src/newproto.cpp index 9dcc4e1a..9a881393 100644 --- a/ESP32_AP-Flasher/src/newproto.cpp +++ b/ESP32_AP-Flasher/src/newproto.cpp @@ -13,6 +13,7 @@ #include "system.h" #include "tag_db.h" #include "udp.h" +#include "util.h" #include "web.h" extern uint16_t sendBlock(const void* data, const uint16_t len); @@ -34,14 +35,15 @@ bool checkCRC(void* p, uint8_t len) { return ((uint8_t*)p)[0] == total; } -uint8_t* getDataForFile(fs::File* file) { - uint8_t* ret = nullptr; - ret = (uint8_t*)malloc(file->size()); +uint8_t* getDataForFile(fs::File& file) { + const size_t fileSize = file.size(); + uint8_t* ret = (uint8_t*)malloc(fileSize); if (ret) { - file->seek(0); - file->readBytes((char*)ret, file->size()); + file.seek(0); + file.readBytes((char*)ret, fileSize); } else { - Serial.print("malloc failed for file...\n"); + Serial.printf("malloc failed for file with size %d\n", fileSize); + util::printHeap(); } return ret; } @@ -51,8 +53,7 @@ void prepareCancelPending(const uint8_t dst[8]) { memcpy(pending.targetMac, dst, 8); sendCancelPending(&pending); - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(dst); + tagRecord* taginfo = tagRecord::findByMAC(dst); if (taginfo == nullptr) { wsErr("Tag not found, this shouldn't happen."); return; @@ -71,9 +72,7 @@ void prepareIdleReq(const uint8_t* dst, uint16_t nextCheckin) { pending.availdatainfo.nextCheckIn = nextCheckin; pending.attemptsLeft = 10 + config.maxsleep; - char buffer[64]; - sprintf(buffer, ">SDA %02X%02X%02X%02X%02X%02X%02X%02X NOP\n", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); - Serial.print(buffer); + Serial.printf(">SDA %02X%02X%02X%02X%02X%02X%02X%02X NOP\n", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); sendDataAvail(&pending); } } @@ -115,11 +114,11 @@ void prepareDataAvail(uint8_t* data, uint16_t len, uint8_t dataType, const uint8 wsSendTaginfo(dst, SYNC_TAGSTATUS); } -bool prepareDataAvail(String* filename, uint8_t dataType, const uint8_t* dst, uint16_t nextCheckin) { +bool prepareDataAvail(String& filename, uint8_t dataType, const uint8_t* dst, uint16_t nextCheckin) { if (nextCheckin > config.maxsleep) nextCheckin = config.maxsleep; if (wsClientCount() && config.stopsleep == 1) nextCheckin = 0; #ifdef YELLOW_IPS_AP - if (*filename == "direct") { + if (filename == "direct") { char dst_path[64]; sprintf(dst_path, "/current/%02X%02X%02X%02X%02X%02X%02X%02X.raw\0", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); contentFS->remove(dst_path); @@ -127,26 +126,25 @@ bool prepareDataAvail(String* filename, uint8_t dataType, const uint8_t* dst, ui } #endif - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(dst); + tagRecord* taginfo = tagRecord::findByMAC(dst); if (taginfo == nullptr) { wsErr("Tag not found, this shouldn't happen."); return true; } - *filename = "/" + *filename; + filename = "/" + filename; Storage.begin(); - if (!contentFS->exists(*filename)) { - wsErr("File not found. " + *filename); + if (!contentFS->exists(filename)) { + wsErr("File not found. " + filename); return false; } - fs::File file = contentFS->open(*filename); + fs::File file = contentFS->open(filename); uint32_t filesize = file.size(); if (filesize == 0) { file.close(); - wsErr("File has size 0. " + *filename); + wsErr("File has size 0. " + filename); return false; } @@ -166,8 +164,8 @@ bool prepareDataAvail(String* filename, uint8_t dataType, const uint8_t* dst, ui if (memcmp(md5bytes, taginfo->md5pending, 16) == 0) { wsLog("new image is the same as current or already pending image. not updating tag."); wsSendTaginfo(dst, SYNC_TAGSTATUS); - if (contentFS->exists(*filename)) { - contentFS->remove(*filename); + if (contentFS->exists(filename)) { + contentFS->remove(filename); } return true; } @@ -190,15 +188,15 @@ bool prepareDataAvail(String* filename, uint8_t dataType, const uint8_t* dst, ui if (contentFS->exists(dst_path)) { contentFS->remove(dst_path); } - contentFS->rename(*filename, dst_path); - *filename = String(dst_path); + contentFS->rename(filename, dst_path); + filename = String(dst_path); wsLog("new image: " + String(dst_path)); time_t now; time(&now); taginfo->expectedNextCheckin = now + nextCheckin * 60 + 60; clearPending(taginfo); - taginfo->filename = *filename; + taginfo->filename = filename; taginfo->len = filesize; taginfo->dataType = dataType; taginfo->pending = true; @@ -206,7 +204,7 @@ bool prepareDataAvail(String* filename, uint8_t dataType, const uint8_t* dst, ui } else { wsLog("firmware upload pending"); clearPending(taginfo); - taginfo->filename = *filename; + taginfo->filename = filename; taginfo->len = filesize; taginfo->dataType = dataType; taginfo->pending = true; @@ -222,22 +220,18 @@ bool prepareDataAvail(String* filename, uint8_t dataType, const uint8_t* dst, ui pending.attemptsLeft = attempts; checkMirror(taginfo, &pending); if (taginfo->isExternal == false) { - char buffer[64]; - sprintf(buffer, ">SDA %02X%02X%02X%02X%02X%02X%02X%02X TYPE 0x%02X\n\0", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0], pending.availdatainfo.dataType); - Serial.print(buffer); + Serial.printf(">SDA %02X%02X%02X%02X%02X%02X%02X%02X TYPE 0x%02X\n", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0], pending.availdatainfo.dataType); sendDataAvail(&pending); } else { udpsync.netSendDataAvail(&pending); } wsSendTaginfo(dst, SYNC_TAGSTATUS); - return true; } void prepareExternalDataAvail(struct pendingData* pending, IPAddress remoteIP) { - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(pending->targetMac); + tagRecord* taginfo = tagRecord::findByMAC(pending->targetMac); if (taginfo == nullptr) { return; } @@ -335,8 +329,7 @@ void processBlockRequest(struct espBlockRequest* br) { return; } - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(br->src); + tagRecord* taginfo = tagRecord::findByMAC(br->src); if (taginfo == nullptr) { prepareCancelPending(br->src); Serial.printf("blockrequest: couldn't find taginfo %02X%02X%02X%02X%02X%02X%02X%02X\n", br->src[7], br->src[6], br->src[5], br->src[4], br->src[3], br->src[2], br->src[1], br->src[0]); @@ -351,7 +344,7 @@ void processBlockRequest(struct espBlockRequest* br) { prepareCancelPending(br->src); return; } - taginfo->data = getDataForFile(&file); + taginfo->data = getDataForFile(file); file.close(); } @@ -400,8 +393,7 @@ void processXferComplete(struct espXferComplete* xfc, bool local) { time_t now; time(&now); - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(xfc->src); + tagRecord* taginfo = tagRecord::findByMAC(xfc->src); if (taginfo != nullptr) { memcpy(taginfo->md5, taginfo->md5pending, sizeof(taginfo->md5pending)); clearPending(taginfo); @@ -430,8 +422,7 @@ void processXferTimeout(struct espXferComplete* xfc, bool local) { time_t now; time(&now); - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(xfc->src); + tagRecord* taginfo = tagRecord::findByMAC(xfc->src); if (taginfo != nullptr) { taginfo->expectedNextCheckin = now + 60; memset(taginfo->md5pending, 0, 16 * sizeof(uint8_t)); @@ -445,8 +436,7 @@ void processDataReq(struct espAvailDataReq* eadr, bool local) { if (config.runStatus == RUNSTATUS_STOP) return; char buffer[64]; - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(eadr->src); + tagRecord* taginfo = tagRecord::findByMAC(eadr->src); if (taginfo == nullptr) { taginfo = new tagRecord; memcpy(taginfo->mac, eadr->src, sizeof(taginfo->mac)); @@ -528,8 +518,7 @@ void processDataReq(struct espAvailDataReq* eadr, bool local) { void refreshAllPending() { for (int16_t c = 0; c < tagDB.size(); c++) { - tagRecord* taginfo = nullptr; - taginfo = tagDB.at(c); + tagRecord* taginfo = tagDB.at(c); if (taginfo->pending) { clearPending(taginfo); taginfo->nextupdate = 0; @@ -541,8 +530,7 @@ void refreshAllPending() { }; void updateContent(const uint8_t* dst) { - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(dst); + tagRecord* taginfo = tagRecord::findByMAC(dst); if (taginfo != nullptr) { clearPending(taginfo); taginfo->nextupdate = 0; @@ -574,9 +562,7 @@ bool sendAPSegmentedData(const uint8_t* dst, String data, uint16_t icons, bool i pending.availdatainfo.dataTypeArgument = inverted; pending.availdatainfo.nextCheckIn = 0; pending.attemptsLeft = 120; - char buffer[64]; - sprintf(buffer, ">AP Segmented Data %02X%02X%02X%02X%02X%02X%02X%02X\n\0", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); - Serial.print(buffer); + Serial.printf(">AP Segmented Data %02X%02X%02X%02X%02X%02X%02X%02X\n\0", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); if (local) { return sendDataAvail(&pending); } else { @@ -594,9 +580,7 @@ bool showAPSegmentedInfo(const uint8_t* dst, bool local) { pending.availdatainfo.dataTypeArgument = 0; pending.availdatainfo.nextCheckIn = 0; pending.attemptsLeft = 120; - char buffer[64]; - sprintf(buffer, ">SDA %02X%02X%02X%02X%02X%02X%02X%02X\n\0", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); - Serial.print(buffer); + Serial.printf(">SDA %02X%02X%02X%02X%02X%02X%02X%02X\n\0", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); if (local) { return sendDataAvail(&pending); } else { @@ -612,9 +596,7 @@ bool sendTagCommand(const uint8_t* dst, uint8_t cmd, bool local) { pending.availdatainfo.dataTypeArgument = cmd; pending.availdatainfo.nextCheckIn = 0; pending.attemptsLeft = 120; - char buffer[64]; - sprintf(buffer, ">Tag CMD %02X%02X%02X%02X%02X%02X%02X%02X\n\0", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); - Serial.print(buffer); + Serial.printf(">Tag CMD %02X%02X%02X%02X%02X%02X%02X%02X\n\0", dst[7], dst[6], dst[5], dst[4], dst[3], dst[2], dst[1], dst[0]); if (local) { return sendDataAvail(&pending); } else { @@ -624,8 +606,7 @@ bool sendTagCommand(const uint8_t* dst, uint8_t cmd, bool local) { } void updateTaginfoitem(struct TagInfo* taginfoitem) { - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(taginfoitem->mac); + tagRecord* taginfo = tagRecord::findByMAC(taginfoitem->mac); if (taginfo == nullptr) { taginfo = new tagRecord; @@ -673,8 +654,7 @@ void updateTaginfoitem(struct TagInfo* taginfoitem) { bool checkMirror(struct tagRecord* taginfo, struct pendingData* pending) { for (int16_t c = 0; c < tagDB.size(); c++) { - tagRecord* taginfo2 = nullptr; - taginfo2 = tagDB.at(c); + tagRecord* taginfo2 = tagDB.at(c); if (taginfo2->contentMode == 20) { DynamicJsonDocument doc(500); deserializeJson(doc, taginfo2->modeConfigJson); @@ -686,7 +666,7 @@ bool checkMirror(struct tagRecord* taginfo, struct pendingData* pending) { if (!file) { return false; } - taginfo->data = getDataForFile(&file); + taginfo->data = getDataForFile(file); file.close(); } diff --git a/ESP32_AP-Flasher/src/serialap.cpp b/ESP32_AP-Flasher/src/serialap.cpp index 8d1afe94..de8a2e4a 100644 --- a/ESP32_AP-Flasher/src/serialap.cpp +++ b/ESP32_AP-Flasher/src/serialap.cpp @@ -40,7 +40,7 @@ struct espSetChannelPower curChannel = {0, 11, 10}; volatile uint32_t lastAPActivity = 0; struct APInfoS apInfo; -extern uint8_t* getDataForFile(File* file); +extern uint8_t* getDataForFile(File& file); struct rxCmd { uint8_t* data; @@ -207,16 +207,16 @@ bool sendDataAvail(struct pendingData* pending) { for (uint8_t c = 0; c < sizeof(struct pendingData); c++) { AP_SERIAL_PORT.write(((uint8_t*)pending)[c]); } - if (waitCmdReply()) goto sdasend; + if (waitCmdReply()) { + txEnd(); + return true; + } Serial.printf("SDA send failed in try %d\n", attempt); delay(200); } Serial.print("SDA failed to send...\n"); txEnd(); return false; -sdasend: - txEnd(); - return true; } bool sendCancelPending(struct pendingData* pending) { if (!apInfo.isOnline) return false; @@ -228,15 +228,15 @@ bool sendCancelPending(struct pendingData* pending) { for (uint8_t c = 0; c < sizeof(struct pendingData); c++) { AP_SERIAL_PORT.write(((uint8_t*)pending)[c]); } - if (waitCmdReply()) goto cxdsent; + if (waitCmdReply()) { + txEnd(); + return true; + } Serial.printf("CXD send failed in try %d\n", attempt); } Serial.print("CXD failed to send...\n"); txEnd(); return false; -cxdsent: - txEnd(); - return true; } bool sendChannelPower(struct espSetChannelPower* scp) { if ((apInfo.state != AP_STATE_ONLINE) && (apInfo.state != AP_STATE_COMING_ONLINE)) return false; @@ -248,41 +248,41 @@ bool sendChannelPower(struct espSetChannelPower* scp) { for (uint8_t c = 0; c < sizeof(struct espSetChannelPower); c++) { AP_SERIAL_PORT.write(((uint8_t*)scp)[c]); } - if (waitCmdReply()) goto scpSent; + if (waitCmdReply()) { + txEnd(); + return true; + } Serial.printf("SCP send failed in try %d\n", attempt); } Serial.print("SCP failed to send...\n"); txEnd(); return false; -scpSent: - txEnd(); - return true; } bool sendPing() { if (!txStart()) return false; for (uint8_t attempt = 0; attempt < 5; attempt++) { cmdReplyValue = CMD_REPLY_WAIT; AP_SERIAL_PORT.print("RDY?"); - if (waitCmdReply()) goto pingSent; + if (waitCmdReply()) { + txEnd(); + return true; + } } txEnd(); return false; -pingSent: - txEnd(); - return true; } bool sendGetInfo() { if (!txStart()) return false; for (uint8_t attempt = 0; attempt < 5; attempt++) { cmdReplyValue = CMD_REPLY_WAIT; AP_SERIAL_PORT.print("NFO?"); - if (waitCmdReply()) goto nfoRequested; + if (waitCmdReply()) { + txEnd(); + return true; + } } txEnd(); return false; -nfoRequested: - txEnd(); - return true; } // add RX'd request from the AP to the processor queue diff --git a/ESP32_AP-Flasher/src/system.cpp b/ESP32_AP-Flasher/src/system.cpp index d73abd3a..e69cade6 100644 --- a/ESP32_AP-Flasher/src/system.cpp +++ b/ESP32_AP-Flasher/src/system.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include "storage.h" #include "tag_db.h" diff --git a/ESP32_AP-Flasher/src/tag_db.cpp b/ESP32_AP-Flasher/src/tag_db.cpp index a99b3e34..dc3007d0 100644 --- a/ESP32_AP-Flasher/src/tag_db.cpp +++ b/ESP32_AP-Flasher/src/tag_db.cpp @@ -22,9 +22,7 @@ Config config; // SemaphoreHandle_t tagDBOwner; tagRecord* tagRecord::findByMAC(const uint8_t mac[8]) { - for (int32_t c = 0; c < tagDB.size(); c++) { - tagRecord* tag = nullptr; - tag = tagDB.at(c); + for (tagRecord* tag : tagDB) { if (memcmp(tag->mac, mac, 8) == 0) { return tag; } @@ -33,7 +31,7 @@ tagRecord* tagRecord::findByMAC(const uint8_t mac[8]) { } bool deleteRecord(const uint8_t mac[8]) { - for (int32_t c = 0; c < tagDB.size(); c++) { + for (uint32_t c = 0; c < tagDB.size(); c++) { tagRecord* tag = tagDB.at(c); if (memcmp(tag->mac, mac, 8) == 0) { if (tag->data != nullptr) { @@ -73,18 +71,10 @@ String tagDBtoJson(const uint8_t mac[8], uint8_t startPos) { DynamicJsonDocument doc(5000); JsonArray tags = doc.createNestedArray("tags"); - for (int16_t c = startPos; c < tagDB.size(); c++) { - tagRecord* taginfo = nullptr; - taginfo = tagDB.at(c); + for (uint32_t c = startPos; c < tagDB.size(); ++c) { + const tagRecord* taginfo = tagDB.at(c); - bool select = false; - if (mac) { - if (memcmp(taginfo->mac, mac, 8) == 0) { - select = true; - } - } else { - select = true; - } + const bool select = !mac || memcmp(taginfo->mac, mac, 8) == 0; if (select) { JsonObject tag = tags.createNestedObject(); fillNode(tag, taginfo); @@ -92,15 +82,17 @@ String tagDBtoJson(const uint8_t mac[8], uint8_t startPos) { break; } } + if (doc.capacity() - doc.memoryUsage() < doc.memoryUsage() / (c + 1) + 500) { doc["continu"] = c + 1; break; } } + return doc.as(); } -void fillNode(JsonObject& tag, tagRecord*& taginfo) { +void fillNode(JsonObject& tag, const tagRecord* taginfo) { char hexmac[17]; mac2hex(taginfo->mac, hexmac); tag["mac"] = String(hexmac); @@ -130,10 +122,10 @@ void fillNode(JsonObject& tag, tagRecord*& taginfo) { tag["ver"] = taginfo->tagSoftwareVersion; } -void saveDB(String filename) { +void saveDB(const String& filename) { DynamicJsonDocument doc(2500); - long t = millis(); + const long t = millis(); Storage.begin(); fs::File file = contentFS->open(filename, "w"); @@ -143,11 +135,9 @@ void saveDB(String filename) { } file.write('['); - - for (int32_t c = 0; c < tagDB.size(); c++) { + for (size_t c = 0; c < tagDB.size(); c++) { + const tagRecord* taginfo = tagDB.at(c); doc.clear(); - tagRecord* taginfo = nullptr; - taginfo = tagDB.at(c); JsonObject tag = doc.createNestedObject(); fillNode(tag, taginfo); @@ -160,15 +150,11 @@ void saveDB(String filename) { file.close(); Serial.println("DB saved " + String(millis() - t) + "ms"); - - return; } -void loadDB(String filename) { - StaticJsonDocument<1000> doc; - +void loadDB(const String& filename) { Serial.println("reading DB from file"); - long t = millis(); + const long t = millis(); Storage.begin(); fs::File readfile = contentFS->open(filename, "r"); @@ -182,6 +168,7 @@ void loadDB(String filename) { bool parsing = true; if (readfile.find("[")) { + StaticJsonDocument<1000> doc; while (parsing) { DeserializationError err = deserializeJson(doc, readfile); if (!err) { @@ -189,8 +176,7 @@ void loadDB(String filename) { String dst = tag["mac"].as(); uint8_t mac[8]; if (hex2mac(dst, mac)) { - tagRecord* taginfo = nullptr; - taginfo = tagRecord::findByMAC(mac); + tagRecord* taginfo = tagRecord::findByMAC(mac); if (taginfo == nullptr) { taginfo = new tagRecord; memcpy(taginfo->mac, mac, sizeof(taginfo->mac)); @@ -198,7 +184,7 @@ void loadDB(String filename) { } String md5 = tag["hash"].as(); if (md5.length() >= 32) { - for (int i = 0; i < 16; i++) { + for (uint8_t i = 0; i < 16; i++) { taginfo->md5[i] = strtoul(md5.substring(i * 2, i * 2 + 2).c_str(), NULL, 16); } } @@ -236,22 +222,20 @@ void loadDB(String filename) { } readfile.close(); - return; + Serial.println("loadDB took " + String(millis() - t) + "ms"); } void destroyDB() { Serial.println("destoying DB"); util::printHeap(); - for (uint32_t c = 0; c < tagDB.size(); c++) { - tagRecord* tag = nullptr; - tag = tagDB.at(c); + for (tagRecord*& tag : tagDB) { if (tag->data != nullptr) { free(tag->data); } tag->data = nullptr; - delete tagDB[c]; - tagDB.erase(tagDB.begin() + c); + delete tag; } + tagDB.clear(); util::printHeap(); } @@ -264,19 +248,15 @@ uint32_t getTagCount(uint32_t& timeoutcount) { uint32_t tagcount = 0; time_t now; time(&now); - for (uint32_t c = 0; c < tagDB.size(); c++) { - tagRecord* taginfo = nullptr; - taginfo = tagDB.at(c); - if (taginfo->isExternal == false) tagcount++; - int32_t timeout = now - taginfo->lastseen; + for (const tagRecord* taginfo : tagDB) { + if (!taginfo->isExternal) tagcount++; + const int32_t timeout = now - taginfo->lastseen; if (taginfo->expectedNextCheckin < 3600) { // not initialised, timeout if not seen last 10 minutes if (timeout > 600) timeoutcount++; - } else { - if (now - taginfo->expectedNextCheckin > 600) { - //expected checkin is behind, timeout if not seen last 10 minutes - if (timeout > 600) timeoutcount++; - } + } else if (now - taginfo->expectedNextCheckin > 600) { + //expected checkin is behind, timeout if not seen last 10 minutes + if (timeout > 600) timeoutcount++; } } return tagcount; @@ -287,10 +267,14 @@ void clearPending(tagRecord* taginfo) { if (taginfo->data != nullptr) { // check if this is the last copy of the buffer int datacount = 0; - for (uint32_t c = 0; c < tagDB.size(); c++) { - if (tagDB.at(c)->data == taginfo->data) datacount++; + for (const tagRecord* tag : tagDB) { + if (tag->data == taginfo->data) { + datacount++; + } + } + if (datacount == 1) { + free(taginfo->data); } - if (datacount == 1) free(taginfo->data); taginfo->data = nullptr; } taginfo->pending = false; @@ -342,7 +326,7 @@ void saveAPconfig() { configFile.close(); } -HwType getHwType(uint8_t id) { +HwType getHwType(const uint8_t id) { try { return hwdata.at(id); } catch (const std::out_of_range&) { @@ -383,13 +367,13 @@ bool setVarDB(const std::string& key, const String& value) { newVar.changed = true; varDB[key] = newVar; return true; + } + + if (it->second.value != value) { + it->second.value = value; + it->second.changed = true; + return true; } else { - if (it->second.value != value) { - it->second.value = value; - it->second.changed = true; - return true; - } else { - return false; - } + return false; } } diff --git a/ESP32_AP-Flasher/src/web.cpp b/ESP32_AP-Flasher/src/web.cpp index 01dd48c9..c03c14b1 100644 --- a/ESP32_AP-Flasher/src/web.cpp +++ b/ESP32_AP-Flasher/src/web.cpp @@ -116,8 +116,7 @@ void wsSendTaginfo(const uint8_t *mac, uint8_t syncMode) { xSemaphoreGive(wsMutex); } if (syncMode > SYNC_NOSYNC) { - tagRecord *taginfo = nullptr; - taginfo = tagRecord::findByMAC(mac); + const tagRecord *taginfo = tagRecord::findByMAC(mac); if (taginfo != nullptr) { if (taginfo->contentMode != 12 || syncMode == SYNC_DELETE) { UDPcomm udpsync; @@ -237,8 +236,7 @@ void init_web() { String dst = request->getParam("mac")->value(); uint8_t mac[8]; if (hex2mac(dst, mac)) { - tagRecord *taginfo = nullptr; - taginfo = tagRecord::findByMAC(mac); + const tagRecord *taginfo = tagRecord::findByMAC(mac); if (taginfo != nullptr) { if (taginfo->pending == true) { request->send_P(200, "application/octet-stream", taginfo->data, taginfo->len); @@ -255,8 +253,7 @@ void init_web() { String dst = request->getParam("mac", true)->value(); uint8_t mac[8]; if (hex2mac(dst, mac)) { - tagRecord *taginfo = nullptr; - taginfo = tagRecord::findByMAC(mac); + tagRecord *taginfo = tagRecord::findByMAC(mac); if (taginfo != nullptr) { taginfo->alias = request->getParam("alias", true)->value(); taginfo->modeConfigJson = request->getParam("modecfgjson", true)->value(); @@ -285,8 +282,7 @@ void init_web() { if (request->hasParam("mac", true) && request->hasParam("cmd", true)) { uint8_t mac[8]; if (hex2mac(request->getParam("mac", true)->value(), mac)) { - tagRecord *taginfo = nullptr; - taginfo = tagRecord::findByMAC(mac); + tagRecord *taginfo = tagRecord::findByMAC(mac); if (taginfo != nullptr) { const char *cmdValue = request->getParam("cmd", true)->value().c_str(); if (strcmp(cmdValue, "del") == 0) { @@ -533,8 +529,7 @@ void doImageUpload(AsyncWebServerRequest *request, String filename, size_t index String dst = request->getParam("mac", true)->value(); uint8_t mac[8]; if (hex2mac(dst, mac)) { - tagRecord *taginfo = nullptr; - taginfo = tagRecord::findByMAC(mac); + tagRecord *taginfo = tagRecord::findByMAC(mac); if (taginfo != nullptr) { bool dither = true; if (request->hasParam("dither", true)) { @@ -575,8 +570,7 @@ void doJsonUpload(AsyncWebServerRequest *request) { } file.print(request->getParam("json", true)->value()); file.close(); - tagRecord *taginfo = nullptr; - taginfo = tagRecord::findByMAC(mac); + tagRecord *taginfo = tagRecord::findByMAC(mac); if (taginfo != nullptr) { uint32_t ttl = 0; if (request->hasParam("ttl", true)) { diff --git a/ESP32_AP-Flasher/src/wifimanager.cpp b/ESP32_AP-Flasher/src/wifimanager.cpp index 5cff45db..80fd1e50 100644 --- a/ESP32_AP-Flasher/src/wifimanager.cpp +++ b/ESP32_AP-Flasher/src/wifimanager.cpp @@ -28,7 +28,7 @@ WifiManager::WifiManager() { } void WifiManager::poll() { - if (wifiStatus == AP && millis() > _nextReconnectCheck && _ssid!="") { + if (wifiStatus == AP && millis() > _nextReconnectCheck && _ssid != "") { if (apClients == 0) { Serial.println("Attempting to reconnect to WiFi."); _APstarted = false; @@ -239,7 +239,7 @@ void WifiManager::WiFiEvent(WiFiEvent_t event) { #endif std::vector getLocalUrl() { - return { String("http://" + WiFi.localIP().toString()).c_str() }; + return {String("http://" + WiFi.localIP().toString()).c_str()}; } void onErrorCallback(improv::Error err) {