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 <[email protected]>
  • Loading branch information
trantanen authored and carlescufi committed Oct 18, 2024
1 parent f411b5a commit fe6555b
Show file tree
Hide file tree
Showing 6 changed files with 823 additions and 108 deletions.
8 changes: 8 additions & 0 deletions doc/nrf/libraries/others/date_time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ See the API documentation for more information on these functions.
If an application has time-dependent operations immediately after connecting to the LTE network, it should wait for a confirmation indicating that time has been updated.
If the :kconfig:option:`CONFIG_DATE_TIME_AUTO_UPDATE` option is not set, the first date-time update cycle (after boot) does not occur until the time set by the :kconfig:option:`CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS` option has elapsed.

.. note::

Exceptions to the regular date-time update interval set by the :kconfig:option:`CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS` Kconfig option occur when
the :c:func:`date_time_update_async` function is called and a new date-time update is triggered and scheduled.
Either retry or regular update interval is used depending on the outcome of the date-time update procedure.
Date-time update from modem through an ``AT%XTIME`` notification,
or from the client through the :c:func:`date_time_set` function does not disturb the regular update interval.

Configuration
*************

Expand Down
20 changes: 16 additions & 4 deletions lib/date_time/date_time_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ 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");
}
}

static int date_time_core_schedule_work(int interval)
{
if (!IS_ENABLED(CONFIG_DATE_TIME_MODEM) && !IS_ENABLED(CONFIG_DATE_TIME_NTP)) {
LOG_DBG("Skipping requested date time update, modem and NTP are disabled");
return -ENOTSUP;
}

/* If a scheduled update is blocking reschedules, exit.
* Otherwise set the reschedule_blocked flag to true, then proceed with the reschedule.
*/
Expand Down Expand Up @@ -104,10 +111,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 @@ -169,6 +177,8 @@ void date_time_lte_ind_handler(const struct lte_lc_evt *const evt)
case LTE_LC_NW_REG_REGISTERED_HOME:
case LTE_LC_NW_REG_REGISTERED_ROAMING:
if (!date_time_is_valid()) {
LOG_DBG("Date time update scheduled in 1 second "
"due to LTE registration");
k_work_reschedule_for_queue(
&date_time_work_q,
&date_time_update_work,
Expand Down Expand Up @@ -241,6 +251,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 @@ -76,7 +76,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 @@ -125,9 +125,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 @@ -163,7 +160,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 @@ -174,6 +172,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 @@ -238,9 +242,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 fe6555b

Please sign in to comment.