@@ -9,6 +9,9 @@ Rtc::~Rtc(){}
99
1010bool Rtc::setYear (int year)
1111{
12+ if (year < 1970 || year > 2100 ) {
13+ return false ;
14+ }
1215 int currentYear, month, day, hour, minute, second;
1316 int retVal = getTime (currentYear, month, day, hour, minute, second);
1417 if (retVal == 0 )
@@ -25,6 +28,9 @@ bool Rtc::setYear(int year)
2528
2629bool Rtc::setMonthOfYear (int month)
2730{
31+ if (month < 1 || month > 12 ) {
32+ return false ;
33+ }
2834 int year, currentMonth, day, hour, minute, second;
2935 int retVal = getTime (year, currentMonth, day, hour, minute, second);
3036 if (retVal == 0 )
@@ -40,6 +46,9 @@ bool Rtc::setMonthOfYear(int month)
4046
4147bool Rtc::setDayOfMonth (int day)
4248{
49+ if (day < 1 || day > 31 ) {
50+ return false ;
51+ }
4352 int year, month, currentDay, hour, minute, second;
4453 int retVal = getTime (year, month, currentDay, hour, minute, second);
4554 if (retVal == 0 )
@@ -55,6 +64,9 @@ bool Rtc::setDayOfMonth(int day)
5564
5665bool Rtc::setHour (int hour)
5766{
67+ if (hour < 0 || hour > 23 ) {
68+ return false ;
69+ }
5870 int year, month, day, currentHour, minute, second;
5971 int retVal = getTime (year, month, day, currentHour, minute, second);
6072 if (retVal == 0 )
@@ -70,6 +82,9 @@ bool Rtc::setHour(int hour)
7082
7183bool Rtc::setMinute (int minute)
7284{
85+ if (minute < 0 || minute > 59 ) {
86+ return false ;
87+ }
7388 int year, month, day, hour, currentMinute, second;
7489 int retVal = getTime (year, month, day, hour, currentMinute, second);
7590 if (retVal == 0 )
@@ -85,6 +100,9 @@ bool Rtc::setMinute(int minute)
85100
86101bool Rtc::setSecond (int second)
87102{
103+ if (second < 0 || second > 59 ) {
104+ return false ;
105+ }
88106 int year, month, day, hour, minute, currentSecond;
89107 int retVal = getTime (year, month, day, hour, minute, currentSecond);
90108 if (retVal == 0 )
@@ -164,7 +182,7 @@ int Rtc::getSeconds()
164182 return -1 ;
165183}
166184
167- #if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA )
185+ #if DT_NODE_EXISTS(DT_NODELABEL(rtc) )
168186#define RTC_NODE DT_NODELABEL (rtc)
169187
170188/* *
@@ -176,6 +194,9 @@ int Rtc::getSeconds()
176194Rtc::Rtc()
177195{
178196 rtc_dev = DEVICE_DT_GET (RTC_NODE);
197+ if (!rtc_dev) {
198+ printk (" RTC device not found in device tree\n " );
199+ }
179200}
180201
181202/* *
@@ -366,7 +387,7 @@ bool Rtc::isAlarmPending()
366387/* *
367388 * @brief Rtc callback function
368389 *
369- * This alarm callback wrapper is needed to make the connection between C-style low level driver function callabacks
390+ * This alarm callback wrapper is needed to make the connection between C-style low level driver function callbacks
370391 * which need to be static and the objects derived from the RTC class;
371392 * You do not need to call this directly in the app
372393 *
@@ -444,11 +465,23 @@ int Rtc::getCalibration(int32_t &calibration)
444465 return rtc_get_calibration (rtc_dev, &calibration);
445466}
446467
447- #elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME )
468+ #elif DT_NODE_EXISTS(DT_NODELABEL(rtc2) )
448469
449470#define COUNTER_NODE DT_NODELABEL (rtc2)
450471LOG_MODULE_REGISTER(Rtc);
451472
473+ // Utility functions - local scope only
474+ namespace {
475+ static const int days_in_month[] = {
476+ 31 , 28 , 31 , 30 , 31 , 30 ,
477+ 31 , 31 , 30 , 31 , 30 , 31
478+ };
479+
480+ static bool is_leap (int year) {
481+ return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 ));
482+ }
483+ } // namespace
484+
452485/* *
453486 * @brief Static alarm handler for the RTC driver.
454487 *
@@ -526,6 +559,7 @@ int Rtc::setTime(int year, int month, int day, int hour, int minute, int second)
526559
527560 uint32_t freq = counter_get_frequency (counter_dev);
528561 if (freq == 0 ) {
562+ LOG_ERR (" Counter frequency is zero" );
529563 return -1 ;
530564 }
531565
@@ -608,6 +642,8 @@ int Rtc::setAlarm(int year, int month, int day, int hour, int minute, int second
608642 int ret = counter_set_channel_alarm (counter_dev, 0 , &alarm_cfg);
609643 if (ret != 0 ) {
610644 LOG_WRN (" Failed to set alarm: %d" , ret);
645+ user_callback = nullptr ;
646+ user_data = nullptr ;
611647 return ret;
612648 }
613649
@@ -619,22 +655,17 @@ int Rtc::setAlarm(int year, int month, int day, int hour, int minute, int second
619655 *
620656 * This function stops any active alarm and clears the registered callback
621657 * and user data.
658+ * @return 0 on success, negative error code otherwise.
622659 */
623- void Rtc::cancelAlarm ()
660+ int Rtc::cancelAlarm ()
624661{
625- counter_cancel_channel_alarm (counter_dev, 0 );
662+ int ret = counter_cancel_channel_alarm (counter_dev, 0 );
663+ if (ret != 0 ) {
664+ LOG_WRN (" Failed to cancel alarm: %d" , ret);
665+ }
626666 user_callback = nullptr ;
627667 user_data = nullptr ;
628- }
629-
630- // Utility functions
631- static const int days_in_month[] = {
632- 31 , 28 , 31 , 30 , 31 , 30 ,
633- 31 , 31 , 30 , 31 , 30 , 31
634- };
635-
636- static bool is_leap (int year) {
637- return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 ));
668+ return ret;
638669}
639670
640671/* *
0 commit comments