Skip to content

Commit

Permalink
Some bugfixes
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
toneomgomg committed Jan 15, 2018
1 parent 41560eb commit a799dad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,6 @@ src/examples/NTPClient/__vm/
*.sln
examples/NTPClient/__vm/
examples/NTPClientESP8266/WifiConfigGM.h

# Visual Studio Code workspace folder
.vscode/
20 changes: 10 additions & 10 deletions src/AvrNTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -517,4 +517,4 @@ boolean NTPClient::isSummerTimePeriod(time_t moment) {
return summertime(year(), month(), day(), hour(), getTimeZone());
}

#endif // ARDUINO_ARCH_AVR
#endif // ARDUINO_ARCH_AVR
19 changes: 10 additions & 9 deletions src/ESPNTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
}
Expand Down Expand Up @@ -343,7 +344,7 @@ time_t NTPClient::getFirstSync()
_firstSync = now() - getUptime();
}
}

return _firstSync;
}

Expand All @@ -352,7 +353,7 @@ String NTPClient::getUptimeString() {
uint8 hours;
uint8 minutes;
uint8 seconds;

long uptime = getUptime();

seconds = uptime % SECS_PER_MIN;
Expand All @@ -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;
Expand All @@ -383,4 +384,4 @@ boolean NTPClient::isSummerTimePeriod(time_t moment) {
return summertime(year(), month(), day(), hour(), getTimeZone());
}

#endif // ARDUINO_ARCH_ESP8266
#endif // ARDUINO_ARCH_ESP8266

0 comments on commit a799dad

Please sign in to comment.