You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2015/11/13 03:48:14 UTC

incubator-mynewt-larva git commit: Directed Connectable Mode.

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 629247cb1 -> 23fe06693


Directed Connectable Mode.


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

Branch: refs/heads/master
Commit: 23fe06693b103df37832dbb2213c255887586fae
Parents: 629247c
Author: Christopher Collins <cc...@gmail.com>
Authored: Thu Nov 12 18:48:03 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Thu Nov 12 18:48:03 2015 -0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_gap.c               | 33 ++++++++
 net/nimble/host/src/ble_gap.h               |  1 +
 net/nimble/host/src/ble_gap_conn.c          | 99 +++++++++++++++++-------
 net/nimble/host/src/ble_gap_conn.h          |  4 +-
 net/nimble/host/src/ble_hs_work.c           |  8 +-
 net/nimble/host/src/ble_hs_work.h           |  9 ++-
 net/nimble/host/src/test/ble_hs_conn_test.c |  6 +-
 net/nimble/host/src/test/ble_hs_test_util.c |  2 +-
 8 files changed, 126 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23fe0669/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index 8989b13..8da5ead 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -52,3 +52,36 @@ ble_gap_direct_connection_establishment(uint8_t addr_type, uint8_t *addr)
 
     return 0;
 }
+
+/**
+ * Enables Directed Connectable Mode, as described in vol. 3, part C, section
+ * 9.3.3.
+ *
+ * @param addr_type             The peer's address type; one of:
+ *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC
+ *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM
+ *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC_IDENT
+ *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM_IDENT
+ * @param addr                  The address of the peer to connect to.
+ *
+ * @return                      0 on success; nonzero on failure.
+ */
+int
+ble_gap_directed_connectable(uint8_t addr_type, uint8_t *addr)
+{
+    struct ble_hs_work_entry *entry;
+
+    entry = ble_hs_work_entry_alloc();
+    if (entry == NULL) {
+        return ENOMEM;
+    }
+
+    entry->bwe_type = BLE_HS_WORK_TYPE_DIRECT_ADVERTISE;
+    entry->bwe_direct_advertise.bwda_peer_addr_type = addr_type;
+    memcpy(entry->bwe_direct_advertise.bwda_peer_addr, addr,
+           sizeof entry->bwe_direct_advertise.bwda_peer_addr);
+
+    ble_hs_work_enqueue(entry);
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23fe0669/net/nimble/host/src/ble_gap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.h b/net/nimble/host/src/ble_gap.h
index 7637996..78b0e30 100644
--- a/net/nimble/host/src/ble_gap.h
+++ b/net/nimble/host/src/ble_gap.h
@@ -20,5 +20,6 @@
 #include <inttypes.h>
 
 int ble_gap_direct_connection_establishment(uint8_t addr_type, uint8_t *addr);
+int ble_gap_directed_connectable(uint8_t addr_type, uint8_t *addr);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23fe0669/net/nimble/host/src/ble_gap_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap_conn.c b/net/nimble/host/src/ble_gap_conn.c
index 44318a7..ff22b01 100644
--- a/net/nimble/host/src/ble_gap_conn.c
+++ b/net/nimble/host/src/ble_gap_conn.c
@@ -40,13 +40,35 @@ static uint8_t ble_gap_conn_addr_master[BLE_DEV_ADDR_LEN];
 static uint8_t ble_gap_conn_addr_slave[BLE_DEV_ADDR_LEN];
 
 static void
+ble_gap_conn_master_failed(void)
+{
+    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_IDLE;
+    ble_hs_ack_set_callback(NULL, NULL);
+    ble_hs_work_done();
+    /* XXX: Notify someone. */
+}
+
+static void
+ble_gap_conn_slave_failed(void)
+{
+    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_IDLE;
+    ble_hs_ack_set_callback(NULL, NULL);
+    ble_hs_work_done();
+    /* XXX: Notify someone. */
+}
+
+/**
+ * Processes an HCI acknowledgement (either command status or command complete)
+ * while a master connection is being established.
+ */
+static void
 ble_gap_conn_master_ack(struct ble_hs_ack *ack, void *arg)
 {
     assert(ble_gap_conn_master_state ==
            BLE_GAP_CONN_STATE_MASTER_DIRECT_UNACKED);
 
     if (ack->bha_status != 0) {
-        ble_gap_conn_master_state = BLE_GAP_CONN_STATE_IDLE;
+        ble_gap_conn_master_failed();
     } else {
         ble_gap_conn_master_state = BLE_GAP_CONN_STATE_MASTER_DIRECT_ACKED;
     }
@@ -55,32 +77,38 @@ ble_gap_conn_master_ack(struct ble_hs_ack *ack, void *arg)
     ble_hs_work_done();
 }
 
+/**
+ * Processes an HCI acknowledgement (either command status or command complete)
+ * while a slave connection is being established.
+ */
 static void
 ble_gap_conn_slave_ack(struct ble_hs_ack *ack, void *arg)
 {
     int rc;
 
-    assert(ble_gap_conn_slave_state == BLE_GAP_CONN_STATE_SLAVE_UNACKED ||
-           ble_gap_conn_slave_state == BLE_GAP_CONN_STATE_SLAVE_PARAMS_ACKED);
-
-    if (ack->bha_status != 0) {
-        ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_IDLE;
-        return;
-    }
-
     switch (ble_gap_conn_slave_state) {
     case BLE_GAP_CONN_STATE_SLAVE_UNACKED:
-        ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_PARAMS_ACKED;
-        ble_hs_ack_set_callback(ble_gap_conn_slave_ack, NULL);
-        rc = host_hci_cmd_le_set_adv_enable(1);
-        if (rc != 0) {
-            ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_IDLE;
-            ble_hs_ack_set_callback(NULL, NULL);
+        if (ack->bha_status != 0) {
+            ble_gap_conn_slave_failed();
+        } else {
+            ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_PARAMS_ACKED;
+            ble_hs_ack_set_callback(ble_gap_conn_slave_ack, NULL);
+            rc = host_hci_cmd_le_set_adv_enable(1);
+            if (rc != 0) {
+                ble_gap_conn_slave_failed();
+            }
         }
         break;
 
     case BLE_GAP_CONN_STATE_SLAVE_PARAMS_ACKED:
-        ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_ENABLE_ACKED;
+        if (ack->bha_status != 0) {
+            ble_gap_conn_slave_failed();
+        } else {
+            ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_ENABLE_ACKED;
+
+            /* The host is done sending commands to the controller. */
+            ble_hs_work_done();
+        }
         break;
 
     default:
@@ -96,14 +124,15 @@ ble_gap_conn_slave_ack(struct ble_hs_ack *ack, void *arg)
  * @return 0 on success; nonzero on failure.
  */
 int
-ble_gap_conn_initiate_direct(int addr_type, uint8_t *addr)
+ble_gap_conn_direct_connect(int addr_type, uint8_t *addr)
 {
     struct hci_create_conn hcc;
     int rc;
 
     /* Make sure no master connection attempt is already in progress. */
     if (ble_gap_conn_master_in_progress()) {
-        return EALREADY;
+        rc = EALREADY;
+        goto err;
     }
 
     hcc.scan_itvl = 0x0010;
@@ -120,26 +149,32 @@ ble_gap_conn_initiate_direct(int addr_type, uint8_t *addr)
     hcc.max_ce_len = 0x0300; // XXX
 
     memcpy(ble_gap_conn_addr_master, addr, BLE_DEV_ADDR_LEN);
-    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_MASTER_DIRECT_UNACKED;
-    ble_hs_ack_set_callback(ble_gap_conn_master_ack, NULL);
 
     rc = host_hci_cmd_le_create_connection(&hcc);
     if (rc != 0) {
-        return rc;
+        goto err;
     }
 
+    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_MASTER_DIRECT_UNACKED;
+    ble_hs_ack_set_callback(ble_gap_conn_master_ack, NULL);
+
     return 0;
+
+err:
+    /* XXX: Notify someone. */
+    return rc;
 }
 
 int
-ble_gap_conn_advertise_direct(int addr_type, uint8_t *addr)
+ble_gap_conn_direct_advertise(int addr_type, uint8_t *addr)
 {
     struct hci_adv_params hap;
     int rc;
 
     /* Make sure no slave connection attempt is already in progress. */
     if (ble_gap_conn_slave_in_progress()) {
-        return EALREADY;
+        rc = EALREADY;
+        goto err;
     }
 
     hap.adv_itvl_min = BLE_HCI_ADV_ITVL_DEF;
@@ -152,19 +187,24 @@ ble_gap_conn_advertise_direct(int addr_type, uint8_t *addr)
     hap.adv_filter_policy = BLE_HCI_ADV_FILT_DEF;
 
     memcpy(ble_gap_conn_addr_slave, addr, BLE_DEV_ADDR_LEN);
-    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_UNACKED;
-    ble_hs_ack_set_callback(ble_gap_conn_slave_ack, NULL);
 
     rc = host_hci_cmd_le_set_adv_params(&hap);
     if (rc != 0) {
-        return rc;
+        goto err;
     }
 
+    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_UNACKED;
+    ble_hs_ack_set_callback(ble_gap_conn_slave_ack, NULL);
+
     return 0;
+
+err:
+    /* XXX: Notify someone. */
+    return rc;
 }
 
 static int
-ble_gap_conn_accept_conn(uint8_t *addr)
+ble_gap_conn_accept_new_conn(uint8_t *addr)
 {
     switch (ble_gap_conn_master_state) {
     case BLE_GAP_CONN_STATE_MASTER_DIRECT_ACKED:
@@ -199,6 +239,8 @@ ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt)
      * in progress.
      */
     conn = ble_hs_conn_find(evt->connection_handle);
+
+    /* Apply the event to the existing connection if it exists. */
     if (conn != NULL) {
         if (evt->status != 0) {
             ble_hs_conn_remove(conn);
@@ -208,7 +250,8 @@ ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt)
         return 0;
     }
 
-    rc = ble_gap_conn_accept_conn(evt->peer_addr);
+    /* This event refers to a new connection. */
+    rc = ble_gap_conn_accept_new_conn(evt->peer_addr);
     if (rc != 0) {
         return ENOENT;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23fe0669/net/nimble/host/src/ble_gap_conn.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap_conn.h b/net/nimble/host/src/ble_gap_conn.h
index 23b0698..b115ee6 100644
--- a/net/nimble/host/src/ble_gap_conn.h
+++ b/net/nimble/host/src/ble_gap_conn.h
@@ -21,8 +21,8 @@
 struct hci_le_conn_complete;
 struct ble_hs_ack;
 
-int ble_gap_conn_initiate_direct(int addr_type, uint8_t *addr);
-int ble_gap_conn_advertise_direct(int addr_type, uint8_t *addr);
+int ble_gap_conn_direct_connect(int addr_type, uint8_t *addr);
+int ble_gap_conn_direct_advertise(int addr_type, uint8_t *addr);
 int ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt);
 int ble_gap_conn_master_in_progress(void);
 int ble_gap_conn_slave_in_progress(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23fe0669/net/nimble/host/src/ble_hs_work.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_work.c b/net/nimble/host/src/ble_hs_work.c
index b56985f..5098420 100644
--- a/net/nimble/host/src/ble_hs_work.c
+++ b/net/nimble/host/src/ble_hs_work.c
@@ -59,11 +59,17 @@ ble_hs_work_process_next(void)
 
     switch (entry->bwe_type) {
     case BLE_HS_WORK_TYPE_DIRECT_CONNECT:
-        rc = ble_gap_conn_initiate_direct(
+        rc = ble_gap_conn_direct_connect(
             entry->bwe_direct_connect.bwdc_peer_addr_type,
             entry->bwe_direct_connect.bwdc_peer_addr);
         break;
 
+    case BLE_HS_WORK_TYPE_DIRECT_ADVERTISE:
+        rc = ble_gap_conn_direct_advertise(
+            entry->bwe_direct_advertise.bwda_peer_addr_type,
+            entry->bwe_direct_advertise.bwda_peer_addr);
+        break;
+
     default:
         rc = -1;
         assert(0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23fe0669/net/nimble/host/src/ble_hs_work.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_work.h b/net/nimble/host/src/ble_hs_work.h
index df36485..d5bdb05 100644
--- a/net/nimble/host/src/ble_hs_work.h
+++ b/net/nimble/host/src/ble_hs_work.h
@@ -21,19 +21,26 @@
 #include "os/queue.h"
 
 #define BLE_HS_WORK_TYPE_DIRECT_CONNECT     0
-#define BLE_HS_WORK_TYPE_MAX                1
+#define BLE_HS_WORK_TYPE_DIRECT_ADVERTISE   1
+#define BLE_HS_WORK_TYPE_MAX                2
 
 struct ble_hs_work_direct_connect {
     uint8_t bwdc_peer_addr[8];
     uint8_t bwdc_peer_addr_type;
 };
 
+struct ble_hs_work_direct_advertise {
+    uint8_t bwda_peer_addr[8];
+    uint8_t bwda_peer_addr_type;
+};
+
 struct ble_hs_work_entry {
     int bwe_type;
     STAILQ_ENTRY(ble_hs_work_entry) bwe_next;
 
     union {
         struct ble_hs_work_direct_connect bwe_direct_connect;
+        struct ble_hs_work_direct_advertise bwe_direct_advertise;
     };
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23fe0669/net/nimble/host/src/test/ble_hs_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_conn_test.c b/net/nimble/host/src/test/ble_hs_conn_test.c
index 86283b4..ad2cbb1 100644
--- a/net/nimble/host/src/test/ble_hs_conn_test.c
+++ b/net/nimble/host/src/test/ble_hs_conn_test.c
@@ -44,7 +44,7 @@ TEST_CASE(ble_hs_conn_test_master_direct_success)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate connection. */
-    rc = ble_gap_conn_initiate_direct(0, addr);
+    rc = ble_gap_conn_direct_connect(0, addr);
     TEST_ASSERT(rc == 0);
     TEST_ASSERT(ble_gap_conn_master_in_progress());
 
@@ -83,7 +83,7 @@ TEST_CASE(ble_hs_conn_test_master_direct_hci_errors)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate connection. */
-    rc = ble_gap_conn_initiate_direct(0, addr);
+    rc = ble_gap_conn_direct_connect(0, addr);
     TEST_ASSERT(rc == 0);
     TEST_ASSERT(ble_gap_conn_master_in_progress());
 
@@ -125,7 +125,7 @@ TEST_CASE(ble_hs_conn_test_slave_direct_success)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate advertising. */
-    rc = ble_gap_conn_advertise_direct(0, addr);
+    rc = ble_gap_conn_direct_advertise(0, addr);
     TEST_ASSERT(rc == 0);
     TEST_ASSERT(!ble_gap_conn_master_in_progress());
     TEST_ASSERT(ble_gap_conn_slave_in_progress());

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23fe0669/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index 3689cc3..4587657 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -56,7 +56,7 @@ ble_hs_test_util_create_conn(uint16_t handle, uint8_t *addr)
     struct hci_le_conn_complete evt;
     int rc;
 
-    rc = ble_gap_conn_initiate_direct(0, addr);
+    rc = ble_gap_conn_direct_connect(0, addr);
     TEST_ASSERT(rc == 0);
 
     ble_hs_test_util_rx_le_ack(BLE_HCI_OCF_LE_CREATE_CONN, BLE_ERR_SUCCESS);