Skip to content

Commit

Permalink
tests: lib: date_time: Tests for scheduled updates
Browse files Browse the repository at this point in the history
Adding tests for scheduled date-time updates verifying that they occur
at configured intervals.

Some small clean-up and additional logging into date-time library.

Branch coverage is going from ~44% to ~98% having 3 branches untested.
These are related to failure of clock_gettime/clock_settime
which cannot really be achieved without creating a different test set
and mocking those APIs.

With default configuration, the tests run almost 30s due to
update intervals that we need to wait.

Jira: NCSDK-28887

Signed-off-by: Tommi Rantanen <tommi.rantanen@nordicsemi.no>
  • Loading branch information
trantanen committed Oct 7, 2024
1 parent f456180 commit 0327cf4
Show file tree
Hide file tree
Showing 4 changed files with 588 additions and 23 deletions.
13 changes: 9 additions & 4 deletions lib/date_time/date_time_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ static void date_time_core_notify_event(enum date_time_evt_type time_source)

if (app_evt_handler != NULL) {
app_evt_handler(&evt);
} else {
LOG_DBG("No date-time event handler registered");
}
}

Expand Down Expand Up @@ -104,10 +106,11 @@ static void date_time_core_schedule_retry(void)
return;
}

if (date_time_core_schedule_work(CONFIG_DATE_TIME_RETRY_INTERVAL_SECONDS) == 0) {
LOG_DBG("Date time update retry in: %d seconds",
CONFIG_DATE_TIME_RETRY_INTERVAL_SECONDS);
}
/* Scheduling new update cannot fail because we are never doing retries
* if we have fresh enough time
*/
date_time_core_schedule_work(CONFIG_DATE_TIME_RETRY_INTERVAL_SECONDS);
LOG_DBG("Date time update retry in: %d seconds", CONFIG_DATE_TIME_RETRY_INTERVAL_SECONDS);
}

static void date_time_update_work_fn(struct k_work *work)
Expand Down Expand Up @@ -241,6 +244,8 @@ int date_time_core_now_local(int64_t *local_time_ms)

int date_time_core_update_async(date_time_evt_handler_t evt_handler)
{
LOG_DBG("Requesting date-time update asynchronously");

if (evt_handler) {
app_evt_handler = evt_handler;
} else if (app_evt_handler == NULL) {
Expand Down
18 changes: 13 additions & 5 deletions lib/date_time/date_time_modem.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int date_time_modem_get(int64_t *date_time_ms, int *date_time_tz)
/* Want to match 6 or 7 args */
if (rc != 6 && rc != 7) {
LOG_WRN("Did not get time from cellular network (error: %d). "
"This is normal as some cellular networks don't provide it or "
"This may be normal as some cellular networks don't provide it or "
"time may not be available yet.", rc);
return -ENODATA;
}
Expand Down Expand Up @@ -122,9 +122,6 @@ static void date_time_at_xtime_handler(const char *notif)
int err;
int tz;

if (notif == NULL) {
return;
}
modem_valid_network_time = true;

/* Check if current time is valid */
Expand Down Expand Up @@ -160,7 +157,8 @@ static void date_time_at_xtime_handler(const char *notif)
time_buf_len = hex2bin(time_str_start, 14, time_buf, sizeof(time_buf));

if (time_buf_len < sizeof(time_buf)) {
LOG_ERR("%%XTIME notification decoding failed (ret=%d): %s", time_buf_len, notif);
LOG_ERR("Time value decoding failed from %%XTIME notification (ret=%d): %s",
time_buf_len, notif);
return;
}

Expand All @@ -171,6 +169,12 @@ static void date_time_at_xtime_handler(const char *notif)
date_time.tm_min = semioctet_to_dec(time_buf[4]);
date_time.tm_sec = semioctet_to_dec(time_buf[5]);

/* 3GPP TS 23.040 Section 9.2.3.11 says about the time zone as follows:
* The Time Zone indicates the difference, expressed in quarters of an hour,
* between the local time and GMT. In the first of the two semi octets,
* the first bit (bit 3 of the seventh octet of the TP Service Centre Time Stamp field)
* represents the algebraic sign of this difference (0: positive, 1: negative).
*/
tz = semioctet_to_dec(time_buf[6] & 0xF7);
if (time_buf[6] & 0x08) {
tz = -tz;
Expand Down Expand Up @@ -235,9 +239,13 @@ void date_time_modem_xtime_subscribe_work_fn(struct k_work *work_item)
}
}

#if defined(CONFIG_UNITY)
void date_time_modem_on_cfun(int mode, void *ctx)
#else
NRF_MODEM_LIB_ON_CFUN(date_time_cfun_hook, date_time_modem_on_cfun, NULL);

static void date_time_modem_on_cfun(int mode, void *ctx)
#endif
{
ARG_UNUSED(ctx);

Expand Down
5 changes: 5 additions & 0 deletions tests/lib/date_time/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ CONFIG_MOCK_NRF_MODEM_AT=y
CONFIG_LTE_LINK_CONTROL=y
CONFIG_POSIX_API=y

CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS=3
CONFIG_DATE_TIME_TOO_OLD_SECONDS=1
CONFIG_DATE_TIME_RETRY_COUNT=2
CONFIG_DATE_TIME_RETRY_INTERVAL_SECONDS=1

# Enable logs if you want to explore them
CONFIG_LOG=n
CONFIG_DATE_TIME_LOG_LEVEL_DBG=n
Loading

0 comments on commit 0327cf4

Please sign in to comment.