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 2016/01/15 23:00:49 UTC

[1/2] incubator-mynewt-larva git commit: Tests for stop-advertise GAP procedure.

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 496f118e1 -> 17c32e815


Tests for stop-advertise GAP procedure.


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

Branch: refs/heads/master
Commit: a05d1469532d59024372508b11498bacb323caa4
Parents: 496f118
Author: Christopher Collins <cc...@gmail.com>
Authored: Fri Jan 15 12:48:57 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Fri Jan 15 14:00:10 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gap.h      |  19 ++--
 net/nimble/host/src/ble_gap_conn.c          |  19 +---
 net/nimble/host/src/ble_gap_priv.h          |   4 +-
 net/nimble/host/src/test/ble_gap_test.c     | 132 +++++++++++++++++++----
 net/nimble/host/src/test/ble_hs_adv_test.c  |   2 +-
 net/nimble/host/src/test/ble_hs_conn_test.c |   6 +-
 6 files changed, 128 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a05d1469/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 55d1a6b..5b7412b 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -30,7 +30,7 @@ struct hci_le_conn_complete;
 #define BLE_GAP_EVENT_DISC_FINISHED         4
 #define BLE_GAP_EVENT_ADV_FINISHED          5
 #define BLE_GAP_EVENT_ADV_FAILURE           6
