This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git


The following commit(s) were added to refs/heads/master by this push:
     new 9d7c5c50 nimble/ll: Simplify sync sched handling
9d7c5c50 is described below

commit 9d7c5c5006b1ea2c568db53f211ebb937b482e15
Author: Andrzej Kaczmarek <andrzej.kaczma...@codecoup.pl>
AuthorDate: Tue Jun 6 00:43:05 2023 +0200

    nimble/ll: Simplify sync sched handling
    
    Make sync sched handling more like other items, i.e. scheduler item is
    set via internal API and scheduling funcs just do the necessary
    scheduling.
---
 .../controller/include/controller/ble_ll_sched.h   |  9 +---
 nimble/controller/src/ble_ll_sched.c               | 58 ++--------------------
 nimble/controller/src/ble_ll_sync.c                | 46 +++++++++++++----
 3 files changed, 44 insertions(+), 69 deletions(-)

diff --git a/nimble/controller/include/controller/ble_ll_sched.h 
b/nimble/controller/include/controller/ble_ll_sched.h
index 961a20aa..cceb96ce 100644
--- a/nimble/controller/include/controller/ble_ll_sched.h
+++ b/nimble/controller/include/controller/ble_ll_sched.h
@@ -142,13 +142,8 @@ int ble_ll_sched_adv_new(struct ble_ll_sched_item *sch,
 /* Schedule periodic advertising event */
 int ble_ll_sched_periodic_adv(struct ble_ll_sched_item *sch, bool first_event);
 
-int ble_ll_sched_sync_reschedule(struct ble_ll_sched_item *sch,
-                                 uint32_t anchor_point,
-                                 uint8_t anchor_point_usecs,
-                                 uint32_t window_widening, int8_t phy_mode);
-int ble_ll_sched_sync(struct ble_ll_sched_item *sch,
-                      uint32_t beg_cputime, uint32_t rem_usecs, uint32_t 
offset,
-                      int8_t phy_mode);
+int ble_ll_sched_sync_reschedule(struct ble_ll_sched_item *sch, uint32_t 
ww_us);
+int ble_ll_sched_sync(struct ble_ll_sched_item *sch);
 
 /* Reschedule an advertising event */
 int ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch,
diff --git a/nimble/controller/src/ble_ll_sched.c 
b/nimble/controller/src/ble_ll_sched.c
index c4cbe142..8e8cee18 100644
--- a/nimble/controller/src/ble_ll_sched.c
+++ b/nimble/controller/src/ble_ll_sched.c
@@ -671,38 +671,13 @@ ble_ll_sched_sync_overlaps_current(struct 
ble_ll_sched_item *sch)
 }
 
 int
