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/12/22 02:52:13 UTC

[2/3] incubator-mynewt-larva git commit: split adv from gap

split adv from gap


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

Branch: refs/heads/master
Commit: 1ce6fd0114f78d6f710a7c5d19624604ab62c99b
Parents: a232d32
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Dec 21 16:25:48 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Dec 21 17:51:28 2015 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gap.h | 10 ++--
 net/nimble/host/include/host/ble_hs.h  |  6 +++
 net/nimble/host/src/ble_gap_conn.c     | 76 ++++++++-------------------
 net/nimble/host/src/ble_gap_conn.h     | 15 ------
 net/nimble/host/src/ble_hs_adv.c       | 81 +++++++++++++++++++++++++++++
 net/nimble/host/src/ble_hs_adv.h       | 42 +++++++++++++++
 net/nimble/host/src/test/ble_os_test.c |  2 +-
 7 files changed, 156 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/1ce6fd01/net/nimble/host/include/host/ble_gap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gap.h b/net/nimble/host/include/host/ble_gap.h
index 0ec5f96..48fb278 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -19,6 +19,7 @@
 
 #include <inttypes.h>
 struct hci_le_conn_complete;
+struct ble_hs_adv_fields;
 
 #define BLE_GAP_CONN_EVENT_TYPE_CONNECT     1
 #define BLE_GAP_CONN_EVENT_TYPE_ADV_RPT     2
@@ -68,16 +69,11 @@ typedef void ble_gap_connect_fn(struct ble_gap_conn_event *event, void *arg);
 #define BLE_GAP_DISC_MODE_LTD               2
 #define BLE_GAP_DISC_MODE_GEN               3
 
-struct ble_gap_conn_adv_fields {
-    char *name;
-    unsigned name_is_complete:1;
-};
-
 void ble_gap_conn_set_cb(ble_gap_connect_fn *cb, void *arg);
 int ble_gap_conn_advertise(uint8_t discoverable_mode, uint8_t connectable_mode,
                            uint8_t *peer_addr, uint8_t peer_addr_type);
-int ble_gap_conn_set_adv_fields(struct ble_gap_conn_adv_fields *adv_fields);
-int ble_gap_conn_disc(uint32_t duration_ms);
+int ble_gap_conn_set_adv_fields(struct ble_hs_adv_fields *adv_fields);
+int ble_gap_conn_disc(uint32_t duration_ms, uint8_t discovery_mode);
 int ble_gap_conn_direct_connect(int addr_type, uint8_t *addr);
 int ble_gap_conn_terminate(uint16_t handle);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/1ce6fd01/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index 5d1a5bf..32c34fd 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -38,6 +38,12 @@ struct ble_hs_cfg {
 };
 extern struct ble_hs_cfg ble_hs_cfg;
 
+struct ble_hs_adv_fields {
+    char *name;
+    uint8_t name_len;
+    unsigned name_is_complete:1;
+};
+
 int ble_hs_init(uint8_t prio);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/1ce6fd01/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 1163b11..ec6fa39 100644
--- a/net/nimble/host/src/ble_gap_conn.c
+++ b/net/nimble/host/src/ble_gap_conn.c
@@ -22,6 +22,7 @@
 #include "host/host_hci.h"
 #include "ble_hci_ack.h"
 #include "ble_hs_conn.h"
+#include "ble_hs_adv.h"
 #include "ble_hci_ack.h"
 #include "ble_hci_sched.h"
 #include "ble_gatt_priv.h"
@@ -110,6 +111,7 @@ static ble_hci_sched_tx_fn * const
 
 static uint8_t ble_gap_conn_s_conn_mode;
 static uint8_t ble_gap_conn_s_disc_mode;
+static uint8_t ble_gap_conn_m_disc_mode;
 
 static struct hci_adv_params ble_gap_conn_adv_params;
 
@@ -579,11 +581,11 @@ ble_gap_conn_adv_data_tx(void *arg)
         break;
 
     case BLE_GAP_DISC_MODE_LTD:
