From 6f9601ced916a745cba5e2ad3039455196e8a694 Mon Sep 17 00:00:00 2001 From: DanielR92 Date: Sat, 19 Oct 2024 19:45:15 +0200 Subject: [PATCH 1/4] perfomance boost with ip2addr --- src/utils/helper.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/utils/helper.cpp b/src/utils/helper.cpp index b30f58e0..f974901e 100644 --- a/src/utils/helper.cpp +++ b/src/utils/helper.cpp @@ -14,15 +14,17 @@ const char dayShortNames_P[] PROGMEM = STR_DAYNAME_3_CHAR_LIST; namespace ah { void ip2Arr(uint8_t ip[], const char *ipStr) { - uint8_t p = 1; memset(ip, 0, 4); - for(uint8_t i = 0; i < 16; i++) { - if(ipStr[i] == 0) - return; - if(0 == i) - ip[0] = atoi(ipStr); - else if(ipStr[i] == '.') - ip[p++] = atoi(&ipStr[i+1]); + uint8_t p = 0; + const char *start = ipStr; + + for (uint8_t i = 0; i < 4; i++) { + ip[i] = (uint8_t)strtol(start, (char**)&start, 10); + if (*start == '.') { + start++; + } else if (*start == '\0') { + break; + } } } From 3290d1223c022c9fc4d53252fb54d3a7e18df393 Mon Sep 17 00:00:00 2001 From: DanielR92 Date: Sat, 19 Oct 2024 20:04:28 +0200 Subject: [PATCH 2/4] round with mathematics --- src/utils/helper.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/utils/helper.cpp b/src/utils/helper.cpp index f974901e..f57bf368 100644 --- a/src/utils/helper.cpp +++ b/src/utils/helper.cpp @@ -6,6 +6,7 @@ #include "helper.h" #include "dbg.h" #include "../plugins/plugin_lang.h" +#include #define dt_SHORT_STR_LEN_i18n 3 // the length of short strings static char buffer_i18n[dt_SHORT_STR_LEN_i18n + 1]; // must be big enough for longest string and the terminating null @@ -37,11 +38,15 @@ namespace ah { } double round1(double value) { - return (int)(value * 10 + 0.5) / 10.0; + return round(value * 10) / 10.0; + } + + double round2(double value) { + return round(value * 100) / 100.0; } double round3(double value) { - return (int)(value * 1000 + 0.5) / 1000.0; + return round(value * 1000) / 1000.0; } String getDateTimeStr(time_t t) { From 7e0bf3eacdc315a9c7bdc365bbd7cf893a5997c3 Mon Sep 17 00:00:00 2001 From: DanielR92 Date: Mon, 21 Oct 2024 15:41:17 +0200 Subject: [PATCH 3/4] remove 'uint8_t p' --- src/utils/helper.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/helper.cpp b/src/utils/helper.cpp index f57bf368..70de3ec4 100644 --- a/src/utils/helper.cpp +++ b/src/utils/helper.cpp @@ -16,7 +16,6 @@ const char dayShortNames_P[] PROGMEM = STR_DAYNAME_3_CHAR_LIST; namespace ah { void ip2Arr(uint8_t ip[], const char *ipStr) { memset(ip, 0, 4); - uint8_t p = 0; const char *start = ipStr; for (uint8_t i = 0; i < 4; i++) { From bc746643e76a460423eca1f8915cff2902328249 Mon Sep 17 00:00:00 2001 From: DanielR92 Date: Mon, 21 Oct 2024 18:57:12 +0200 Subject: [PATCH 4/4] test --- src/network/AhoyNetwork.h | 58 ++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/src/network/AhoyNetwork.h b/src/network/AhoyNetwork.h index 1945fbb4..d0e26707 100644 --- a/src/network/AhoyNetwork.h +++ b/src/network/AhoyNetwork.h @@ -13,6 +13,9 @@ #include "AsyncJson.h" #include +#include +#include + #define NTP_PACKET_SIZE 48 class AhoyNetwork { public: @@ -91,6 +94,7 @@ class AhoyNetwork { ip_addr_t ipaddr; mNtpIp = WiFi.gatewayIP(); + // dns_gethostbyname runs asynchronous and sets the member mNtpIp which is then checked on // next call of updateNtpTime err_t err = dns_gethostbyname(mConfig->ntp.addr, &ipaddr, dnsCallback, this); @@ -102,14 +106,19 @@ class AhoyNetwork { mNtpIp = ipaddr.addr; #endif startNtpUpdate(); - } + } else if (err != ERR_INPROGRESS) { + // Handle DNS error (other than ERR_INPROGRESS) + DPRINTLN(DBG_ERROR, F("DNS lookup failed")); + mOnTimeCB(0); // Signal failure } + } protected: void startNtpUpdate() { DPRINTLN(DBG_INFO, F("get time from: ") + mNtpIp.toString()); if (!mUdp.connected()) { if (!mUdp.connect(mNtpIp, mConfig->ntp.port)) { + DPRINTLN(DBG_ERROR, F("UDP connection failed")); mOnTimeCB(0); return; } @@ -224,15 +233,30 @@ class AhoyNetwork { } } - void sortRSSI(int *sort, int n) { - for (int i = 0; i < n; i++) - sort[i] = i; - for (int i = 0; i < n; i++) - for (int j = i + 1; j < n; j++) - if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i])) - std::swap(sort[i], sort[j]); + /** + * @brief Sorts the indices of WiFi networks based on their RSSI values in descending order. + * + * This function takes an array of integers and its size, and sorts the array such that + * the indices correspond to WiFi networks sorted by their RSSI values from highest to lowest. + * + * @param sort Pointer to an array of integers where the sorted indices will be stored. + * @param n The number of WiFi networks (size of the array). + */ + void sortRSSI(int *sort, int n) { + std::vector indices(n); + for (int i = 0; i < n; ++i) { + indices[i] = i; } + std::sort(indices.begin(), indices.end(), [](int a, int b) { + return WiFi.RSSI(a) > WiFi.RSSI(b); + }); + + for (int i = 0; i < n; ++i) { + sort[i] = indices[i]; + } + } + protected: void sendNTPpacket(void) { uint8_t buf[NTP_PACKET_SIZE]; @@ -246,13 +270,21 @@ class AhoyNetwork { mUdp.write(buf, NTP_PACKET_SIZE); } + /** + * @brief Handles an incoming NTP packet and extracts the time. + * + * This function processes an NTP packet received via UDP. It checks if the packet + * is of valid length, extracts the NTP timestamp, and invokes a callback with the + * extracted time. If the packet is too small to contain valid NTP data, it signals + * an error via the callback. + * + * @param packet The received UDP packet containing NTP data. + */ void handleNTPPacket(AsyncUDPPacket packet) { - char buf[80]; - - memcpy(buf, packet.data(), sizeof(buf)); + const uint8_t* data = packet.data(); - unsigned long highWord = word(buf[40], buf[41]); - unsigned long lowWord = word(buf[42], buf[43]); + unsigned long highWord = word(data[40], data[41]); + unsigned long lowWord = word(data[42], data[43]); // combine the four bytes (two words) into a long integer // this is NTP time (seconds since Jan 1 1900):