You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/10/09 18:50:58 UTC

[GitHub] ccollins476ad closed pull request #214: Fix unit tests involving `ble_gap_preempt()`

ccollins476ad closed pull request #214: Fix unit tests involving `ble_gap_preempt()`
URL: https://github.com/apache/mynewt-nimble/pull/214
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c
index 5a06ead3..912997cb 100644
--- a/nimble/host/src/ble_gap.c
+++ b/nimble/host/src/ble_gap.c
@@ -1811,12 +1811,15 @@ ble_gap_adv_stop_no_lock(void)
     return BLE_HS_ENOTSUP;
 #endif
 
+    bool active;
     int rc;
 
     BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task());
 
     STATS_INC(ble_gap_stats, adv_stop);
 
+    active = ble_gap_adv_active();
+
     BLE_HS_LOG(INFO, "GAP procedure initiated: stop advertising.\n");
 
     rc = ble_gap_adv_enable_tx(0);
@@ -1826,7 +1829,11 @@ ble_gap_adv_stop_no_lock(void)
 
     ble_gap_slave_reset_state(0);
 
-    rc = 0;
+    if (!active) {
+        rc = BLE_HS_EALREADY;
+    } else {
+        rc = 0;
+    }
 
 done:
     if (rc != 0) {
@@ -2567,15 +2574,14 @@ ble_gap_ext_adv_stop_no_lock(uint8_t instance)
     uint8_t buf[6];
     struct hci_ext_adv_set set;
     uint16_t opcode;
+    bool active;
     int rc;
 
     if (!ble_gap_slave[instance].configured) {
         return BLE_HS_EINVAL;
     }
 
-    if (ble_gap_slave[instance].op != BLE_GAP_OP_S_ADV) {
-        return BLE_HS_EALREADY;
-    }
+    active = ble_gap_adv_active_instance(instance);
 
     opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_EXT_ADV_ENABLE);
 
@@ -2595,7 +2601,11 @@ ble_gap_ext_adv_stop_no_lock(uint8_t instance)
 
     ble_gap_slave[instance].op = BLE_GAP_OP_NULL;
 
-    return 0;
+    if (!active) {
+        return BLE_HS_EALREADY;
+    } else {
+        return 0;
+    }
 }
 
 int
diff --git a/nimble/host/src/ble_hs.c b/nimble/host/src/ble_hs.c
index c96bf2be..917781f5 100644
--- a/nimble/host/src/ble_hs.c
+++ b/nimble/host/src/ble_hs.c
@@ -555,6 +555,11 @@ ble_hs_start(void)
 
     ble_hs_parent_task = ble_npl_get_current_task_id();
 
+    /* Stop the timer just in case the host was already running (e.g., unit
+     * tests).
+     */
+    ble_npl_callout_stop(&ble_hs_timer_timer);
+
     ble_npl_callout_init(&ble_hs_timer_timer, ble_hs_evq,
                     ble_hs_timer_exp, NULL);
 
diff --git a/nimble/host/test/src/ble_hs_pvcy_test.c b/nimble/host/test/src/ble_hs_pvcy_test.c
index c6b73a93..4a8bf623 100644
--- a/nimble/host/test/src/ble_hs_pvcy_test.c
+++ b/nimble/host/test/src/ble_hs_pvcy_test.c
@@ -93,8 +93,23 @@ ble_hs_pvcy_test_util_all_gap_procs(int adv_status,
 }
 
 static void
