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/06/08 11:50:48 UTC

[1/7] incubator-mynewt-core git commit: BLE Host - always honor ctlr encryption events.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/upf54 20eb95abd -> b0f2a9eb5


BLE Host - always honor ctlr encryption events.

Update a connection's encryption state according to the incoming event,
even if the event is unexpected.


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

Branch: refs/heads/upf54
Commit: 50d9951eab02dd260cb1fb3a04eeb1da431aa8ee
Parents: 20eb95a
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jun 8 18:19:45 2016 +0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Jun 8 18:19:45 2016 +0800

----------------------------------------------------------------------
 apps/bletiny/src/misc.c                |   3 +-
 net/nimble/host/include/host/ble_gap.h |   1 -
 net/nimble/host/src/ble_gap.c          |  21 +--
 net/nimble/host/src/ble_gap_priv.h     |   2 +-
 net/nimble/host/src/ble_sm.c           | 228 ++++++++++++++--------------
 net/nimble/host/src/ble_sm_priv.h      |   7 +-
 net/nimble/host/src/test/ble_sm_test.c |  16 --
 7 files changed, 120 insertions(+), 158 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/50d9951e/apps/bletiny/src/misc.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/misc.c b/apps/bletiny/src/misc.c
index 83c28cd..4e7c97d 100644
--- a/apps/bletiny/src/misc.c
+++ b/apps/bletiny/src/misc.c
@@ -106,10 +106,9 @@ print_conn_desc(struct ble_gap_conn_desc *desc)
                    desc->peer_id_addr_type);
     print_addr(desc->peer_id_addr);
     console_printf(" conn_itvl=%d conn_latency=%d supervision_timeout=%d "
-                   "pair_alg=%d enc_enabled=%d authenticated=%d bonded=%d\n",
+                   "enc_enabled=%d authenticated=%d bonded=%d\n",
                    desc->conn_itvl, desc->conn_latency,
                    desc->supervision_timeout,
-                   desc->sec_state.pair_alg,
                    desc->sec_state.enc_enabled,
                    desc->sec_state.authenticated,
                    desc->sec_state.bonded);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/50d9951e/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 f40c477..54a6d4e 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -102,7 +102,6 @@ struct hci_adv_params;
 #define BLE_GAP_EVENT_NOTIFY                12
 
 struct ble_gap_sec_state {
-    uint8_t pair_alg;
     unsigned enc_enabled:1;
     unsigned authenticated:1;
     unsigned bonded:1;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/50d9951e/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 669a3fc..a3d6388 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -2190,24 +2190,14 @@ ble_gap_passkey_event(uint16_t conn_handle,
 }
 
 void
-ble_gap_enc_event(uint16_t conn_handle, int status,
-                    struct ble_gap_sec_state *sec_state)
+ble_gap_enc_event(uint16_t conn_handle, int status, int security_restored)
 {
     struct ble_gap_conn_ctxt ctxt;
     struct ble_gap_snapshot snap;
-    struct ble_hs_conn *conn;
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn != NULL) {
-        conn->bhc_sec_state = *sec_state;
-        ble_gap_conn_to_snapshot(conn, &snap);
-    }
-
-    ble_hs_unlock();
+    int rc;
 
-    if (conn == NULL) {
+    rc = ble_gap_find_snapshot(conn_handle, &snap);
+    if (rc != 0) {
         /* No longer connected. */
         return;
     }
@@ -2218,7 +2208,8 @@ ble_gap_enc_event(uint16_t conn_handle, int status,
     ble_gap_call_event_cb(BLE_GAP_EVENT_ENC_CHANGE, &ctxt,
                           snap.cb, snap.cb_arg);
 
-    if (sec_state->bonded) {
+    if (security_restored) {
+        BLE_HS_DBG_ASSERT(snap.desc.sec_state.bonded);
         ble_gatts_bonding_restored(conn_handle);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/50d9951e/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 16d42d7..590b0fb 100644
--- a/net/nimble/host/src/ble_gap_priv.h
+++ b/net/nimble/host/src/ble_gap_priv.h
@@ -75,7 +75,7 @@ void ble_gap_rx_param_req(struct hci_le_conn_param_req *evt);
 int ble_gap_rx_l2cap_update_req(uint16_t conn_handle,
                                 struct ble_gap_upd_params *params);
 void ble_gap_enc_event(uint16_t conn_handle, int status,
-                            struct ble_gap_sec_state *sec_state);
+                       int security_restored);
 void ble_gap_passkey_event(uint16_t conn_handle,
                            struct ble_gap_passkey_action *passkey_action);
 void ble_gap_notify_event(uint16_t conn_handle, uint16_t attr_handle,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/50d9951e/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index 97ff226..2bed018 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -54,8 +54,6 @@
 /** Procedure timeout; 30 seconds. */
 #define BLE_SM_TIMEOUT_OS_TICKS             (30 * OS_TICKS_PER_SEC)
 
-#define BLE_SM_ENC_STATE_NO_CHANGE          (-1)
-
 STAILQ_HEAD(ble_sm_proc_list, ble_sm_proc);
 
 typedef void ble_sm_rx_fn(uint16_t conn_handle, uint8_t op,
@@ -454,20 +452,22 @@ ble_sm_proc_remove(struct ble_sm_proc *proc,
 }
 
 static void
-ble_sm_sec_state(struct ble_sm_proc *proc,
-                 struct ble_gap_sec_state *out_sec_state,
-                 int enc_enabled)
+ble_sm_update_sec_state(uint16_t conn_handle, int encrypted,
+                        int authenticated, int bonded)
 {
-    out_sec_state->pair_alg = proc->pair_alg;
-    out_sec_state->enc_enabled = enc_enabled;
+    struct ble_hs_conn *conn;
 
-    if (enc_enabled) {
-        out_sec_state->authenticated =
-                (proc->flags & BLE_SM_PROC_F_AUTHENTICATED) ? 1 : 0;
-        out_sec_state->bonded = (proc->flags & BLE_SM_PROC_F_BONDED) ? 1 : 0;
-    } else {
-        out_sec_state->authenticated = 0;
-        out_sec_state->bonded = 0;
+    conn = ble_hs_conn_find(conn_handle);
+    if (conn != NULL) {
+        conn->bhc_sec_state.enc_enabled = encrypted;
+
+        /* Authentication and bonding are never revoked from a secure link */
+        if (authenticated) {
+            conn->bhc_sec_state.authenticated = 1;
+        }
+        if (bonded) {
+            conn->bhc_sec_state.bonded = 1;
+        }
     }
 }
 
@@ -597,18 +597,9 @@ ble_sm_persist_keys(struct ble_sm_proc *proc)
     ble_store_write_mst_sec(&value_sec);
 }
 
-static void
-ble_sm_enc_event(struct ble_sm_proc *proc, int status, int enc_state)
-{
-    struct ble_gap_sec_state sec_state;
-
-    ble_sm_sec_state(proc, &sec_state, enc_state);
-    ble_gap_enc_event(proc->conn_handle, status, &sec_state);
-}
-
 static int
 ble_sm_proc_matches(struct ble_sm_proc *proc, uint16_t conn_handle,
-                          uint8_t state, int is_initiator)
+                    uint8_t state, int is_initiator)
 {
     int proc_is_initiator;
 
@@ -646,7 +637,7 @@ ble_sm_proc_matches(struct ble_sm_proc *proc, uint16_t conn_handle,
  */
 struct ble_sm_proc *
 ble_sm_proc_find(uint16_t conn_handle, uint8_t state, int is_initiator,
-                       struct ble_sm_proc **out_prev)
+                 struct ble_sm_proc **out_prev)
 {
     struct ble_sm_proc *proc;
     struct ble_sm_proc *prev;
@@ -655,8 +646,7 @@ ble_sm_proc_find(uint16_t conn_handle, uint8_t state, int is_initiator,
 
     prev = NULL;
     STAILQ_FOREACH(proc, &ble_sm_procs, next) {
-        if (ble_sm_proc_matches(proc, conn_handle, state,
-                                      is_initiator)) {
+        if (ble_sm_proc_matches(proc, conn_handle, state, is_initiator)) {
             if (out_prev != NULL) {
                 *out_prev = prev;
             }
@@ -807,7 +797,6 @@ ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res)
 {
     struct ble_sm_proc *prev;
     struct ble_sm_proc *proc;
-    struct ble_hs_conn *conn;
     int rm;
 
     rm = 0;
@@ -835,15 +824,6 @@ ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res)
             } else {
                 ble_sm_proc_set_timer(proc);
             }
-
-            if (res->enc_cb && res->enc_state == BLE_SM_ENC_STATE_NO_CHANGE) {
-                conn = ble_hs_conn_find(conn_handle);
-                if (conn != NULL) {
-                    res->enc_state = conn->bhc_sec_state.enc_enabled;
-                } else {
-                    res->enc_state = 0;
-                }
-            }
         }
 
         if (res->sm_err != 0) {
@@ -857,8 +837,8 @@ ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res)
         }
 
         if (res->enc_cb) {
-            BLE_HS_DBG_ASSERT(rm);
-            ble_sm_enc_event(proc, res->app_status, res->enc_state);
+            BLE_HS_DBG_ASSERT(proc == NULL || rm);
+            ble_gap_enc_event(conn_handle, res->app_status, res->restore);
         }
 
         if (res->app_status == 0 &&
@@ -956,100 +936,108 @@ ble_sm_enc_restore_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
     res->app_status = ble_sm_start_encrypt_tx(cmd);
 }
 
-void
-ble_sm_enc_change_rx(struct hci_encrypt_change *evt)
+static void
+ble_sm_enc_event_rx(uint16_t conn_handle, uint8_t evt_status, int encrypted)
 {
     struct ble_sm_result res;
     struct ble_sm_proc *proc;
-    int do_key_exchange = 0;
+    int authenticated;
+    int bonded;
 
     memset(&res, 0, sizeof res);
 
+    /* Assume no change in authenticated and bonded statuses. */
+    authenticated = 0;
+    bonded = 0;
+
     ble_hs_lock();
-    proc = ble_sm_proc_find(evt->connection_handle, BLE_SM_PROC_STATE_NONE, -1,
-                            NULL);
-    if (proc == NULL) {
-        res.app_status = BLE_HS_ENOENT;
-    } else if (proc->state == BLE_SM_PROC_STATE_ENC_START) {
-        res.enc_state = evt->encryption_enabled & 0x01; /* LE bit. */
-        do_key_exchange = proc->flags & BLE_SM_PROC_F_KEY_EXCHANGE;
-        res.app_status = 0;
-    } else if (proc->state == BLE_SM_PROC_STATE_ENC_RESTORE) {
-        res.enc_state = evt->encryption_enabled & 0x01; /* LE bit. */
-        do_key_exchange = 0;
-        res.app_status = 0;
-    } else {
-        proc = NULL;
-        res.app_status = BLE_HS_ENOENT;
-    }
 
-    if (res.app_status == 0) {
-        if (evt->status != 0) {
-            res.app_status = BLE_HS_HCI_ERR(evt->status);
-            res.enc_cb = 1;
-        } else {
-            if (res.enc_state == 1 && do_key_exchange) {
+    proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_NONE, -1, NULL);
+    if (proc != NULL) {
+        switch (proc->state) {
+        case BLE_SM_PROC_STATE_ENC_START:
+            /* We are completing a pairing procedure; keys may need to be
+             * exchanged.
+             */
+            if (evt_status == 0 && proc->flags & BLE_SM_PROC_F_KEY_EXCHANGE) {
+                /* If the responder has any keys to send, it sends them
+                 * first.
+                 */
                 proc->state = BLE_SM_PROC_STATE_KEY_EXCH;
-
-                /* The responder sends its keys first. */
                 if (!(proc->flags & BLE_SM_PROC_F_INITIATOR) ||
                     proc->rx_key_flags == 0) {
 
                     res.execute = 1;
                 }
             } else {
+                /* Failure or no keys to exchange; procedure is complete. */
                 proc->state = BLE_SM_PROC_STATE_NONE;
-                res.enc_cb = 1;
             }
+            if (proc->flags & BLE_SM_PROC_F_AUTHENTICATED) {
+                authenticated = 1;
+            }
+            break;
+
+        case BLE_SM_PROC_STATE_ENC_RESTORE:
+            /* A secure link is being restored via the encryption
+             * procedure.  Keys were exchanged during pairing; they don't
+             * get exchanged again now.  Procedure is complete.
+             */
+            BLE_HS_DBG_ASSERT(!(proc->flags & BLE_SM_PROC_F_KEY_EXCHANGE));
+            proc->state = BLE_SM_PROC_STATE_NONE;
+            if (proc->flags & BLE_SM_PROC_F_AUTHENTICATED) {
+                authenticated = 1;
+            }
+            bonded = 1;
+            res.restore = 1;
+            break;
+
+        default:
+            /* The encryption change event is unexpected.  We take the
+             * controller at its word that the state has changed and we
+             * terminate the procedure.
+             */
+            proc->state = BLE_SM_PROC_STATE_NONE;
+            res.sm_err = BLE_SM_ERR_UNSPECIFIED;
+            break;
         }
     }
 
+    if (evt_status == 0) {
+        /* Set the encrypted state of the connection as indicated in the
+         * event.
+         */
+        ble_sm_update_sec_state(conn_handle, encrypted, authenticated, bonded);
+    }
+
+    /* Unless keys need to be exchanged, notify the application of the security
+     * change.  If key exchange is pending, the application callback is
+     * triggered after exchange completes.
+     */
+    if (proc == NULL || proc->state == BLE_SM_PROC_STATE_NONE) {
+        res.enc_cb = 1;
+        res.app_status = BLE_HS_HCI_ERR(evt_status);
+    }
+
     ble_hs_unlock();
 
-    ble_sm_process_result(evt->connection_handle, &res);
+    ble_sm_process_result(conn_handle, &res);
 }
 
 void
-ble_sm_enc_key_refresh_rx(struct hci_encrypt_key_refresh *evt)
+ble_sm_enc_change_rx(struct hci_encrypt_change *evt)
 {
-    struct ble_sm_result res;
-    struct ble_sm_proc *proc;
-    int do_key_exchange;
-
-    memset(&res, 0, sizeof res);
-
-    ble_hs_lock();
-    proc = ble_sm_proc_find(evt->connection_handle,
-                            BLE_SM_PROC_STATE_ENC_START, -1, NULL);
-    if (proc == NULL) {
-        res.app_status = BLE_HS_ENOENT;
-        do_key_exchange = 0;
-    } else {
-        res.app_status = 0;
-        do_key_exchange = proc->flags & BLE_SM_PROC_F_KEY_EXCHANGE;
-    }
-
-    if (res.app_status == 0) {
-        res.enc_state = BLE_SM_ENC_STATE_NO_CHANGE;
-        if (evt->status != 0) {
-            res.app_status = BLE_HS_HCI_ERR(evt->status);
-            res.enc_cb = 1;
-        } else if (do_key_exchange) {
-            proc->state = BLE_SM_PROC_STATE_KEY_EXCH;
-
-            /* The responder sends its keys first. */
-            if (!(proc->flags & BLE_SM_PROC_F_INITIATOR)) {
-                res.execute = 1;
-            }
-        } else {
-            proc->state = BLE_SM_PROC_STATE_NONE;
-            res.enc_cb = 1;
-        }
-    }
-
-    ble_hs_unlock();
+    /* For encrypted state: read LE-encryption bit; ignore BR/EDR and reserved
+     * bits.
+     */
+    ble_sm_enc_event_rx(evt->connection_handle, evt->status,
+                        evt->encryption_enabled & 0x01);
+}
 
-    ble_sm_process_result(evt->connection_handle, &res);
+void
+ble_sm_enc_key_refresh_rx(struct hci_encrypt_key_refresh *evt)
+{
+    ble_sm_enc_event_rx(evt->connection_handle, evt->status, 1);
 }
 
 /*****************************************************************************
@@ -1176,7 +1164,6 @@ ble_sm_ltk_restore_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
         res->app_status = BLE_HS_ENOENT;
     }
 
-
     if (res->app_status == 0) {
         proc->state = BLE_SM_PROC_STATE_ENC_RESTORE;
     }
@@ -1216,7 +1203,6 @@ ble_sm_ltk_req_rx(struct hci_le_lt_key_req *evt)
         } else {
             proc->conn_handle = evt->connection_handle;
             proc->state = BLE_SM_PROC_STATE_LTK_RESTORE;
-            proc->flags |= BLE_SM_PROC_F_BONDED;
             ble_sm_insert(proc);
         }
     } else if (proc->state == BLE_SM_PROC_STATE_SEC_REQ) {
@@ -1225,7 +1211,6 @@ ble_sm_ltk_req_rx(struct hci_le_lt_key_req *evt)
          */
         bonding = 1;
         proc->state = BLE_SM_PROC_STATE_LTK_RESTORE;
-        proc->flags |= BLE_SM_PROC_F_BONDED;
     } else if (proc->state == BLE_SM_PROC_STATE_LTK_START) {
         /* Short-term key pairing just completed.  Send the short term key to
          * the controller.
@@ -1715,6 +1700,19 @@ ble_sm_sec_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
  *****************************************************************************/
 
 static void
+ble_sm_key_exch_success(struct ble_sm_proc *proc, struct ble_sm_result *res)
+{
+    /* The procedure is now complete.  Update connection bonded state and
+     * terminate procedure.
+     */
+    ble_sm_update_sec_state(proc->conn_handle, 1, 0, 1);
+    proc->state = BLE_SM_PROC_STATE_NONE;
+
+    res->app_status = 0;
+    res->enc_cb = 1;
+}
+
+static void
 ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
                      void *arg)
 {
@@ -1812,10 +1810,7 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
 
     if (proc->flags & BLE_SM_PROC_F_INITIATOR) {
         /* The procedure is now complete. */
-        proc->flags |= BLE_SM_PROC_F_BONDED;
-        proc->state = BLE_SM_PROC_STATE_NONE;
-        res->enc_state = 1;
-        res->enc_cb = 1;
+        ble_sm_key_exch_success(proc, res);
     }
 
     return;
@@ -1838,10 +1833,7 @@ ble_sm_key_rxed(struct ble_sm_proc *proc, struct ble_sm_result *res)
         if (proc->flags & BLE_SM_PROC_F_INITIATOR) {
             res->execute = 1;
         } else {
-            proc->flags |= BLE_SM_PROC_F_BONDED;
-            proc->state = BLE_SM_PROC_STATE_NONE;
-            res->enc_state = 1;
-            res->enc_cb = 1;
+            ble_sm_key_exch_success(proc, res);
         }
     }
 }
@@ -2071,7 +2063,7 @@ ble_sm_heartbeat(void)
      * procedures without reconnect.
      */
     while ((proc = STAILQ_FIRST(&exp_list)) != NULL) {
-        ble_sm_enc_event(proc, BLE_HS_ETIMEOUT, BLE_SM_ENC_STATE_NO_CHANGE);
+        ble_gap_enc_event(proc->conn_handle, BLE_HS_ETIMEOUT, 0);
 
         STAILQ_REMOVE_HEAD(&exp_list, next);
         ble_sm_proc_free(proc);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/50d9951e/net/nimble/host/src/ble_sm_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_priv.h b/net/nimble/host/src/ble_sm_priv.h
index aeb6d19..11de110 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -218,8 +218,7 @@ struct ble_sm_dhkey_check {
 #define BLE_SM_PROC_F_ADVANCE_ON_IO         0x04
 #define BLE_SM_PROC_F_AUTHENTICATED         0x08
 #define BLE_SM_PROC_F_KEY_EXCHANGE          0x10
-#define BLE_SM_PROC_F_BONDED                0x20
-#define BLE_SM_PROC_F_SC                    0x40
+#define BLE_SM_PROC_F_SC                    0x20
 
 #define BLE_SM_KE_F_ENC_INFO                0x01
 #define BLE_SM_KE_F_MASTER_ID               0x02
@@ -286,9 +285,7 @@ struct ble_sm_result {
     unsigned execute:1;
     unsigned enc_cb:1;
     unsigned persist_keys:1;
-
-    /* 0=disabled; 1=enabled; -1=no-change. */
-    uint8_t enc_state;
+    unsigned restore:1;
 };
 
 #ifdef BLE_HS_DEBUG

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/50d9951e/net/nimble/host/src/test/ble_sm_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test.c b/net/nimble/host/src/test/ble_sm_test.c
index 71a5fca..42a597e 100644
--- a/net/nimble/host/src/test/ble_sm_test.c
+++ b/net/nimble/host/src/test/ble_sm_test.c
@@ -901,13 +901,10 @@ ble_sm_test_util_us_lgcy_good(
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg == params->pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
     TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg ==
-                conn->bhc_sec_state.pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
                 conn->bhc_sec_state.enc_enabled);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
@@ -1038,14 +1035,10 @@ ble_sm_test_util_peer_lgcy_fail_confirm(
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status ==
                 BLE_HS_SM_US_ERR(BLE_SM_ERR_CONFIRM_MISMATCH));
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg ==
-                BLE_SM_PAIR_ALG_JW);
     TEST_ASSERT(!ble_sm_test_sec_state.enc_enabled);
     TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg ==
-                conn->bhc_sec_state.pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
                 conn->bhc_sec_state.enc_enabled);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
@@ -1171,14 +1164,11 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg == params->pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 params->authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg ==
-                conn->bhc_sec_state.pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
                 conn->bhc_sec_state.enc_enabled);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
@@ -1503,14 +1493,11 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg == params->pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 params->authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg ==
-                conn->bhc_sec_state.pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
                 conn->bhc_sec_state.enc_enabled);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
@@ -1681,14 +1668,11 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg == params->pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 params->authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.pair_alg ==
-                conn->bhc_sec_state.pair_alg);
     TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
                 conn->bhc_sec_state.enc_enabled);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==


[5/7] incubator-mynewt-core git commit: BLE Host - Don't kill SM proc when bad io injected

Posted by cc...@apache.org.
BLE Host - Don't kill SM proc when bad io injected


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

Branch: refs/heads/upf54
Commit: 41b5a783dcf505c0c4568dafc158080468b1283e
Parents: f4673d2
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jun 8 19:19:28 2016 +0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Jun 8 19:19:28 2016 +0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_sm.h  |  1 +
 net/nimble/host/src/ble_sm.c           | 32 +++++++----
 net/nimble/host/src/test/ble_sm_test.c | 86 ++++++++++++++++++++++++++++-
 3 files changed, 106 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/41b5a783/net/nimble/host/include/host/ble_sm.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_sm.h b/net/nimble/host/include/host/ble_sm.h
index cc3b779..51052ea 100644
--- a/net/nimble/host/include/host/ble_sm.h
+++ b/net/nimble/host/include/host/ble_sm.h
@@ -80,6 +80,7 @@
 #define BLE_SM_IOACT_INPUT                      2
 #define BLE_SM_IOACT_DISP                       3
 #define BLE_SM_IOACT_NUMCMP                     4
+#define BLE_SM_IOACT_MAX_PLUS_ONE               5
 
 struct ble_sm_io {
     uint8_t action;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/41b5a783/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index 49e7d53..4c93110 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -2264,6 +2264,7 @@ ble_sm_inject_io(uint16_t conn_handle, struct ble_sm_io *pkey)
     struct ble_sm_result res;
     struct ble_sm_proc *proc;
     struct ble_sm_proc *prev;
+    int rc;
 
     memset(&res, 0, sizeof res);
 
@@ -2272,19 +2273,19 @@ ble_sm_inject_io(uint16_t conn_handle, struct ble_sm_io *pkey)
     proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_NONE, -1, &prev);
 
     if (proc == NULL) {
-        res.app_status = BLE_HS_ENOENT;
+        rc = BLE_HS_ENOENT;
     } else if (proc->flags & BLE_SM_PROC_F_IO_INJECTED) {
-        res.app_status = BLE_HS_EALREADY;
+        rc = BLE_HS_EALREADY;
     } else if (pkey->action != ble_sm_io_action(proc)) {
-        /* Response doesn't match what we asked for. */
-        res.app_status = BLE_HS_EINVAL;
-        res.sm_err = BLE_SM_ERR_PASSKEY;
+        /* Application provided incorrect IO type. */
+        rc = BLE_HS_EINVAL;
     } else if (ble_sm_ioact_state(pkey->action) != proc->state) {
         /* Procedure is not ready for user input. */
-        res.app_status = BLE_HS_EINVAL;
-        res.sm_err = BLE_SM_ERR_UNSPECIFIED;
+        rc = BLE_HS_EINVAL;
     } else {
-        /* Add the passkey range. */
+        /* Assume valid input. */
+        rc = 0;
+
         switch (pkey->action) {
         case BLE_SM_IOACT_OOB:
             if (pkey->oob == NULL) {
@@ -2304,8 +2305,7 @@ ble_sm_inject_io(uint16_t conn_handle, struct ble_sm_io *pkey)
         case BLE_SM_IOACT_INPUT:
         case BLE_SM_IOACT_DISP:
             if (pkey->passkey > 999999) {
-                res.app_status = BLE_HS_EINVAL;
-                res.sm_err = BLE_SM_ERR_PASSKEY;
+                rc = BLE_HS_EINVAL;
             } else {
                 proc->flags |= BLE_SM_PROC_F_IO_INJECTED;
                 memset(proc->tk, 0, 16);
@@ -2336,13 +2336,21 @@ ble_sm_inject_io(uint16_t conn_handle, struct ble_sm_io *pkey)
             break;
 
         default:
-            res.sm_err = BLE_SM_ERR_UNSPECIFIED;
-            res.app_status = BLE_HS_EINVAL;
+            BLE_HS_DBG_ASSERT(0);
+            rc = BLE_HS_EINVAL;
+            break;
         }
     }
 
     ble_hs_unlock();
 
+    /* If application provided invalid input, return error without modifying
+     * SMP state.
+     */
+    if (rc != 0) {
+        return rc;
+    }
+
     ble_sm_process_result(conn_handle, &res);
     return res.app_status;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/41b5a783/net/nimble/host/src/test/ble_sm_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test.c b/net/nimble/host/src/test/ble_sm_test.c
index 550c151..c4a8f10 100644
--- a/net/nimble/host/src/test/ble_sm_test.c
+++ b/net/nimble/host/src/test/ble_sm_test.c
@@ -642,6 +642,41 @@ ble_sm_test_util_io_inject(struct ble_sm_test_passkey_info *passkey_info,
 }
 
 static void
+ble_sm_test_util_io_inject_bad(uint16_t conn_handle, uint8_t correct_io_act)
+{
+    struct ble_sm_proc *proc;
+    struct ble_sm_io io;
+    uint8_t io_sm_state;
+    int rc;
+    int i;
+
+    /* Lock mutex to prevent thread-safety assert from failing. */
+    ble_hs_lock();
+    proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_NONE, -1, NULL);
+    ble_hs_unlock();
+
+    TEST_ASSERT_FATAL(proc != NULL);
+
+    io_sm_state = ble_sm_ioact_state(correct_io_act);
+
+    for (i = 1; i < BLE_SM_IOACT_MAX_PLUS_ONE; i++) {
+        if (io_sm_state != proc->state  ||
+            i != correct_io_act         ||
+            proc->flags & BLE_SM_PROC_F_IO_INJECTED) {
+
+            io.action = i;
+            rc = ble_sm_inject_io(conn_handle, &io);
+
+            if (proc->flags & BLE_SM_PROC_F_IO_INJECTED) {
+                TEST_ASSERT(rc == BLE_HS_EALREADY);
+            } else {
+                TEST_ASSERT(rc == BLE_HS_EINVAL);
+            }
+        }
+    }
+}
+
+static void
 ble_sm_test_util_io_check_pre(struct ble_sm_test_passkey_info *passkey_info,
                               uint8_t cur_sm_state)
 {
@@ -852,33 +887,39 @@ ble_sm_test_util_us_lgcy_good(
     ble_sm_test_util_verify_tx_pair_req(&params->pair_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a pair response from the peer. */
     ble_sm_test_util_rx_pair_rsp(2, &params->pair_rsp, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected pair confirm. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_confirm(&params->confirm_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a pair confirm from the peer. */
     ble_sm_test_util_rx_confirm(2, &params->confirm_rsp);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected pair random. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_random(&params->random_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a pair random from the peer. */
     ble_sm_test_util_rx_random(2, &params->random_rsp, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure keys are distributed, if necessary. */
     if (params->has_enc_info_req) {
@@ -888,9 +929,10 @@ ble_sm_test_util_us_lgcy_good(
     /* Ensure we sent the expected start encryption command. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_start_enc(2, params->r, params->ediv,
-                                               params->stk);
+                                         params->stk);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive an encryption changed event. */
     ble_sm_test_util_rx_enc_change(2, 0, 1);
@@ -1005,20 +1047,24 @@ ble_sm_test_util_peer_lgcy_fail_confirm(
     /* Receive a pair request from the peer. */
     ble_sm_test_util_rx_pair_req(2, pair_req, 0);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
 
     /* Ensure we sent the expected pair response. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_rsp(pair_rsp);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
 
     /* Receive a pair confirm from the peer. */
     ble_sm_test_util_rx_confirm(2, confirm_req);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
 
     /* Ensure we sent the expected pair confirm. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_confirm(confirm_rsp);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
 
     /* Receive a pair random from the peer. */
     ble_sm_test_util_rx_random(
@@ -1105,12 +1151,14 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
     ble_sm_test_util_rx_pair_req(2, &params->pair_req, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected pair response. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_rsp(&params->pair_rsp);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     ble_sm_test_util_io_check_pre(&params->passkey_info,
                                   BLE_SM_PROC_STATE_CONFIRM);
@@ -1119,6 +1167,7 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
     ble_sm_test_util_rx_confirm(2, &params->confirm_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     ble_sm_test_util_io_check_post(&params->passkey_info,
                                    BLE_SM_PROC_STATE_CONFIRM);
@@ -1128,28 +1177,33 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
     ble_sm_test_util_verify_tx_pair_confirm(&params->confirm_rsp);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a pair random from the peer. */
     ble_sm_test_util_rx_random(2, &params->random_req, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected pair random. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_random(&params->random_rsp);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a long term key request from the controller. */
     ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
     ble_sm_test_util_rx_lt_key_req(2, params->r, params->ediv);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected long term key request reply command. */
     ble_sm_test_util_verify_tx_lt_key_req_reply(2, params->stk);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive an encryption changed event. */
     ble_sm_test_util_rx_enc_change(2, 0, 1);
@@ -1256,11 +1310,13 @@ ble_sm_test_util_peer_bonding_good(int send_enc_req, uint8_t *ltk,
 
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
 
     /* Ensure we sent the expected long term key request reply command. */
     ble_sm_test_util_verify_tx_lt_key_req_reply(2, value_sec.ltk);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
 
     /* Receive an encryption changed event. */
     ble_sm_test_util_rx_enc_change(2, 0, 1);
@@ -1386,23 +1442,27 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
     ble_sm_test_util_rx_pair_req(2, &params->pair_req, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected pair response. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_rsp(&params->pair_rsp);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a public key from the peer. */
     ble_sm_test_util_rx_public_key(2, &params->public_key_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected public key. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_public_key(&params->public_key_rsp);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     switch (params->pair_alg) {
     case BLE_SM_PAIR_ALG_PASSKEY:
@@ -1425,6 +1485,8 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
             ble_sm_test_util_rx_confirm(2, params->confirm_req + i);
             TEST_ASSERT(!conn->bhc_sec_state.encrypted);
             TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+            ble_sm_test_util_io_inject_bad(
+                2, params->passkey_info.passkey.action);
 
             if (i < num_iters - 1) {
                 ble_sm_dbg_set_next_pair_rand(
@@ -1442,17 +1504,20 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
         ble_sm_test_util_verify_tx_pair_confirm(params->confirm_rsp + i);
         TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
         /* Receive a pair random from the peer. */
         ble_sm_test_util_rx_random(2, params->random_req + i, 0);
         TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
         /* Ensure we sent the expected pair random. */
         ble_hs_test_util_tx_all();
         ble_sm_test_util_verify_tx_pair_random(params->random_rsp + i);
         TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     }
 
@@ -1463,6 +1528,7 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
     ble_sm_test_util_rx_dhkey_check(2, &params->dhkey_check_req, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     ble_sm_test_util_io_check_post(&params->passkey_info,
                                    BLE_SM_PROC_STATE_DHKEY_CHECK);
@@ -1472,17 +1538,20 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
     ble_sm_test_util_verify_tx_dhkey_check(&params->dhkey_check_rsp);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a long term key request from the controller. */
     ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
     ble_sm_test_util_rx_lt_key_req(2, 0, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected long term key request reply command. */
     ble_sm_test_util_verify_tx_lt_key_req_reply(2, params->ltk);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive an encryption changed event. */
     ble_sm_test_util_rx_enc_change(2, 0, 1);
@@ -1576,22 +1645,26 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
     ble_sm_test_util_verify_tx_pair_req(&params->pair_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a pair response from the peer. */
     ble_sm_test_util_rx_pair_rsp(2, &params->pair_rsp, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected public key. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_public_key(&params->public_key_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a public key from the peer. */
     ble_sm_test_util_rx_public_key(2, &params->public_key_rsp);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     switch (params->pair_alg) {
     case BLE_SM_PAIR_ALG_PASSKEY:
@@ -1620,23 +1693,28 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
             ble_sm_test_util_verify_tx_pair_confirm(params->confirm_req + i);
             TEST_ASSERT(!conn->bhc_sec_state.encrypted);
             TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+            ble_sm_test_util_io_inject_bad(
+                2, params->passkey_info.passkey.action);
         }
 
         /* Receive a pair confirm from the peer. */
         ble_sm_test_util_rx_confirm(2, params->confirm_rsp + i);
         TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
         /* Ensure we sent the expected pair random. */
         ble_hs_test_util_tx_all();
         ble_sm_test_util_verify_tx_pair_random(params->random_req + i);
         TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
         /* Receive a pair random from the peer. */
         ble_sm_test_util_rx_random(2, params->random_rsp + i, 0);
         TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
     }
 
     ble_sm_test_util_io_inject(&params->passkey_info,
@@ -1647,17 +1725,20 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
     ble_sm_test_util_verify_tx_dhkey_check(&params->dhkey_check_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a dhkey check from the peer. */
     ble_sm_test_util_rx_dhkey_check(2, &params->dhkey_check_rsp, 0);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Ensure we sent the expected start encryption command. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_start_enc(2, 0, 0, params->ltk);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive an encryption changed event. */
     ble_sm_test_util_rx_enc_change(2, 0, 1);
@@ -1719,6 +1800,7 @@ ble_sm_test_util_us_fail_inval(
     ble_sm_test_util_verify_tx_pair_req(&params->pair_req);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
 
     /* Receive a pair response from the peer. */
     ble_sm_test_util_rx_pair_rsp(
@@ -1805,6 +1887,7 @@ ble_sm_test_util_us_bonding_good(int send_enc_req, uint8_t *ltk,
     ble_sm_test_util_verify_tx_start_enc(2, rand_num, ediv, ltk);
     TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
 
     /* Receive an encryption changed event. */
     ble_sm_test_util_rx_enc_change(2, 0, 1);
@@ -1945,6 +2028,7 @@ TEST_CASE(ble_sm_test_case_conn_broken)
     rc = ble_hs_test_util_security_initiate(2, 0);
     TEST_ASSERT_FATAL(rc == 0);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
 
     /* Terminate the connection. */
     disconn_evt.connection_handle = 2;


[6/7] incubator-mynewt-core git commit: BLE Host - Clear queue on gatt-long-write failure.

Posted by cc...@apache.org.
BLE Host - Clear queue on gatt-long-write failure.


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

Branch: refs/heads/upf54
Commit: 1aa4e461d564ba48f93bb32d5784ec7a59d7e260
Parents: 41b5a78
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jun 8 19:48:22 2016 +0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Jun 8 19:48:22 2016 +0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_gattc.c                | 12 +++++
 net/nimble/host/src/test/ble_gatt_write_test.c | 53 +++++++++++++++++++++
 net/nimble/host/src/test/ble_hs_test_util.c    | 14 ++++++
 net/nimble/host/src/test/ble_hs_test_util.h    |  1 +
 4 files changed, 80 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1aa4e461/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gattc.c b/net/nimble/host/src/ble_gattc.c
index 876abf5..1796bcc 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -3175,8 +3175,20 @@ static void
 ble_gattc_write_long_err(struct ble_gattc_proc *proc, int status,
                          uint16_t att_handle)
 {
+    struct ble_att_exec_write_req exec_req;
+
     ble_gattc_dbg_assert_proc_not_inserted(proc);
     ble_gattc_write_long_cb(proc, status, att_handle);
+
+    /* If we have successfully queued any data, and the failure occurred before
+     * we could send the execute write command, then erase all queued data.
+     */
+    if (proc->write_long.attr.offset > 0 &&
+        proc->write_long.attr.offset < proc->write_long.attr.value_len) {
+
+        exec_req.baeq_flags = 0;
+        ble_att_clt_tx_exec_write(proc->conn_handle, &exec_req);
+    }
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1aa4e461/net/nimble/host/src/test/ble_gatt_write_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_write_test.c b/net/nimble/host/src/test/ble_gatt_write_test.c
index f12ea77..56e88d3 100644
--- a/net/nimble/host/src/test/ble_gatt_write_test.c
+++ b/net/nimble/host/src/test/ble_gatt_write_test.c
@@ -189,6 +189,7 @@ ble_gatt_write_test_misc_long_bad(int attr_len,
     while (off < attr_len) {
         /* Send the pending ATT Prep Write Command. */
         ble_hs_test_util_tx_all();
+        TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() != NULL);
 
         /* Receive Prep Write response. */
         len = BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
@@ -498,6 +499,57 @@ TEST_CASE(ble_gatt_write_test_reliable_good)
         } }));
 }
 
+TEST_CASE(ble_gatt_write_test_long_queue_full)
+{
+    int off;
+    int len;
+    int rc;
+    int i;
+
+    ble_gatt_write_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    rc = ble_gattc_write_long(2, 100, ble_gatt_write_test_attr_value,
+                              128, ble_gatt_write_test_cb_good, NULL);
+    TEST_ASSERT(rc == 0);
+
+    off = 0;
+    for (i = 0; i < 2; i++) {
+        /* Verify prep write request was sent. */
+        ble_hs_test_util_tx_all();
+        TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() != NULL);
+
+        /* Receive Prep Write response. */
+        len = BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        ble_gatt_write_test_rx_prep_rsp(
+            2, 100, off, ble_gatt_write_test_attr_value + off, len);
+
+        /* Verify callback hasn't gotten called. */
+        TEST_ASSERT(!ble_gatt_write_test_cb_called);
+
+        off += len;
+    }
+
+    /* Verify prep write request was sent. */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() != NULL);
+
+    /* Receive queue full error. */
+    ble_hs_test_util_rx_att_err_rsp(2, BLE_ATT_OP_PREP_WRITE_REQ,
+                                    BLE_ATT_ERR_PREPARE_QUEUE_FULL, 100);
+
+    /* Verify callback was called. */
+    TEST_ASSERT(ble_gatt_write_test_cb_called);
+    TEST_ASSERT(ble_gatt_write_test_error.status ==
+                BLE_HS_ATT_ERR(BLE_ATT_ERR_PREPARE_QUEUE_FULL));
+    TEST_ASSERT(ble_gatt_write_test_error.att_handle == 100);
+
+    /* Verify clear queue command got sent. */
+    ble_hs_test_util_verify_tx_exec_write(0);
+}
+
 TEST_SUITE(ble_gatt_write_test_suite)
 {
     ble_gatt_write_test_no_rsp();
@@ -507,6 +559,7 @@ TEST_SUITE(ble_gatt_write_test_suite)
     ble_gatt_write_test_long_bad_offset();
     ble_gatt_write_test_long_bad_value();
     ble_gatt_write_test_long_bad_length();
+    ble_gatt_write_test_long_queue_full();
     ble_gatt_write_test_reliable_good();
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1aa4e461/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 5a8cac1..8f99ddd 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -776,6 +776,20 @@ ble_hs_test_util_set_public_addr(uint8_t *addr)
     ble_hs_priv_update_identity(addr);
 }
 
+void
+ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags)
+{
+    struct ble_att_exec_write_req req;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+    TEST_ASSERT(om->om_len == BLE_ATT_EXEC_WRITE_REQ_SZ);
+
+    ble_att_exec_write_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.baeq_flags == expected_flags);
+}
 
 void
 ble_hs_test_util_init(void)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1aa4e461/net/nimble/host/src/test/ble_hs_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.h b/net/nimble/host/src/test/ble_hs_test_util.h
index d53a7ea..55dcb21 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ b/net/nimble/host/src/test/ble_hs_test_util.h
@@ -102,6 +102,7 @@ uint8_t *ble_hs_test_util_verify_tx_hci(uint8_t ogf, uint16_t ocf,
                                         uint8_t *out_param_len);
 void ble_hs_test_util_tx_all(void);
 void ble_hs_test_util_set_public_addr(uint8_t *addr);
+void ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags);
 void ble_hs_test_util_init(void);
 
 #endif


[3/7] incubator-mynewt-core git commit: BLE Host - Nack unexpected LTK reqs from ctlr.

Posted by cc...@apache.org.
BLE Host - Nack unexpected LTK reqs from ctlr.


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

Branch: refs/heads/upf54
Commit: a33be9194d61c6d060264bcde3bc249b3152d0a5
Parents: a78d726
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jun 8 18:36:44 2016 +0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Jun 8 18:36:44 2016 +0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_sm.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a33be919/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index d9c4fd5..49e7d53 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -1176,11 +1176,11 @@ ble_sm_ltk_req_rx(struct hci_le_lt_key_req *evt)
     struct ble_sm_result res;
     struct ble_sm_proc *proc;
     struct ble_hs_conn *conn;
-    int store_rc;
-    int bonding;
     uint8_t peer_addr[6];
     uint8_t *peer_id_addr;
     uint8_t peer_addr_type;
+    int store_rc;
+    int restore;
 
     /* Silence gcc warning. */
     peer_addr_type = BLE_STORE_ADDR_TYPE_NONE;
@@ -1192,11 +1192,11 @@ ble_sm_ltk_req_rx(struct hci_le_lt_key_req *evt)
                             0, NULL);
     if (proc == NULL) {
         /* The peer is attempting to restore a encrypted connection via the
-         * encryption procedure (bonding).  Create a proc entry to indicate
-         * that security establishment is in progress and execute the procedure
-         * after the mutex gets unlocked.
+         * encryption procedure.  Create a proc entry to indicate that security
+         * establishment is in progress and execute the procedure after the
+         * mutex gets unlocked.
          */
-        bonding = 1;
+        restore = 1;
         proc = ble_sm_proc_alloc();
         if (proc == NULL) {
             res.app_status = BLE_HS_ENOMEM;
@@ -1204,25 +1204,30 @@ ble_sm_ltk_req_rx(struct hci_le_lt_key_req *evt)
             proc->conn_handle = evt->connection_handle;
             proc->state = BLE_SM_PROC_STATE_LTK_RESTORE;
             ble_sm_insert(proc);
+
+            res.execute = 1;
         }
     } else if (proc->state == BLE_SM_PROC_STATE_SEC_REQ) {
         /* Same as above, except we solicited the encryption procedure by
          * sending a security request.
          */
-        bonding = 1;
+        restore = 1;
         proc->state = BLE_SM_PROC_STATE_LTK_RESTORE;
+        res.execute = 1;
     } else if (proc->state == BLE_SM_PROC_STATE_LTK_START) {
-        /* Short-term key pairing just completed.  Send the short term key to
-         * the controller.
+        /* Legacy pairing just completed.  Send the short term key to the
+         * controller.
          */
-        bonding = 0;
+        restore = 0;
+        res.execute = 1;
     } else {
-        /* The request is unexpected.  Quietly ignore it. */
+        /* The request is unexpected; nack and forget. */
+        restore = 0;
+        ble_sm_ltk_req_neg_reply_tx(evt->connection_handle);
         proc = NULL;
-        bonding = 0;
     }
 
-    if (bonding) {
+    if (restore) {
         conn = ble_hs_conn_find(evt->connection_handle);
         if (conn == NULL) {
             res.app_status = BLE_HS_ENOTCONN;
@@ -1241,15 +1246,17 @@ ble_sm_ltk_req_rx(struct hci_le_lt_key_req *evt)
     }
 
     if (res.app_status == 0) {
-        if (bonding) {
+        if (restore) {
             store_rc = ble_sm_retrieve_ltk(evt, peer_addr_type, peer_addr,
                                            &value_sec);
             if (store_rc == 0) {
+                /* Send the key to the controller. */
                 res.state_arg = &value_sec;
+            } else {
+                /* Send a nack to the controller. */
+                res.state_arg = NULL;
             }
         }
-
-        res.execute = 1;
     }
 
     ble_sm_process_result(evt->connection_handle, &res);


[2/7] incubator-mynewt-core git commit: BLE Host - Rename sec_state.enc_enabled->encrypted

Posted by cc...@apache.org.
BLE Host - Rename sec_state.enc_enabled->encrypted


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

Branch: refs/heads/upf54
Commit: a78d726c97de1c860483409988076485a60e8589
Parents: 50d9951
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jun 8 18:21:30 2016 +0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Jun 8 18:21:30 2016 +0800

----------------------------------------------------------------------
 apps/bleprph/src/main.c                         |   2 +-
 apps/bletiny/src/misc.c                         |   4 +-
 net/nimble/host/include/host/ble_gap.h          |   2 +-
 net/nimble/host/src/ble_att_svr.c               |   2 +-
 net/nimble/host/src/ble_sm.c                    |   2 +-
 .../host/src/test/ble_gatts_notify_test.c       |   4 +-
 net/nimble/host/src/test/ble_sm_test.c          | 162 +++++++++----------
 7 files changed, 89 insertions(+), 89 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a78d726c/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index e44e58a..f2d0c09 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -104,7 +104,7 @@ bleprph_print_conn_desc(struct ble_gap_conn_desc *desc)
                 desc->conn_itvl,
                 desc->conn_latency,
                 desc->supervision_timeout,
-                desc->sec_state.enc_enabled,
+                desc->sec_state.encrypted,
                 desc->sec_state.authenticated);
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a78d726c/apps/bletiny/src/misc.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/misc.c b/apps/bletiny/src/misc.c
index 4e7c97d..b0c3ddb 100644
--- a/apps/bletiny/src/misc.c
+++ b/apps/bletiny/src/misc.c
@@ -106,10 +106,10 @@ print_conn_desc(struct ble_gap_conn_desc *desc)
                    desc->peer_id_addr_type);
     print_addr(desc->peer_id_addr);
     console_printf(" conn_itvl=%d conn_latency=%d supervision_timeout=%d "
-                   "enc_enabled=%d authenticated=%d bonded=%d\n",
+                   "encrypted=%d authenticated=%d bonded=%d\n",
                    desc->conn_itvl, desc->conn_latency,
                    desc->supervision_timeout,
-                   desc->sec_state.enc_enabled,
+                   desc->sec_state.encrypted,
                    desc->sec_state.authenticated,
                    desc->sec_state.bonded);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a78d726c/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 54a6d4e..df9ae64 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -102,7 +102,7 @@ struct hci_adv_params;
 #define BLE_GAP_EVENT_NOTIFY                12
 
 struct ble_gap_sec_state {
-    unsigned enc_enabled:1;
+    unsigned encrypted:1;
     unsigned authenticated:1;
     unsigned bonded:1;
 };

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a78d726c/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_svr.c b/net/nimble/host/src/ble_att_svr.c
index 8508240..2e4e137 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -261,7 +261,7 @@ ble_att_svr_check_security(uint16_t conn_handle, int is_read,
         return rc;
     }
 
-    if (enc && !sec_state.enc_enabled) {
+    if (enc && !sec_state.encrypted) {
         /* XXX: Check security database; if required key present, respond with
          * insufficient encryption error code.
          */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a78d726c/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index 2bed018..d9c4fd5 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -459,7 +459,7 @@ ble_sm_update_sec_state(uint16_t conn_handle, int encrypted,
 
     conn = ble_hs_conn_find(conn_handle);
     if (conn != NULL) {
-        conn->bhc_sec_state.enc_enabled = encrypted;
+        conn->bhc_sec_state.encrypted = encrypted;
 
         /* Authentication and bonding are never revoked from a secure link */
         if (authenticated) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a78d726c/net/nimble/host/src/test/ble_gatts_notify_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatts_notify_test.c b/net/nimble/host/src/test/ble_gatts_notify_test.c
index 8c74024..6a0f218 100644
--- a/net/nimble/host/src/test/ble_gatts_notify_test.c
+++ b/net/nimble/host/src/test/ble_gatts_notify_test.c
@@ -149,7 +149,7 @@ ble_gatts_notify_test_misc_init(uint16_t *out_conn_handle, int bonding,
         ble_hs_lock();
         conn = ble_hs_conn_find(2);
         TEST_ASSERT_FATAL(conn != NULL);
-        conn->bhc_sec_state.enc_enabled = 1;
+        conn->bhc_sec_state.encrypted = 1;
         conn->bhc_sec_state.authenticated = 1;
         conn->bhc_sec_state.bonded = 1;
         ble_hs_unlock();
@@ -197,7 +197,7 @@ ble_gatts_restore_bonding(uint16_t conn_handle)
     ble_hs_lock();
     conn = ble_hs_conn_find(conn_handle);
     TEST_ASSERT_FATAL(conn != NULL);
-    conn->bhc_sec_state.enc_enabled = 1;
+    conn->bhc_sec_state.encrypted = 1;
     conn->bhc_sec_state.authenticated = 1;
     conn->bhc_sec_state.bonded = 1;
     ble_hs_unlock();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a78d726c/net/nimble/host/src/test/ble_sm_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test.c b/net/nimble/host/src/test/ble_sm_test.c
index 42a597e..550c151 100644
--- a/net/nimble/host/src/test/ble_sm_test.c
+++ b/net/nimble/host/src/test/ble_sm_test.c
@@ -834,7 +834,7 @@ ble_sm_test_util_us_lgcy_good(
     TEST_ASSERT_FATAL(conn != NULL);
     ble_hs_unlock();
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     ble_hs_test_util_set_ack(
@@ -850,34 +850,34 @@ ble_sm_test_util_us_lgcy_good(
     /* Ensure we sent the expected pair request. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_req(&params->pair_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a pair response from the peer. */
     ble_sm_test_util_rx_pair_rsp(2, &params->pair_rsp, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected pair confirm. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_confirm(&params->confirm_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a pair confirm from the peer. */
     ble_sm_test_util_rx_confirm(2, &params->confirm_rsp);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected pair random. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_random(&params->random_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a pair random from the peer. */
     ble_sm_test_util_rx_random(2, &params->random_rsp, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure keys are distributed, if necessary. */
@@ -889,7 +889,7 @@ ble_sm_test_util_us_lgcy_good(
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_start_enc(2, params->r, params->ediv,
                                                params->stk);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive an encryption changed event. */
@@ -901,12 +901,12 @@ ble_sm_test_util_us_lgcy_good(
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
-                conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 conn->bhc_sec_state.authenticated);
 
@@ -943,19 +943,19 @@ ble_sm_test_util_peer_fail_inval(
         conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
     }
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Receive a pair request from the peer. */
     ble_sm_test_util_rx_pair_req(2, pair_req,
                                  BLE_HS_SM_US_ERR(pair_fail->reason));
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Ensure we sent the expected pair fail. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_fail(pair_fail);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Verify that security callback was not executed. */
@@ -963,7 +963,7 @@ ble_sm_test_util_peer_fail_inval(
     TEST_ASSERT(ble_sm_test_gap_status == -1);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(!conn->bhc_sec_state.authenticated);
 }
 
@@ -1035,12 +1035,12 @@ ble_sm_test_util_peer_lgcy_fail_confirm(
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status ==
                 BLE_HS_SM_US_ERR(BLE_SM_ERR_CONFIRM_MISMATCH));
-    TEST_ASSERT(!ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(!ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
-                conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 conn->bhc_sec_state.authenticated);
 }
@@ -1090,7 +1090,7 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
     /* Peer is the initiator so we must be the slave. */
     conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     if (params->has_sec_req) {
@@ -1103,13 +1103,13 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
 
     /* Receive a pair request from the peer. */
     ble_sm_test_util_rx_pair_req(2, &params->pair_req, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected pair response. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_rsp(&params->pair_rsp);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     ble_sm_test_util_io_check_pre(&params->passkey_info,
@@ -1117,7 +1117,7 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
 
     /* Receive a pair confirm from the peer. */
     ble_sm_test_util_rx_confirm(2, &params->confirm_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     ble_sm_test_util_io_check_post(&params->passkey_info,
@@ -1126,29 +1126,29 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
     /* Ensure we sent the expected pair confirm. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_confirm(&params->confirm_rsp);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a pair random from the peer. */
     ble_sm_test_util_rx_random(2, &params->random_req, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected pair random. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_random(&params->random_rsp);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a long term key request from the controller. */
     ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
     ble_sm_test_util_rx_lt_key_req(2, params->r, params->ediv);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected long term key request reply command. */
     ble_sm_test_util_verify_tx_lt_key_req_reply(2, params->stk);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive an encryption changed event. */
@@ -1164,13 +1164,13 @@ ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_lgcy_params *params)
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 params->authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
-                conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 conn->bhc_sec_state.authenticated);
 
@@ -1219,7 +1219,7 @@ ble_sm_test_util_peer_bonding_good(int send_enc_req, uint8_t *ltk,
     conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
     ble_hs_unlock();
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Populate the SM database with an LTK for this peer. */
@@ -1243,7 +1243,7 @@ ble_sm_test_util_peer_bonding_good(int send_enc_req, uint8_t *ltk,
     /* Receive a long term key request from the controller. */
     ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
     ble_sm_test_util_rx_lt_key_req(2, rand_num, ediv);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
 
     /* Ensure the LTK request event got sent to the application. */
     TEST_ASSERT(ble_sm_test_store_obj_type ==
@@ -1254,12 +1254,12 @@ ble_sm_test_util_peer_bonding_good(int send_enc_req, uint8_t *ltk,
     TEST_ASSERT(ble_sm_test_store_key.sec.ediv == ediv);
     TEST_ASSERT(ble_sm_test_store_key.sec.rand_num == rand_num);
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected long term key request reply command. */
     ble_sm_test_util_verify_tx_lt_key_req_reply(2, value_sec.ltk);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive an encryption changed event. */
@@ -1271,12 +1271,12 @@ ble_sm_test_util_peer_bonding_good(int send_enc_req, uint8_t *ltk,
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 authenticated);
 }
@@ -1302,13 +1302,13 @@ ble_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num)
     conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
     ble_hs_unlock();
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Receive a long term key request from the controller. */
     ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
     ble_sm_test_util_rx_lt_key_req(2, rand_num, ediv);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
 
     /* Ensure the LTK request event got sent to the application. */
     TEST_ASSERT(ble_sm_test_store_obj_type ==
@@ -1317,13 +1317,13 @@ ble_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num)
     TEST_ASSERT(ble_sm_test_store_key.sec.ediv == ediv);
     TEST_ASSERT(ble_sm_test_store_key.sec.rand_num == rand_num);
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
 
     /* Ensure we sent the expected long term key request neg reply command. */
     ble_sm_test_util_verify_tx_lt_key_req_neg_reply(2);
 
     /* Ensure the security procedure was aborted. */
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(!conn->bhc_sec_state.authenticated);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 }
@@ -1371,7 +1371,7 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
     /* Peer is the initiator so we must be the slave. */
     conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     if (params->has_sec_req) {
@@ -1384,24 +1384,24 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
 
     /* Receive a pair request from the peer. */
     ble_sm_test_util_rx_pair_req(2, &params->pair_req, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected pair response. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_rsp(&params->pair_rsp);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a public key from the peer. */
     ble_sm_test_util_rx_public_key(2, &params->public_key_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected public key. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_public_key(&params->public_key_rsp);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     switch (params->pair_alg) {
@@ -1423,7 +1423,7 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
 
             /* Receive a pair confirm from the peer. */
             ble_sm_test_util_rx_confirm(2, params->confirm_req + i);
-            TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+            TEST_ASSERT(!conn->bhc_sec_state.encrypted);
             TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
             if (i < num_iters - 1) {
@@ -1440,18 +1440,18 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
         /* Ensure we sent the expected pair confirm. */
         ble_hs_test_util_tx_all();
         ble_sm_test_util_verify_tx_pair_confirm(params->confirm_rsp + i);
-        TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
         /* Receive a pair random from the peer. */
         ble_sm_test_util_rx_random(2, params->random_req + i, 0);
-        TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
         /* Ensure we sent the expected pair random. */
         ble_hs_test_util_tx_all();
         ble_sm_test_util_verify_tx_pair_random(params->random_rsp + i);
-        TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     }
@@ -1461,7 +1461,7 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
 
     /* Receive a dhkey check from the peer. */
     ble_sm_test_util_rx_dhkey_check(2, &params->dhkey_check_req, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     ble_sm_test_util_io_check_post(&params->passkey_info,
@@ -1470,18 +1470,18 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
     /* Ensure we sent the expected dhkey check. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_dhkey_check(&params->dhkey_check_rsp);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a long term key request from the controller. */
     ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
     ble_sm_test_util_rx_lt_key_req(2, 0, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected long term key request reply command. */
     ble_sm_test_util_verify_tx_lt_key_req_reply(2, params->ltk);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive an encryption changed event. */
@@ -1493,13 +1493,13 @@ ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_sc_params *params)
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 params->authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
-                conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 conn->bhc_sec_state.authenticated);
 
@@ -1558,7 +1558,7 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
     TEST_ASSERT_FATAL(conn != NULL);
     ble_hs_unlock();
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     ble_hs_test_util_set_ack(
@@ -1574,23 +1574,23 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
     /* Ensure we sent the expected pair request. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_req(&params->pair_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a pair response from the peer. */
     ble_sm_test_util_rx_pair_rsp(2, &params->pair_rsp, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected public key. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_public_key(&params->public_key_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a public key from the peer. */
     ble_sm_test_util_rx_public_key(2, &params->public_key_rsp);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     switch (params->pair_alg) {
@@ -1618,24 +1618,24 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
             /* Ensure we sent the expected pair confirm. */
             ble_hs_test_util_tx_all();
             ble_sm_test_util_verify_tx_pair_confirm(params->confirm_req + i);
-            TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+            TEST_ASSERT(!conn->bhc_sec_state.encrypted);
             TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
         }
 
         /* Receive a pair confirm from the peer. */
         ble_sm_test_util_rx_confirm(2, params->confirm_rsp + i);
-        TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
         /* Ensure we sent the expected pair random. */
         ble_hs_test_util_tx_all();
         ble_sm_test_util_verify_tx_pair_random(params->random_req + i);
-        TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
         /* Receive a pair random from the peer. */
         ble_sm_test_util_rx_random(2, params->random_rsp + i, 0);
-        TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
         TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
     }
 
@@ -1645,18 +1645,18 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
     /* Ensure we sent the expected dhkey check. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_dhkey_check(&params->dhkey_check_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a dhkey check from the peer. */
     ble_sm_test_util_rx_dhkey_check(2, &params->dhkey_check_rsp, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Ensure we sent the expected start encryption command. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_start_enc(2, 0, 0, params->ltk);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive an encryption changed event. */
@@ -1668,13 +1668,13 @@ ble_sm_test_util_us_sc_good(struct ble_sm_test_sc_params *params)
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 params->authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled ==
-                conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 conn->bhc_sec_state.authenticated);
 
@@ -1707,7 +1707,7 @@ ble_sm_test_util_us_fail_inval(
     TEST_ASSERT_FATAL(conn != NULL);
     ble_hs_unlock();
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Initiate the pairing procedure. */
@@ -1717,19 +1717,19 @@ ble_sm_test_util_us_fail_inval(
     /* Ensure we sent the expected pair request. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_req(&params->pair_req);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive a pair response from the peer. */
     ble_sm_test_util_rx_pair_rsp(
         2, &params->pair_rsp, BLE_HS_SM_US_ERR(BLE_SM_ERR_INVAL));
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Ensure we sent the expected pair fail. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_pair_fail(&params->pair_fail);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Verify that security callback was not executed. */
@@ -1737,7 +1737,7 @@ ble_sm_test_util_us_fail_inval(
     TEST_ASSERT(ble_sm_test_gap_status == -1);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(!conn->bhc_sec_state.authenticated);
 }
 
@@ -1772,7 +1772,7 @@ ble_sm_test_util_us_bonding_good(int send_enc_req, uint8_t *ltk,
     TEST_ASSERT_FATAL(conn != NULL);
     ble_hs_unlock();
 
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
 
     /* Populate the SM database with an LTK for this peer. */
@@ -1803,7 +1803,7 @@ ble_sm_test_util_us_bonding_good(int send_enc_req, uint8_t *ltk,
     /* Ensure we sent the expected start encryption command. */
     ble_hs_test_util_tx_all();
     ble_sm_test_util_verify_tx_start_enc(2, rand_num, ediv, ltk);
-    TEST_ASSERT(!conn->bhc_sec_state.enc_enabled);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
     TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
 
     /* Receive an encryption changed event. */
@@ -1815,12 +1815,12 @@ ble_sm_test_util_us_bonding_good(int send_enc_req, uint8_t *ltk,
     /* Verify that security callback was executed. */
     TEST_ASSERT(ble_sm_test_gap_event == BLE_GAP_EVENT_ENC_CHANGE);
     TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 authenticated);
 
     /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
                 authenticated);
 }
@@ -1954,7 +1954,7 @@ TEST_CASE(ble_sm_test_case_conn_broken)
 
     /* Verify security callback got called. */
     TEST_ASSERT(ble_sm_test_gap_status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(!ble_sm_test_sec_state.enc_enabled);
+    TEST_ASSERT(!ble_sm_test_sec_state.encrypted);
     TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
 }
 


[4/7] incubator-mynewt-core git commit: BLE Host - Don't print status for no-op event.

Posted by cc...@apache.org.
BLE Host - Don't print status for no-op event.


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

Branch: refs/heads/upf54
Commit: f4673d2f2361b1f8cac0db4fed0e4e458428253f
Parents: a33be91
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jun 8 18:46:27 2016 +0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Jun 8 18:46:27 2016 +0800

----------------------------------------------------------------------
 net/nimble/host/src/host_dbg.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f4673d2f/net/nimble/host/src/host_dbg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_dbg.c b/net/nimble/host/src/host_dbg.c
index 6303f62..d7a31e3 100644
--- a/net/nimble/host/src/host_dbg.c
+++ b/net/nimble/host/src/host_dbg.c
@@ -325,18 +325,30 @@ host_hci_dbg_cmd_complete_disp(uint8_t *evdata, uint8_t len)
     uint8_t status;
     uint16_t opcode;
 
+    if (len < 3) {
+        BLE_HS_LOG(DEBUG, "Invalid command complete: len=%d "
+                          "(expected >= 3)", len);
+        goto done;
+    }
+
     cmd_pkts = evdata[0];
     opcode = le16toh(evdata + 1);
     ogf = BLE_HCI_OGF(opcode);
     ocf = BLE_HCI_OCF(opcode);
+
+    BLE_HS_LOG(DEBUG, "Command complete: cmd_pkts=%u ogf=0x%x ocf=0x%x",
+               cmd_pkts, ogf, ocf);
+
+    if (len == 3) {
+        goto done;
+    }
+
     status = evdata[3];
+    BLE_HS_LOG(DEBUG, " status=%u ", status);
 
     /* Move past header and status */
     evdata += 4;
 
-    BLE_HS_LOG(DEBUG, "Command Complete: cmd_pkts=%u ogf=0x%x ocf=0x%x "
-                      "status=%u ", cmd_pkts, ogf, ocf, status);
-
     /* Display parameters based on command. */
     switch (ogf) {
     case BLE_HCI_OGF_INFO_PARAMS:
@@ -398,6 +410,8 @@ host_hci_dbg_cmd_complete_disp(uint8_t *evdata, uint8_t len)
     default:
         break;
     }
+
+done:
     BLE_HS_LOG(DEBUG, "\n");
 }
 


[7/7] incubator-mynewt-core git commit: BLE Host - Clear queue on gatt-reliable-write fail

Posted by cc...@apache.org.
BLE Host - Clear queue on gatt-reliable-write fail


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

Branch: refs/heads/upf54
Commit: b0f2a9eb5e487546fd1d4a2ead1e1167e38cec06
Parents: 1aa4e46
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Jun 8 19:50:20 2016 +0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Jun 8 19:50:20 2016 +0800

----------------------------------------------------------------------
 net/nimble/host/src/ble_gattc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b0f2a9eb/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gattc.c b/net/nimble/host/src/ble_gattc.c
index 1796bcc..db5ab5f 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -3398,7 +3398,21 @@ static void
 ble_gattc_write_reliable_err(struct ble_gattc_proc *proc, int status,
                              uint16_t att_handle)
 {
+    struct ble_att_exec_write_req exec_req;
+
+    ble_gattc_dbg_assert_proc_not_inserted(proc);
     ble_gattc_write_reliable_cb(proc, status, att_handle);
+
+    /* If we have successfully queued any data, and the failure occurred before
+     * we could send the execute write command, then erase all queued data.
+     */
+    if (proc->write_reliable.attr.offset > 0 &&
+        proc->write_reliable.attr.offset <
+            proc->write_reliable.attr.value_len) {
+
+        exec_req.baeq_flags = 0;
+        ble_att_clt_tx_exec_write(proc->conn_handle, &exec_req);
+    }
 }
 
 /**