Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 604c05d7f -> 449ef81c5


BLE Host - Allow reconf of connection GAP event cb

/**
 * Configures a connection to use the specified GAP event callback.  A
 * connection's GAP event callback is first specified when the
 * connection is created, either via advertising or initiation.  This
 * function replaces the callback that was last configured.
 *
 * @param conn_handle           The handle of the connection to
 *                                  configure.
 * @param cb                    The callback to associate with the
 *                                  connection.
 * @param cb_arg                An optional argument that the callback
 *                                  receives.
 *
 * @return                      0 on success;
 *                              BLE_HS_ENOTCONN if there is no
 *                                  connection with the specified
 *                                  handle.
 */
int
ble_gap_set_event_cb(uint16_t conn_handle, ble_gap_event_fn *cb,
                     void *cb_arg)


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/449ef81c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/449ef81c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/449ef81c

Branch: refs/heads/develop
Commit: 449ef81c5a0ae01f6a767c3797535ce212461e93
Parents: 604c05d
Author: Christopher Collins <[email protected]>
Authored: Fri Jan 20 11:57:59 2017 -0800
Committer: Christopher Collins <[email protected]>
Committed: Fri Jan 20 11:57:59 2017 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gap.h      |  2 +
 net/nimble/host/src/ble_gap.c               | 37 +++++++++++++
 net/nimble/host/test/src/ble_gap_test.c     | 68 ++++++++++++++++++++++++
 net/nimble/host/test/src/ble_hs_test_util.c | 13 ++---
 4 files changed, 114 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/449ef81c/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 08d4a8a..e960975 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -534,6 +534,8 @@ struct ble_gap_white_entry {
 };
 
 int ble_gap_conn_find(uint16_t handle, struct ble_gap_conn_desc *out_desc);
+int ble_gap_set_event_cb(uint16_t conn_handle,
+                         ble_gap_event_fn *cb, void *cb_arg);
 
 int ble_gap_adv_start(uint8_t own_addr_type, uint8_t peer_addr_type,
                       const uint8_t *peer_addr, int32_t duration_ms,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/449ef81c/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 d3cfb40..f36656a 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -1351,6 +1351,43 @@ ble_gap_update_timer(void)
 }
 
 /**
+ * Configures a connection to use the specified GAP event callback.  A
+ * connection's GAP event callback is first specified when the connection is
+ * created, either via advertising or initiation.  This function replaces the
+ * callback that was last configured.
+ *
+ * @param conn_handle           The handle of the connection to configure.
+ * @param cb                    The callback to associate with the connection.
+ * @param cb_arg                An optional argument that the callback
+ *                                  receives.
+ * 
+ * @return                      0 on success;
+ *                              BLE_HS_ENOTCONN if there is no connection with
+ *                                  the specified handle.
+ */
+int
+ble_gap_set_event_cb(uint16_t conn_handle, ble_gap_event_fn *cb, void *cb_arg)
+{
+    struct ble_hs_conn *conn;
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_find(conn_handle);
+    if (conn != NULL) {
+        conn->bhc_cb = cb;
+        conn->bhc_cb_arg = cb_arg;
+    }
+
+    ble_hs_unlock();
+
+    if (conn == NULL) {
+        return BLE_HS_ENOTCONN;
+    }
+
+    return 0;
+}
+
+/**
  * Handles timed-out GAP procedures.
  *
  * @return                      The number of ticks until this function should

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/449ef81c/net/nimble/host/test/src/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gap_test.c 
b/net/nimble/host/test/src/ble_gap_test.c
index 983a892..d41ae74 100644
--- a/net/nimble/host/test/src/ble_gap_test.c
+++ b/net/nimble/host/test/src/ble_gap_test.c
@@ -2923,6 +2923,73 @@ TEST_SUITE(ble_gap_test_suite_mtu)
 }
 
 /*****************************************************************************
+ * $set cb                                                                   *
+ *****************************************************************************/
+
+static int
+ble_gap_test_util_set_cb_event(struct ble_gap_event *event, void *arg)
+{
+    struct ble_gap_event *event_arg;
+
+    event_arg = arg;
+
+    *event_arg = *event;
+
+    return 0;
+}
+
+TEST_CASE(ble_gap_test_case_set_cb_good)
+{
+    const uint8_t peer_addr[6] = { 1,2,3,4,5,6 };
+    struct hci_disconn_complete disconn_evt;
+    struct ble_gap_event event;
+    int rc;
+
+    ble_gap_test_util_init();
+
+    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
+                                 NULL);
+
+
+    /* Reconfigure the callback. */
+    rc = ble_gap_set_event_cb(2, ble_gap_test_util_set_cb_event, &event);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Terminate the connection and ensure the new callback gets called. */
+    rc = ble_hs_test_util_conn_terminate(2, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    disconn_evt.connection_handle = 2;
+    disconn_evt.status = 0;
+    disconn_evt.reason = BLE_ERR_REM_USER_CONN_TERM;
+    ble_hs_test_util_rx_disconn_complete_event(&disconn_evt);
+
+    TEST_ASSERT(event.type == BLE_GAP_EVENT_DISCONNECT);
+    TEST_ASSERT(event.disconnect.reason ==
+                BLE_HS_HCI_ERR(BLE_ERR_REM_USER_CONN_TERM));
+    TEST_ASSERT(event.disconnect.conn.conn_handle == 2);
+}
+
+TEST_CASE(ble_gap_test_case_set_cb_bad)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Ensure error is reported when specified connection doesn't exist. */
+    rc = ble_gap_set_event_cb(123, ble_gap_test_util_set_cb_event, NULL);
+    TEST_ASSERT(rc == BLE_HS_ENOTCONN);
+}
+
+TEST_SUITE(ble_gap_test_suite_set_cb)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_set_cb_good();
+    ble_gap_test_case_set_cb_bad();
+}
+
+/*****************************************************************************
  * $all                                                                      *
  *****************************************************************************/
 
@@ -2940,6 +3007,7 @@ ble_gap_test_all(void)
     ble_gap_test_suite_update_conn();
     ble_gap_test_suite_timeout();
     ble_gap_test_suite_mtu();
+    ble_gap_test_suite_set_cb();
 
     return tu_any_failed;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/449ef81c/net/nimble/host/test/src/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util.c 
b/net/nimble/host/test/src/ble_hs_test_util.c
index fbe1213..aa3b97d 100644
--- a/net/nimble/host/test/src/ble_hs_test_util.c
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@ -243,15 +243,16 @@ ble_hs_test_util_rx_hci_evt(uint8_t *evt)
     totlen = BLE_HCI_EVENT_HDR_LEN + evt[1];
     TEST_ASSERT_FATAL(totlen <= UINT8_MAX + BLE_HCI_EVENT_HDR_LEN);
 
-    if (os_started()) {
-        evbuf = ble_hci_trans_buf_alloc(
-            BLE_HCI_TRANS_BUF_EVT_LO);
-        TEST_ASSERT_FATAL(evbuf != NULL);
+    evbuf = ble_hci_trans_buf_alloc(
+        BLE_HCI_TRANS_BUF_EVT_LO);
+    TEST_ASSERT_FATAL(evbuf != NULL);
+
+    memcpy(evbuf, evt, totlen);
 
-        memcpy(evbuf, evt, totlen);
+    if (os_started()) {
         rc = ble_hci_trans_ll_evt_tx(evbuf);
     } else {
-        rc = ble_hs_hci_evt_process(evt);
+        rc = ble_hs_hci_evt_process(evbuf);
     }
 
     TEST_ASSERT_FATAL(rc == 0);

Reply via email to