-ble_hs_pvcy_test_util_add_irk_set_acks(void)
+ble_hs_pvcy_test_util_add_irk_set_acks(bool scanning, bool connecting)
 {
+    ble_hs_test_util_hci_ack_append(
+        BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_ENABLE), 0);
+
+    if (connecting) {
+        ble_hs_test_util_hci_ack_append(
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
+            0);
+    }
+
+    if (scanning) {
+        ble_hs_test_util_hci_ack_append(
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+            0);
+    }
+
     ble_hs_test_util_hci_ack_append(
         BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_RESOLV_LIST), 0);
     ble_hs_test_util_hci_ack_append(
@@ -118,7 +133,7 @@ ble_hs_pvcy_test_util_start_host(int num_expected_irks)
     ble_hs_test_util_hci_ack_set_startup();
 
     for (i = 0; i < num_expected_irks; i++) {
-        ble_hs_pvcy_test_util_add_irk_set_acks();
+        ble_hs_pvcy_test_util_add_irk_set_acks(false, false);
     }
 
     rc = ble_hs_start();
@@ -131,8 +146,26 @@ ble_hs_pvcy_test_util_start_host(int num_expected_irks)
 static void
 ble_hs_pvcy_test_util_add_irk_verify_tx(const ble_addr_t *peer_addr,
                                         const uint8_t *peer_irk,
-                                        const uint8_t *local_irk)
+                                        const uint8_t *local_irk,
+                                        bool scanning,
+                                        bool connecting)
 {
+    ble_hs_test_util_hci_verify_tx(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_SET_ADV_ENABLE,
+                                   NULL);
+
+    if (connecting) {
+        ble_hs_test_util_hci_verify_tx(BLE_HCI_OGF_LE,
+                                       BLE_HCI_OCF_LE_CREATE_CONN_CANCEL,
+                                       NULL);
+    }
+
+    if (scanning) {
+        ble_hs_test_util_hci_verify_tx(BLE_HCI_OGF_LE,
+                                       BLE_HCI_OCF_LE_SET_SCAN_ENABLE,
+                                       NULL);
+    }
+
     ble_hs_test_util_hci_verify_tx_add_irk(peer_addr->type,
                                            peer_addr->val,
                                            peer_irk,
@@ -146,21 +179,32 @@ ble_hs_pvcy_test_util_add_irk_verify_tx(const ble_addr_t *peer_addr,
 static void
 ble_hs_pvcy_test_util_add_irk(const ble_addr_t *peer_addr,
                               const uint8_t *peer_irk,
-                              const uint8_t *local_irk)
+                              const uint8_t *local_irk,
+                              bool scanning,
+                              bool connecting)
 {
+    int num_acks;
     int rc;
 
-    ble_hs_pvcy_test_util_add_irk_set_acks();
+    ble_hs_pvcy_test_util_add_irk_set_acks(scanning, connecting);
 
     rc = ble_hs_pvcy_add_entry(peer_addr->val, peer_addr->type, peer_irk);
     TEST_ASSERT_FATAL(rc == 0);
 
-    ble_hs_test_util_hci_out_adj(-2);
-    ble_hs_pvcy_test_util_add_irk_verify_tx(peer_addr, peer_irk, local_irk);
+    num_acks = 3;
+    if (scanning) {
+        num_acks++;
+    }
+    if (connecting) {
+        num_acks++;
+    }
+    ble_hs_test_util_hci_out_adj(-num_acks);
+    ble_hs_pvcy_test_util_add_irk_verify_tx(peer_addr, peer_irk, local_irk,
+                                            scanning, connecting);
 }
 
 static void
-ble_hs_pvcy_test_util_add_arbitrary_irk(void)
+ble_hs_pvcy_test_util_add_arbitrary_irk(bool scanning, bool connecting)
 {
     ble_addr_t peer_addr;
 
@@ -171,22 +215,28 @@ ble_hs_pvcy_test_util_add_arbitrary_irk(void)
     ble_hs_pvcy_test_util_add_irk(
         &peer_addr,
         (uint8_t[16]){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
-        ble_hs_pvcy_default_irk);
+        ble_hs_pvcy_default_irk,
+        scanning,
+        connecting);
 }
 
 static void
-ble_hs_pvcy_test_util_restore_irk(const struct ble_store_value_sec *value_sec)
+ble_hs_pvcy_test_util_restore_irk(const struct ble_store_value_sec *value_sec,
+                                  bool scanning,
+                                  bool connecting)
 {
     int rc;
 
-    ble_hs_pvcy_test_util_add_irk_set_acks();
+    ble_hs_pvcy_test_util_add_irk_set_acks(scanning, connecting);
 
     rc = ble_store_write_peer_sec(value_sec);
     TEST_ASSERT_FATAL(rc == 0);
 
     ble_hs_pvcy_test_util_add_irk_verify_tx(&value_sec->peer_addr,
                                             value_sec->irk,
-                                            ble_hs_pvcy_default_irk);
+                                            ble_hs_pvcy_default_irk,
+                                            scanning,
+                                            connecting);
 }
 
 TEST_CASE(ble_hs_pvcy_test_case_restore_irks)
@@ -210,13 +260,14 @@ TEST_CASE(ble_hs_pvcy_test_case_restore_irks)
         .irk = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 },
         .irk_present = 1,
     };
-    ble_hs_pvcy_test_util_restore_irk(&value_sec1);
+    ble_hs_pvcy_test_util_restore_irk(&value_sec1, false, false);
 
     /* Ensure it gets added to list on startup. */
     ble_hs_pvcy_test_util_start_host(1);
     ble_hs_pvcy_test_util_add_irk_verify_tx(&value_sec1.peer_addr,
                                             value_sec1.irk,
-                                            ble_hs_pvcy_default_irk);
+                                            ble_hs_pvcy_default_irk,
+                                            false, false);
 
     /* Two persisted IRKs. */
     value_sec2 = (struct ble_store_value_sec) {
@@ -227,16 +278,18 @@ TEST_CASE(ble_hs_pvcy_test_case_restore_irks)
         .irk = { 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 9, 9, 9, 9, 9, 10 },
         .irk_present = 1,
     };
