Skip to content

Commit 2df4adc

Browse files
committed
net: nrf_provisioning: Simplify scheluding calculations
The nrf_provisioning_schedule() had too many if-else things. Simplyfy the calculations a bit. * If date_time_now() fails, just retry until we get a proper time. * Rescheduling is just now+interval * Initialize the srand() by kernel timer. Fix testcases to use correct clock always. Signed-off-by: Seppo Takalo <[email protected]>
1 parent d6706a1 commit 2df4adc

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

subsys/net/lib/nrf_provisioning/src/nrf_provisioning.c

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ int nrf_provisioning_schedule(void)
491491
: CONFIG_NRF_PROVISIONING_INTERVAL_S;
492492
LOG_DBG("First provisioning, setting interval to %lld seconds",
493493
(int64_t)provisioning_interval);
494+
/* Delay spread does not need high-entropy randomness */
495+
srand(k_uptime_get_32());
494496
goto out;
495497
}
496498

@@ -501,47 +503,34 @@ int nrf_provisioning_schedule(void)
501503
LOG_ERR("Getting time failed, error: %d", ret);
502504
}
503505

504-
if (provisioning_interval) {
505-
retry_s = provisioning_interval;
506-
} else {
507-
/* Backoff... */
508-
retry_s = retry_s * 2;
506+
/* Backoff... */
507+
retry_s = retry_s * 2;
509508

510-
/* ...up to a degree */
511-
if (retry_s > CONFIG_NRF_PROVISIONING_INTERVAL_S) {
512-
retry_s = CONFIG_NRF_PROVISIONING_INTERVAL_S;
513-
}
509+
/* ...up to a degree */
510+
if (retry_s > CONFIG_NRF_PROVISIONING_INTERVAL_S) {
511+
retry_s = CONFIG_NRF_PROVISIONING_INTERVAL_S;
514512
}
515513

516514
goto out;
517515
}
518516

519517
now_s /= 1000; /* date_time_now() is ms */
520-
if (now_s > deadline_s || reschedule) {
521-
522-
/* Provision now */
523-
if (!provisioning_interval && !deadline_s) {
524-
deadline_s = now_s + CONFIG_NRF_PROVISIONING_INTERVAL_S;
525-
retry_s = 0;
526-
goto out;
527-
}
528518

519+
if (now_s > deadline_s || reschedule) {
529520
/* Interval set by the server takes precedence */
530-
deadline_s = provisioning_interval ? now_s + provisioning_interval :
531-
now_s + CONFIG_NRF_PROVISIONING_INTERVAL_S;
521+
deadline_s = now_s + (provisioning_interval ? provisioning_interval
522+
: CONFIG_NRF_PROVISIONING_INTERVAL_S);
532523
}
533524

534525
retry_s = deadline_s - now_s;
535526
out:
536-
/* Delay spread does not need high-entropy randomness */
537-
srand(now_s);
538527
spread_s = rand() %
539528
(CONFIG_NRF_PROVISIONING_SPREAD_S ? CONFIG_NRF_PROVISIONING_SPREAD_S : 1);
540529

541530
/* To even the load on server side */
542531
retry_s += spread_s;
543532

544-
LOG_INF("Checking for provisioning commands in %lld seconds", retry_s);
533+
LOG_INF("Checking for provisioning commands in %lld seconds", (int64_t) retry_s);
545534
reschedule = false;
546535

547536
return retry_s;

tests/subsys/net/lib/nrf_provisioning/src/coap.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,17 @@ struct coap_transmission_parameters coap_get_transmission_parameters(void)
183183
return coap_transmission_params;
184184
}
185185

186+
static int time_now(int64_t *unix_time_ms, int cmock_num_calls)
187+
{
188+
*unix_time_ms = (int64_t)time(NULL) * MSEC_PER_SEC;
189+
return 0;
190+
}
191+
186192
void setUp(void)
187193
{
188194
k_sem_reset(&stopped_sem);
189195
k_sem_reset(&done_sem);
196+
__cmock_date_time_now_Stub(time_now);
190197
}
191198

