- Notifications
You must be signed in to change notification settings - Fork4
Library converts datetime_t incorrectly, causing example to fail #4
Description
Describe the bug
This library does
RP2040_RTC/src/DateTime_Generic.h
Lines 456 to 477 inf7650a1
boolrtc_set_datetime(DateTime dt) | |
{ | |
datetime_t tm; | |
tm.year = dt.year(); | |
tm.month = dt.month(); | |
tm.day = dt.day(); | |
tm.hour = dt.hour(); | |
tm.min = dt.minute(); | |
tm.sec = dt.second(); | |
#if RTC_DEBUG | |
Serial.print("Year ="); Serial.print(tm.year); | |
Serial.print(", Mo ="); Serial.print(tm.month); | |
Serial.print(", day ="); Serial.print(tm.day); | |
Serial.print(", hour ="); Serial.print(tm.hour); | |
Serial.print(", min ="); Serial.print(tm.min); | |
Serial.print(", sec ="); Serial.println(tm.sec); | |
#endif | |
return (rtc_set_datetime(&tm)); | |
} |
to convert aDateTime
intodatetime_t
as needed by the Pico SDK'srtc_set_datetime
function. However, the library fails to initialize thetm.dotw
(day-of-the-week) member, and as such the field will have a random value from the stack.
Calling intortc_set_datetime
with such a datastructure leads tothe validation function failing
staticboolvalid_datetime(datetime_t *t) {// Valid ranges taken from RTC doc. Note when setting an RTC alarm// these values are allowed to be -1 to say "don't match this value"if (!(t->year >=0 && t->year <=4095))returnfalse;if (!(t->month >=1 && t->month <=12))returnfalse;if (!(t->day >=1 && t->day <=31))returnfalse;if (!(t->dotw >=0 && t->dotw <=6))returnfalse;if (!(t->hour >=0 && t->hour <=23))returnfalse;if (!(t->min >=0 && t->min <=59))returnfalse;if (!(t->sec >=0 && t->sec <=59))returnfalse;returntrue;}boolrtc_set_datetime(datetime_t *t) {if (!valid_datetime(t)) {returnfalse; }// rest of code}
Sinceif (!(t->dotw >= 0 && t->dotw <= 6)) return false;
triggers. (Since variable is allocated on the stack it has an initial random value, 0-255, only by chance if the value is 0-6 it passes this check)
Hence, the example Time with NiNa (https://github.com/khoih-prog/RP2040_RTC/tree/main/examples/Time/RP2040_RTC_Time_WiFiNINA) module that uses this codepath fails randomly.
13:04:31.792 -> Start RP2040_RTC_Time_WiFiNINA on MBED NANO_RP2040_CONNECT with WiFiNINA using WiFiNINA_Generic Library13:04:31.792 -> RP2040_RTC v1.0.613:04:32.515 -> Please upgrade the firmware13:04:32.515 -> Connecting to WPA SSID: xxx13:04:35.829 -> You're connected to the network, IP = xxxxx13:04:36.829 -> Packet received13:04:36.829 -> Seconds since Jan 1 1900 = 384475707513:04:36.829 -> Unix time = 163576827513:04:36.829 -> rtc_set_datetime failed13:04:37.343 -> rtc_set_datetime failed [repeated multiple times until the loop ends]13:04:41.835 -> The UTC time is 12:04:3513:04:41.835 -> ============================13:04:41.835 -> 00:00:00 Tue 31 Dec 2047 UTC13:05:42.250 -> ============================
And thus the example does not run correctly.
Steps to Reproduce
Use thehttps://github.com/khoih-prog/RP2040_RTC/tree/main/examples/Time/RP2040_RTC_Time_WiFiNINA example with theplatformio.ini
[env:nanorp2040connect]platform = raspberrypiboard = nanorp2040connectframework = arduinolib_ldf_mode = chain+lib_compat_mode = strictlib_deps = paulstoffregen/Time@^1.6.1 khoih-prog/WiFiNINA_Generic@^1.8.13 khoih-prog/RP2040_RTC@^1.0.6 khoih-prog/Timezone_Generic@^1.7.1lib_ignore = WiFi101 WiFiEspAT WiFi ESP_AT_Lib ESP8266_AT_WebServer EthernetENC Ethernet2 Ethernet3 EthernetLarge Ethernet WiFiWebServer EthernetWebServer
Expected behavior
The library converts todatetime_t
correctly without leaving any datamember uninitialized.
Actual behavior
Library does not initialize the.dotw
member causing a probabilistic failure ofrtc_set_datetime()
.
Debug and AT-command log (if applicable)
None appliciable.
Screenshots
None appliciable.
Information
Please ensure to specify the following:
- PlatformIO core 5.2.2
RP2040
Core Version: Arduino-mbed RP2040 v2.5.2RP2040
Board type: NANO_RP2040_CONNECT- Contextual information (e.g. what you were trying to achieve): Run the NTP synchronization example
- Simplest possible steps to reproduce
- Anything that might be relevant in your opinion, such as:
- Operating system: Windows 10
Additional context
Discussed inthe PlatformIO forum thread.