-    ble_hs_pvcy_test_util_restore_irk(&value_sec2);
+    ble_hs_pvcy_test_util_restore_irk(&value_sec2, false, false);
 
     /* Ensure both get added to list on startup. */
     ble_hs_pvcy_test_util_start_host(2);
     ble_hs_pvcy_test_util_add_irk_verify_tx(&value_sec1.peer_addr,
                                             value_sec1.irk,
-                                            ble_hs_pvcy_default_irk);
+                                            ble_hs_pvcy_default_irk,
+                                            false, false);
     ble_hs_pvcy_test_util_add_irk_verify_tx(&value_sec2.peer_addr,
                                             value_sec2.irk,
-                                            ble_hs_pvcy_default_irk);
+                                            ble_hs_pvcy_default_irk,
+                                            false, false);
 }
 
 /** No active GAP procedures. */
@@ -244,7 +297,7 @@ TEST_CASE(ble_hs_pvcy_test_case_add_irk_idle)
 {
     ble_hs_pvcy_test_util_init();
 
-    ble_hs_pvcy_test_util_add_arbitrary_irk();
+    ble_hs_pvcy_test_util_add_arbitrary_irk(false, false);
     TEST_ASSERT(ble_hs_pvcy_test_num_gap_events == 0);
 }
 
@@ -263,10 +316,7 @@ TEST_CASE(ble_hs_pvcy_test_case_add_irk_adv)
                                     NULL, 0, 0);
     TEST_ASSERT_FATAL(rc == 0);
 
-    ble_hs_test_util_hci_ack_set(
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_ENABLE),
-        0);
-    ble_hs_pvcy_test_util_add_arbitrary_irk();
+    ble_hs_pvcy_test_util_add_arbitrary_irk(false, false);
 
     TEST_ASSERT(ble_hs_pvcy_test_num_gap_events == 1);
     TEST_ASSERT(ble_hs_pvcy_test_gap_events[0].type ==
@@ -293,10 +343,7 @@ TEST_CASE(ble_hs_pvcy_test_case_add_irk_disc)
                                NULL, -1, 0);
     TEST_ASSERT_FATAL(rc == 0);
 
-    ble_hs_test_util_hci_ack_set(
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
-        0);
-    ble_hs_pvcy_test_util_add_arbitrary_irk();
+    ble_hs_pvcy_test_util_add_arbitrary_irk(true, false);
 
     TEST_ASSERT(ble_hs_pvcy_test_num_gap_events == 1);
     TEST_ASSERT(ble_hs_pvcy_test_gap_events[0].type ==
@@ -323,10 +370,7 @@ TEST_CASE(ble_hs_pvcy_test_case_add_irk_conn)
                                   ble_hs_pvcy_test_util_gap_event, NULL, 0);
     TEST_ASSERT_FATAL(rc == 0);
 
-    ble_hs_test_util_hci_ack_set(
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
-        0);
-    ble_hs_pvcy_test_util_add_arbitrary_irk();
+    ble_hs_pvcy_test_util_add_arbitrary_irk(false, true);
 
     /* Cancel is now in progress. */
     TEST_ASSERT(ble_hs_pvcy_test_num_gap_events == 0);
@@ -372,13 +416,7 @@ TEST_CASE(ble_hs_pvcy_test_case_add_irk_adv_disc)
                                NULL, -1, 0);
     TEST_ASSERT_FATAL(rc == 0);
 
-    ble_hs_test_util_hci_ack_set_seq((struct ble_hs_test_util_hci_ack[]) {
-        { BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_ENABLE), 0 },
-        { BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_ENABLE), 0 },
-        { 0 },
-    });
-
-    ble_hs_pvcy_test_util_add_arbitrary_irk();
+    ble_hs_pvcy_test_util_add_arbitrary_irk(true, false);
 
     TEST_ASSERT(ble_hs_pvcy_test_num_gap_events == 2);
     TEST_ASSERT(ble_hs_pvcy_test_gap_events[0].type ==
@@ -417,13 +455,7 @@ TEST_CASE(ble_hs_pvcy_test_case_add_irk_adv_conn)
                                   ble_hs_pvcy_test_util_gap_event, NULL, 0);
     TEST_ASSERT_FATAL(rc == 0);
 
