From a799dad5e51b90cfedf4ca4fb98f2f580366cd82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= Date: Mon, 15 Jan 2018 23:22:21 +0100 Subject: [PATCH] Some bugfixes * Fix type error '%u' insted of '%d' for unsigned integer in NTPClient::getUptimeString (AvrNTPClient.cpp) * Fix memory leak in NTPClient::setNtpServerName (ESPNTPClent.cpp) * Fix logical error in NTPClient::setTimeZone (AvrNTPClient.cpp) * Test for for UTC-12:00 => timeZone => UTC+14:00 in NTPClient::setTimeZone (AvrNTPClient.cpp) See https://en.wikipedia.org/wiki/Time_zone#List_of_UTC_offsets for valid UTC offsets * Convenience: Added Visual Studio Code workspace folder to .gitignore --- .gitignore | 3 +++ src/AvrNTPClient.cpp | 20 ++++++++++---------- src/ESPNTPClient.cpp | 19 ++++++++++--------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 72e8bb0..4fa627c 100644 --- a/.gitignore +++ b/.gitignore @@ -217,3 +217,6 @@ src/examples/NTPClient/__vm/ *.sln examples/NTPClient/__vm/ examples/NTPClientESP8266/WifiConfigGM.h + +# Visual Studio Code workspace folder +.vscode/ diff --git a/src/AvrNTPClient.cpp b/src/AvrNTPClient.cpp index 9ab227c..1020dc9 100644 --- a/src/AvrNTPClient.cpp +++ b/src/AvrNTPClient.cpp @@ -68,7 +68,7 @@ boolean sendNTPpacket(IPAddress &address, EthernetUDP udp) { ntpPacketBuffer[14] = 49; ntpPacketBuffer[15] = 52; // all NTP fields have been given values, now - // you can send a packet requesting a timestamp: + // you can send a packet requesting a timestamp: udp.beginPacket(address, 123); //NTP requests are to port 123 udp.write(ntpPacketBuffer, NTP_PACKET_SIZE); udp.endPacket(); @@ -130,14 +130,14 @@ time_t getTime() { setSyncInterval(NTP.getShortInterval()); // Retry connection more often if (onSyncEvent) onSyncEvent(noResponse); - return 0; // return 0 if unable to get the time + return 0; // return 0 if unable to get the time } else { DEBUGLOGCR(F("-- Invalid address :-((")); if (onSyncEvent) onSyncEvent(invalidAddress); udp.stop(); - return 0; // return 0 if unable to get the time + return 0; // return 0 if unable to get the time } } #elif NETWORK_TYPE == NETWORK_WIFI101 @@ -159,7 +159,7 @@ boolean sendNTPpacket(const char* address, WiFiUDP udp) { ntpPacketBuffer[14] = 49; ntpPacketBuffer[15] = 52; // all NTP fields have been given values, now - // you can send a packet requesting a timestamp: + // you can send a packet requesting a timestamp: udp.beginPacket(address, 123); //NTP requests are to port 123 udp.write(ntpPacketBuffer, NTP_PACKET_SIZE); udp.endPacket(); @@ -210,7 +210,7 @@ time_t getTime() { NTP.setLastNTPSync(timeValue); DEBUGLOG(F("Succeccful NTP sync at ")); DEBUGLOGCR(NTP.getTimeDateString(NTP.getLastNTPSync())); - + if (onSyncEvent) onSyncEvent(timeSyncd); return timeValue; @@ -221,7 +221,7 @@ time_t getTime() { setSyncInterval(NTP.getShortInterval()); // Retry connection more often if (onSyncEvent) onSyncEvent(noResponse); - return 0; // return 0 if unable to get the time + return 0; // return 0 if unable to get the time } #endif //NETWORK_TYPE @@ -304,7 +304,7 @@ String NTPClient::getTimeStr() { String NTPClient::getDateStr(time_t moment) { if ((timeStatus() != timeNotSet) || (moment != 0)) { String timeStr = ""; - + timeStr += printDigits(day(moment)); timeStr += "/"; timeStr += printDigits(month(moment)); @@ -420,7 +420,7 @@ boolean NTPClient::setInterval(int shortInterval, int longInterval) { boolean NTPClient::setTimeZone(int timeZone) { - if (timeZone >= -11 || timeZone <= 13) { + if (timeZone >= -12 && timeZone <= 14) { _timeZone = timeZone; DEBUGLOGCR(F("Time zone set to ")); DEBUGLOGCR(_timeZone); @@ -492,7 +492,7 @@ String NTPClient::getUptimeString() { String uptimeStr = ""; char buffer[20]; - sprintf(buffer, "%4d days %02d:%02d:%02d", days, hours, minutes, seconds); + sprintf(buffer, "%4u days %02d:%02d:%02d", days, hours, minutes, seconds); uptimeStr += buffer; return uptimeStr; @@ -517,4 +517,4 @@ boolean NTPClient::isSummerTimePeriod(time_t moment) { return summertime(year(), month(), day(), hour(), getTimeZone()); } -#endif // ARDUINO_ARCH_AVR \ No newline at end of file +#endif // ARDUINO_ARCH_AVR diff --git a/src/ESPNTPClient.cpp b/src/ESPNTPClient.cpp index 7d97264..28a0c1a 100644 --- a/src/ESPNTPClient.cpp +++ b/src/ESPNTPClient.cpp @@ -25,9 +25,9 @@ The views and conclusions contained in the software and documentation are those authors and should not be interpreted as representing official policies, either expressed or implied, of German Martin */ -// -// -// +// +// +// #ifdef ARDUINO_ARCH_ESP8266 @@ -51,12 +51,13 @@ NTPClient::NTPClient() bool NTPClient::setNtpServerName(String ntpServerName, int idx) { - char * buffer = (char *)malloc((ntpServerName.length()+1) * sizeof(char)); if ((idx >= 0) && (idx <= 2)) { sntp_stop(); - ntpServerName.toCharArray(buffer, ntpServerName.length()+1); + char* buffer = (char*)malloc((ntpServerName.length() + 1) * sizeof(char)); + ntpServerName.toCharArray(buffer, ntpServerName.length() + 1); sntp_setservername(idx, buffer); DEBUGLOG("NTP server %d set to: %s \r\n", idx, buffer); + free(buffer); sntp_init(); return true; } @@ -343,7 +344,7 @@ time_t NTPClient::getFirstSync() _firstSync = now() - getUptime(); } } - + return _firstSync; } @@ -352,7 +353,7 @@ String NTPClient::getUptimeString() { uint8 hours; uint8 minutes; uint8 seconds; - + long uptime = getUptime(); seconds = uptime % SECS_PER_MIN; @@ -363,7 +364,7 @@ String NTPClient::getUptimeString() { uptime -= hours * SECS_PER_HOUR; days = uptime / SECS_PER_DAY; - String uptimeStr = ""; + String uptimeStr = ""; char buffer[20]; sprintf(buffer, "%d days %02d:%02d:%02d", days, hours, minutes, seconds); uptimeStr += buffer; @@ -383,4 +384,4 @@ boolean NTPClient::isSummerTimePeriod(time_t moment) { return summertime(year(), month(), day(), hour(), getTimeZone()); } -#endif // ARDUINO_ARCH_ESP8266 \ No newline at end of file +#endif // ARDUINO_ARCH_ESP8266