-ble_ll_sched_sync_reschedule(struct ble_ll_sched_item *sch,
-                             uint32_t anchor_point, uint8_t anchor_point_usecs,
-                             uint32_t window_widening,
-                             int8_t phy_mode)
+ble_ll_sched_sync_reschedule(struct ble_ll_sched_item *sch, uint32_t ww_us)
 {
-    uint8_t start_time_rem_usecs;
-    uint32_t start_time;
-    uint32_t end_time;
-    uint32_t dur;
-    int rc = 0;
     os_sr_t sr;
+    int rc = 0;
 
-    start_time = anchor_point;
-    start_time_rem_usecs = anchor_point_usecs;
-
-    ble_ll_tmr_sub(&start_time, &start_time_rem_usecs, window_widening);
-
-    dur = ble_ll_pdu_us(MYNEWT_VAL(BLE_LL_SCHED_SCAN_SYNC_PDU_LEN),
-                                 phy_mode);
-    end_time = start_time + ble_ll_tmr_u2t(dur);
-
-    start_time -= g_ble_ll_sched_offset_ticks;
-
-    /* Set schedule start and end times */
-    sch->start_time = start_time;
-    sch->remainder = start_time_rem_usecs;
-    sch->end_time = end_time;
-
-    /* Better be past current time or we just leave */
-    if (LL_TMR_LEQ(sch->start_time, ble_ll_tmr_get())) {
-        return -1;
-    }
+    /* Adjust start time to include window widening */
+    ble_ll_tmr_sub(&sch->start_time, &sch->remainder, ww_us);
 
     OS_ENTER_CRITICAL(sr);
 
@@ -721,32 +696,11 @@ ble_ll_sched_sync_reschedule(struct ble_ll_sched_item 
*sch,
 }
 
 int
-ble_ll_sched_sync(struct ble_ll_sched_item *sch,
-                  uint32_t beg_cputime, uint32_t rem_usecs,
-                  uint32_t offset, int8_t phy_mode)
+ble_ll_sched_sync(struct ble_ll_sched_item *sch)
 {
-    uint8_t start_time_rem_usecs;
-    uint32_t start_time;
-    uint32_t end_time;
-    uint32_t dur;
     os_sr_t sr;
     int rc = 0;
 
-    start_time = beg_cputime;
-    start_time_rem_usecs = rem_usecs;
-
-    ble_ll_tmr_add(&start_time, &start_time_rem_usecs, offset);
-
-    dur = ble_ll_pdu_us(MYNEWT_VAL(BLE_LL_SCHED_SCAN_SYNC_PDU_LEN),
-                                  phy_mode);
-    end_time = start_time + ble_ll_tmr_u2t(dur);
-
-    start_time -= g_ble_ll_sched_offset_ticks;
-
-    sch->start_time = start_time;
-    sch->remainder = start_time_rem_usecs;
-    sch->end_time = end_time;
-
     OS_ENTER_CRITICAL(sr);
 
     rc = ble_ll_sched_insert(sch, 0, preempt_none);
@@ -755,8 +709,6 @@ ble_ll_sched_sync(struct ble_ll_sched_item *sch,
 
     ble_ll_sched_restart();
 
-    STATS_INC(ble_ll_stats, sync_scheduled);
-
     return rc;
 }
 #endif
diff --git a/nimble/controller/src/ble_ll_sync.c 
b/nimble/controller/src/ble_ll_sync.c
index f10a5993..8aef8e2c 100644
--- a/nimble/controller/src/ble_ll_sync.c
+++ b/nimble/controller/src/ble_ll_sync.c
@@ -25,6 +25,7 @@
 #include "syscfg/syscfg.h"
 
 #include "controller/ble_ll.h"
+#include "controller/ble_ll_pdu.h"
 #include "controller/ble_ll_hci.h"
 #include "controller/ble_ll_sync.h"
 #include "controller/ble_ll_utils.h"
@@ -946,6 +947,26 @@ ble_ll_sync_parse_aux_ptr(const uint8_t *buf, uint8_t 
*chan, uint32_t *offset,
     *phy = (aux_ptr_field >> 21) & 0x07;
 }
 
+static void
+ble_ll_sync_sched_set(struct ble_ll_sched_item *sch, uint32_t start_ticks,
+                      uint8_t start_rem_us, uint32_t offset, uint8_t phy_mode)
+{
+    uint32_t duration_us;
+    uint32_t duration;
+
+    if (offset > 0) {
+        ble_ll_tmr_add(&start_ticks, &start_rem_us, offset);
+    }
+
+    duration_us = ble_ll_pdu_us(MYNEWT_VAL(BLE_LL_SCHED_SCAN_SYNC_PDU_LEN),
+                                phy_mode);
+    duration = ble_ll_tmr_u2t(duration_us);
+
+    sch->start_time = start_ticks - g_ble_ll_sched_offset_ticks;
+    sch->remainder = start_rem_us;
+    sch->end_time = start_ticks + duration;
+}
+
 static int
 ble_ll_sync_schedule_chain(struct ble_ll_sync_sm *sm, struct ble_mbuf_hdr *hdr,
                            const uint8_t *aux)
@@ -980,8 +1001,10 @@ ble_ll_sync_schedule_chain(struct ble_ll_sync_sm *sm, 
struct ble_mbuf_hdr *hdr,
 
     sm->flags |= BLE_LL_SYNC_SM_FLAG_CHAIN;
 
-    return ble_ll_sched_sync(&sm->sch, hdr->beg_cputime, hdr->rem_usecs,
-                             offset, sm->phy_mode);
+    ble_ll_sync_sched_set(&sm->sch, hdr->beg_cputime, hdr->rem_usecs, offset,
+                          sm->phy_mode);
+
+    return ble_ll_sched_sync(&sm->sch);
 }
 
 static void
@@ -1358,9 +1381,10 @@ ble_ll_sync_event_end(struct ble_npl_event *ev)
             ble_ll_sync_sm_clear(sm);
             return;
         }
-    } while (ble_ll_sched_sync_reschedule(&sm->sch, sm->anchor_point,
-                                          sm->anchor_point_usecs,
-                                          sm->window_widening, sm->phy_mode));
+
+        ble_ll_sync_sched_set(&sm->sch, sm->anchor_point,
+                              sm->anchor_point_usecs, 0, sm->phy_mode);
+    } while (ble_ll_sched_sync_reschedule(&sm->sch, sm->window_widening));
 }
 
 void
@@ -1498,8 +1522,10 @@ ble_ll_sync_info_event(struct ble_ll_scan_addr_data 
*addrd,
     sm->chan_index = ble_ll_utils_dci_csa2(sm->event_cntr, sm->channel_id,
                                            sm->chan_map_used, sm->chan_map);
 
-    if (ble_ll_sched_sync(&sm->sch, rxhdr->beg_cputime, rxhdr->rem_usecs,
-                          offset, sm->phy_mode)) {
+    ble_ll_sync_sched_set(&sm->sch, rxhdr->beg_cputime, rxhdr->rem_usecs,
+                          offset, sm->phy_mode);
+
+    if (ble_ll_sched_sync(&sm->sch)) {
         return;
     }
 
@@ -2093,8 +2119,10 @@ ble_ll_sync_periodic_ind(struct ble_ll_conn_sm *connsm,
         }
     }
 
-    if (ble_ll_sched_sync(&sm->sch, sm->anchor_point, sm->anchor_point_usecs,
-                          offset, sm->phy_mode)) {
+    ble_ll_sync_sched_set(&sm->sch, sm->anchor_point, sm->anchor_point_usecs,
+                          offset, sm->phy_mode);
+
+    if (ble_ll_sched_sync(&sm->sch)) {
         /* release SM if this failed */
         ble_ll_sync_transfer_received(sm, BLE_ERR_CONN_ESTABLISHMENT);
         memset(sm, 0, sizeof(*sm));

Reply via email to