Skip to content

Commit bfcd887

Browse files
martinjaegercarlescufi
authored andcommitted
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 <[email protected]>
1 parent 776c850 commit bfcd887

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

subsys/lorawan/services/clock_sync.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ static int clock_sync_serialize_device_time(uint8_t *buf, size_t size)
8585
return sizeof(uint32_t);
8686
}
8787

88+
static inline k_timeout_t clock_sync_calc_periodicity(void)
89+
{
90+
/* add +-30s jitter to nominal periodicity as required by the spec */
91+
return K_SECONDS(ctx.periodicity - 30 + sys_rand32_get() % 61);
92+
}
93+
8894
static void clock_sync_package_callback(uint8_t port, bool data_pending, int16_t rssi, int8_t snr,
8995
uint8_t len, const uint8_t *rx_buf)
9096
{
@@ -145,6 +151,9 @@ static void clock_sync_package_callback(uint8_t port, bool data_pending, int16_t
145151
tx_pos += clock_sync_serialize_device_time(tx_buf + tx_pos,
146152
sizeof(tx_buf) - tx_pos);
147153

154+
lorawan_services_reschedule_work(&ctx.resync_work,
155+
clock_sync_calc_periodicity());
156+
148157
LOG_DBG("DeviceAppTimePeriodicityReq period: %u", period);
149158
break;
150159
}
@@ -196,18 +205,14 @@ static int clock_sync_app_time_req(void)
196205

197206
static void clock_sync_resync_handler(struct k_work *work)
198207
{
199-
uint32_t periodicity;
200-
201208
clock_sync_app_time_req();
202209

203210
if (ctx.nb_transmissions > 0) {
204211
ctx.nb_transmissions--;
205212
lorawan_services_reschedule_work(&ctx.resync_work, K_SECONDS(CLOCK_RESYNC_DELAY));
206213
} else {
207-
/* Add +-30s jitter to actual periodicity as required */
208-
periodicity = ctx.periodicity - 30 + sys_rand32_get() % 61;
209-
210-
lorawan_services_reschedule_work(&ctx.resync_work, K_SECONDS(periodicity));
214+
lorawan_services_reschedule_work(&ctx.resync_work,
215+
clock_sync_calc_periodicity());
211216
}
212217
}
213218

0 commit comments

Comments
 (0)