-#define BLE_GAP_EVENT_ADV_CANCEL_FAILURE    7
+#define BLE_GAP_EVENT_ADV_STOP_FAILURE      7
 
 struct ble_gap_conn_desc {
     uint16_t conn_handle;
@@ -56,24 +56,23 @@ typedef void ble_gap_disc_fn(int event, int status,
 
 typedef void ble_gap_wl_fn(int status, void *arg);
 
-#define BLE_GAP_CONN_MODE_NULL              0
-#define BLE_GAP_CONN_MODE_NON               1
-#define BLE_GAP_CONN_MODE_DIR               2
-#define BLE_GAP_CONN_MODE_UND               3
+#define BLE_GAP_CONN_MODE_NON               0
+#define BLE_GAP_CONN_MODE_DIR               1
+#define BLE_GAP_CONN_MODE_UND               2
 
-#define BLE_GAP_DISC_MODE_NULL              0
-#define BLE_GAP_DISC_MODE_NON               1
-#define BLE_GAP_DISC_MODE_LTD               2
-#define BLE_GAP_DISC_MODE_GEN               3
+#define BLE_GAP_DISC_MODE_NON               0
+#define BLE_GAP_DISC_MODE_LTD               1
+#define BLE_GAP_DISC_MODE_GEN               2
 
 struct ble_gap_white_entry {
     uint8_t addr_type;
     uint8_t addr[6];
 };
 
-int ble_gap_conn_advertise(uint8_t discoverable_mode, uint8_t connectable_mode,
+int ble_gap_conn_adv_start(uint8_t discoverable_mode, uint8_t connectable_mode,
                            uint8_t *peer_addr, uint8_t peer_addr_type,
                            ble_gap_conn_fn *cb, void *cb_arg);
+int ble_gap_conn_adv_stop(void);
 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,
                       ble_gap_disc_fn *cb, void *cb_arg);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a05d1469/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 432175b..c6f203e 100644
--- a/net/nimble/host/src/ble_gap_conn.c
+++ b/net/nimble/host/src/ble_gap_conn.c
@@ -329,7 +329,7 @@ ble_gap_conn_notify_adv_finished(void)
 static void
 ble_gap_conn_notify_slave_adv_cancel_failure(int status)
 {
-    ble_gap_conn_call_slave_cb(BLE_GAP_EVENT_ADV_CANCEL_FAILURE, status);
+    ble_gap_conn_call_slave_cb(BLE_GAP_EVENT_ADV_STOP_FAILURE, status);
 }
 
 static void
@@ -914,8 +914,8 @@ ble_gap_conn_adv_disable_tx(void *arg)
     return 0;
 }
 
-static int
-ble_gap_conn_adv_stop(ble_gap_conn_fn *cb, void *cb_arg)
+int
+ble_gap_conn_adv_stop(void)
 {
     int rc;
 
@@ -924,9 +924,6 @@ ble_gap_conn_adv_stop(ble_gap_conn_fn *cb, void *cb_arg)
         return BLE_HS_EALREADY;
     }
 
-    ble_gap_conn_slave.cb = cb;
-    ble_gap_conn_slave.cb_arg = cb_arg;
-
     rc = ble_hci_sched_enqueue(ble_gap_conn_adv_disable_tx, NULL);
     if (rc != 0) {
         return rc;
@@ -1238,7 +1235,7 @@ ble_gap_conn_adv_initiate(void)
  * @return                      0 on success; nonzero on failure.
  */
 int
-ble_gap_conn_advertise(uint8_t discoverable_mode, uint8_t connectable_mode,
+ble_gap_conn_adv_start(uint8_t discoverable_mode, uint8_t connectable_mode,
                        uint8_t *peer_addr, uint8_t peer_addr_type,
                        ble_gap_conn_fn *cb, void *cb_arg)
 {
@@ -1250,14 +1247,6 @@ ble_gap_conn_advertise(uint8_t discoverable_mode, uint8_t connectable_mode,
         return BLE_HS_EINVAL;
     }
 
-    /* Process a request to stop advertising separately. */
-    if (discoverable_mode == BLE_GAP_DISC_MODE_NULL ||
-        connectable_mode == BLE_GAP_DISC_MODE_NULL) {
-
-        rc = ble_gap_conn_adv_stop(cb, cb_arg);
-        return rc;
-    }
-
     /* Make sure no slave connection attempt is already in progress. */
     if (ble_gap_conn_slave_in_progress()) {
         return BLE_HS_EALREADY;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a05d1469/net/nimble/host/src/ble_gap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap_priv.h b/net/nimble/host/src/ble_gap_priv.h
index 08d29e8..8c8a6f2 100644
--- a/net/nimble/host/src/ble_gap_priv.h
+++ b/net/nimble/host/src/ble_gap_priv.h
@@ -24,8 +24,8 @@ struct hci_disconn_complete;
 struct ble_hci_ack;
 struct ble_hs_adv;
 
-#define BLE_GAP_CONN_MODE_MAX               4
-#define BLE_GAP_DISC_MODE_MAX               4
+#define BLE_GAP_CONN_MODE_MAX               3
+#define BLE_GAP_DISC_MODE_MAX               3
 
 void ble_gap_conn_rx_adv_report(struct ble_hs_adv *adv);
 int ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a05d1469/net/nimble/host/src/test/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gap_test.c b/net/nimble/host/src/test/ble_gap_test.c
index 5333bcb..b47c763 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -196,15 +196,15 @@ static void
 ble_gap_test_util_verify_tx_adv_params(void)
 {
     uint8_t param_len;
-    uint8_t *param;
 
     TEST_ASSERT_FATAL(ble_hs_test_util_prev_hci_tx != NULL);
 
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_ADV_PARAMS,
-                                           &param_len);
+    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_SET_ADV_PARAMS,
+                                   &param_len);
     TEST_ASSERT(param_len == BLE_HCI_SET_ADV_PARAM_LEN);
-    (void)param; /* XXX: Verify other fields. */
+    
+    /* Note: Content of message verified in ble_hs_adv_test.c. */
 }
 
 static void
@@ -224,14 +224,13 @@ static void
 ble_gap_test_util_verify_tx_adv_data(void)
 {
     uint8_t param_len;
-    uint8_t *param;
 
     TEST_ASSERT_FATAL(ble_hs_test_util_prev_hci_tx != NULL);
 
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_ADV_DATA,
-                                           &param_len);
-    (void)param; /* XXX: Verify other fields. */
+    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_SET_ADV_DATA,
+                                   &param_len);
+    /* Note: Content of message verified in ble_hs_adv_test.c. */
 }
 
 static void
@@ -249,14 +248,18 @@ ble_gap_test_util_verify_tx_rsp_data(void)
 }
 
 static void
