Remove scanning events from the scheduler as the code was just too ugly. 
Scanning is started/stopped from link layer task


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/425a9254
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/425a9254
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/425a9254

Branch: refs/heads/master
Commit: 425a9254eb3b860cd5239bb8f616f89dff214864
Parents: 8a8b601
Author: wes3 <w...@micosa.io>
Authored: Tue Feb 9 23:12:26 2016 -0800
Committer: wes3 <w...@micosa.io>
Committed: Tue Feb 9 23:12:42 2016 -0800

----------------------------------------------------------------------
 .../controller/include/controller/ble_ll_scan.h |   4 +-
 .../include/controller/ble_ll_sched.h           |   3 -
 net/nimble/controller/src/ble_ll_scan.c         | 269 ++++++++-----------
 net/nimble/controller/src/ble_ll_sched.c        | 259 +++++-------------
 4 files changed, 178 insertions(+), 357 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/425a9254/net/nimble/controller/include/controller/ble_ll_scan.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_scan.h 
b/net/nimble/controller/include/controller/ble_ll_scan.h
index 7d690d8..5e3632b 100644
--- a/net/nimble/controller/include/controller/ble_ll_scan.h
+++ b/net/nimble/controller/include/controller/ble_ll_scan.h
@@ -21,6 +21,7 @@
 #define H_BLE_LL_SCAN_
 
 #include "controller/ble_ll_sched.h"
+#include "hal/hal_cputime.h"
 
 /* 
  * Configuration items for the number of duplicate advertisers and the
@@ -82,11 +83,10 @@ struct ble_ll_scan_sm
     uint16_t backoff_count;
     uint16_t scan_itvl;
     uint16_t scan_window;
-    uint32_t last_sched_time;
     uint32_t scan_win_start_time;
     struct os_mbuf *scan_req_pdu;
     struct os_event scan_sched_ev;
-    struct ble_ll_sched_item scan_sch;
+    struct cpu_timer scan_timer;
 };
 
 /* Scan types */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/425a9254/net/nimble/controller/include/controller/ble_ll_sched.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_sched.h 
b/net/nimble/controller/include/controller/ble_ll_sched.h
index c773460..7fe2b06 100644
--- a/net/nimble/controller/include/controller/ble_ll_sched.h
+++ b/net/nimble/controller/include/controller/ble_ll_sched.h
@@ -79,9 +79,6 @@ int ble_ll_sched_adv_reschedule(struct ble_ll_sched_item 
*sch);
 /* Reschedule a connection that had previously been scheduled or that is over 
*/
 int ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm * connsm);
 
-/* Schedule a scanning schedule item (start or stop) */
-void ble_ll_sched_scan(struct ble_ll_sched_item *sch);
-
 /* Stop the scheduler */
 void ble_ll_sched_stop(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/425a9254/net/nimble/controller/src/ble_ll_scan.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_scan.c 
b/net/nimble/controller/src/ble_ll_scan.c
index 1a79855..ada4407 100644
--- a/net/nimble/controller/src/ble_ll_scan.c
+++ b/net/nimble/controller/src/ble_ll_scan.c
@@ -48,11 +48,6 @@
  * 3) Interleave sending scan requests to different advertisers? I guess I 
need 
  * a list of advertisers to which I sent a scan request and have yet to
  * receive a scan response from? Implement this.
- * 
- * 4) The current way I do things, it is possible that keep rescheduling the
- * scanning item but never get a chance to execute it. This means that the
- * last scan win start time could get really old. If we keep the current
- * way of doing things we will need to address this issue.
  */
 
 /* The scanning state machine global object */
@@ -63,7 +58,6 @@ struct ble_ll_scan_stats
 {
     uint32_t scan_starts;
     uint32_t scan_stops;
-    uint32_t scan_event_misses;
     uint32_t scan_req_txf;
     uint32_t scan_req_txg;
 };
@@ -493,15 +487,15 @@ ble_ll_scan_chk_filter_policy(uint8_t pdu_type, uint8_t 
*rxbuf, uint8_t flags)
 }
 
 /**
- * Schedule callback for the start of a scan window 
+ * Called to enable the receiver for scanning.
  *  
- * Context: Interrupt 
+ * Context: Link Layer task
  * 
  * @param sch 
  * 
  * @return int 
  */
