From bfcd887903ecc0e54d0f271a620128433a87f524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Mon, 5 Feb 2024 15:33:21 +0100 Subject: [PATCH] lorawan: services: clock_sync: apply changed periodicity immediately MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- subsys/lorawan/services/clock_sync.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/subsys/lorawan/services/clock_sync.c b/subsys/lorawan/services/clock_sync.c index 8f9077927d..a4532fdee1 100644 --- a/subsys/lorawan/services/clock_sync.c +++ b/subsys/lorawan/services/clock_sync.c @@ -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()); } }