-ble_gap_test_util_verify_tx_adv_enable(void)
+ble_gap_test_util_verify_tx_adv_enable(int enabled)
 {
     uint8_t param_len;
+    uint8_t *param;
 
     TEST_ASSERT_FATAL(ble_hs_test_util_prev_hci_tx != NULL);
 
-    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                   BLE_HCI_OCF_LE_SET_ADV_ENABLE, &param_len);
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_SET_ADV_ENABLE,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_SET_ADV_ENABLE_LEN);
+    TEST_ASSERT(param[0] == !!enabled);
 }
 
 /*****************************************************************************
@@ -788,7 +791,7 @@ ble_gap_test_util_adv(uint8_t disc_mode, uint8_t conn_mode,
 
     TEST_ASSERT(!ble_gap_conn_slave_in_progress());
 
-    rc = ble_gap_conn_advertise(disc_mode, conn_mode, peer_addr,
+    rc = ble_gap_conn_adv_start(disc_mode, conn_mode, peer_addr,
                                 peer_addr_type, ble_gap_test_util_connect_cb,
                                 NULL);
     TEST_ASSERT(rc == 0);
@@ -841,7 +844,7 @@ ble_gap_test_util_adv(uint8_t disc_mode, uint8_t conn_mode,
 
     /* Verify tx of set advertise enable command. */
     ble_hci_sched_wakeup();
-    ble_gap_test_util_verify_tx_adv_enable();
+    ble_gap_test_util_verify_tx_adv_enable(1);
     rc = ble_gap_test_util_rx_hci_ack(&cmd_idx, cmd_fail_idx, BLE_HCI_OGF_LE,
                                       BLE_HCI_OCF_LE_SET_ADV_ENABLE,
                                       hci_status);
