Skip to content

Commit

Permalink
nimble/ll: Fix sync packet offset in syncinfo
Browse files Browse the repository at this point in the history
We need to move periodic advertising event start time by full interval
(not only ticks) as otherwise any calculation of start time in the
future event will not be accurate.
  • Loading branch information
andrzej-kaczmarek committed Mar 20, 2024
1 parent 84c8912 commit eb5f483
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions nimble/controller/src/ble_ll_adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,48 +623,51 @@ ble_ll_adv_put_syncinfo(struct ble_ll_adv_sm *advsm,
uint8_t *dptr)
{
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV_SYNC_TRANSFER)
uint8_t anchor_usecs;
uint16_t conn_cnt;
#endif
unsigned int event_cnt_off = 0;
uint32_t offset = 0;
uint32_t anchor;
uint32_t itvl_us;
uint32_t anchor_ticks;
uint8_t anchor_rem_us;
uint8_t units;

if (connsm) {
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV_SYNC_TRANSFER)
anchor = connsm->anchor_point;
anchor_usecs = connsm->anchor_point_usecs;
anchor_ticks = connsm->anchor_point;
anchor_rem_us = connsm->anchor_point_usecs;
conn_cnt = connsm->event_cntr;

/* get anchor for conn event that is before periodic_adv_event_start_time */
while (LL_TMR_GT(anchor, advsm->periodic_adv_event_start_time)) {
ble_ll_conn_get_anchor(connsm, --conn_cnt, &anchor, &anchor_usecs);
while (LL_TMR_GT(anchor_ticks, advsm->periodic_adv_event_start_time)) {
ble_ll_conn_get_anchor(connsm, --conn_cnt, &anchor_ticks, &anchor_rem_us);
}

offset = ble_ll_tmr_t2u(advsm->periodic_adv_event_start_time - anchor);
offset -= anchor_usecs;
offset = ble_ll_tmr_t2u(advsm->periodic_adv_event_start_time - anchor_ticks);
offset -= anchor_rem_us;
offset += advsm->periodic_adv_event_start_time_remainder;

/* connEventCount */
put_le16(conn_event_cnt, conn_cnt);
#endif
} else {
anchor = advsm->periodic_adv_event_start_time;
anchor_ticks = advsm->periodic_adv_event_start_time;
anchor_rem_us = advsm->periodic_adv_event_start_time_remainder;
itvl_us = advsm->periodic_adv_itvl * BLE_LL_ADV_PERIODIC_ITVL;

/* Get periodic event that is past AUX start time (so that we always
* provide valid offset if it is not too far in future). This can
* happen if advertising event is interleaved with periodic advertising
* event (when chaining).
*/
while (LL_TMR_GT(AUX_CURRENT(advsm)->start_time, anchor)) {
anchor += advsm->periodic_adv_itvl_ticks;
while (LL_TMR_GEQ(AUX_CURRENT(advsm)->start_time, anchor_ticks)) {
ble_ll_tmr_add(&anchor_ticks, &anchor_rem_us, itvl_us);
event_cnt_off++;
}

offset = ble_ll_tmr_t2u(anchor - AUX_CURRENT(advsm)->start_time);
offset += advsm->periodic_adv_event_start_time_remainder;
offset += advsm->periodic_adv_itvl_rem_us;
/* We always schedule aux with 0 rem_us so no need to include it here */
offset = ble_ll_tmr_t2u(anchor_ticks - AUX_CURRENT(advsm)->start_time);
offset += anchor_rem_us;
}

/* Sync Packet Offset (13 bits), Offset Units (1 bit), Offset Adjust (1 bit),
Expand Down

0 comments on commit eb5f483

Please sign in to comment.