-        flags |= BLE_GAP_CONN_AD_F_DISC_LTD;
+        flags |= BLE_HS_ADV_F_DISC_LTD;
         break;
 
     case BLE_GAP_DISC_MODE_GEN:
-        flags |= BLE_GAP_CONN_AD_F_DISC_GEN;
+        flags |= BLE_HS_ADV_F_DISC_GEN;
         break;
 
     default:
@@ -598,7 +600,7 @@ ble_gap_conn_adv_data_tx(void *arg)
 
         ble_gap_conn_adv_data[ble_gap_conn_adv_data_len] = 2;
         ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 1] =
-            BLE_GAP_CONN_AD_TYPE_FLAGS;
+            BLE_HS_ADV_TYPE_FLAGS;
         ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 2] = flags;
     } else {
         adv_data_len = ble_gap_conn_adv_data_len;
@@ -609,7 +611,7 @@ ble_gap_conn_adv_data_tx(void *arg)
     assert(adv_data_len <= BLE_HCI_MAX_ADV_DATA_LEN);
     ble_gap_conn_adv_data[ble_gap_conn_adv_data_len] = 2;
     ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 1] =
-        BLE_GAP_CONN_AD_TYPE_TX_PWR_LEVEL;
+        BLE_HS_ADV_TYPE_TX_PWR_LEVEL;
     ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 2] =
         ble_gap_conn_tx_pwr_lvl;
 
@@ -804,56 +806,16 @@ ble_gap_conn_advertise(uint8_t discoverable_mode, uint8_t connectable_mode,
     return 0;
 }
 
-static int
-ble_gap_conn_set_adv_field(uint8_t type, uint8_t data_len, void *data)
-{
-    int new_len;
-
-    new_len = ble_gap_conn_adv_data_len + 2 + data_len;
-    if (new_len > BLE_GAP_CONN_ADV_DATA_LIMIT) {
-        return BLE_HS_EMSGSIZE;
-    }
-
-    ble_gap_conn_adv_data[ble_gap_conn_adv_data_len] = data_len + 1;
-    ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 1] = type;
-    memcpy(ble_gap_conn_adv_data + ble_gap_conn_adv_data_len + 2,
-           data, data_len);
-
-    ble_gap_conn_adv_data_len = new_len;
-
-    return 0;
-}
-
-/**
- * Sets the significant part of the data in outgoing advertisements.
- *
- * @return                      0 on success;  on failure.
- */
 int