@@ -852,9 +855,7 @@ ble_gap_test_util_adv(uint8_t disc_mode, uint8_t conn_mode,
     TEST_ASSERT(ble_gap_conn_slave_in_progress());
 
     /* Receive a connection complete event. */
-    if (conn_mode != BLE_GAP_CONN_MODE_NULL &&
-        conn_mode != BLE_GAP_CONN_MODE_NON) {
-
+    if (conn_mode != BLE_GAP_CONN_MODE_NON) {
         memset(&evt, 0, sizeof evt);
         evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
         evt.status = connect_status;
@@ -874,34 +875,34 @@ TEST_CASE(ble_gap_test_case_conn_adv_bad_args)
     TEST_ASSERT(!ble_gap_conn_slave_in_progress());
 
     /*** Invalid discoverable mode. */
-    rc = ble_gap_conn_advertise(-1, BLE_GAP_CONN_MODE_DIR, peer_addr,
+    rc = ble_gap_conn_adv_start(-1, BLE_GAP_CONN_MODE_DIR, peer_addr,
                                 BLE_ADDR_TYPE_PUBLIC,
                                 ble_gap_test_util_connect_cb, NULL);
     TEST_ASSERT(rc == BLE_HS_EINVAL);
     TEST_ASSERT(!ble_gap_conn_slave_in_progress());
 
     /*** Invalid connectable mode. */
-    rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_GEN, -1, peer_addr,
+    rc = ble_gap_conn_adv_start(BLE_GAP_DISC_MODE_GEN, -1, peer_addr,
                                 BLE_ADDR_TYPE_PUBLIC,
                                 ble_gap_test_util_connect_cb, NULL);
     TEST_ASSERT(rc == BLE_HS_EINVAL);
     TEST_ASSERT(!ble_gap_conn_slave_in_progress());
 
     /*** Invalid peer address type with directed advertisable mode. */
-    rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_DIR,
+    rc = ble_gap_conn_adv_start(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_DIR,
                                 peer_addr, -1,
                                 ble_gap_test_util_connect_cb, NULL);
     TEST_ASSERT(rc == BLE_HS_EINVAL);
     TEST_ASSERT(!ble_gap_conn_slave_in_progress());
 
     /*** Advertising already in progress. */
-    rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_DIR,
+    rc = ble_gap_conn_adv_start(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_DIR,
                                 peer_addr, BLE_ADDR_TYPE_PUBLIC,
                                 ble_gap_test_util_connect_cb, NULL);
     TEST_ASSERT(rc == 0);
     TEST_ASSERT(ble_gap_conn_slave_in_progress());
 
-    rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_DIR,
+    rc = ble_gap_conn_adv_start(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_DIR,
                                 peer_addr, BLE_ADDR_TYPE_PUBLIC,
                                 ble_gap_test_util_connect_cb, NULL);
     TEST_ASSERT(rc == BLE_HS_EALREADY);
@@ -995,6 +996,90 @@ TEST_SUITE(ble_gap_test_suite_conn_adv)
 }
 
 /*****************************************************************************
+ * $stop advertise                                                           *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_stop_adv(uint8_t disc_mode, uint8_t conn_mode,
+                           uint8_t *peer_addr, uint8_t peer_addr_type,
+                           int cmd_fail_idx, uint8_t hci_status)
+{
+    int cmd_idx;
+    int rc;
+
+    ble_gap_test_util_init();
+    cmd_idx = 0;
+
+    /* Start advertising; don't rx a successful connection event. */
+    ble_gap_test_util_adv(disc_mode, conn_mode, peer_addr,
+                          BLE_ADDR_TYPE_PUBLIC, BLE_ERR_UNSUPPORTED, -1, 0);
+
+    TEST_ASSERT(ble_gap_conn_slave_in_progress());
+
+    /* Stop advertising. */
+    rc = ble_gap_conn_adv_stop();
+    TEST_ASSERT(rc == 0);
+
+    /* Verify tx of advertising enable command. */
+    ble_hci_sched_wakeup();
+    ble_gap_test_util_verify_tx_adv_enable(0);
+    rc = ble_gap_test_util_rx_hci_ack(&cmd_idx, cmd_fail_idx, BLE_HCI_OGF_LE,
+                                      BLE_HCI_OCF_LE_SET_ADV_ENABLE,
+                                      hci_status);
+    if (rc != 0) {
+        return;
+    }
+}
+
+TEST_CASE(ble_gap_test_case_conn_stop_adv_good)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int d;
+    int c;
+
+    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
+        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
+            ble_gap_test_util_stop_adv(d, c, peer_addr, BLE_ADDR_TYPE_PUBLIC,
+                                       -1, 0);
+            TEST_ASSERT(!ble_gap_conn_slave_in_progress());
+            TEST_ASSERT(ble_gap_test_conn_event == BLE_GAP_EVENT_ADV_FINISHED);
+            TEST_ASSERT(ble_gap_test_conn_status == 0);
+            TEST_ASSERT(ble_gap_test_conn_desc.conn_handle ==
+                        BLE_HS_CONN_HANDLE_NONE);
+            TEST_ASSERT(ble_gap_test_conn_arg == NULL);
+        }
+    }
+}
+
+TEST_CASE(ble_gap_test_case_conn_stop_adv_hci_fail)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int d;
+    int c;
+
+    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
+        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
+            ble_gap_test_util_stop_adv(d, c, peer_addr, BLE_ADDR_TYPE_PUBLIC,
+                                       0, BLE_ERR_UNSUPPORTED);
+            TEST_ASSERT(ble_gap_conn_slave_in_progress());
+            TEST_ASSERT(ble_gap_test_conn_event ==
+                        BLE_GAP_EVENT_ADV_STOP_FAILURE);
+            TEST_ASSERT(ble_gap_test_conn_status ==
+                        BLE_HS_HCI_ERR(BLE_ERR_UNSUPPORTED));
+            TEST_ASSERT(ble_gap_test_conn_desc.conn_handle ==
+                        BLE_HS_CONN_HANDLE_NONE);
+            TEST_ASSERT(ble_gap_test_conn_arg == NULL);
+        }
+    }
+}
+
+TEST_SUITE(ble_gap_test_suite_conn_stop_adv)
+{
+    ble_gap_test_case_conn_stop_adv_good();
+    ble_gap_test_case_conn_stop_adv_hci_fail();
+}
+
+/*****************************************************************************
  * $all                                                                      *
  *****************************************************************************/
 
@@ -1006,6 +1091,7 @@ ble_gap_test_all(void)
     ble_gap_test_suite_conn_cancel();
     ble_gap_test_suite_conn_terminate();
     ble_gap_test_suite_conn_adv();
+    ble_gap_test_suite_conn_stop_adv();
 
     return tu_any_failed;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a05d1469/net/nimble/host/src/test/ble_hs_adv_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_adv_test.c b/net/nimble/host/src/test/ble_hs_adv_test.c
index 37bd048..8da913b 100644
--- a/net/nimble/host/src/test/ble_hs_adv_test.c
+++ b/net/nimble/host/src/test/ble_hs_adv_test.c
@@ -117,7 +117,7 @@ ble_hs_adv_test_misc_tx_and_verify_data(uint8_t disc_mode,
 
     ble_hs_test_util_init();
 
-    rc = ble_gap_conn_advertise(disc_mode, BLE_GAP_CONN_MODE_UND, NULL, 0,
+    rc = ble_gap_conn_adv_start(disc_mode, BLE_GAP_CONN_MODE_UND, NULL, 0,
                                 NULL, NULL);
     TEST_ASSERT_FATAL(rc == 0);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a05d1469/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 a490446..f7db96d 100644
--- a/net/nimble/host/src/test/ble_hs_conn_test.c
+++ b/net/nimble/host/src/test/ble_hs_conn_test.c
@@ -138,7 +138,7 @@ TEST_CASE(ble_hs_conn_test_direct_connectable_success)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate advertising. */
-    rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_DIR,
+    rc = ble_gap_conn_adv_start(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_DIR,
                                 addr, BLE_HCI_ADV_PEER_ADDR_PUBLIC,
                                 NULL, NULL);
     TEST_ASSERT(rc == 0);
@@ -187,7 +187,7 @@ TEST_CASE(ble_hs_conn_test_direct_connectable_hci_errors)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate connection. */
-    rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_DIR,
+    rc = ble_gap_conn_adv_start(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_DIR,
                                 addr, BLE_HCI_ADV_PEER_ADDR_PUBLIC,
                                 NULL, NULL);
     TEST_ASSERT(rc == 0);
@@ -234,7 +234,7 @@ TEST_CASE(ble_hs_conn_test_undirect_connectable_success)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate advertising. */
-    rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_UND,
+    rc = ble_gap_conn_adv_start(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_UND,
                                 NULL, 0, NULL, NULL);
     TEST_ASSERT(rc == 0);
 


[2/2] incubator-mynewt-larva git commit: Tests for discovery GAP procedures.

Posted by cc...@apache.org.
Tests for discovery GAP procedures.


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

Branch: refs/heads/master
Commit: 17c32e81511c077bac9878d1bb874fa173edd2bd
Parents: a05d146
Author: Christopher Collins <cc...@gmail.com>
Authored: Fri Jan 15 13:46:07 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Fri Jan 15 14:00:18 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_gap_conn.c      |   7 +-
 net/nimble/host/src/test/ble_gap_test.c | 248 ++++++++++++++++++++++++++-
 2 files changed, 245 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/17c32e81/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 c6f203e..bdcebae 100644
--- a/net/nimble/host/src/ble_gap_conn.c
+++ b/net/nimble/host/src/ble_gap_conn.c
@@ -39,12 +39,12 @@
 
 #define BLE_GAP_CONN_OP_W_SET                               1
 
-/** General discovery master states. */
+/** Discovery master states. */
 #define BLE_GAP_CONN_M_STATE_DISC_PARAMS                    0
 #define BLE_GAP_CONN_M_STATE_DISC_ENABLE                    1
 #define BLE_GAP_CONN_M_STATE_DISC_ACKED                     2
 
-/** General discovery master states. */
+/** Connect master states. */
 #define BLE_GAP_CONN_STATE_M_PENDING                        0
 #define BLE_GAP_CONN_STATE_M_UNACKED                        1
 #define BLE_GAP_CONN_STATE_M_ACKED                          2
@@ -62,6 +62,7 @@
 #define BLE_GAP_CONN_S_STATE_DIR_ENABLE                     1
 #define BLE_GAP_CONN_S_STATE_DIR_ADV                        2
 
+/** White list states. */
 #define BLE_GAP_CONN_STATE_W_CLEAR                          0
 #define BLE_GAP_CONN_STATE_W_ADD                            1
 
@@ -583,7 +584,7 @@ ble_gap_conn_rx_adv_report(struct ble_hs_adv *adv)
     struct ble_hs_adv_fields fields;
     int rc;
 
-    if (ble_gap_conn_master.op      != BLE_GAP_CONN_OP_M_DISC &&
+    if (ble_gap_conn_master.op      != BLE_GAP_CONN_OP_M_DISC ||
         ble_gap_conn_master.state   != BLE_GAP_CONN_M_STATE_DISC_ACKED) {
 
         return;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/17c32e81/net/nimble/host/src/test/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gap_test.c b/net/nimble/host/src/test/ble_gap_test.c
index b47c763..4c12e60 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -22,6 +22,7 @@
 #include "host/ble_hs_test.h"
 #include "ble_hs_priv.h"
 #include "ble_hs_conn.h"
+#include "ble_hs_adv.h"
 #include "ble_hci_sched.h"
 #include "ble_gatt_priv.h"
 #include "ble_gap_priv.h"
@@ -32,6 +33,11 @@ static int ble_gap_test_conn_status;
 static struct ble_gap_conn_desc ble_gap_test_conn_desc;
 static void *ble_gap_test_conn_arg;
 
+static int ble_gap_test_disc_event;
+static int ble_gap_test_disc_status;
+static struct ble_gap_disc_desc ble_gap_test_disc_desc;
+static void *ble_gap_test_disc_arg;
+
 static int ble_gap_test_wl_status;
 static void *ble_gap_test_wl_arg;
 
@@ -45,14 +51,37 @@ ble_gap_test_util_init(void)
     ble_hs_test_util_init();
 
     ble_gap_test_conn_event = -1;
+    ble_gap_test_conn_status = -1;
     memset(&ble_gap_test_conn_desc, 0xff, sizeof ble_gap_test_conn_desc);
     ble_gap_test_conn_arg = (void *)-1;
 
+    ble_gap_test_disc_event = -1;
+    ble_gap_test_disc_status = -1;
+    memset(&ble_gap_test_disc_desc, 0xff, sizeof ble_gap_test_disc_desc);
+    ble_gap_test_disc_arg = (void *)-1;
+
     ble_gap_test_wl_status = -1;
     ble_gap_test_wl_arg = (void *)-1;
 }
 
 static void
+ble_gap_test_util_wl_cb(int status, void *arg)
+{
+    ble_gap_test_wl_status = status;
+    ble_gap_test_wl_arg = arg;
+}
+
+static void
+ble_gap_test_util_disc_cb(int event, int status,
+                          struct ble_gap_disc_desc *desc, void *arg)
+{
+    ble_gap_test_disc_event = event;
+    ble_gap_test_disc_status = status;
+    ble_gap_test_disc_desc = *desc;
+    ble_gap_test_disc_arg = arg;
+}
+
+static void
 ble_gap_test_util_connect_cb(int event, int status,
                              struct ble_gap_conn_desc *desc, void *arg)
 {
@@ -62,13 +91,6 @@ ble_gap_test_util_connect_cb(int event, int status,
     ble_gap_test_conn_arg = arg;
 }
 
-static void
-ble_gap_test_util_wl_cb(int status, void *arg)
-{
-    ble_gap_test_wl_status = status;
-    ble_gap_test_wl_arg = arg;
-}
-
 static int
 ble_gap_test_util_rx_hci_ack(int *cmd_idx, int cmd_fail_idx,
                              uint8_t ogf, uint16_t ocf, uint8_t fail_status)
@@ -146,6 +168,42 @@ ble_gap_test_util_verify_tx_add_wl(struct ble_gap_white_entry *entry)
 }
 
 static void
+ble_gap_test_util_verify_tx_set_scan_params(uint16_t itvl,
+                                            uint16_t scan_window,
+                                            uint8_t filter_policy)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    TEST_ASSERT_FATAL(ble_hs_test_util_prev_hci_tx != NULL);
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_SET_SCAN_PARAMS,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_SET_SCAN_PARAM_LEN);
+    TEST_ASSERT(param[0] == BLE_HCI_SCAN_TYPE_ACTIVE);
+    TEST_ASSERT(le16toh(param + 1) == itvl);
+    TEST_ASSERT(le16toh(param + 3) == scan_window);
+    TEST_ASSERT(param[5] == BLE_HCI_ADV_OWN_ADDR_PUBLIC);
+    TEST_ASSERT(param[6] == filter_policy);
+}
+
+static void
+ble_gap_test_util_verify_tx_scan_enable(uint8_t enable)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    TEST_ASSERT_FATAL(ble_hs_test_util_prev_hci_tx != NULL);
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_SET_SCAN_ENABLE,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_SET_SCAN_ENABLE_LEN);
+    TEST_ASSERT(param[0] == enable);
+}
+
+static void
 ble_gap_test_util_verify_tx_create_conn(uint8_t filter_policy)
 {
     uint8_t param_len;
@@ -393,6 +451,181 @@ TEST_SUITE(ble_gap_test_suite_conn_wl)
 }
 
 /*****************************************************************************
+ * $discovery                                                                *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_disc(uint8_t disc_mode, uint8_t *peer_addr,
+                       struct ble_hs_adv *adv, int cmd_fail_idx,
+                       uint8_t hci_status)
+{
+    int cmd_idx;
+    int rc;
+
+    ble_gap_test_util_init();
+    cmd_idx = 0;
+
+    /* Begin the discovery procedure. */
+    rc = ble_gap_conn_disc(0, disc_mode, ble_gap_test_util_disc_cb, NULL);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(ble_gap_conn_master_in_progress());
+
+    ble_gap_conn_rx_adv_report(adv);
+    TEST_ASSERT(ble_gap_test_disc_status == -1);
+
+    /* Verify tx of set scan parameters command. */
+    ble_hci_sched_wakeup();
+    ble_gap_test_util_verify_tx_set_scan_params(
+        30 * 1000 / BLE_HCI_ADV_ITVL,
+        30 * 1000 / BLE_HCI_SCAN_ITVL,
+        BLE_HCI_SCAN_FILT_NO_WL);
+
+    rc = ble_gap_test_util_rx_hci_ack(&cmd_idx, cmd_fail_idx, BLE_HCI_OGF_LE,
+                                      BLE_HCI_OCF_LE_SET_SCAN_PARAMS,
+                                      hci_status);
+    if (rc != 0) {
+        return;
+    }
+
+    ble_gap_conn_rx_adv_report(adv);
+    TEST_ASSERT(ble_gap_test_disc_status == -1);
+
+    /* Verify tx of scan enable command. */
+    ble_hci_sched_wakeup();
+    ble_gap_test_util_verify_tx_scan_enable(1);
+    rc = ble_gap_test_util_rx_hci_ack(&cmd_idx, cmd_fail_idx, BLE_HCI_OGF_LE,
+                                      BLE_HCI_OCF_LE_SET_SCAN_ENABLE,
+                                      hci_status);
+    if (rc != 0) {
+        return;
+    }
+
+    ble_gap_conn_rx_adv_report(adv);
+}
+
+TEST_CASE(ble_gap_test_case_conn_disc_bad_args)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /*** Invalid discovery mode. */
+    rc = ble_gap_conn_disc(0, BLE_GAP_DISC_MODE_NON,
+                           ble_gap_test_util_disc_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** Master operation already in progress. */
+    rc = ble_gap_conn_direct_connect(BLE_GAP_ADDR_TYPE_WL, NULL,
+                                     ble_gap_test_util_connect_cb, NULL);
+    rc = ble_gap_conn_disc(0, BLE_GAP_DISC_MODE_GEN,
+                           ble_gap_test_util_disc_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EALREADY);
+}
+
+TEST_CASE(ble_gap_test_case_conn_disc_good)
+{
+    uint8_t adv_data[32];
+    uint8_t flags;
+    int rc;
+    int d;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    struct ble_hs_adv adv = {
+        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
+        .addr_type = BLE_ADDR_TYPE_PUBLIC,
+        .length_data = 0,
+        .rssi = 0,
+        .addr = { 1, 2, 3, 4, 5, 6 },
+        .data = adv_data,
+    };
+
+    flags = BLE_HS_ADV_F_DISC_LTD;
+    rc = ble_hs_adv_set_flat(BLE_HS_ADV_TYPE_FLAGS, 1, &flags,
+                             adv.data, &adv.length_data,
+                             sizeof adv_data);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    for (d = BLE_GAP_DISC_MODE_LTD; d < BLE_GAP_DISC_MODE_MAX; d++) {
+        ble_gap_test_util_disc(d, peer_addr, &adv, -1, 0);
+
+        TEST_ASSERT(ble_gap_conn_master_in_progress());
+
+        TEST_ASSERT(ble_gap_test_disc_event ==
+                    BLE_GAP_EVENT_DISC_SUCCESS);
+        TEST_ASSERT(ble_gap_test_disc_status == 0);
+        TEST_ASSERT(ble_gap_test_disc_desc.event_type ==
+                    adv.event_type);
+        TEST_ASSERT(ble_gap_test_disc_desc.addr_type == adv.addr_type);
+        TEST_ASSERT(ble_gap_test_disc_desc.length_data ==
+                    adv.length_data);
+        TEST_ASSERT(ble_gap_test_disc_desc.rssi == adv.rssi);
+        TEST_ASSERT(memcmp(ble_gap_test_disc_desc.addr, adv.addr,
+                           6) == 0);
+        TEST_ASSERT(ble_gap_test_disc_desc.data == adv.data);
+        TEST_ASSERT(ble_gap_test_disc_desc.fields != NULL);
+        TEST_ASSERT(ble_gap_test_disc_arg == NULL);
+    }
+}
+
+TEST_CASE(ble_gap_test_case_conn_disc_bad_flags)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    struct ble_hs_adv adv = {
+        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
+        .addr_type = BLE_ADDR_TYPE_PUBLIC,
+        .length_data = 0,
+        .rssi = 0,
+        .addr = { 1, 2, 3, 4, 5, 6 },
+        .data = NULL,
+    };
+
+    ble_gap_test_util_disc(BLE_GAP_DISC_MODE_LTD, peer_addr, &adv, -1, 0);
+    TEST_ASSERT(ble_gap_conn_master_in_progress());
+
+    /* Verify that the report was ignored becuase of a mismatched LTD flag. */
+    TEST_ASSERT(ble_gap_test_disc_event == -1);
+}
+
+TEST_CASE(ble_gap_test_case_conn_disc_hci_fail)
+{
+    int fail_idx;
+    int d;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    struct ble_hs_adv adv = {
+        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
+        .addr_type = BLE_ADDR_TYPE_PUBLIC,
+        .length_data = 0,
+        .rssi = 0,
+        .addr = { 1, 2, 3, 4, 5, 6 },
+        .data = NULL,
+    };
+
+    for (d = BLE_GAP_DISC_MODE_LTD; d < BLE_GAP_DISC_MODE_MAX; d++) {
+        for (fail_idx = 0; fail_idx < 2; fail_idx++) {
+            ble_gap_test_util_disc(d, peer_addr, &adv, fail_idx,
+                                   BLE_ERR_UNSUPPORTED);
+
+            TEST_ASSERT(!ble_gap_conn_master_in_progress());
+
+            TEST_ASSERT(ble_gap_test_disc_event ==
+                        BLE_GAP_EVENT_DISC_FINISHED);
+            TEST_ASSERT(ble_gap_test_disc_status ==
+                        BLE_HS_HCI_ERR(BLE_ERR_UNSUPPORTED));
+            TEST_ASSERT(ble_gap_test_disc_arg == NULL);
+        }
+    }
+}
+
+TEST_SUITE(ble_gap_test_suite_conn_disc)
+{
+    ble_gap_test_case_conn_disc_bad_args();
+    ble_gap_test_case_conn_disc_good();
+    ble_gap_test_case_conn_disc_bad_flags();
+    ble_gap_test_case_conn_disc_hci_fail();
+}
+
+/*****************************************************************************
  * $direct connect                                                           *
  *****************************************************************************/
 
@@ -1087,6 +1320,7 @@ int
 ble_gap_test_all(void)
 {
     ble_gap_test_suite_conn_wl();
+    ble_gap_test_suite_conn_disc();
     ble_gap_test_suite_conn_dir();
     ble_gap_test_suite_conn_cancel();
     ble_gap_test_suite_conn_terminate();