You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by we...@apache.org on 2015/11/06 02:47:02 UTC

incubator-mynewt-larva git commit: Add whitelist to advertising

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 34d3b2e51 -> 3176204a8


Add whitelist to advertising


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/3176204a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/3176204a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/3176204a

Branch: refs/heads/master
Commit: 3176204a88f266b6ac54ba850f25f7caca5aa07d
Parents: 34d3b2e
Author: Willam San Filippo <wi...@micosa.io>
Authored: Thu Nov 5 17:46:43 2015 -0800
Committer: Willam San Filippo <wi...@micosa.io>
Committed: Thu Nov 5 17:46:52 2015 -0800

----------------------------------------------------------------------
 .../controller/include/controller/ble_ll_adv.h  |  4 +-
 net/nimble/controller/src/ble_ll.c              | 29 +++---
 net/nimble/controller/src/ble_ll_adv.c          | 93 ++++++++++++++++++--
 net/nimble/controller/src/ble_ll_scan.c         |  4 +-
 net/nimble/controller/src/ble_ll_whitelist.c    | 16 ++--
 net/nimble/controller/src/ble_phy.c             |  2 +
 net/nimble/host/src/host_dbg.c                  | 37 ++++++--
 project/bletest/src/main.c                      | 28 +++---
 8 files changed, 151 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3176204a/net/nimble/controller/include/controller/ble_ll_adv.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_adv.h b/net/nimble/controller/include/controller/ble_ll_adv.h
index 5d46d9f..b7bca30 100644
--- a/net/nimble/controller/include/controller/ble_ll_adv.h
+++ b/net/nimble/controller/include/controller/ble_ll_adv.h
@@ -147,8 +147,8 @@ void ble_ll_adv_tx_done_proc(void *arg);
 /* Called to initialize advertising functionality. */
 void ble_ll_adv_init(void);
 
-/* Called when a scan request has been received. */
-int ble_ll_adv_rx_scan_req(uint8_t *rxbuf);
+/* Called on rx pdu end when in advertising state */
+int ble_ll_adv_rx_pdu_end(uint8_t pdu_type, struct os_mbuf *rxpdu);
 
 /* Boolean function denoting whether or not the whitelist can be changed */
 int ble_ll_adv_can_chg_whitelist(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3176204a/net/nimble/controller/src/ble_ll.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll.c b/net/nimble/controller/src/ble_ll.c
index 1894e92..f7b8746 100644
--- a/net/nimble/controller/src/ble_ll.c
+++ b/net/nimble/controller/src/ble_ll.c
@@ -26,7 +26,14 @@
 #include "controller/ble_ll_scan.h"
 #include "controller/ble_ll_hci.h"
 
-/* XXX: use the sanity task! */
+/* XXX:
+ * 
+ * 1) use the sanity task!
+ * 2) Need to figure out what to do with packets that we hand up that did
+ * not pass the filter policy for the given state. Currently I count all
+ * packets I think. Need to figure out what to do with this.
+ * 
+ */
 
 /* Connection related define */
 #define BLE_LL_CONN_INIT_MAX_REMOTE_OCTETS  (27)
@@ -502,24 +509,8 @@ ble_ll_rx_end(struct os_mbuf *rxpdu, uint8_t crcok)
     rc = -1;
     switch (g_ble_ll_data.ll_state) {
     case BLE_LL_STATE_ADV:
-        /* If we get a scan request*/
-        if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) {
-            /* Just bail if CRC is not good */
-            if (crcok) {
-                rc = ble_ll_adv_rx_scan_req(rxbuf);
-                if (rc) {
-                    /* XXX: One thing left to reconcile here. We have
-                     * the advertisement schedule element still running.
-                     * How to deal with the end of the advertising event?
-                     * Need to figure that out.
-                     */
-                }
-            }
-        } else {
-            if (pdu_type == BLE_ADV_PDU_TYPE_CONNECT_REQ) {
-                rc = 0;
-                /* XXX: deal with this */
-            }
+        if (crcok) {
+            rc = ble_ll_adv_rx_pdu_end(pdu_type, rxpdu);
         }
         break;
     case BLE_LL_STATE_SCANNING:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3176204a/net/nimble/controller/src/ble_ll_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_adv.c b/net/nimble/controller/src/ble_ll_adv.c
index 959f4ce..adab080 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -25,6 +25,7 @@
 #include "controller/ble_ll_adv.h"
 #include "controller/ble_ll_sched.h"
 #include "controller/ble_ll_scan.h"
+#include "controller/ble_ll_whitelist.h"
 #include "hal/hal_cputime.h"
 #include "hal/hal_gpio.h"
 
@@ -56,8 +57,7 @@
  *    we need to send a CONNECTION COMPLETE event. Do this.
  * 9) How does the advertising channel tx power get set? I dont implement
  * that currently.
- * 10) Add whitelist! Do this to the code. Need to enable/disable whitelisting
- * appropriately. Also need to set appropriate bits at the PHY (DEVMATCH).
+ * 10) Deal with whitelisting at LL when pdu is handed up to LL task.
  */
 
 /* 
@@ -463,8 +463,8 @@ ble_ll_adv_set_adv_params(uint8_t *cmd)
     adv_itvl_max = le16toh(cmd + 2);
     adv_type = cmd[4];
 
-    /* Min has to be less than max and cannot equal max */
-    if ((adv_itvl_min > adv_itvl_max) || (adv_itvl_min == adv_itvl_max)) {
+    /* Min has to be less than max */
+    if (adv_itvl_min >= adv_itvl_max) {
         return BLE_ERR_INV_HCI_CMD_PARMS;
     }
 
@@ -483,7 +483,6 @@ ble_ll_adv_set_adv_params(uint8_t *cmd)
         break;
     }
 