-    ble_hs_test_util_hci_ack_set_seq((struct ble_hs_test_util_hci_ack[]) {
-        { BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_ENABLE), 0 },
-        { BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CREATE_CONN_CANCEL), 0 },
-        { 0 },
-    });
-
-    ble_hs_pvcy_test_util_add_arbitrary_irk();
+    ble_hs_pvcy_test_util_add_arbitrary_irk(false, true);
 
     /* Cancel is now in progress. */
     TEST_ASSERT(ble_hs_pvcy_test_num_gap_events == 1);
diff --git a/nimble/host/test/src/ble_hs_test_util.c b/nimble/host/test/src/ble_hs_test_util.c
index d58677df..cf16a826 100644
--- a/nimble/host/test/src/ble_hs_test_util.c
+++ b/nimble/host/test/src/ble_hs_test_util.c
@@ -619,16 +619,20 @@ ble_hs_test_util_set_our_irk(const uint8_t *irk, int fail_idx,
             ble_hs_test_util_hci_misc_exp_status(2, fail_idx, hci_status),
         },
         {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_ENABLE),
             ble_hs_test_util_hci_misc_exp_status(3, fail_idx, hci_status),
         },
         {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_PRIVACY_MODE),
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
             ble_hs_test_util_hci_misc_exp_status(4, fail_idx, hci_status),
         },
         {
             BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_PRIVACY_MODE),
-            ble_hs_test_util_hci_misc_exp_status(4, fail_idx, hci_status),
+            ble_hs_test_util_hci_misc_exp_status(5, fail_idx, hci_status),
+        },
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_PRIVACY_MODE),
+            ble_hs_test_util_hci_misc_exp_status(6, fail_idx, hci_status),
         },
         {
             0
@@ -1985,6 +1989,7 @@ ble_hs_test_util_reg_svcs(const struct ble_gatt_svc_def *svcs,
     TEST_ASSERT_FATAL(rc == 0);
 }
 
+
 void
 ble_hs_test_util_init_no_start(void)
 {
diff --git a/nimble/host/test/src/ble_hs_test_util_hci.c b/nimble/host/test/src/ble_hs_test_util_hci.c
index f485133d..20aa61ca 100644
--- a/nimble/host/test/src/ble_hs_test_util_hci.c
+++ b/nimble/host/test/src/ble_hs_test_util_hci.c
@@ -288,6 +288,10 @@ static const struct ble_hs_test_util_hci_ack hci_startup_seq[] = {
         .opcode = ble_hs_hci_util_opcode_join(
             BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
     },
+    {
+        .opcode = ble_hs_hci_util_opcode_join(
+            BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_ENABLE),
+    },
     {
         .opcode = ble_hs_hci_util_opcode_join(
             BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
@@ -399,6 +403,7 @@ ble_hs_test_util_hci_verify_tx_add_irk(uint8_t addr_type,
     uint8_t param_len;
     uint8_t *param;
 
+
     param = ble_hs_test_util_hci_verify_tx(BLE_HCI_OGF_LE,
                                            BLE_HCI_OCF_LE_ADD_RESOLV_LIST,
                                            &param_len);
diff --git a/nimble/host/test/src/ble_sm_test_util.c b/nimble/host/test/src/ble_sm_test_util.c
index 88854ce0..81c15ace 100644
--- a/nimble/host/test/src/ble_sm_test_util.c
+++ b/nimble/host/test/src/ble_sm_test_util.c
@@ -1344,6 +1344,10 @@ ble_sm_test_util_verify_tx_add_resolve_list(uint8_t peer_id_addr_type,
     uint8_t param_len;
     uint8_t *param;
 
+    ble_hs_test_util_hci_verify_tx(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_SET_ADV_ENABLE,
+                                   NULL);
+
     param = ble_hs_test_util_hci_verify_tx(BLE_HCI_OGF_LE,
                                            BLE_HCI_OCF_LE_ADD_RESOLV_LIST,
                                            &param_len);
@@ -2036,10 +2040,14 @@ ble_sm_test_util_rx_keys(struct ble_sm_test_params *params,
     if (peer_key_dist & BLE_SM_PAIR_KEY_DIST_ID) {
 
         ble_hs_test_util_hci_ack_set_seq(((struct ble_hs_test_util_hci_ack[]) {
+            {
+                .opcode = ble_hs_hci_util_opcode_join(
+                                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_ENABLE),
+            },
             {
                 .opcode = ble_hs_hci_util_opcode_join(
                                 BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
-            } ,
+            },
             {
                 .opcode = ble_hs_hci_util_opcode_join(
                                 BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_PRIVACY_MODE),


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services