rymanluk commented on a change in pull request #440: LL: Support for Periodic Sync URL: https://github.com/apache/mynewt-nimble/pull/440#discussion_r285261875
########## File path: nimble/controller/src/ble_ll_sched.c ########## @@ -826,6 +827,198 @@ ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm) return rc; } +/* + * Determines if the schedule item overlaps the currently running schedule + * item. This function cares about connection and sync. + */ +static int +ble_ll_sched_sync_overlaps_current(struct ble_ll_sched_item *sch) +{ + uint32_t end_time; + uint8_t state; + + state = ble_ll_state_get(); + switch (state) { + case BLE_LL_STATE_CONNECTION: + end_time = ble_ll_conn_get_ce_end_time(); + break; + case BLE_LL_STATE_SYNC: + end_time = ble_ll_sync_get_event_end_time(); + break; + default: + return 0; + } + + return (int32_t)(end_time - sch->start_time) > 0; +} + +int +ble_ll_sched_sync_reschedule(struct ble_ll_sched_item *sch, + uint32_t anchor_point, uint32_t window_widening, + int8_t phy_mode) +{ + os_sr_t sr; + struct ble_ll_sched_item *entry; + + /* Set schedule start and end times */ + sch->start_time = anchor_point - g_ble_ll_sched_offset_ticks; + + sch->start_time -= (os_cputime_usecs_to_ticks(window_widening) + 1); + sch->remainder = 0; + + /* TODO For now assume max sync packet, make expected data size + * configurable + */ + sch->end_time = sch->start_time + os_cputime_usecs_to_ticks( + ble_ll_pdu_tx_time_get(256, phy_mode)); + + /* Better be past current time or we just leave */ + if ((int32_t)(sch->start_time - os_cputime_get32()) < 0) { + return -1; + } + + /* We have to find a place for this schedule */ + OS_ENTER_CRITICAL(sr); + + if (ble_ll_sched_sync_overlaps_current(sch)) { + OS_EXIT_CRITICAL(sr); + return -1; + } + + /* Try to find slot for sync scan. */ + os_cputime_timer_stop(&g_ble_ll_sched_timer); + TAILQ_FOREACH(entry, &g_ble_ll_sched_q, link) { + /* We can insert if before entry in list */ + if ((int32_t)(sch->end_time - entry->start_time) <= 0) { + TAILQ_INSERT_BEFORE(entry, sch, link); + sch->enqueued = 1; + break; + } + + /* Check for overlapping events. For now drop if it overlaps with + * anything. We can make it smarter later on + */ + if (ble_ll_sched_is_overlap(sch, entry)) { Review comment: looks like here we have an issue, because we return and leave `g_ble_ll_sched_timer` not running in such a case. I just fixed similar issue for `ble_ll_sched_aux_scan` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services