-static int
+static void
 ble_ll_scan_start(struct ble_ll_scan_sm *scansm, uint8_t chan)
 {
     int rc;
@@ -518,49 +512,19 @@ ble_ll_scan_start(struct ble_ll_scan_sm *scansm, uint8_t 
chan)
 
     /* Start receiving */
     rc = ble_phy_rx();
-    if (rc) {
-        rc =  BLE_LL_SCHED_STATE_DONE;
-    } else {
+    if (!rc) {
         /* Set link layer state to scanning */
         if (scansm->scan_type == BLE_SCAN_TYPE_INITIATE) {
             ble_ll_state_set(BLE_LL_STATE_INITIATING);
         } else {
             ble_ll_state_set(BLE_LL_STATE_SCANNING);
         }
-
-        /* Set end time to end of scan window */
-        rc = BLE_LL_SCHED_STATE_RUNNING;
     }
 
     /* If there is a still a scan response pending, we have failed! */
     if (scansm->scan_rsp_pending) {
         ble_ll_scan_req_backoff(scansm, 0);
     }
-
-    return rc;
-}
-
-static void
-ble_ll_scan_update_window(struct ble_ll_scan_sm *scansm, uint32_t cputime)
-{
-    uint8_t chan;
-    uint32_t itvl;
-    uint32_t win_start;
-
-    itvl = cputime_usecs_to_ticks(scansm->scan_itvl * BLE_HCI_SCAN_ITVL);
-    chan = scansm->scan_chan;
-    win_start = scansm->scan_win_start_time;
-    while ((int32_t)(cputime - win_start) >= itvl) {
-        win_start += itvl;
-        ++chan;
-        if (chan == BLE_PHY_NUM_CHANS) {
-            chan = BLE_PHY_ADV_CHAN_START;
-        }
-    }
-
-    scansm->scan_chan = chan;
-    scansm->scan_win_start_time = win_start;
-    scansm->last_sched_time = cputime;
 }
 
 /**
@@ -568,7 +532,7 @@ ble_ll_scan_update_window(struct ble_ll_scan_sm *scansm, 
uint32_t cputime)
  * are inside the scan window it means that the device should be receiving 
  * on the scan channel. 
  * 
- * Context: Interrupt and Link Layer
+ * Context: Link Layer
  *  
  * @param scansm 
  * 
@@ -610,70 +574,6 @@ ble_ll_scan_window_chk(struct ble_ll_scan_sm *scansm, 
uint32_t cputime)
 }
 
 /**
- * Called when the scanning schedule item executes
- *  
- * Context: Interrupt 
- * 
- * @param sch 
- * 
- * @return int 
- */
-int
-ble_ll_scan_sched_cb(struct ble_ll_sched_item *sch)
-{
-    int rc;
-    uint32_t now;
-    struct ble_ll_scan_sm *scansm;
-
-    /* 
-     * If we are not in the standby state it means that the scheduled
-     * scanning event was overlapped in the schedule. In this case all we do
-     * is post the scan schedule end event.
-     */
-    switch (ble_ll_state_get()) {
-    case BLE_LL_STATE_ADV:
-    case BLE_LL_STATE_CONNECTION:
-        rc = -1;
-        break;
-    case BLE_LL_STATE_INITIATING:
-    case BLE_LL_STATE_SCANNING:
-        /* Must disable PHY since we will move to a new channel */
-        ble_phy_disable();
-        rc = 0;
-        break;
-    case BLE_LL_STATE_STANDBY:
-        rc = 0;
-        break;
-    default:
-        rc = 0;
-        assert(0);
-        break;
-    }
-
-    now = cputime_get32();
-    scansm = (struct ble_ll_scan_sm *)sch->cb_arg;
-    if (rc) {
-        rc =  BLE_LL_SCHED_STATE_DONE;
-    } else {
-        /* Determine if we should be off or receiving */
-        rc = ble_ll_scan_window_chk(scansm, now);
-        if (!rc) {
-            rc = BLE_LL_SCHED_STATE_RUNNING;
-        } else {
-            rc =  BLE_LL_SCHED_STATE_DONE;
-        }
-    }
-
-    /* Update scan window start time and channel */
-    ble_ll_scan_update_window(scansm, now);
-
-    /* Post scanning scheduled done event */
-    ble_ll_event_send(&scansm->scan_sched_ev);
-
-    return rc;
-}
-
-/**
  * Stop the scanning state machine 
  */
 void
@@ -683,9 +583,9 @@ ble_ll_scan_sm_stop(int chk_disable)
     uint8_t lls;
     struct ble_ll_scan_sm *scansm;
 
-    /* Remove any scheduled advertising items */
+    /* Stop the scanning timer  */
     scansm = &g_ble_ll_scan_sm;
-    ble_ll_sched_rmv_elem(&scansm->scan_sch);
+    cputime_timer_stop(&scansm->scan_timer);
 
     /* Disable scanning state machine */
     scansm->scan_enabled = 0;
@@ -714,9 +614,6 @@ ble_ll_scan_sm_stop(int chk_disable)
 static int
 ble_ll_scan_sm_start(struct ble_ll_scan_sm *scansm)
 {
-    os_sr_t sr;
-    struct ble_ll_sched_item *sch;
-
     /* 
      * XXX: not sure if I should do this or just report whatever random
      * address the host sent. For now, I will reject the command with a
@@ -755,75 +652,107 @@ ble_ll_scan_sm_start(struct ble_ll_scan_sm *scansm)
     scansm->backoff_count = 1;
     scansm->scan_rsp_pending = 0;
 
-    /* Set schedule elements */
-    sch = &scansm->scan_sch;
-    sch->sched_type = BLE_LL_SCHED_TYPE_SCAN;
-    sch->cb_arg = scansm;
-    sch->sched_cb = ble_ll_scan_sched_cb;
-
     /* XXX: align to current or next slot???. */
     /* Schedule start time now */
     scansm->scan_win_start_time = cputime_get32();
-    scansm->last_sched_time = scansm->scan_win_start_time;
-
-    /* 
-     * If we are in standby, start scanning. Otherwise, scanning will resume
-     * when the currently scheduled event ends.
-     */ 
-    OS_ENTER_CRITICAL(sr);
-    if (ble_ll_state_get() == BLE_LL_STATE_STANDBY) {
-        ble_ll_scan_start(scansm, BLE_PHY_ADV_CHAN_START);
-    }
-    OS_EXIT_CRITICAL(sr);
 
-    /* Post scanning scheduled done event */
+    /* Post scanning event to start off the scanning process */
     ble_ll_event_send(&scansm->scan_sched_ev);
 
     return BLE_ERR_SUCCESS;
 }
 
+/**
+ * Called to process the scanning OS event which was posted to the LL task 
+ *  
+ * Context: Link Layer task. 
+ * 
+ * @param arg 
+ */
 void
 ble_ll_scan_event_proc(void *arg)
 {
+    os_sr_t sr;
+    int rxstate;
+    int start_scan;
+    uint8_t chan;
     uint32_t now;
-    uint32_t scan_win_start;
-    uint32_t itvl;
-    uint32_t win_ticks;
+    uint32_t dt;
+    uint32_t win;
+    uint32_t win_start;
+    uint32_t scan_itvl;
+    uint32_t next_event_time;
     struct ble_ll_scan_sm *scansm;
-    struct ble_ll_sched_item *sch;
 
+    /* 
+     * Get the scanning state machine. If not enabled (this is possible), just
+     * leave and do nothing (just make sure timer is stopped).
+     */
     scansm = (struct ble_ll_scan_sm *)arg;
-    sch = &scansm->scan_sch;
+    if (!scansm->scan_enabled) {
+        cputime_timer_stop(&scansm->scan_timer);
+        return;
+    }
 
-    assert((int32_t)(scansm->last_sched_time-scansm->scan_win_start_time) >= 
0);
+    /* Make sure the scan window start time and channel are up to date. */
+    now = cputime_get32();
 
-    itvl = cputime_usecs_to_ticks(scansm->scan_itvl * BLE_HCI_SCAN_ITVL);
-    scan_win_start = scansm->scan_win_start_time;
-    while ((int32_t)(scansm->last_sched_time - scan_win_start) >= itvl) {
-        scan_win_start += itvl;
+    scan_itvl = cputime_usecs_to_ticks(scansm->scan_itvl * BLE_HCI_SCAN_ITVL);
+    chan = scansm->scan_chan;
+    win_start = scansm->scan_win_start_time;
+    while ((int32_t)(now - win_start) >= scan_itvl) {
+        win_start += scan_itvl;
+        ++chan;
+        if (chan == BLE_PHY_NUM_CHANS) {
+            chan = BLE_PHY_ADV_CHAN_START;
+        }
     }
 
-    /* Determine what the next scheduled scanning item should be. */
-    if (scansm->scan_window == scansm->scan_itvl) {
-        scan_win_start += itvl;
-    } else {
-        win_ticks = cputime_usecs_to_ticks(scansm->scan_window * 
BLE_HCI_SCAN_ITVL);
-        if ((scansm->last_sched_time - scansm->scan_win_start_time) < 
win_ticks) {
-            scan_win_start += win_ticks;
+    dt = now - win_start;
+    scansm->scan_chan = chan;
+    scansm->scan_win_start_time = win_start;
+
+    /* Determine on/off state based on scan window */
+    rxstate = 1;
+    next_event_time = win_start + scan_itvl;
+    if (scansm->scan_window != scansm->scan_itvl) {
+        win = cputime_usecs_to_ticks(scansm->scan_window * BLE_HCI_SCAN_ITVL);
+        if (dt >= win) {
+            rxstate = 0;
         } else {
-            scan_win_start += itvl;
+            next_event_time = win_start + win;
         }
     }
 
-    now = cputime_get32();
-    if ((int32_t)(now - scan_win_start) <= 0) {
-        sch->start_time = scan_win_start;
-    } else {
-        ++g_ble_ll_scan_stats.scan_event_misses;
-        sch->start_time = now;
+    OS_ENTER_CRITICAL(sr);
+    /* 
+     * If we are not in the standby state it means that the scheduled
+     * scanning event was overlapped in the schedule. In this case all we do
+     * is post the scan schedule end event.
+     */
+    start_scan = 1;
+    switch (ble_ll_state_get()) {
+    case BLE_LL_STATE_ADV:
+    case BLE_LL_STATE_CONNECTION:
+         start_scan = 0;
+        break;
+    case BLE_LL_STATE_INITIATING:
+    case BLE_LL_STATE_SCANNING:
+        /* Must disable PHY since we will move to a new channel */
+        ble_phy_disable();
+        break;
+    case BLE_LL_STATE_STANDBY:
+        break;
+    default:
+        assert(0);
+        break;
+    }
+    if (start_scan && rxstate) {
+        ble_ll_scan_start(scansm, scansm->scan_chan);
     }
-    sch->end_time = sch->start_time;
-    ble_ll_sched_scan(sch);
+    OS_EXIT_CRITICAL(sr);
+
+    cputime_timer_start(&scansm->scan_timer, next_event_time);
 }
 
 /**
@@ -860,14 +789,18 @@ ble_ll_scan_rx_isr_start(uint8_t pdu_type, struct os_mbuf 
*rxpdu)
 
         /* 
          * If this is the first PDU after we sent the scan response (as
-         * denoted by the scan rsp pending flag, we set a bit in the ble
+         * denoted by the scan rsp pending flag), we set a bit in the ble
          * header so the link layer can check to see if the scan request
          * was successful. We do it this way to let the Link Layer do the
-         * work for successful scan requests.
+         * work for successful scan requests. If failed, we do the work here.
          */ 
         if (scansm->scan_rsp_pending) {
-            ble_hdr = BLE_MBUF_HDR_PTR(rxpdu);
-            ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_SCAN_RSP_CHK;
+            if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_RSP) {
+                ble_hdr = BLE_MBUF_HDR_PTR(rxpdu);
+                ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_SCAN_RSP_CHK;
+            } else {
+                ble_ll_scan_req_backoff(scansm, 0);
+            }
         }
 
         /* Disable wfr if running */
@@ -1025,6 +958,23 @@ ble_ll_scan_chk_resume(void)
 }
 
 /**
+ * Connection supervision timer callback; means that the connection supervision
+ * timeout has been reached and we should perform the appropriate actions. 
+ *  
+ * Context: Interrupt (cputimer)
+ * 
+ * @param arg Pointer to connection state machine.
+ */
+void
+ble_ll_scan_timer_cb(void *arg)
+{
+    struct ble_ll_scan_sm *scansm;
+
+    scansm = (struct ble_ll_scan_sm *)arg;
+    ble_ll_event_send(&scansm->scan_sched_ev);
+}
+
+/**
  * Called when the wait for response timer expires while in the scanning 
  * state. 
  *  
@@ -1368,6 +1318,9 @@ ble_ll_scan_init(void)
     scansm->scan_itvl = BLE_HCI_SCAN_ITVL_DEF;
     scansm->scan_window = BLE_HCI_SCAN_WINDOW_DEF;
 
+    /* Initialize connection supervision timer */
+    cputime_timer_init(&scansm->scan_timer, ble_ll_scan_timer_cb, scansm);
+
     /* Get a scan request mbuf (packet header) and attach to state machine */
     ble_get_packet(scansm->scan_req_pdu);
     assert(scansm->scan_req_pdu != NULL);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/425a9254/net/nimble/controller/src/ble_ll_sched.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_sched.c 
b/net/nimble/controller/src/ble_ll_sched.c
index a41c43c..c3b0436 100644
--- a/net/nimble/controller/src/ble_ll_sched.c
+++ b/net/nimble/controller/src/ble_ll_sched.c
@@ -77,36 +77,6 @@ ble_ll_sched_is_overlap(struct ble_ll_sched_item *s1,
     return rc;
 }
 
-/**
- * Checks if the scheduled item 'sch' overlaps the scanning schedule item 
- * 'scan'. If so, the scanning scheduled item is removed and we post the 
- * scan event so that we can reschedule the next scanning opportunity. 
- * 
- * 
- * @param scan 
- * @param sch 
- */
-static void
-ble_ll_sched_chk_scan_overlap(struct ble_ll_sched_item *scan, 
-                              struct ble_ll_sched_item *sch)
-{
-    struct ble_ll_scan_sm *scansm;
-
-    if (ble_ll_sched_is_overlap(scan, sch)) {
-        scansm = (struct ble_ll_scan_sm *)scan->cb_arg;
-        ble_ll_event_send(&scansm->scan_sched_ev);
-        TAILQ_REMOVE(&g_ble_ll_sched_q, scan, link);
-        scan->enqueued = 0;
-    } else {
-        /* If scheduled item is before the scan item, we need to reinsert */
-        if ((int32_t)(sch->end_time - scan->start_time) <= 0) {
-            TAILQ_REMOVE(&g_ble_ll_sched_q, scan, link);
-            scan->enqueued = 0;
-            ble_ll_sched_scan(scan);
-        }
-    }
-}
-
 /* 
  * Determines if the schedule item overlaps the currently running schedule
  * item. We only care about connection schedule items
@@ -171,7 +141,6 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
     struct ble_ll_sched_item *end_overlap;
     struct ble_ll_sched_item *entry;
     struct ble_ll_conn_sm *tmp;
-    struct ble_ll_scan_sm *scansm;
 
     /* Get schedule element from connection */
     sch = &connsm->conn_sch;
@@ -208,10 +177,9 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
     TAILQ_FOREACH(entry, &g_ble_ll_sched_q, link) {
         if (ble_ll_sched_is_overlap(sch, entry)) {
             /* Only insert if this element is older than all that we overlap */
-            if ((entry->sched_type != BLE_LL_SCHED_TYPE_SCAN) &&
-                ((entry->sched_type == BLE_LL_SCHED_TYPE_ADV) ||
-                 !ble_ll_conn_is_lru((struct ble_ll_conn_sm *)sch->cb_arg, 
-                                     (struct ble_ll_conn_sm *)entry->cb_arg))) 
{
+            if ((entry->sched_type == BLE_LL_SCHED_TYPE_ADV) ||
+                !ble_ll_conn_is_lru((struct ble_ll_conn_sm *)sch->cb_arg, 
+                                    (struct ble_ll_conn_sm *)entry->cb_arg)) {
                 start_overlap = NULL;
                 rc = -1;
                 break;
@@ -245,9 +213,6 @@ ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm *connsm)
         if (entry->sched_type == BLE_LL_SCHED_TYPE_CONN) {
             tmp = (struct ble_ll_conn_sm *)entry->cb_arg;
             ble_ll_event_send(&tmp->conn_ev_end);
-        } else {
-            scansm = (struct ble_ll_scan_sm *)entry->cb_arg;
-            ble_ll_event_send(&scansm->scan_sched_ev);
         }
 
         TAILQ_REMOVE(&g_ble_ll_sched_q, entry, link);
@@ -284,14 +249,10 @@ ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, 
uint32_t adv_rxend,
     uint32_t ce_end_time;
     struct ble_ll_sched_item *entry;
     struct ble_ll_sched_item *sch;
-    struct ble_ll_sched_item *scan;
 
     /* Better have a connsm */
     assert(connsm != NULL);
 
-    /* Assume no scheduled item is a scanning item */
-    scan = NULL;
-
     /* Get schedule element from connection */
     rc = -1;
     sch = &connsm->conn_sch;
@@ -340,25 +301,20 @@ ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, 
uint32_t adv_rxend,
             sch->start_time = earliest_start;
             sch->end_time = earliest_end;
 
-            /* Ignore scan/initiate types */
-            if (entry->sched_type == BLE_LL_SCHED_TYPE_SCAN) {
-                scan = entry;
-            } else {
-                /* We can insert if before entry in list */
-                if ((int32_t)(sch->end_time - entry->start_time) < 0) {
-                    if ((earliest_start - initial_start) <= itvl_t) {
-                        rc = 0;
-                        TAILQ_INSERT_BEFORE(entry, sch, link);
-                    }
-                    break;
+            /* We can insert if before entry in list */
+            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
+                if ((earliest_start - initial_start) <= itvl_t) {
+                    rc = 0;
+                    TAILQ_INSERT_BEFORE(entry, sch, link);
                 }
+                break;
+            }
 
-                /* Check for overlapping events */
-                if (ble_ll_sched_is_overlap(sch, entry)) {
-                    /* Earliest start is end of this event since we overlap */
-                    earliest_start = entry->end_time;
-                    earliest_end = earliest_start + dur;
-                }
+            /* Check for overlapping events */
+            if (ble_ll_sched_is_overlap(sch, entry)) {
+                /* Earliest start is end of this event since we overlap */
+                earliest_start = entry->end_time;
+                earliest_end = earliest_start + dur;
             }
         }
 
@@ -386,17 +342,6 @@ ble_ll_sched_master_new(struct ble_ll_conn_sm *connsm, 
uint32_t adv_rxend,
         connsm->anchor_point = earliest_start +
             cputime_usecs_to_ticks(XCVR_TX_SCHED_DELAY_USECS);
         connsm->ce_end_time = earliest_end; 
-
-        /* 
-         * If there was a scanning item it is possible that the order of the
-         * scanning item and the newly added connection are incorrect. So,
-         * we check for overlap and if there is one, we reschedule the
-         * scanning item
-         */
-        if (scan) {
-            ble_ll_sched_chk_scan_overlap(scan, sch);
-            sch = TAILQ_FIRST(&g_ble_ll_sched_q);
-        }
     }
 
     OS_EXIT_CRITICAL(sr);
@@ -414,7 +359,6 @@ ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm)
     struct ble_ll_sched_item *entry;
     struct ble_ll_sched_item *next_sch;
     struct ble_ll_sched_item *sch;
-    struct ble_ll_sched_item *scan;
 
     /* Get schedule element from connection */
     rc = -1;
@@ -440,26 +384,20 @@ ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm)
         /* Nothing in schedule. Schedule as soon as possible */
         rc = 0;
     } else {
-        scan = NULL;
         cputime_timer_stop(&g_ble_ll_sched_timer);
         while (1) {
-            /* Skip scanning schedule items */
             next_sch = entry->link.tqe_next;
-            if (entry->sched_type == BLE_LL_SCHED_TYPE_SCAN) {
-                scan = entry;
-            } else {
-                /* Insert if event ends before next starts */
-                if ((int32_t)(sch->end_time - entry->start_time) < 0) {
-                    rc = 0;
-                    TAILQ_INSERT_BEFORE(entry, sch, link);
-                    break;
-                }
+            /* Insert if event ends before next starts */
+            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
+                rc = 0;
+                TAILQ_INSERT_BEFORE(entry, sch, link);
+                break;
+            }
 
-                if (ble_ll_sched_is_overlap(sch, entry)) {
-                    /* If we overlap with a connection, we re-schedule */
-                    if (ble_ll_sched_conn_overlap(entry)) {
-                        break;
-                    }
+            if (ble_ll_sched_is_overlap(sch, entry)) {
+                /* If we overlap with a connection, we re-schedule */
+                if (ble_ll_sched_conn_overlap(entry)) {
+                    break;
                 }
             }
 
@@ -476,9 +414,6 @@ ble_ll_sched_slave_new(struct ble_ll_conn_sm *connsm)
 
         if (!rc) {
             sch->enqueued = 1;
-            if (scan) {
-                ble_ll_sched_chk_scan_overlap(scan, sch);
-            }
         }
         sch = TAILQ_FIRST(&g_ble_ll_sched_q);
     }
@@ -499,7 +434,6 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
     uint32_t adv_start;
     uint32_t duration;
     struct ble_ll_sched_item *entry;
-    struct ble_ll_sched_item *scan;
 
     /* Get length of schedule item */
     duration = sch->end_time - sch->start_time;
@@ -524,26 +458,20 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
         rc = 0;
         adv_start = sch->start_time;
     } else {
-        scan = NULL;
         cputime_timer_stop(&g_ble_ll_sched_timer);
         TAILQ_FOREACH(entry, &g_ble_ll_sched_q, link) {
-            /* Ignore scan/initiate types */
-            if (entry->sched_type == BLE_LL_SCHED_TYPE_SCAN) {
-                scan = entry;
-            } else {
-                /* We can insert if before entry in list */
-                if ((int32_t)(sch->end_time - entry->start_time) < 0) {
-                    rc = 0;
-                    TAILQ_INSERT_BEFORE(entry, sch, link);
-                    break;
-                }
+            /* We can insert if before entry in list */
+            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
+                rc = 0;
+                TAILQ_INSERT_BEFORE(entry, sch, link);
+                break;
+            }
 
-                /* Check for overlapping events */
-                if (ble_ll_sched_is_overlap(sch, entry)) {
-                    /* Earliest start is end of this event since we overlap */
-                    sch->start_time = entry->end_time;
-                    sch->end_time = sch->start_time + duration;
-                }
+            /* Check for overlapping events */
+            if (ble_ll_sched_is_overlap(sch, entry)) {
+                /* Earliest start is end of this event since we overlap */
+                sch->start_time = entry->end_time;
+                sch->end_time = sch->start_time + duration;
             }
         }
 
@@ -555,9 +483,6 @@ ble_ll_sched_adv_new(struct ble_ll_sched_item *sch)
 
         if (!rc) {
             sch->enqueued = 1;
-            if (scan) {
-                ble_ll_sched_chk_scan_overlap(scan, sch);
-            }
         }
 
         /* Restart with head of list */
@@ -586,7 +511,6 @@ ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch)
     os_sr_t sr;
     struct ble_ll_sched_item *entry;
     struct ble_ll_sched_item *next_sch;
-    struct ble_ll_sched_item *scan;
 
     rc = 0;
     OS_ENTER_CRITICAL(sr);
@@ -599,25 +523,19 @@ ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch)
 
     entry = ble_ll_sched_insert_if_empty(sch);
     if (entry) {
-        scan = NULL;
         cputime_timer_stop(&g_ble_ll_sched_timer);
         while (1) {
-            /* Skip scanning schedule items */
+            /* Insert before if adv event is before this event */
             next_sch = entry->link.tqe_next;
-            if (entry->sched_type == BLE_LL_SCHED_TYPE_SCAN) {
-                scan = entry;
-            } else {
-                /* Insert before if adv event is before this event */
-                if ((int32_t)(sch->end_time - entry->start_time) < 0) {
-                    rc = 0;
-                    TAILQ_INSERT_BEFORE(entry, sch, link);
-                    break;
-                }
+            if ((int32_t)(sch->end_time - entry->start_time) < 0) {
+                rc = 0;
+                TAILQ_INSERT_BEFORE(entry, sch, link);
+                break;
+            }
 
-                if (ble_ll_sched_is_overlap(sch, entry)) {
-                    if (ble_ll_sched_conn_overlap(entry)) {
-                        assert(0);
-                    }
+            if (ble_ll_sched_is_overlap(sch, entry)) {
+                if (ble_ll_sched_conn_overlap(entry)) {
+                    assert(0);
                 }
             }
 
@@ -634,9 +552,6 @@ ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch)
 
         if (!rc) {
             sch->enqueued = 1;
-            if (scan) {
-                ble_ll_sched_chk_scan_overlap(scan, sch);
-            }
         }
 
         sch = TAILQ_FIRST(&g_ble_ll_sched_q);
@@ -649,46 +564,6 @@ ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch)
     return rc;
 }
 
-void
-ble_ll_sched_scan(struct ble_ll_sched_item *sch)
-{
-    int insert_head;
-    os_sr_t sr;
-    struct ble_ll_sched_item *entry;
-
-    /* Assume insert to head */
-    insert_head = 1;
-
-    OS_ENTER_CRITICAL(sr);
-
-    entry = ble_ll_sched_insert_if_empty(sch);
-    if (entry) {
-        TAILQ_FOREACH(entry, &g_ble_ll_sched_q, link) {
-            /* No need to worry about overlap. */
-            if ((int32_t)(sch->start_time - entry->start_time) < 0) {
-                TAILQ_INSERT_BEFORE(entry, sch, link);
-                break;
-            }
-            insert_head = 0;
-        }
-
-        if (!entry) {
-            TAILQ_INSERT_TAIL(&g_ble_ll_sched_q, sch, link);
-        } else {
-            if (insert_head) {
-                cputime_timer_stop(&g_ble_ll_sched_timer);
-            }
-        }
-        sch->enqueued = 1;
-    }
-
-    OS_EXIT_CRITICAL(sr);
-
-    if (insert_head) {
-        cputime_timer_start(&g_ble_ll_sched_timer, sch->start_time);
-    }
-}
-
 /**
  * Remove a schedule element
  * 
@@ -727,7 +602,9 @@ ble_ll_sched_rmv_elem(struct ble_ll_sched_item *sch)
 }
 
 /**
- * Executes a schedule item by calling the schedule callback function.
+ * Executes a schedule item by calling the schedule callback function. 
+ *  
+ * Context: Interrupt 
  * 
  * @param sch Pointer to schedule item
  * 
@@ -740,29 +617,23 @@ ble_ll_sched_execute_item(struct ble_ll_sched_item *sch)
     uint8_t lls;
 
     /* 
-     * Scanning items are special. If this is one, just call the callback.
-     * If we are doing something else the callback will handle it.
-     */ 
-    if (sch->sched_type != BLE_LL_SCHED_TYPE_SCAN) {
-        /* 
-         * This is either an advertising event or connection event start. If
-         * we are scanning or initiating just stop it.
-         */
-        lls = ble_ll_state_get();
-        if (lls != BLE_LL_STATE_STANDBY) {
-            /* We have to disable the PHY no matter what */
-            ble_phy_disable();
-            ble_ll_wfr_disable();
-            if ((lls == BLE_LL_STATE_SCANNING) || 
-                (lls == BLE_LL_STATE_INITIATING)) {
-                ble_ll_state_set(BLE_LL_STATE_STANDBY);
-            } else if (lls == BLE_LL_STATE_ADV) {
-                ++g_ble_ll_stats.sched_state_adv_errs;
-                ble_ll_adv_halt();
-            } else {
-                ble_ll_conn_event_halt();
-                ++g_ble_ll_stats.sched_state_conn_errs;
-            }
+     * This is either an advertising event or connection event start. If
+     * we are scanning or initiating just stop it.
+     */
+    lls = ble_ll_state_get();
+    if (lls != BLE_LL_STATE_STANDBY) {
+        /* We have to disable the PHY no matter what */
+        ble_phy_disable();
+        ble_ll_wfr_disable();
+        if ((lls == BLE_LL_STATE_SCANNING) || 
+            (lls == BLE_LL_STATE_INITIATING)) {
+            ble_ll_state_set(BLE_LL_STATE_STANDBY);
+        } else if (lls == BLE_LL_STATE_ADV) {
+            ++g_ble_ll_stats.sched_state_adv_errs;
+            ble_ll_adv_halt();
+        } else {
+            ble_ll_conn_event_halt();
+            ++g_ble_ll_stats.sched_state_conn_errs;
         }
     }
 

Reply via email to