lorawan: services: clock_sync: apply changed periodicity immediately

The work item for re-synchronization should be rescheduled immediately
after the periodicity is changed.

Calculation of the periodicity incl. jitter is moved to a dedicated
function so that it can be re-used.

Signed-off-by: Martin Jäger <martin@libre.solar>
This commit is contained in:
Martin Jäger 2024-02-05 15:33:21 +01:00 committed by Carles Cufí
parent 776c850896
commit bfcd887903

View file

@ -85,6 +85,12 @@ static int clock_sync_serialize_device_time(uint8_t *buf, size_t size)
return sizeof(uint32_t);
}
static inline k_timeout_t clock_sync_calc_periodicity(void)
{
/* add +-30s jitter to nominal periodicity as required by the spec */
return K_SECONDS(ctx.periodicity - 30 + sys_rand32_get() % 61);
}
static void clock_sync_package_callback(uint8_t port, bool data_pending, int16_t rssi, int8_t snr,
uint8_t len, const uint8_t *rx_buf)
{
@ -145,6 +151,9 @@ static void clock_sync_package_callback(uint8_t port, bool data_pending, int16_t
tx_pos += clock_sync_serialize_device_time(tx_buf + tx_pos,
sizeof(tx_buf) - tx_pos);
lorawan_services_reschedule_work(&ctx.resync_work,
clock_sync_calc_periodicity());
LOG_DBG("DeviceAppTimePeriodicityReq period: %u", period);
break;
}
@ -196,18 +205,14 @@ static int clock_sync_app_time_req(void)
static void clock_sync_resync_handler(struct k_work *work)
{
uint32_t periodicity;
clock_sync_app_time_req();
if (ctx.nb_transmissions > 0) {
ctx.nb_transmissions--;
lorawan_services_reschedule_work(&ctx.resync_work, K_SECONDS(CLOCK_RESYNC_DELAY));
} else {
/* Add +-30s jitter to actual periodicity as required */
periodicity = ctx.periodicity - 30 + sys_rand32_get() % 61;
lorawan_services_reschedule_work(&ctx.resync_work, K_SECONDS(periodicity));
lorawan_services_reschedule_work(&ctx.resync_work,
clock_sync_calc_periodicity());
}
}