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:
parent
776c850896
commit
bfcd887903
|
@ -85,6 +85,12 @@ static int clock_sync_serialize_device_time(uint8_t *buf, size_t size)
|
||||||
return sizeof(uint32_t);
|
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,
|
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)
|
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,
|
tx_pos += clock_sync_serialize_device_time(tx_buf + tx_pos,
|
||||||
sizeof(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);
|
LOG_DBG("DeviceAppTimePeriodicityReq period: %u", period);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -196,18 +205,14 @@ static int clock_sync_app_time_req(void)
|
||||||
|
|
||||||
static void clock_sync_resync_handler(struct k_work *work)
|
static void clock_sync_resync_handler(struct k_work *work)
|
||||||
{
|
{
|
||||||
uint32_t periodicity;
|
|
||||||
|
|
||||||
clock_sync_app_time_req();
|
clock_sync_app_time_req();
|
||||||
|
|
||||||
if (ctx.nb_transmissions > 0) {
|
if (ctx.nb_transmissions > 0) {
|
||||||
ctx.nb_transmissions--;
|
ctx.nb_transmissions--;
|
||||||
lorawan_services_reschedule_work(&ctx.resync_work, K_SECONDS(CLOCK_RESYNC_DELAY));
|
lorawan_services_reschedule_work(&ctx.resync_work, K_SECONDS(CLOCK_RESYNC_DELAY));
|
||||||
} else {
|
} else {
|
||||||
/* Add +-30s jitter to actual periodicity as required */
|
lorawan_services_reschedule_work(&ctx.resync_work,
|
||||||
periodicity = ctx.periodicity - 30 + sys_rand32_get() % 61;
|
clock_sync_calc_periodicity());
|
||||||
|
|
||||||
lorawan_services_reschedule_work(&ctx.resync_work, K_SECONDS(periodicity));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue