Skip to content

Commit 37143be

Browse files
nimble/ll: Simplify SyncInfo calculation
This simplifies calculation of SyncInfo used in AUX_ADV_IND and LL_PERIODIC_SYNC_IND to use the same code in both cases. In case of LL Control PDU we do not "rewind" connsm to find proper connection event but instead "advance" padv event the same way as it was done for AUX_ADV_IND PDU.
1 parent fd8d1b4 commit 37143be

File tree

4 files changed

+67
-62
lines changed

4 files changed

+67
-62
lines changed

nimble/controller/include/controller/ble_ll_conn.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,15 @@ void ble_ll_conn_chan_map_update(void);
448448
/* required for unit testing */
449449
uint8_t ble_ll_conn_calc_dci(struct ble_ll_conn_sm *conn, uint16_t latency);
450450

451-
/* used to get anchor point for connection event specified */
452-
void ble_ll_conn_get_anchor(struct ble_ll_conn_sm *connsm, uint16_t conn_event,
451+
/* get current event counter and anchor point */
452+
void ble_ll_conn_anchor_get(struct ble_ll_conn_sm *connsm, uint16_t *event_cntr,
453453
uint32_t *anchor, uint8_t *anchor_usecs);
454454

455+
/* get anchor point for specified connection event */
456+
void ble_ll_conn_anchor_event_cntr_get(struct ble_ll_conn_sm *connsm,
457+
uint16_t event_cntr, uint32_t *anchor,
458+
uint8_t *anchor_usecs);
459+
455460
#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
456461
int ble_ll_conn_move_anchor(struct ble_ll_conn_sm *connsm, uint16_t offset);
457462
#endif

nimble/controller/src/ble_ll_adv.c

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -643,83 +643,73 @@ ble_ll_adv_pdu_make(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte)
643643
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV)
644644
static void
645645
ble_ll_adv_put_syncinfo(struct ble_ll_adv_sm *advsm,
646-
struct ble_ll_conn_sm *connsm, uint8_t *conn_event_cnt,
646+
struct ble_ll_conn_sm *connsm, uint8_t *conn_event_cntr,
647647
uint8_t *dptr)
648648
{
649-
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV_SYNC_TRANSFER)
650-
uint16_t conn_cnt;
651-
#endif
652-
unsigned int event_cnt_off = 0;
653-
uint32_t offset = 0;
654-
uint32_t itvl_us;
649+
uint32_t offset_us;
650+
uint32_t offset;
655651
uint32_t anchor_ticks;
656652
uint8_t anchor_rem_us;
657-
uint8_t units;
653+
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV_SYNC_TRANSFER)
654+
uint16_t anchor_event_cntr;
655+
#endif
656+
uint32_t event_start;
657+
uint8_t event_start_us;
658+
uint32_t event_cntr_offset = 0;
658659

659660
if (connsm) {
660661
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV_SYNC_TRANSFER)
661-
anchor_ticks = connsm->anchor_point;
662-
anchor_rem_us = connsm->anchor_point_usecs;
663-
conn_cnt = connsm->event_cntr;
664-
665-
/* get anchor for conn event that is before periodic_adv_event_start_time */
666-
while (LL_TMR_GT(anchor_ticks, advsm->padv_event_start)) {
667-
ble_ll_conn_get_anchor(connsm, --conn_cnt, &anchor_ticks, &anchor_rem_us);
668-
}
669-
670-
offset = ble_ll_tmr_t2u(advsm->padv_event_start - anchor_ticks);
671-
offset -= anchor_rem_us;
672-
offset += advsm->padv_event_start_rem_us;
662+
ble_ll_conn_anchor_get(connsm, &anchor_event_cntr,
663+
&anchor_ticks, &anchor_rem_us);
673664

674665
/* connEventCount */
675-
put_le16(conn_event_cnt, conn_cnt);
666+
put_le16(conn_event_cntr, anchor_event_cntr);
667+
#else
668+
BLE_LL_ASSERT(0);
676669
#endif
677670
} else {
678-
anchor_ticks = advsm->padv_event_start;
679-
anchor_rem_us = advsm->padv_event_start_rem_us;
680-
itvl_us = advsm->periodic_adv_itvl * BLE_LL_ADV_PERIODIC_ITVL;
681-
682-
/* Get periodic event that is past AUX start time (so that we always
683-
* provide valid offset if it is not too far in future). This can
684-
* happen if advertising event is interleaved with periodic advertising
685-
* event (when chaining).
686-
*/
687-
while (LL_TMR_GEQ(AUX_CURRENT(advsm)->start_time, anchor_ticks)) {
688-
ble_ll_tmr_add(&anchor_ticks, &anchor_rem_us, itvl_us);
689-
event_cnt_off++;
690-
}
671+
anchor_ticks = AUX_CURRENT(advsm)->start_time;
672+
anchor_rem_us = 0;
673+
}
691674

692-
/* We always schedule aux with 0 rem_us so no need to include it here */
693-
offset = ble_ll_tmr_t2u(anchor_ticks - AUX_CURRENT(advsm)->start_time);
694-
offset += anchor_rem_us;
675+
event_start = advsm->padv_event_start;
676+
event_start_us = advsm->padv_event_start_rem_us;
677+
678+
/* Find padv event that is past anchor item */
679+
while (LL_TMR_LEQ(event_start, anchor_ticks)) {
680+
ble_ll_tmr_add(&event_start, &event_start_us, advsm->padv_itvl_us);
681+
event_cntr_offset++;
695682
}
696683

684+
offset_us = ble_ll_tmr_t2u(event_start - anchor_ticks);
685+
offset_us += event_start_us;
686+
offset_us -= anchor_rem_us;
687+
697688
/* Sync Packet Offset (13 bits), Offset Units (1 bit), Offset Adjust (1 bit),
698689
* RFU (1 bit)
699690
*/
700-
if (offset > 245700) {
701-
units = 0x20;
702-
offset = offset / 300;
703-
691+
if (offset_us > 245700) {
692+
offset = offset_us / 300;
704693
if (offset >= 0x2000) {
705694
if (connsm) {
706695
offset -= 0x2000;
707-
units |= 0x40;
696+
/* Offset Units = 1, Offset Adjust = 1 */
697+
offset |= 0x6000;
708698
} else {
709-
/* not able to represent time in offset */
710699
offset = 0;
711-
units = 0x00;
712-
event_cnt_off = 0;
700+
event_cntr_offset = 0;
713701
}
702+
} else {
703+
/* Offset Units = 1, Offset Adjust = 0 */
704+
offset |= 0x2000;
714705
}
715706

716707
} else {
717-
units = 0x00;
718-
offset = offset / 30;
708+
offset = offset_us / 30;
709+
/* Offset Units = 0, Offset Adjust = 0 */
719710
}
720711

721-
dptr[0] = (offset & 0x000000ff);
722-
dptr[1] = ((offset >> 8) & 0x0000001f) | units;
712+
put_le16(&dptr[0], offset);
723713

724714
/* Interval (2 bytes) */
725715
put_le16(&dptr[2], advsm->periodic_adv_itvl);
@@ -743,7 +733,7 @@ ble_ll_adv_put_syncinfo(struct ble_ll_adv_sm *advsm,
743733
dptr[15] = (uint8_t)(advsm->periodic_crcinit >> 16);
744734

745735
/* Event Counter (2 bytes) */
746-
put_le16(&dptr[16], advsm->periodic_event_cntr + event_cnt_off);
736+
put_le16(&dptr[16], advsm->periodic_event_cntr + event_cntr_offset);
747737
}
748738
#endif
749739

nimble/controller/src/ble_ll_conn.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,10 +2275,20 @@ ble_ll_conn_end(struct ble_ll_conn_sm *connsm, uint8_t ble_err)
22752275
connsm->event_cntr, (uint32_t)ble_err);
22762276
}
22772277

2278-
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV_SYNC_TRANSFER)
22792278
void
2280-
ble_ll_conn_get_anchor(struct ble_ll_conn_sm *connsm, uint16_t conn_event,
2279+
ble_ll_conn_anchor_get(struct ble_ll_conn_sm *connsm, uint16_t *event_cntr,
22812280
uint32_t *anchor, uint8_t *anchor_usecs)
2281+
{
2282+
*event_cntr = connsm->event_cntr;
2283+
*anchor = connsm->anchor_point;
2284+
*anchor_usecs = connsm->anchor_point_usecs;
2285+
}
2286+
2287+
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV_SYNC_TRANSFER)
2288+
void
2289+
ble_ll_conn_anchor_event_cntr_get(struct ble_ll_conn_sm *connsm,
2290+
uint16_t event_cntr, uint32_t *anchor,
2291+
uint8_t *anchor_usecs)
22822292
{
22832293
uint32_t itvl;
22842294

@@ -2287,11 +2297,11 @@ ble_ll_conn_get_anchor(struct ble_ll_conn_sm *connsm, uint16_t conn_event,
22872297
*anchor = connsm->anchor_point;
22882298
*anchor_usecs = connsm->anchor_point_usecs;
22892299

2290-
if ((int16_t)(conn_event - connsm->event_cntr) < 0) {
2291-
itvl *= connsm->event_cntr - conn_event;
2300+
if ((int16_t)(event_cntr - connsm->event_cntr) < 0) {
2301+
itvl *= connsm->event_cntr - event_cntr;
22922302
ble_ll_tmr_sub(anchor, anchor_usecs, itvl);
22932303
} else {
2294-
itvl *= conn_event - connsm->event_cntr;
2304+
itvl *= event_cntr - connsm->event_cntr;
22952305
ble_ll_tmr_add(anchor, anchor_usecs, itvl);
22962306
}
22972307
}

nimble/controller/src/ble_ll_sync.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,17 +2094,17 @@ ble_ll_sync_periodic_ind(struct ble_ll_conn_sm *connsm,
20942094

20952095
/* get anchor for specified conn event */
20962096
conn_event_count = get_le16(sync_ind + 20);
2097-
ble_ll_conn_get_anchor(connsm, conn_event_count, &sm->anchor_point,
2098-
&sm->anchor_point_usecs);
2097+
ble_ll_conn_anchor_event_cntr_get(connsm, conn_event_count, &sm->anchor_point,
2098+
&sm->anchor_point_usecs);
20992099

21002100
/* Set last anchor point */
21012101
sm->last_anchor_point = sm->anchor_point - (last_pa_diff * sm->itvl_ticks);
21022102

21032103
/* calculate extra window widening */
21042104
sync_conn_event_count = get_le16(sync_ind + 32);
21052105
sca = sync_ind[24] >> 5;
2106-
ble_ll_conn_get_anchor(connsm, sync_conn_event_count, &sync_anchor,
2107-
&sync_anchor_usecs);
2106+
ble_ll_conn_anchor_event_cntr_get(connsm, sync_conn_event_count, &sync_anchor,
2107+
&sync_anchor_usecs);
21082108
ww_adjust = ble_ll_utils_calc_window_widening(connsm->anchor_point,
21092109
sync_anchor, sca);
21102110

@@ -2155,7 +2155,7 @@ ble_ll_sync_put_syncinfo(struct ble_ll_sync_sm *syncsm,
21552155

21562156
/* get anchor for conn event that is before periodic_adv_event_start_time */
21572157
while (LL_TMR_GT(anchor, syncsm->anchor_point)) {
2158-
ble_ll_conn_get_anchor(connsm, --conn_cnt, &anchor, &anchor_usecs);
2158+
ble_ll_conn_anchor_event_cntr_get(connsm, --conn_cnt, &anchor, &anchor_usecs);
21592159
}
21602160

21612161
offset = ble_ll_tmr_t2u(syncsm->anchor_point - anchor);

0 commit comments

Comments
 (0)