-ble_gap_conn_set_adv_fields(struct ble_gap_conn_adv_fields *adv_fields)
+ble_gap_conn_set_adv_fields(struct ble_hs_adv_fields *adv_fields)
 {
-    uint8_t type;
-    int name_len;
     int rc;
 
-    ble_gap_conn_adv_data_len = 0;
-
-    if (adv_fields->name != NULL) {
-        name_len = strlen(adv_fields->name);
-        if (name_len > UINT8_MAX) {
-            return BLE_HS_EINVAL;
-        }
-
-        if (adv_fields->name_is_complete) {
-            type = BLE_GAP_CONN_AD_TYPE_COMP_NAME;
-        } else {
-            type = BLE_GAP_CONN_AD_TYPE_INCOMP_NAME;
-        }
-
-        rc = ble_gap_conn_set_adv_field(type, name_len, adv_fields->name);
-        if (rc != 0) {
-            return rc;
-        }
+    rc = ble_hs_adv_set_fields(adv_fields, ble_gap_conn_adv_data,
+                               &ble_gap_conn_adv_data_len,
+                               BLE_GAP_CONN_ADV_DATA_LIMIT);
+    if (rc != 0) {
+        return rc;
     }
 
     return 0;
@@ -934,16 +896,22 @@ ble_gap_conn_disc_tx_params(void *arg)
 }
 
 /**
- * Performs the General Discovery Procedure, as described in
- * vol. 3, part C, section 9.2.6.
+ * Performs the Limited or General Discovery Procedures, as described in
+ * vol. 3, part C, section 9.2.5 / 9.2.6.
  *
  * @return                      0 on success; nonzero on failure.
  */
 int
-ble_gap_conn_disc(uint32_t duration_ms)
+ble_gap_conn_disc(uint32_t duration_ms, uint8_t discovery_mode)
 {
     int rc;
 
+    if (discovery_mode != BLE_GAP_DISC_MODE_LTD &&
+        discovery_mode != BLE_GAP_DISC_MODE_GEN) {
+
+        return BLE_HS_EINVAL;
+    }
+
     /* Make sure no master connection attempt is already in progress. */
     if (ble_gap_conn_master_in_progress()) {
         return BLE_HS_EALREADY;
@@ -956,6 +924,8 @@ ble_gap_conn_disc(uint32_t duration_ms)
     ble_gap_conn_master_state = BLE_GAP_CONN_M_STATE_DISC_PARAMS;
     memset(ble_gap_conn_master_addr, 0, BLE_DEV_ADDR_LEN);
 
+    ble_gap_conn_m_disc_mode = discovery_mode;
+
     rc = ble_hci_sched_enqueue(ble_gap_conn_disc_tx_params, NULL);
     if (rc != 0) {
         ble_gap_conn_master_reset_state();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/1ce6fd01/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 767cff5..a9ae5b8 100644
--- a/net/nimble/host/src/ble_gap_conn.h
+++ b/net/nimble/host/src/ble_gap_conn.h
@@ -23,21 +23,6 @@ struct hci_le_conn_complete;
 struct hci_disconn_complete;
 struct ble_hci_ack;
 
-#define BLE_GAP_CONN_AD_TYPE_FLAGS                  0x01
-#define BLE_GAP_CONN_AD_TYPE_INCOMP_16BIT_UUIDS     0x02
-#define BLE_GAP_CONN_AD_TYPE_COMP_16BIT_UUIDS       0x03
-#define BLE_GAP_CONN_AD_TYPE_INCOMP_32BIT_UUIDS     0x04
-#define BLE_GAP_CONN_AD_TYPE_COMP_32BIT_UUIDS       0x05
-#define BLE_GAP_CONN_AD_TYPE_INCOMP_128BIT_UUIDS    0x06
-#define BLE_GAP_CONN_AD_TYPE_COMP_128BIT_UUIDS      0x07
-#define BLE_GAP_CONN_AD_TYPE_INCOMP_NAME            0x08
-#define BLE_GAP_CONN_AD_TYPE_COMP_NAME              0x09
-#define BLE_GAP_CONN_AD_TYPE_TX_PWR_LEVEL           0x0a
-#define BLE_GAP_CONN_AD_TYPE_DEVICE_CLASS           0x0b
-
-#define BLE_GAP_CONN_AD_F_DISC_LTD                  0x01
-#define BLE_GAP_CONN_AD_F_DISC_GEN                  0x02
-
 void ble_gap_conn_rx_adv_report(struct ble_gap_conn_adv_rpt *rpt);
 int ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt);
 void ble_gap_conn_rx_disconn_complete(struct hci_disconn_complete *evt);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/1ce6fd01/net/nimble/host/src/ble_hs_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_adv.c b/net/nimble/host/src/ble_hs_adv.c
new file mode 100644
index 0000000..a3366ab
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_adv.c
@@ -0,0 +1,81 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include "ble_hs_adv.h"
+#include "ble_hs_priv.h"
+
+static int
+ble_hs_adv_set_one_field(uint8_t type, uint8_t data_len, void *data,
+                         uint8_t *dst, uint8_t *dst_len, uint8_t max_len)
+{
+    int new_len;
+
+    new_len = *dst_len + 2 + data_len;
+    if (new_len > max_len) {
+        return BLE_HS_EMSGSIZE;
+    }
+
+    dst[*dst_len] = data_len + 1;
+    dst[*dst_len + 1] = type;
+    memcpy(dst + *dst_len + 2, data, data_len);
+
+    *dst_len = new_len;
+
+    return 0;
+}
+
+/**
+ * Sets the significant part of the data in outgoing advertisements.
+ *
+ * @return                      0 on success;  on failure.
+ */
+int
+ble_hs_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
+                      uint8_t *dst, uint8_t *dst_len, uint8_t max_len)
+{
+    uint8_t type;
+    int rc;
+
+    *dst_len = 0;
+
+    if (adv_fields->name != NULL && adv_fields->name_len > 0) {
+        if (adv_fields->name_is_complete) {
+            type = BLE_HS_ADV_TYPE_COMP_NAME;
+        } else {
+            type = BLE_HS_ADV_TYPE_INCOMP_NAME;
+        }
+
+        rc = ble_hs_adv_set_one_field(type, adv_fields->name_len,
+                                      adv_fields->name, dst, dst_len, max_len);
+        if (rc != 0) {
+            return rc;
+        }
+    }
+
+    return 0;
+}
+
+int
+ble_hs_adv_parse_fields(struct ble_hs_adv_fields *adv_fields, uint8_t *src,
+                        uint8_t src_len)
+{
+    memset(adv_fields, 0, sizeof *adv_fields);
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/1ce6fd01/net/nimble/host/src/ble_hs_adv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_adv.h b/net/nimble/host/src/ble_hs_adv.h
new file mode 100644
index 0000000..08e0a0b
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_adv.h
@@ -0,0 +1,42 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef H_BLE_HS_ADV_
+#define H_BLE_HS_ADV_
+
+#include <inttypes.h>
+struct ble_hs_adv_fields;
+
+#define BLE_HS_ADV_TYPE_FLAGS                   0x01
+#define BLE_HS_ADV_TYPE_INCOMP_16BIT_UUIDS      0x02
+#define BLE_HS_ADV_TYPE_COMP_16BIT_UUIDS        0x03
+#define BLE_HS_ADV_TYPE_INCOMP_32BIT_UUIDS      0x04
+#define BLE_HS_ADV_TYPE_COMP_32BIT_UUIDS        0x05
+#define BLE_HS_ADV_TYPE_INCOMP_128BIT_UUIDS     0x06
+#define BLE_HS_ADV_TYPE_COMP_128BIT_UUIDS       0x07
+#define BLE_HS_ADV_TYPE_INCOMP_NAME             0x08
+#define BLE_HS_ADV_TYPE_COMP_NAME               0x09
+#define BLE_HS_ADV_TYPE_TX_PWR_LEVEL            0x0a
+#define BLE_HS_ADV_TYPE_DEVICE_CLASS            0x0b
+
+#define BLE_HS_ADV_F_DISC_LTD                   0x01
+#define BLE_HS_ADV_F_DISC_GEN                   0x02
+
+int ble_hs_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
+                          uint8_t *dst, uint8_t *dst_len, uint8_t max_len);
+int ble_hs_adv_parse_fields(struct ble_hs_adv_fields *adv_fields, uint8_t *src,
+                            uint8_t src_len);
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/1ce6fd01/net/nimble/host/src/test/ble_os_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_os_test.c b/net/nimble/host/src/test/ble_os_test.c
index 8cf3b1e..e82c6ff 100644
--- a/net/nimble/host/src/test/ble_os_test.c
+++ b/net/nimble/host/src/test/ble_os_test.c
@@ -178,7 +178,7 @@ ble_gap_gen_disc_test_task_handler(void *arg)
     TEST_ASSERT(!ble_gap_conn_master_in_progress());
 
     /* Initiate the general discovery procedure with a 200 ms timeout. */
-    ble_gap_conn_disc(200);
+    ble_gap_conn_disc(200, BLE_GAP_DISC_MODE_GEN);
     TEST_ASSERT(ble_hs_conn_first() == NULL);
     TEST_ASSERT(ble_gap_conn_master_in_progress());
     TEST_ASSERT(!cb_called);