-    /* XXX: isnt there a maximum that we need to check? */
     /* Make sure interval minimum is valid for the advertising type */
     if ((adv_itvl_min < min_itvl) || (adv_itvl_min > BLE_HCI_ADV_ITVL_MAX)) {
         return BLE_ERR_INV_HCI_CMD_PARMS;
@@ -534,6 +533,9 @@ ble_ll_adv_sm_stop(struct ble_ll_adv_sm *advsm)
 {
     /* XXX: Stop any timers we may have started */
 
+    /* Disable whitelisting (just in case) */
+    ble_ll_whitelist_disable();
+
     /* Remove any scheduled advertising items */
     ble_ll_sched_rmv(BLE_LL_SCHED_TYPE_ADV);
 
@@ -542,7 +544,9 @@ ble_ll_adv_sm_stop(struct ble_ll_adv_sm *advsm)
 }
 
 /**
- * Start the advertising state machine. 
+ * Start the advertising state machine. This is called when the host sends 
+ * the "enable advertising" command and is not called again while in the 
+ * advertising state. 
  *  
  * Context: Link-layer task. 
  * 
@@ -591,6 +595,13 @@ ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
     adv_chan = ble_ll_adv_first_chan(advsm);
     advsm->adv_chan = adv_chan;
 
+    /* Enable/disable whitelisting based on filter policy */
+    if (advsm->adv_filter_policy != BLE_HCI_ADV_FILT_NONE) {
+        ble_ll_whitelist_enable();
+    } else {
+        ble_ll_whitelist_disable();
+    }
+
     /* 
      * Set start time for the advertising event. This time is the same
      * as the time we will send the first PDU. Since there does not seem
@@ -751,15 +762,20 @@ ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t len)
  *          0: Scan request is for us and we successfully went from rx to tx.
  *        > 0: PHY error attempting to go from rx to tx.
  */
-int
-ble_ll_adv_rx_scan_req(uint8_t *rxbuf)
+static int
+ble_ll_adv_rx_scan_req(struct os_mbuf *rxpdu)
 {
     int rc;
     uint8_t rxaddr_type;
     uint8_t *our_addr;
     uint8_t *adva;
+    uint8_t addr_type;
+    uint8_t *rxbuf;
+    struct ble_mbuf_hdr *ble_hdr;
+    struct ble_ll_adv_sm *advsm;
 
     /* Determine if this is addressed to us */
+    rxbuf = rxpdu->om_data;
     rxaddr_type = rxbuf[0] & BLE_ADV_PDU_HDR_RXADD_MASK;
     if (rxaddr_type) {
         our_addr = g_random_addr;
@@ -770,8 +786,28 @@ ble_ll_adv_rx_scan_req(uint8_t *rxbuf)
     rc = -1;
     adva = rxbuf + BLE_LL_PDU_HDR_LEN + BLE_DEV_ADDR_LEN;
     if (!memcmp(our_addr, adva, BLE_DEV_ADDR_LEN)) {
+        /* Set device match bit if we are whitelisting */
+        advsm = &g_ble_ll_adv_sm;
+        if (advsm->adv_filter_policy & 1) {
+            /* Get the scanners address type */
+            if (rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK) {
+                addr_type = BLE_ADDR_TYPE_RANDOM;
+            } else {
+                addr_type = BLE_ADDR_TYPE_PUBLIC;
+            }
+
+            /* Check for whitelist match */
+            if (!ble_ll_whitelist_match(rxbuf + BLE_LL_PDU_HDR_LEN, 
+                                        addr_type)) {
+                return rc;
+            } else {
+                ble_hdr = BLE_MBUF_HDR_PTR(rxpdu);
+                ble_hdr->flags |= BLE_MBUF_HDR_F_DEVMATCH;
+            }
+        }
+
         /* Setup to transmit the scan response */
-        rc = ble_phy_tx(g_ble_ll_adv_sm.scan_rsp_pdu, BLE_PHY_TRANSITION_RX_TX, 
+        rc = ble_phy_tx(advsm->scan_rsp_pdu, BLE_PHY_TRANSITION_RX_TX, 
                         BLE_PHY_TRANSITION_NONE);
         if (!rc) {
             ++g_ble_ll_adv_stats.scan_rsp_txg;
@@ -782,6 +818,45 @@ ble_ll_adv_rx_scan_req(uint8_t *rxbuf)
 }
 
 /**
+ * Called on phy rx pdu end when in advertising state.
+ * 
+ * Context: Interrupt 
+ *  
+ * @param pdu_type 
+ * @param rxpdu 
+ *  
+ * @return int 
+ *       < 0: Disable the phy after reception.
+ *      == 0: Do not disable the PHY as we are gi
+ *       > 0: Do not disable PHY as that has already been done.
+ */
+int
+ble_ll_adv_rx_pdu_end(uint8_t pdu_type, struct os_mbuf *rxpdu)
+{
+    int rc;
+
+    /* We only care about scan requests and connection requests */
+    rc = -1;
+    if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) {
+        rc = ble_ll_adv_rx_scan_req(rxpdu);
+        if (rc) {
+            /* XXX: One thing left to reconcile here. We have
+             * the advertisement schedule element still running.
+             * How to deal with the end of the advertising event?
+             * Need to figure that out.
+             */
+        }
+    } else {
+        if (pdu_type == BLE_ADV_PDU_TYPE_CONNECT_REQ) {
+            rc = 0;
+            /* XXX: deal with this. Dont forget filter policy  */
+        }
+    }
+
+    return rc;
+}
+
+/**
  * Process advertistement tx done event. 
  *  
  * Context: Link Layer task. 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3176204a/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 e95d9c6..521ec2b 100644
--- a/net/nimble/controller/src/ble_ll_scan.c
+++ b/net/nimble/controller/src/ble_ll_scan.c
@@ -706,10 +706,10 @@ ble_ll_scan_win_end_proc(void *arg)
     /* Set next scan window start time */
     itvl = cputime_usecs_to_ticks(scansm->scan_itvl * BLE_HCI_SCAN_ITVL);
     scansm->scan_win_start_time += itvl;
-        
+      
     /* Set next scanning window start. */        
     delta_t = (int32_t)(cputime_get32() - scansm->scan_win_start_time);
-    if (delta_t >= win_ticks) {
+    while (delta_t >= (int32_t)win_ticks) {
         /* 
          * Since it is possible to scan continuously, it is possible
          * that we will be late here if the scan window is equal to the

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3176204a/net/nimble/controller/src/ble_ll_whitelist.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_whitelist.c b/net/nimble/controller/src/ble_ll_whitelist.c
index 6eea1c4..5cfcf07 100644
--- a/net/nimble/controller/src/ble_ll_whitelist.c
+++ b/net/nimble/controller/src/ble_ll_whitelist.c
@@ -228,25 +228,29 @@ ble_ll_whitelist_rmv(uint8_t *addr, uint8_t addr_type)
     return BLE_ERR_SUCCESS;
 }
 
-/* Enable whitelisting */
+/**
+ * Enable whitelisting. 
+ *  
+ * Note: This function has no effect if we are not using HW whitelisting
+ */
 void
 ble_ll_whitelist_enable(void)
 {
 #ifdef BLE_USES_HW_WHITELIST
     ble_hw_whitelist_enable();
-#else
-    /* XXX: Is there anything to do here? */
 #endif
 }
 
-/* Disable whitelisting */
+/**
+ * Disable whitelisting.
+ *  
+ * Note: This function has no effect if we are not using HW whitelisting
+ */
 void
 ble_ll_whitelist_disable(void)
 {
 #ifdef BLE_USES_HW_WHITELIST
     ble_hw_whitelist_disable();
-#else
-    /* XXX: Is there anything to do here? */
 #endif
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3176204a/net/nimble/controller/src/ble_phy.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_phy.c b/net/nimble/controller/src/ble_phy.c
index 126c4a8..6be96b0 100644
--- a/net/nimble/controller/src/ble_phy.c
+++ b/net/nimble/controller/src/ble_phy.c
@@ -61,6 +61,7 @@ struct ble_phy_statistics
     uint32_t tx_fail;
     uint32_t tx_bytes;
     uint32_t rx_starts;
+    uint32_t rx_aborts;
     uint32_t rx_valid;
     uint32_t rx_crc_err;
     uint32_t phy_isrs;
@@ -226,6 +227,7 @@ ble_phy_isr(void)
             /* Disable PHY */
             ble_phy_disable();
             irq_en = 0;
+            ++g_ble_phy_stats.rx_aborts;
         }
 
         /* Count rx starts */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3176204a/net/nimble/host/src/host_dbg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_dbg.c b/net/nimble/host/src/host_dbg.c
index ebd164e..f2e1245 100644
--- a/net/nimble/host/src/host_dbg.c
+++ b/net/nimble/host/src/host_dbg.c
@@ -34,7 +34,7 @@ host_hci_dbg_le_event_disp(uint8_t subev, uint8_t len, uint8_t *evdata)
     char adv_data_buf[32];
 
     switch (subev) {
-    case 0x02:
+    case BLE_HCI_LE_SUBEV_ADV_RPT:
         advlen = evdata[9];
         rssi = evdata[10 + advlen];
         console_printf("LE advertising report. len=%u num=%u evtype=%u "
@@ -68,22 +68,47 @@ host_hci_dbg_le_event_disp(uint8_t subev, uint8_t len, uint8_t *evdata)
 }
 
 void
+host_hci_dbg_cmd_complete_disp(uint8_t *evdata, uint8_t len)
+{
+    uint8_t ogf;
+    uint8_t ocf;
+    uint16_t opcode;
+    char parmbuf[32];
+
+    opcode = le16toh(evdata + 1);
+    ogf = BLE_HCI_OGF(opcode);
+    ocf = BLE_HCI_OCF(opcode);
+
+    /* Display parameters based on command. */
+    parmbuf[0] = '\0';
+    if (ogf == BLE_HCI_OGF_LE) {
+        switch (ocf) {
+        case BLE_HCI_OCF_LE_SET_ADV_DATA:
+            snprintf(parmbuf, 12, "status=%-3d ", evdata[3]);
+            break;
+        default:
+            break;
+        }
+    }
+
+    console_printf("Command Complete: cmd_pkts=%u ocf=0x%x ogf=0x%x %s",
+                   evdata[0], ocf, ogf, parmbuf);
+}
+
+void
 host_hci_dbg_event_disp(uint8_t *evbuf)
 {
     uint8_t *evdata;
     uint8_t evcode;
     uint8_t len;
-    uint16_t opcode;
-
+ 
     evcode = evbuf[0];
     len = evbuf[1];
     evdata = evbuf + 2;
 
     switch (evcode) {
     case BLE_HCI_EVCODE_COMMAND_COMPLETE:
-        opcode = le16toh(evdata +1);
-        console_printf("Command Complete: cmd_pkts=%u ocf=0x%x ogf=0x%x",
-                       evdata[0], opcode & 0x3FF, opcode >> 10);
+        host_hci_dbg_cmd_complete_disp(evdata, len);
         break;
     case BLE_HCI_EVCODE_LE_META:
         host_hci_dbg_le_event_disp(evdata[0], len, evdata + 1);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3176204a/project/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/project/bletest/src/main.c b/project/bletest/src/main.c
index 748e7ee..b9ff2b9 100755
--- a/project/bletest/src/main.c
+++ b/project/bletest/src/main.c
@@ -66,13 +66,14 @@ os_membuf_t g_mbuf_buffer[MBUF_MEMPOOL_SIZE];
 /* Some application configurations */
 #define BLETEST_ROLE_ADVERTISER         (0)
 #define BLETEST_ROLE_SCANNER            (1)
+#define BLETEST_CFG_ROLE                (BLETEST_ROLE_SCANNER)
 #define BLETEST_CFG_FILT_DUP_ADV        (0)
 #define BLETEST_CFG_ADV_ITVL            (500000 / BLE_HCI_ADV_ITVL)
 #define BLETEST_CFG_ADV_TYPE            BLE_HCI_ADV_TYPE_ADV_SCAN_IND
+#define BLETEST_CFG_ADV_FILT_POLICY     (BLE_HCI_ADV_FILT_NONE)
 #define BLETEST_CFG_SCAN_ITVL           (700000 / BLE_HCI_SCAN_ITVL)
 #define BLETEST_CFG_SCAN_WINDOW         (650000 / BLE_HCI_SCAN_ITVL)
-#define BLETEST_CFG_ROLE                (BLETEST_ROLE_SCANNER)
-#define BLETEST_CFG_SCAN_TYPE           (BLE_HCI_SCAN_TYPE_PASSIVE)
+#define BLETEST_CFG_SCAN_TYPE           (BLE_HCI_SCAN_TYPE_ACTIVE)
 #define BLETEST_CFG_SCAN_FILT_POLICY    (BLE_HCI_SCAN_FILT_USE_WL)
 
 /* BLETEST variables */
@@ -169,7 +170,7 @@ bletest_init_advertising(void)
     adv_itvl = BLETEST_CFG_ADV_ITVL; /* Advertising interval */
     adv.adv_type = BLETEST_CFG_ADV_TYPE;
     adv.adv_channel_map = 0x07;
-    adv.adv_filter_policy = BLE_HCI_ADV_FILT_NONE;
+    adv.adv_filter_policy = BLETEST_CFG_ADV_FILT_POLICY;
     adv.own_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC;
     adv.peer_addr_type = BLE_HCI_ADV_PEER_ADDR_PUBLIC;
     adv.adv_itvl_min = BLE_HCI_ADV_ITVL_NONCONN_MIN;
@@ -204,23 +205,14 @@ bletest_init_scanner(void)
     filter_policy = BLETEST_CFG_SCAN_FILT_POLICY;
     if (filter_policy & 1) {
         /* Add some whitelist addresses */
-        dev_addr[0] = 0x91;
-        dev_addr[1] = 0xab;
-        dev_addr[2] = 0x1c;
-        dev_addr[3] = 0x7e;
-        dev_addr[4] = 0x4f;
-        dev_addr[5] = 0xd0;
+        dev_addr[0] = 0x00;
+        dev_addr[1] = 0x00;
+        dev_addr[2] = 0x00;
+        dev_addr[3] = 0x08;
+        dev_addr[4] = 0x08;
+        dev_addr[5] = 0x08;
         rc = host_hci_cmd_le_add_to_whitelist(dev_addr, BLE_ADDR_TYPE_PUBLIC);
         assert(rc == 0);
-
-        dev_addr[0] = 0x94;
-        dev_addr[1] = 0x0e;
-        dev_addr[2] = 0xf4;
-        dev_addr[3] = 0x8f;
-        dev_addr[4] = 0x20;
-        dev_addr[5] = 0xe7;
-        rc = host_hci_cmd_le_add_to_whitelist(dev_addr, BLE_ADDR_TYPE_RANDOM);
-        assert(rc == 0);
     }
 }