192199
void tearDown(void)
@@ -753,7 +760,6 @@ void test_provisioning_init_change_cbs_valid(void)
753760
__cmock_lte_lc_connect_Stub(lte_lc_connect_cb);
754761
/* No way to check that the correct handler is passed as an argument */
755762
__cmock_lte_lc_register_handler_Ignore();
756-
__cmock_date_time_now_IgnoreAndReturn(0);
757763

758764
/* To make certain init has been called at least once beforehand */
759765
int ret = nrf_provisioning_init(NULL, NULL);
@@ -794,7 +800,6 @@ void test_provisioning_task_valid(void)
794800
__cmock_lte_lc_func_mode_get_IgnoreAndReturn(0);
795801
__cmock_nrf_provisioning_at_cmee_enable_IgnoreAndReturn(0);
796802
__cmock_nrf_provisioning_at_cmee_control_IgnoreAndReturn(0);
797-
__cmock_date_time_now_IgnoreAndReturn(0);
798803

799804
__cmock_modem_info_get_fw_version_ExpectAnyArgsAndReturn(0);
800805
__cmock_modem_info_get_fw_version_ReturnArrayThruPtr_buf(MFW_VER, sizeof(MFW_VER));
@@ -844,7 +849,6 @@ void test_provisioning_commands(void)
844849
__cmock_lte_lc_func_mode_get_IgnoreAndReturn(0);
845850
__cmock_nrf_provisioning_at_cmee_enable_IgnoreAndReturn(0);
846851
__cmock_nrf_provisioning_at_cmee_control_IgnoreAndReturn(0);
847-
__cmock_date_time_now_IgnoreAndReturn(0);
848852
__cmock_settings_load_subtree_IgnoreAndReturn(0);
849853

850854
/* To make certain init has been called at least once beforehand */
@@ -1013,13 +1017,6 @@ void test_coap_rsp_unsupported_code(void)
10131017
TEST_ASSERT_EQUAL_INT(-ENOTSUP, ret);
10141018
}
10151019

1016-
static int time_now(int64_t *unix_time_ms, int cmock_num_calls)
1017-
{
1018-
*unix_time_ms = (int64_t)time(NULL);
1019-
1020-
return 0;
1021-
}
1022-
10231020
/*
10241021
* - Get when next provisioning should be executed
10251022
* - The client should follow the interval configured
@@ -1031,14 +1028,8 @@ void test_provisioning_schedule_valid(void)
10311028
/* To avoid being entangled to other tests let's assume that the function has
10321029
* never been called earlier. If that's not the case let's just ignore the first invocation.
10331030
*/
1034-
__cmock_date_time_now_IgnoreAndReturn(0);
10351031
(void)nrf_provisioning_schedule();
10361032

1037-
__cmock_date_time_now_StopIgnore();
1038-
1039-
__cmock_date_time_now_AddCallback(time_now);
1040-
__cmock_date_time_now_ExpectAnyArgsAndReturn(0);
1041-
10421033
int ret = nrf_provisioning_schedule();
10431034

10441035
/* Can't have exact match due to the spread factor */
@@ -1059,12 +1050,8 @@ void test_provisioning_schedule_no_nw_time_valid(void)
10591050
/* To avoid being entangled to other tests let's assume that the function has
10601051
* never been called earlier. If that's not the case let's just ignore the first invocation.
10611052
*/
1062-
__cmock_date_time_now_AddCallback(time_now);
1063-
__cmock_date_time_now_IgnoreAndReturn(0);
10641053
(void)nrf_provisioning_schedule();
10651054

1066-
__cmock_date_time_now_StopIgnore();
1067-
10681055
__cmock_date_time_now_ExpectAnyArgsAndReturn(-1);
10691056

10701057
int ret = nrf_provisioning_schedule();

0 commit comments

Comments
 (0)