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/07/11 23:50:27 UTC

[37/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Fill in privacy API.

BLE Host - Fill in privacy API.


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

Branch: refs/heads/ble_hs_api
Commit: 7efbf0ee9cdc27bda570f3e3e6c8f27436840c67
Parents: 0853958
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Jul 1 15:58:11 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Mon Jul 11 16:43:33 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gap.h      |   1 -
 net/nimble/host/include/host/ble_hs.h       |   1 +
 net/nimble/host/include/host/ble_hs_id.h    |  30 +++
 net/nimble/host/src/ble_gap.c               |  64 +++---
 net/nimble/host/src/ble_gatts.c             |  12 +-
 net/nimble/host/src/ble_hci_priv.h          |   2 +-
 net/nimble/host/src/ble_hci_util.c          |   2 +-
 net/nimble/host/src/ble_hs_conn.c           |  50 ++---
 net/nimble/host/src/ble_hs_conn_priv.h      |  20 +-
 net/nimble/host/src/ble_hs_id.c             | 248 +++++++++++++++++++++++
 net/nimble/host/src/ble_hs_id_priv.h        |  30 +++
 net/nimble/host/src/ble_hs_misc.c           |   6 +-
 net/nimble/host/src/ble_hs_priv.h           |   5 +-
 net/nimble/host/src/ble_hs_pvcy.c           |  99 ++-------
 net/nimble/host/src/ble_hs_pvcy_priv.h      |   7 +-
 net/nimble/host/src/ble_hs_startup.c        |   3 +-
 net/nimble/host/src/ble_sm.c                |  29 ++-
 net/nimble/host/src/ble_sm_alg.c            |   9 +-
 net/nimble/host/src/ble_sm_priv.h           |   7 +-
 net/nimble/host/src/ble_sm_sc.c             |  12 +-
 net/nimble/host/src/test/ble_gap_test.c     |   1 +
 net/nimble/host/src/test/ble_hs_conn_test.c |   6 +-
 net/nimble/host/src/test/ble_hs_test.c      |   2 +-
 net/nimble/host/src/test/ble_hs_test_util.c |  24 ++-
 net/nimble/host/src/test/ble_hs_test_util.h |   2 +-
 net/nimble/host/src/test/ble_sm_test_util.c |  21 +-
 net/nimble/include/nimble/ble.h             |   2 +-
 net/nimble/src/util.c                       |   2 +-
 28 files changed, 489 insertions(+), 208 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/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 c0dc278..f83b1de 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -321,6 +321,5 @@ int ble_gap_security_initiate(uint16_t conn_handle);
 int ble_gap_pair_initiate(uint16_t conn_handle);
 int ble_gap_encryption_initiate(uint16_t conn_handle, const uint8_t *ltk,
                                 uint16_t ediv, uint64_t rand_val, int auth);
-void ble_gap_init_identity_addr(const uint8_t *addr);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index 7786b9a..2d2a93a 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -58,6 +58,7 @@ struct os_event;
 #define BLE_HS_ETIMEOUT_HCI         20
 #define BLE_HS_ENOMEM_HCI           21
 #define BLE_HS_ENOMEM_EVT           22
+#define BLE_HS_ENOADDR              23
 
 #define BLE_HS_ERR_ATT_BASE         0x100   /* 256 */
 #define BLE_HS_ATT_ERR(x)           ((x) ? BLE_HS_ERR_ATT_BASE + (x) : 0)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/include/host/ble_hs_id.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs_id.h b/net/nimble/host/include/host/ble_hs_id.h
new file mode 100644
index 0000000..749524b
--- /dev/null
+++ b/net/nimble/host/include/host/ble_hs_id.h
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BLE_HS_ID_
+#define H_BLE_HS_ID_
+
+#include <inttypes.h>
+
+int ble_hs_id_gen_rnd(int nrpa, uint8_t *out_addr);
+int ble_hs_id_set_rnd(const uint8_t *rnd_addr);
+int ble_hs_id_copy_addr(uint8_t id_addr_type, uint8_t *out_id_addr,
+                        int *out_is_nrpa);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/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 2745379..7254287 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -971,10 +971,12 @@ ble_gap_rx_conn_complete(struct hci_le_conn_complete *evt)
     BLE_HS_DBG_ASSERT(conn != NULL);
 
     conn->bhc_handle = evt->connection_handle;
-    memcpy(conn->bhc_addr, evt->peer_addr, sizeof conn->bhc_addr);
-    conn->bhc_addr_type = evt->peer_addr_type;
-    memcpy(conn->our_rpa_addr, evt->local_rpa, sizeof(conn->our_rpa_addr));
-    memcpy(conn->peer_rpa_addr, evt->peer_rpa, sizeof(conn->peer_rpa_addr));
+    memcpy(conn->bhc_peer_addr, evt->peer_addr, sizeof conn->bhc_peer_addr);
+    conn->bhc_peer_addr_type = evt->peer_addr_type;
+    memcpy(conn->bhc_our_rpa_addr, evt->local_rpa,
+           sizeof conn->bhc_our_rpa_addr);
+    memcpy(conn->bhc_peer_rpa_addr, evt->peer_rpa,
+           sizeof conn->bhc_peer_rpa_addr);
     conn->bhc_itvl = evt->conn_itvl;
     conn->bhc_latency = evt->conn_latency;
     conn->bhc_supervision_timeout = evt->supervision_timeout;
@@ -982,22 +984,25 @@ ble_gap_rx_conn_complete(struct hci_le_conn_complete *evt)
     if (evt->role == BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER) {
         conn->bhc_flags |= BLE_HS_CONN_F_MASTER;
         conn->bhc_cb = ble_gap_master.conn.cb;
-        conn->our_addr_type = ble_gap_master.conn.our_addr_type;
+        conn->bhc_our_addr_type = ble_gap_master.conn.our_addr_type;
         conn->bhc_cb_arg = ble_gap_master.conn.cb_arg;
         ble_gap_master_reset_state();
     } else {
         conn->bhc_cb = ble_gap_slave.cb;
         conn->bhc_cb_arg = ble_gap_slave.cb_arg;
-        conn->our_addr_type = ble_gap_slave.our_addr_type;
+        conn->bhc_our_addr_type = ble_gap_slave.our_addr_type;
         ble_gap_slave_reset_state();
     }
 
-    memcpy(conn->our_rpa_addr, evt->local_rpa, 6);
-    memcpy(conn->peer_rpa_addr, evt->peer_rpa, 6);
+    memcpy(conn->bhc_our_rpa_addr, evt->local_rpa, 6);
+    memcpy(conn->bhc_peer_rpa_addr, evt->peer_rpa, 6);
+
+    ble_hs_lock();
 
     ble_gap_conn_to_snapshot(conn, &snap);
+    ble_hs_conn_insert(conn);
 
-    ble_hs_atomic_conn_insert(conn);
+    ble_hs_unlock();
 
     memset(&ctxt, 0, sizeof ctxt);
     ctxt.desc = &snap.desc;
@@ -1660,6 +1665,11 @@ ble_gap_adv_start(uint8_t own_addr_type, uint8_t peer_addr_type,
         }
     }
 
+    rc = ble_hs_id_use_addr(own_addr_type);
+    if (rc != 0) {
+        return rc;
+    }
+
     BLE_HS_LOG(INFO, "GAP procedure initiated: advertise; ");
     ble_gap_log_adv(own_addr_type, peer_addr_type, peer_addr, adv_params);
     BLE_HS_LOG(INFO, "\n");
@@ -1974,10 +1984,10 @@ ble_gap_disc_validate(uint8_t own_addr_type,
  * Performs the Limited or General Discovery Procedures (vol. 3, part C,
  * section 9.2.5 / 9.2.6).
  *
- * @param own_addr_type         This parameter is ignored unless active scanning
- *                                  is being used. The type of address the
- *                                  stack should use for itself when sending
- *                                  scan requests.  Valid values are:
+ * @param own_addr_type         This parameter is ignored unless active
+ *                                  scanning is being used. The type of address
+ *                                  the stack should use for itself when
+ *                                  sending scan requests.  Valid values are:
  *                                      o BLE_ADDR_TYPE_PUBLIC
  *                                      o BLE_ADDR_TYPE_RANDOM
  *                                      o BLE_ADDR_TYPE_RPA_PUB_DEFAULT
@@ -1990,9 +2000,9 @@ ble_gap_disc_validate(uint8_t own_addr_type,
  * @param disc_params           Additional arguments specifying the particulars
  *                                  of the discovery procedure.
  * @param cb                    The callback to associate with this discovery
- *                                  procedure.  Advertising reports and discovery
- *                                  termination events are reported through
- *                                  this callback.
+ *                                  procedure.  Advertising reports and
+ *                                  discovery termination events are reported
+ *                                  through this callback.
  * @param cb_arg                The optional argument to pass to the callback
  *                                  function.
  *
@@ -2039,6 +2049,13 @@ ble_gap_disc(uint8_t own_addr_type, int32_t duration_ms,
         }
     }
 
+    if (!params.passive) {
+        rc = ble_hs_id_use_addr(own_addr_type);
+        if (rc != 0) {
+            return rc;
+        }
+    }
+
     ble_gap_master.disc.limited = params.limited;
     ble_gap_master.disc.cb = cb;
     ble_gap_master.disc.cb_arg = cb_arg;
@@ -2208,6 +2225,11 @@ ble_gap_connect(uint8_t own_addr_type,
 
     /* XXX: Verify conn_params. */
 
+    rc = ble_hs_id_use_addr(own_addr_type);
+    if (rc != 0) {
+        return rc;
+    }
+
     BLE_HS_LOG(INFO, "GAP procedure initiated: connect; ");
     ble_gap_log_conn(own_addr_type, peer_addr_type, peer_addr, conn_params);
     BLE_HS_LOG(INFO, "\n");
@@ -2766,16 +2788,6 @@ ble_gap_notify_event(uint16_t conn_handle, uint16_t attr_handle,
 }
 
 /*****************************************************************************
- * $privacy                                                                  *
- *****************************************************************************/
-
-void
-ble_gap_init_identity_addr(const uint8_t *addr)
-{
-    ble_hs_pvcy_set_our_id_addr(addr);
-}
-
-/*****************************************************************************
  * $init                                                                     *
  *****************************************************************************/
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 2318fdf..3b6b0b6 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -601,8 +601,8 @@ ble_gatts_clt_cfg_access_locked(struct ble_hs_conn *conn, uint16_t attr_handle,
 
         /* Successful writes get persisted for bonded connections. */
         if (conn->bhc_sec_state.bonded) {
-            out_cccd->peer_addr_type = conn->bhc_addr_type;
-            memcpy(out_cccd->peer_addr, conn->bhc_addr, 6);
+            out_cccd->peer_addr_type = conn->bhc_peer_addr_type;
+            memcpy(out_cccd->peer_addr, conn->bhc_peer_addr, 6);
             out_cccd->chr_val_handle = chr_val_handle;
             out_cccd->flags = clt_cfg->flags;
             out_cccd->value_changed = 0;
@@ -1186,8 +1186,8 @@ ble_gatts_rx_indicate_ack(uint16_t conn_handle, uint16_t chr_val_handle)
         persist = conn->bhc_sec_state.bonded &&
                   !(clt_cfg->flags & BLE_GATTS_CLT_CFG_F_INDICATE_PENDING);
         if (persist) {
-            cccd_value.peer_addr_type = conn->bhc_addr_type;
-            memcpy(cccd_value.peer_addr, conn->bhc_addr, 6);
+            cccd_value.peer_addr_type = conn->bhc_peer_addr_type;
+            memcpy(cccd_value.peer_addr, conn->bhc_peer_addr, 6);
             cccd_value.chr_val_handle = chr_val_handle;
             cccd_value.flags = clt_cfg->flags;
             cccd_value.value_changed = 0;
@@ -1356,8 +1356,8 @@ ble_gatts_bonding_restored(uint16_t conn_handle)
     BLE_HS_DBG_ASSERT(conn != NULL);
     BLE_HS_DBG_ASSERT(conn->bhc_sec_state.bonded);
 
-    cccd_key.peer_addr_type = conn->bhc_addr_type;
-    memcpy(cccd_key.peer_addr, conn->bhc_addr, 6);
+    cccd_key.peer_addr_type = conn->bhc_peer_addr_type;
+    memcpy(cccd_key.peer_addr, conn->bhc_peer_addr, 6);
     cccd_key.chr_val_handle = 0;
     cccd_key.idx = 0;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hci_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hci_priv.h b/net/nimble/host/src/ble_hci_priv.h
index dbe3d15..1526a20 100644
--- a/net/nimble/host/src/ble_hci_priv.h
+++ b/net/nimble/host/src/ble_hci_priv.h
@@ -42,7 +42,7 @@ void ble_hci_set_phony_ack_cb(ble_hci_cmd_phony_ack_fn *cb);
 int ble_hci_util_read_adv_tx_pwr(int8_t *out_pwr);
 int ble_hci_util_rand(void *dst, int len);
 int ble_hci_util_read_rssi(uint16_t conn_handle, int8_t *out_rssi);
-int ble_hci_util_set_random_addr(uint8_t *addr);
+int ble_hci_util_set_random_addr(const uint8_t *addr);
 int ble_hci_util_set_data_len(uint16_t conn_handle, uint16_t tx_octets,
                               uint16_t tx_time);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hci_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hci_util.c b/net/nimble/host/src/ble_hci_util.c
index c44c012..3850903 100644
--- a/net/nimble/host/src/ble_hci_util.c
+++ b/net/nimble/host/src/ble_hci_util.c
@@ -107,7 +107,7 @@ ble_hci_util_read_rssi(uint16_t conn_handle, int8_t *out_rssi)
 }
 
 int
-ble_hci_util_set_random_addr(uint8_t *addr)
+ble_hci_util_set_random_addr(const uint8_t *addr)
 {
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RAND_ADDR_LEN];
     int rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index a4b0f16..cd9f4b7 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include "os/os.h"
 #include "host/host_hci.h"
+#include "host/ble_hs_id.h"
 #include "ble_hs_priv.h"
 
 /** At least three channels required per connection (sig, att, sm). */
@@ -258,8 +259,8 @@ ble_hs_conn_find_by_addr(uint8_t addr_type, uint8_t *addr)
     BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task());
 
     SLIST_FOREACH(conn, &ble_hs_conns, bhc_next) {
-        if (conn->bhc_addr_type == addr_type &&
-            memcmp(conn->bhc_addr, addr, 6) == 0) {
+        if (conn->bhc_peer_addr_type == addr_type &&
+            memcmp(conn->bhc_peer_addr, addr, 6) == 0) {
 
             return conn;
         }
@@ -316,59 +317,52 @@ ble_hs_conn_first(void)
 }
 
 void
-ble_hs_conn_addrs(struct ble_hs_conn *conn,
+ble_hs_conn_addrs(const struct ble_hs_conn *conn,
                   struct ble_hs_conn_addrs *addrs)
 {
+    int rc;
+
     /* Determine our address information. */
-    addrs->our_id_addr =
-        ble_hs_pvcy_our_id_addr(&addrs->our_id_addr_type);
-    if (memcmp(conn->our_rpa_addr, ble_hs_conn_null_addr, 6) == 0) {
+    addrs->our_id_addr_type =
+        ble_hs_misc_addr_type_to_id(conn->bhc_our_addr_type);
+    rc = ble_hs_id_addr(addrs->our_id_addr_type, &addrs->our_id_addr, NULL);
+    assert(rc == 0);
+
+    if (memcmp(conn->bhc_our_rpa_addr, ble_hs_conn_null_addr, 6) == 0) {
         addrs->our_ota_addr_type = addrs->our_id_addr_type;
         addrs->our_ota_addr = addrs->our_id_addr;
     } else {
-        switch (addrs->our_id_addr_type) {
-        case BLE_ADDR_TYPE_PUBLIC:
-            addrs->our_ota_addr_type = BLE_ADDR_TYPE_RPA_PUB_DEFAULT;
-            break;
-
-        case BLE_ADDR_TYPE_RANDOM:
-            addrs->our_ota_addr_type = BLE_ADDR_TYPE_RPA_RND_DEFAULT;
-            break;
-
-        default:
-            BLE_HS_DBG_ASSERT(0);
-        }
-
-        addrs->our_ota_addr = conn->our_rpa_addr;
+        addrs->our_ota_addr_type = conn->bhc_our_addr_type;
+        addrs->our_ota_addr = conn->bhc_our_rpa_addr;
     }
 
     /* Determine peer address information. */
-    addrs->peer_ota_addr_type = conn->bhc_addr_type;
-    addrs->peer_id_addr = conn->bhc_addr;
-    switch (conn->bhc_addr_type) {
+    addrs->peer_ota_addr_type = conn->bhc_peer_addr_type;
+    addrs->peer_id_addr = conn->bhc_peer_addr;
+    switch (conn->bhc_peer_addr_type) {
     case BLE_ADDR_TYPE_PUBLIC:
         addrs->peer_id_addr_type = BLE_ADDR_TYPE_PUBLIC;
-        addrs->peer_ota_addr = conn->bhc_addr;
+        addrs->peer_ota_addr = conn->bhc_peer_addr;
         break;
 
     case BLE_ADDR_TYPE_RANDOM:
         addrs->peer_id_addr_type = BLE_ADDR_TYPE_RANDOM;
-        addrs->peer_ota_addr = conn->bhc_addr;
+        addrs->peer_ota_addr = conn->bhc_peer_addr;
         break;
 
     case BLE_ADDR_TYPE_RPA_PUB_DEFAULT:
         addrs->peer_id_addr_type = BLE_ADDR_TYPE_PUBLIC;
-        addrs->peer_ota_addr = conn->peer_rpa_addr;
+        addrs->peer_ota_addr = conn->bhc_peer_rpa_addr;
         break;
 
     case BLE_ADDR_TYPE_RPA_RND_DEFAULT:
         addrs->peer_id_addr_type = BLE_ADDR_TYPE_RANDOM;
-        addrs->peer_ota_addr = conn->peer_rpa_addr;
+        addrs->peer_ota_addr = conn->bhc_peer_rpa_addr;
         break;
 
     default:
         BLE_HS_DBG_ASSERT(0);
-        return;
+        break;
     }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_conn_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn_priv.h b/net/nimble/host/src/ble_hs_conn_priv.h
index 3ff7f76..cdcb363 100644
--- a/net/nimble/host/src/ble_hs_conn_priv.h
+++ b/net/nimble/host/src/ble_hs_conn_priv.h
@@ -36,11 +36,11 @@ typedef uint8_t ble_hs_conn_flags_t;
 struct ble_hs_conn {
     SLIST_ENTRY(ble_hs_conn) bhc_next;
     uint16_t bhc_handle;
-    uint8_t bhc_addr_type;
-    uint8_t our_addr_type;
-    uint8_t bhc_addr[6];
-    uint8_t our_rpa_addr[6];
-    uint8_t peer_rpa_addr[6];
+    uint8_t bhc_peer_addr_type;
+    uint8_t bhc_our_addr_type;
+    uint8_t bhc_peer_addr[6];
+    uint8_t bhc_our_rpa_addr[6];
+    uint8_t bhc_peer_rpa_addr[6];
 
     uint16_t bhc_itvl;
     uint16_t bhc_latency;
@@ -67,10 +67,10 @@ struct ble_hs_conn_addrs {
     uint8_t our_id_addr_type;
     uint8_t peer_ota_addr_type;
     uint8_t peer_id_addr_type;
-    uint8_t *our_ota_addr;
-    uint8_t *our_id_addr;
-    uint8_t *peer_ota_addr;
-    uint8_t *peer_id_addr;
+    const uint8_t *our_ota_addr;
+    const uint8_t *our_id_addr;
+    const uint8_t *peer_ota_addr;
+    const uint8_t *peer_id_addr;
 };
 
 int ble_hs_conn_can_alloc(void);
@@ -87,7 +87,7 @@ struct ble_l2cap_chan *ble_hs_conn_chan_find(struct ble_hs_conn *conn,
                                              uint16_t cid);
 int ble_hs_conn_chan_insert(struct ble_hs_conn *conn,
                             struct ble_l2cap_chan *chan);
-void ble_hs_conn_addrs(struct ble_hs_conn *conn,
+void ble_hs_conn_addrs(const struct ble_hs_conn *conn,
                        struct ble_hs_conn_addrs *addrs);
 
 int ble_hs_conn_init(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_id.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_id.c b/net/nimble/host/src/ble_hs_id.c
new file mode 100644
index 0000000..6d7df2b
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_id.c
@@ -0,0 +1,248 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include "host/ble_hs_id.h"
+#include "ble_hs_priv.h"
+
+static uint8_t ble_hs_id_pub[6];
+static uint8_t ble_hs_id_rnd[6];
+
+void
+ble_hs_id_set_pub(const uint8_t *pub_addr)
+{
+    ble_hs_lock();
+    memcpy(ble_hs_id_pub, pub_addr, 6);
+    ble_hs_unlock();
+}
+
+/**
+ * Generates a new random address.  This function does not configure the device
+ * with the new address; the caller can use the address in subsequent
+ * operations.
+ *
+ * @param nrpa                  The type of random address to generate:
+ *                                  0: static
+ *                                  1: non-resolvable private
+ * @param out_addr              On success, the generated address gets written
+ *                                  here.
+ *
+ * @return                      0 on success; nonzero on failure.
+ */
+int
+ble_hs_id_gen_rnd(int nrpa, uint8_t *out_addr)
+{
+    int rc;
+
+    rc = ble_hci_util_rand(out_addr, 6);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (nrpa) {
+        out_addr[5] &= ~0xc0;
+    } else {
+        out_addr[5] |= 0xc0;
+    }
+
+    return 0;
+}
+
+/**
+ * Sets the device's random address.  The address type (static vs.
+ * non-resolvable private) is inferred from the most-significant byte of the
+ * address.  The address is specified in host byte order (little-endian!).
+ *
+ * @param rnd_addr              The random address to set.
+ *
+ * @return                      0 on success;
+ *                              BLE_HS_EINVAL if the specified address is not a
+ *                                  valid static random or non-resolvable
+ *                                  private address.
+ *                              Other nonzero on error.
+ */
+int
+ble_hs_id_set_rnd(const uint8_t *rnd_addr)
+{
+    uint8_t addr_type_byte;
+    int rc;
+
+    ble_hs_lock();
+
+    addr_type_byte = rnd_addr[5] & 0xc0;
+    if (addr_type_byte != 0x00 && addr_type_byte != 0xc0) {
+        rc = BLE_HS_EINVAL;
+        goto done;
+    }
+
+    rc = ble_hci_util_set_random_addr(rnd_addr);
+    if (rc != 0) {
+        goto done;
+    }
+
+    memcpy(ble_hs_id_rnd, rnd_addr, 6);
+
+    rc = 0;
+
+done:
+    ble_hs_unlock();
+    return rc;
+}
+
+/**
+ * Retrieves one of the device's identity addresses.  The device can have two
+ * identity addresses: one public and one random.  The id_addr_type argument
+ * specifies which of these two addresses to retrieve.
+ *
+ * @param id_addr_type          The type of identity address to retrieve.
+ *                                  Valid values are:
+ *                                      o BLE_ADDR_TYPE_PUBLIC
+ *                                      o BLE_ADDR_TYPE_RANDOM
+ * @param out_id_addr           On success, this is reseated to point to the
+ *                                  retrieved 6-byte identity address.
+ * @param out_is_nrpa           On success, the pointed-to value indicates
+ *                                  whether the retrieved address is a
+ *                                  non-resolvable private address.
+ *
+ * @return                      0 on success;
+ *                              BLE_HS_EINVAL if an invalid address type was
+ *                                  specified;
+ *                              BLE_HS_ENOADDR if the device does not have an
+ *                                  identity address of the requested type;
+ *                              Other BLE host core code on error.
+ */
+int
+ble_hs_id_addr(uint8_t id_addr_type, const uint8_t **out_id_addr,
+               int *out_is_nrpa)
+{
+    const uint8_t *id_addr;
+    int nrpa;
+
+    BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task());
+
+    switch (id_addr_type) {
+    case BLE_ADDR_TYPE_PUBLIC:
+        id_addr = ble_hs_id_pub;
+        nrpa = 0;
+        break;
+
+    case BLE_ADDR_TYPE_RANDOM:
+        id_addr = ble_hs_id_rnd;
+        nrpa = (ble_hs_id_rnd[5] & 0xc0) == 0;
+        break;
+
+    default:
+        return BLE_HS_EINVAL;
+    }
+
+    if (memcmp(id_addr, ble_hs_misc_null_addr, 6) == 0) {
+        return BLE_HS_ENOADDR;
+    }
+
+    if (out_id_addr != NULL) {
+        *out_id_addr = id_addr;
+    }
+    if (out_is_nrpa != NULL) {
+        *out_is_nrpa = nrpa;
+    }
+
+    return 0;
+}
+
+/**
+ * Retrieves one of the device's identity addresses.  The device can have two
+ * identity addresses: one public and one random.  The id_addr_type argument
+ * specifies which of these two addresses to retrieve.
+ *
+ * @param id_addr_type          The type of identity address to retrieve.
+ *                                  Valid values are:
+ *                                      o BLE_ADDR_TYPE_PUBLIC
+ *                                      o BLE_ADDR_TYPE_RANDOM
+ * @param out_id_addr           On success, the requested identity address is
+ *                                  copied into this buffer.  The buffer must
+ *                                  be at least six bytes in size.
+ * @param out_is_nrpa           On success, the pointed-to value indicates
+ *                                  whether the retrieved address is a
+ *                                  non-resolvable private address.
+ *
+ * @return                      0 on success;
+ *                              BLE_HS_EINVAL if an invalid address type was
+ *                                  specified;
+ *                              BLE_HS_ENOADDR if the device does not have an
+ *                                  identity address of the requested type;
+ *                              Other BLE host core code on error.
+ */
+int
+ble_hs_id_copy_addr(uint8_t id_addr_type, uint8_t *out_id_addr,
+                    int *out_is_nrpa)
+{
+    const uint8_t *addr;
+    int rc;
+
+    ble_hs_lock();
+
+    rc = ble_hs_id_addr(id_addr_type, &addr, out_is_nrpa);
+    if (rc == 0) {
+        memcpy(out_id_addr, addr, 6);
+    }
+
+    ble_hs_unlock();
+
+    return rc;
+}
+
+int
+ble_hs_id_use_addr(uint8_t addr_type)
+{
+    uint8_t id_addr_type;
+    int nrpa;
+    int rc;
+
+    switch (addr_type) {
+    case BLE_ADDR_TYPE_PUBLIC:
+    case BLE_ADDR_TYPE_RANDOM:
+        rc = ble_hs_id_addr(addr_type, NULL, NULL);
+        if (rc != 0) {
+            return rc;
+        }
+        break;
+
+    case BLE_ADDR_TYPE_RPA_PUB_DEFAULT:
+    case BLE_ADDR_TYPE_RPA_RND_DEFAULT:
+        id_addr_type = ble_hs_misc_addr_type_to_id(addr_type);
+        rc = ble_hs_id_addr(id_addr_type, NULL, &nrpa);
+        if (rc != 0) {
+            return rc;
+        }
+        if (nrpa) {
+            return BLE_HS_ENOADDR;
+        }
+
+        rc = ble_hs_pvcy_ensure_started();
+        if (rc != 0) {
+            return rc;
+        }
+        break;
+
+    default:
+        return BLE_HS_EINVAL;
+    }
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_id_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_id_priv.h b/net/nimble/host/src/ble_hs_id_priv.h
new file mode 100644
index 0000000..5c0728c
--- /dev/null
+++ b/net/nimble/host/src/ble_hs_id_priv.h
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BLE_HS_ID_PRIV_
+#define H_BLE_HS_ID_PRIV_
+
+#include <inttypes.h>
+
+void ble_hs_id_set_pub(const uint8_t *pub_addr);
+int ble_hs_id_addr(uint8_t id_addr_type, const uint8_t **out_id_addr,
+                   int *out_is_nrpa);
+int ble_hs_id_use_addr(uint8_t addr_type);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_misc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_misc.c b/net/nimble/host/src/ble_hs_misc.c
index 597779c..b893cd3 100644
--- a/net/nimble/host/src/ble_hs_misc.c
+++ b/net/nimble/host/src/ble_hs_misc.c
@@ -46,7 +46,7 @@ ble_hs_misc_malloc_mempool(void **mem, struct os_mempool *pool,
 }
 
 void
-ble_hs_misc_log_mbuf(struct os_mbuf *om)
+ble_hs_misc_log_mbuf(const struct os_mbuf *om)
 {
     uint8_t u8;
     int i;
@@ -58,9 +58,9 @@ ble_hs_misc_log_mbuf(struct os_mbuf *om)
 }
 
 void
-ble_hs_misc_log_flat_buf(void *data, int len)
+ble_hs_misc_log_flat_buf(const void *data, int len)
 {
-    uint8_t *u8ptr;
+    const uint8_t *u8ptr;
     int i;
 
     u8ptr = data;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_priv.h b/net/nimble/host/src/ble_hs_priv.h
index 8cd4a83..e6fe55e 100644
--- a/net/nimble/host/src/ble_hs_priv.h
+++ b/net/nimble/host/src/ble_hs_priv.h
@@ -37,6 +37,7 @@
 #include "ble_sm_priv.h"
 #include "ble_hs_adv_priv.h"
 #include "ble_hs_pvcy_priv.h"
+#include "ble_hs_id_priv.h"
 #include "ble_uuid_priv.h"
 #include "host/ble_hs.h"
 #include "log/log.h"
@@ -71,8 +72,8 @@ int ble_hs_tx_data(struct os_mbuf *om);
 
 int ble_hs_misc_malloc_mempool(void **mem, struct os_mempool *pool,
                                int num_entries, int entry_size, char *name);
-void ble_hs_misc_log_mbuf(struct os_mbuf *om);
-void ble_hs_misc_log_flat_buf(void *data, int len);
+void ble_hs_misc_log_mbuf(const struct os_mbuf *om);
+void ble_hs_misc_log_flat_buf(const void *data, int len);
 int ble_hs_misc_conn_chan_find(uint16_t conn_handle, uint16_t cid,
                                struct ble_hs_conn **out_conn,
                                struct ble_l2cap_chan **out_chan);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_pvcy.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_pvcy.c b/net/nimble/host/src/ble_hs_pvcy.c
index 3a20ce4..05f9122 100644
--- a/net/nimble/host/src/ble_hs_pvcy.c
+++ b/net/nimble/host/src/ble_hs_pvcy.c
@@ -21,13 +21,9 @@
 #include <string.h>
 #include "ble_hs_priv.h"
 
-static int ble_hs_pvcy_initialized;
-static uint8_t ble_hs_pvcy_id_addr[6];
-static uint8_t ble_hs_pvcy_id_addr_type;
-static uint8_t ble_hs_pvcy_nrpa[6];
+static uint8_t ble_hs_pvcy_started;
 uint8_t ble_hs_pvcy_irk[16];
 
-
 /** Use this as a default IRK if none gets set. */
 const uint8_t default_irk[16] = {
     0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
@@ -35,18 +31,6 @@ const uint8_t default_irk[16] = {
 };
 
 static int
-ble_hs_pvcy_gen_static_random_addr(uint8_t *addr)
-{
-    int rc;
-
-    rc = ble_hci_util_rand(addr, 6);
-    /* TODO -- set bits properly */
-    addr[5] |= 0xc0;
-
-    return rc;
-}
-
-static int
 ble_hs_pvcy_set_addr_timeout(uint16_t timeout)
 {
     uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_SET_RESOLV_PRIV_ADDR_TO_LEN];
@@ -125,7 +109,7 @@ ble_hs_pvcy_add_entry(uint8_t *addr, uint8_t addr_type, uint8_t *irk)
 
     add.addr_type = addr_type;
     memcpy(add.addr, addr, 6);
-    memcpy(add.local_irk, ble_hs_pvcy_our_irk(), 16);
+    memcpy(add.local_irk, ble_hs_pvcy_irk, 16);
     memcpy(add.peer_irk, irk, 16);
 
     rc = host_hci_cmd_build_add_to_resolv_list(&add, buf, sizeof(buf));
@@ -141,66 +125,24 @@ ble_hs_pvcy_add_entry(uint8_t *addr, uint8_t addr_type, uint8_t *irk)
     return 0;
 }
 
-void
-ble_hs_pvcy_our_nrpa(uint8_t *addr)
-{
-    memcpy(addr, ble_hs_pvcy_nrpa, 6);
-}
-
-int 
-ble_hs_pvcy_set_our_nrpa(void)
+int
+ble_hs_pvcy_ensure_started(void)
 {
     int rc;
-    uint8_t addr[6];
 
-    rc = ble_hci_util_rand(addr, 6);
-    assert(rc == 0);
-    addr[5] &= ~(0xc0);
-    memcpy(ble_hs_pvcy_nrpa, addr, 6);
-
-    return ble_hci_util_set_random_addr(addr);
-}
-
-uint8_t *
-ble_hs_pvcy_our_id_addr(uint8_t *type)
-{
-    if (!ble_hs_pvcy_initialized) {
-        ble_hs_pvcy_set_our_id_addr(NULL);
+    if (ble_hs_pvcy_started) {
+        return 0;
     }
 
-    if (type != NULL) {
-        *type = ble_hs_pvcy_id_addr_type;
-    }
-
-    return ble_hs_pvcy_id_addr;
-}
-
-void
-ble_hs_pvcy_set_our_id_addr(const uint8_t *addr)
-{
-    uint8_t random_addr[6];
-    int rc;
-
-    if (!ble_hs_pvcy_initialized) {
-        /* Set up the periodic change of our RPA. */
-        rc = ble_hs_pvcy_set_addr_timeout(ble_hs_cfg.rpa_timeout);
-        assert(rc == 0);
+    /* Set up the periodic change of our RPA. */
+    rc = ble_hs_pvcy_set_addr_timeout(ble_hs_cfg.rpa_timeout);
+    if (rc != 0) {
+        return rc;
     }
 
-    if (addr != NULL) {
-        memcpy(ble_hs_pvcy_id_addr, addr, 6);
-        ble_hs_pvcy_id_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC;
-    } else {
-        /* Generate a new static random address. */
-        ble_hs_pvcy_gen_static_random_addr(random_addr);
-        rc = ble_hci_util_set_random_addr(random_addr);
-        assert(rc == 0);
-
-        ble_hs_pvcy_id_addr_type = BLE_HCI_ADV_OWN_ADDR_RANDOM;
-        memcpy(ble_hs_pvcy_id_addr, random_addr, 6);
-    }
+    ble_hs_pvcy_started = 1;
 
-    ble_hs_pvcy_initialized = 1;
+    return 0;
 }
 
 void
@@ -225,21 +167,20 @@ ble_hs_pvcy_set_our_irk(const uint8_t *irk)
         ble_hs_pvcy_clear_entries();
         ble_hs_pvcy_set_resolve_enabled(1);
 
-        /* Push our identity to the controller as a keycache entry with a null
-         * MAC address. The controller uses this entry to generate an RPA when
-         * we do advertising with own-addr-type = rpa.
+        /* Push a null address identity to the controller.  The controller uses
+         * this entry to generate an RPA when we do advertising with
+         * own-addr-type = rpa.
          */
         memset(tmp_addr, 0, 6);
         ble_hs_pvcy_add_entry(tmp_addr, 0, ble_hs_pvcy_irk);
     }
 }
 
-uint8_t *
-ble_hs_pvcy_our_irk(void)
+int
+ble_hs_pvcy_our_irk(const uint8_t **out_irk)
 {
-    if (!ble_hs_pvcy_initialized) {
-        ble_hs_pvcy_set_our_id_addr(NULL);
-    }
+    /* XXX: Return error if privacy not supported. */
 
-    return ble_hs_pvcy_irk;
+    *out_irk = ble_hs_pvcy_irk;
+    return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_pvcy_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_pvcy_priv.h b/net/nimble/host/src/ble_hs_pvcy_priv.h
index 282f2ad..2635778 100644
--- a/net/nimble/host/src/ble_hs_pvcy_priv.h
+++ b/net/nimble/host/src/ble_hs_pvcy_priv.h
@@ -22,13 +22,10 @@
 
 #include <inttypes.h>
 
-int ble_hs_pvcy_set_our_nrpa(void);
-void ble_hs_pvcy_our_nrpa(uint8_t *addr);
-void ble_hs_pvcy_set_our_id_addr(const uint8_t *addr);
 void ble_hs_pvcy_set_our_irk(const uint8_t *irk);
-uint8_t *ble_hs_pvcy_our_id_addr(uint8_t *type);
-uint8_t *ble_hs_pvcy_our_irk(void);
+int ble_hs_pvcy_our_irk(const uint8_t **out_irk);
 int ble_hs_pvcy_remove_entry(uint8_t addr_type, uint8_t *addr);
 int ble_hs_pvcy_add_entry(uint8_t *addr, uint8_t addrtype, uint8_t *irk);
+int ble_hs_pvcy_ensure_started(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_hs_startup.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_startup.c b/net/nimble/host/src/ble_hs_startup.c
index 1973cf9..568f1ed 100644
--- a/net/nimble/host/src/ble_hs_startup.c
+++ b/net/nimble/host/src/ble_hs_startup.c
@@ -225,7 +225,8 @@ ble_hs_startup_go(void)
     }
 
     /* XXX: Read BD_ADDR. */
-    ble_hs_pvcy_set_our_id_addr(g_dev_addr);
+
+    ble_hs_id_set_pub(g_dev_addr);
     ble_hs_pvcy_set_our_irk(NULL);
 
     return rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/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 5f9ad05..6b53dce 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -505,8 +505,8 @@ ble_sm_peer_addr(struct ble_sm_proc *proc,
         return BLE_HS_ENOTCONN;
     }
 
-    *out_type = conn->bhc_addr_type;
-    *out_addr = conn->bhc_addr;
+    *out_type = conn->bhc_peer_addr_type;
+    *out_addr = conn->bhc_peer_addr;
 
     return 0;
 }
@@ -562,8 +562,8 @@ ble_sm_persist_keys(struct ble_sm_proc *proc)
         peer_addr_type = proc->peer_keys.addr_type;
         memcpy(peer_addr, proc->peer_keys.addr, sizeof peer_addr);
     } else {
-        peer_addr_type = ble_hs_misc_addr_type_to_id(conn->bhc_addr_type);
-        memcpy(peer_addr, conn->bhc_addr, sizeof peer_addr);
+        peer_addr_type = ble_hs_misc_addr_type_to_id(conn->bhc_peer_addr_type);
+        memcpy(peer_addr, conn->bhc_peer_addr, sizeof peer_addr);
     }
 
     ble_hs_unlock();
@@ -1703,15 +1703,16 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
                      void *arg)
 {
     struct ble_sm_id_addr_info addr_info;
+    struct ble_hs_conn_addrs addrs;
     struct ble_sm_sign_info sign_info;
     struct ble_sm_master_id master_id;
     struct ble_sm_enc_info enc_info;
     struct ble_sm_id_info id_info;
+    struct ble_hs_conn *conn;
     uint8_t init_key_dist;
     uint8_t resp_key_dist;
     uint8_t our_key_dist;
-    uint8_t *our_id_addr;
-    uint8_t *irk;
+    const uint8_t *irk;
     int rc;
 
     ble_sm_key_dist(proc, &init_key_dist, &resp_key_dist);
@@ -1754,7 +1755,10 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
 
     if (our_key_dist & BLE_SM_PAIR_KEY_DIST_ID) {
         /* Send identity information. */
-        irk = ble_hs_pvcy_our_irk();
+        rc = ble_hs_pvcy_our_irk(&irk);
+        if (rc != 0) {
+            goto err;
+        }
 
         memcpy(id_info.irk, irk, 16);
 
@@ -1765,8 +1769,15 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
         proc->our_keys.irk_valid = 1;
 
         /* Send identity address information. */
-        our_id_addr = ble_hs_pvcy_our_id_addr(&addr_info.addr_type);
-        memcpy(addr_info.bd_addr, our_id_addr, 6);
+        conn = ble_hs_conn_find(proc->conn_handle);
+        if (conn == NULL) {
+            rc = BLE_HS_ENOTCONN;
+            goto err;
+        }
+
+        ble_hs_conn_addrs(conn, &addrs);
+        addr_info.addr_type = addrs.our_id_addr_type;
+        memcpy(addr_info.bd_addr, addrs.our_id_addr, 6);
         rc = ble_sm_id_addr_info_tx(proc->conn_handle, &addr_info);
         if (rc != 0) {
             goto err;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_sm_alg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_alg.c b/net/nimble/host/src/ble_sm_alg.c
index 1885d09..b8e8530 100644
--- a/net/nimble/host/src/ble_sm_alg.c
+++ b/net/nimble/host/src/ble_sm_alg.c
@@ -69,7 +69,7 @@ static const uint8_t ble_sm_alg_dbg_f6[16] = {
 };
 
 static void
-ble_sm_alg_log_buf(char *name, uint8_t *buf, int len)
+ble_sm_alg_log_buf(const char *name, const uint8_t *buf, int len)
 {
     BLE_HS_LOG(DEBUG, "    %s=", name);
     ble_hs_misc_log_flat_buf(buf, len);
@@ -362,9 +362,10 @@ ble_sm_alg_f5(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t a1t,
 }
 
 int
-ble_sm_alg_f6(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t *r,
-              uint8_t *iocap, uint8_t a1t, uint8_t *a1,
-              uint8_t a2t, uint8_t *a2, uint8_t *check)
+ble_sm_alg_f6(const uint8_t *w, const uint8_t *n1, const uint8_t *n2,
+              const uint8_t *r, const uint8_t *iocap, uint8_t a1t,
+              const uint8_t *a1, uint8_t a2t, const uint8_t *a2,
+              uint8_t *check)
 {
     uint8_t ws[16];
     uint8_t m[65];

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/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 71af1a3..95b312b 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -386,9 +386,10 @@ int ble_sm_alg_g2(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t *y,
 int ble_sm_alg_f5(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t a1t,
                   uint8_t *a1, uint8_t a2t, uint8_t *a2,
                   uint8_t *mackey, uint8_t *ltk);
-int ble_sm_alg_f6(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t *r,
-                  uint8_t *iocap, uint8_t a1t, uint8_t *a1,
-                  uint8_t a2t, uint8_t *a2, uint8_t *check);
+int ble_sm_alg_f6(const uint8_t *w, const uint8_t *n1, const uint8_t *n2,
+                  const uint8_t *r, const uint8_t *iocap, uint8_t a1t,
+                  const uint8_t *a1, uint8_t a2t, const uint8_t *a2,
+                  uint8_t *check);
 int ble_sm_alg_gen_dhkey(uint8_t *peer_pub_key_x, uint8_t *peer_pub_key_y,
                          uint32_t *our_priv_key, void *out_dhkey);
 int ble_sm_alg_gen_key_pair(void *pub, uint32_t *priv);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/ble_sm_sc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_sc.c b/net/nimble/host/src/ble_sm_sc.c
index fc56804..d98c93b 100644
--- a/net/nimble/host/src/ble_sm_sc.c
+++ b/net/nimble/host/src/ble_sm_sc.c
@@ -534,9 +534,9 @@ ble_sm_sc_public_key_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
 static int
 ble_sm_sc_dhkey_addrs(struct ble_sm_proc *proc,
                       uint8_t *out_our_id_addr_type,
-                      uint8_t **out_our_ota_addr,
+                      const uint8_t **out_our_ota_addr,
                       uint8_t *out_peer_id_addr_type,
-                      uint8_t **out_peer_ota_addr)
+                      const uint8_t **out_peer_ota_addr)
 {
     struct ble_hs_conn_addrs addrs;
     struct ble_hs_conn *conn;
@@ -569,8 +569,8 @@ ble_sm_sc_dhkey_check_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
                            void *arg)
 {
     struct ble_sm_dhkey_check cmd;
-    uint8_t *our_ota_addr;
-    uint8_t *peer_ota_addr;
+    const uint8_t *our_ota_addr;
+    const uint8_t *peer_ota_addr;
     uint8_t peer_id_addr_type;
     uint8_t our_id_addr_type;
     uint8_t iocap[3];
@@ -621,8 +621,8 @@ ble_sm_dhkey_check_process(struct ble_sm_proc *proc,
                            struct ble_sm_result *res)
 {
     uint8_t exp_value[16];
-    uint8_t *peer_ota_addr;
-    uint8_t *our_ota_addr;
+    const uint8_t *peer_ota_addr;
+    const uint8_t *our_ota_addr;
     uint8_t peer_id_addr_type;
     uint8_t our_id_addr_type;
     uint8_t iocap[3];

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/test/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gap_test.c b/net/nimble/host/src/test/ble_gap_test.c
index bb08bd0..43a0269 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -70,6 +70,7 @@ static void
 ble_gap_test_util_init(void)
 {
     ble_hs_test_util_init();
+    ble_hs_test_util_set_static_rnd_addr();
     ble_gap_test_util_reset_cb_info();
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/test/ble_hs_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_conn_test.c b/net/nimble/host/src/test/ble_hs_conn_test.c
index fcab787..02eba78 100644
--- a/net/nimble/host/src/test/ble_hs_conn_test.c
+++ b/net/nimble/host/src/test/ble_hs_conn_test.c
@@ -77,7 +77,7 @@ TEST_CASE(ble_hs_conn_test_direct_connect_success)
     conn = ble_hs_conn_first();
     TEST_ASSERT_FATAL(conn != NULL);
     TEST_ASSERT(conn->bhc_handle == 2);
-    TEST_ASSERT(memcmp(conn->bhc_addr, addr, 6) == 0);
+    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
 
     chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
     TEST_ASSERT_FATAL(chan != NULL);
@@ -131,7 +131,7 @@ TEST_CASE(ble_hs_conn_test_direct_connectable_success)
     conn = ble_hs_conn_first();
     TEST_ASSERT_FATAL(conn != NULL);
     TEST_ASSERT(conn->bhc_handle == 2);
-    TEST_ASSERT(memcmp(conn->bhc_addr, addr, 6) == 0);
+    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
 
     chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
     TEST_ASSERT_FATAL(chan != NULL);
@@ -191,7 +191,7 @@ TEST_CASE(ble_hs_conn_test_undirect_connectable_success)
     conn = ble_hs_conn_first();
     TEST_ASSERT_FATAL(conn != NULL);
     TEST_ASSERT(conn->bhc_handle == 2);
-    TEST_ASSERT(memcmp(conn->bhc_addr, addr, 6) == 0);
+    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
 
     chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
     TEST_ASSERT_FATAL(chan != NULL);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/test/ble_hs_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test.c b/net/nimble/host/src/test/ble_hs_test.c
index 5b38bb8..feabb72 100644
--- a/net/nimble/host/src/test/ble_hs_test.c
+++ b/net/nimble/host/src/test/ble_hs_test.c
@@ -24,7 +24,7 @@
 #include "ble_hs_test_util.h"
 
 /* Our global device address. */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
+uint8_t g_dev_addr[BLE_DEV_ADDR_LEN] = { 0x0a, 0x54, 0xab, 0x49, 0x7f, 0x06 };
 
 void
 ble_hs_test_pkt_txed(struct os_mbuf *om)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/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 e2040ae..fe701d8 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -25,6 +25,7 @@
 #include "nimble/hci_common.h"
 #include "nimble/hci_transport.h"
 #include "host/ble_hs_adv.h"
+#include "host/ble_hs_id.h"
 #include "host/host_hci.h"
 #include "ble_hs_test_util.h"
 
@@ -866,12 +867,6 @@ ble_hs_test_util_tx_all(void)
 }
 
 void
-ble_hs_test_util_set_public_addr(uint8_t *addr)
-{
-    ble_hs_pvcy_set_our_id_addr(addr);
-}
-
-void
 ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags)
 {
     struct ble_att_exec_write_req req;
@@ -913,6 +908,21 @@ ble_hs_test_util_verify_tx_read_rsp(uint8_t *attr_data, int attr_len)
 }
 
 void
+ble_hs_test_util_set_static_rnd_addr(void)
+{
+    uint8_t addr[6] = { 1, 2, 3, 4, 5, 0xc1 };
+    int rc;
+
+    ble_hs_test_util_set_ack(
+        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_RAND_ADDR), 0);
+
+    rc = ble_hs_id_set_rnd(addr);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_hs_test_util_get_first_hci_tx();
+}
+
+void
 ble_hs_test_util_init(void)
 {
     struct ble_hs_cfg cfg;
@@ -952,5 +962,5 @@ ble_hs_test_util_init(void)
 
     ble_hs_test_util_prev_hci_tx_clear();
 
-    ble_hs_test_util_set_public_addr(g_dev_addr);
+    ble_hs_id_set_pub(g_dev_addr);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/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 0a7c4e3..d54c8aa 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ b/net/nimble/host/src/test/ble_hs_test_util.h
@@ -115,9 +115,9 @@ void ble_hs_test_util_rx_disconn_complete_event(
 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_verify_tx_read_rsp(uint8_t *attr_data, int attr_len);
+void ble_hs_test_util_set_static_rnd_addr(void);
 void ble_hs_test_util_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/host/src/test/ble_sm_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test_util.c b/net/nimble/host/src/test/ble_sm_test_util.c
index 1cc8af9..524e8f1 100644
--- a/net/nimble/host/src/test/ble_sm_test_util.c
+++ b/net/nimble/host/src/test/ble_sm_test_util.c
@@ -26,6 +26,7 @@
 #include "host/host_hci.h"
 #include "host/ble_sm.h"
 #include "host/ble_hs_test.h"
+#include "host/ble_hs_id.h"
 #include "ble_hs_test_util.h"
 #include "ble_sm_test_util.h"
 
@@ -223,7 +224,7 @@ ble_sm_test_util_init_good(struct ble_sm_test_params *params,
         ble_hs_cfg.sm_their_key_dist = out_us->pair_cmd->init_key_dist;
     }
 
-    ble_hs_test_util_set_public_addr(out_us->id_addr);
+    ble_hs_id_set_pub(out_us->id_addr);
     ble_sm_dbg_set_next_pair_rand(out_us->randoms[0].value);
     ble_sm_dbg_set_next_ediv(out_us->ediv);
     ble_sm_dbg_set_next_master_id_rand(out_us->rand_num);
@@ -797,10 +798,14 @@ ble_sm_test_util_verify_tx_id_addr_info(struct ble_sm_id_addr_info *exp_cmd)
 {
     struct ble_sm_id_addr_info cmd;
     struct os_mbuf *om;
-    uint8_t *our_id_addr;
-    uint8_t our_id_addr_type;
+    const uint8_t *our_id_addr;
+    int rc;
+
+    ble_hs_lock();
+    rc = ble_hs_id_addr(exp_cmd->addr_type, &our_id_addr, NULL);
+    ble_hs_unlock();
 
-    our_id_addr = ble_hs_pvcy_our_id_addr(&our_id_addr_type);
+    TEST_ASSERT_FATAL(rc == 0);
 
     ble_hs_test_util_tx_all();
     om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_IDENTITY_ADDR_INFO,
@@ -809,8 +814,6 @@ ble_sm_test_util_verify_tx_id_addr_info(struct ble_sm_id_addr_info *exp_cmd)
 
     TEST_ASSERT(cmd.addr_type == exp_cmd->addr_type);
     TEST_ASSERT(memcmp(cmd.bd_addr, exp_cmd->bd_addr, 6) == 0);
-
-    TEST_ASSERT(cmd.addr_type == our_id_addr_type);
     TEST_ASSERT(memcmp(cmd.bd_addr, our_id_addr, 6) == 0);
 }
 
@@ -1405,7 +1408,7 @@ ble_sm_test_util_peer_fail_inval(
     struct ble_hs_conn *conn;
 
     ble_sm_test_util_init();
-    ble_hs_test_util_set_public_addr(resp_addr);
+    ble_hs_id_set_pub(resp_addr);
 
     ble_hs_test_util_create_conn(2, init_id_addr, ble_sm_test_util_conn_cb,
                                  NULL);
@@ -1462,7 +1465,7 @@ ble_sm_test_util_peer_lgcy_fail_confirm(
     struct ble_hs_conn *conn;
 
     ble_sm_test_util_init();
-    ble_hs_test_util_set_public_addr(resp_addr);
+    ble_hs_id_set_pub(resp_addr);
     ble_sm_dbg_set_next_pair_rand(random_rsp->value);
 
     ble_hs_test_util_create_conn(2, init_id_addr, ble_sm_test_util_conn_cb,
@@ -2329,7 +2332,7 @@ ble_sm_test_util_us_fail_inval(struct ble_sm_test_params *params)
     int rc;
 
     ble_sm_test_util_init();
-    ble_hs_test_util_set_public_addr(params->resp_id_addr);
+    ble_hs_id_set_pub(params->resp_id_addr);
 
     ble_sm_dbg_set_next_pair_rand(((uint8_t[16]){0}));
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/include/nimble/ble.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/ble.h b/net/nimble/include/nimble/ble.h
index ec030f5..1607eb8 100644
--- a/net/nimble/include/nimble/ble.h
+++ b/net/nimble/include/nimble/ble.h
@@ -150,7 +150,7 @@ uint16_t be16toh(const void *buf);
 uint32_t be32toh(const void *buf);
 uint64_t be64toh(const void *buf);
 void swap_in_place(void *buf, int len);
-void swap_buf(uint8_t *dst, uint8_t *src, int len);
+void swap_buf(uint8_t *dst, const uint8_t *src, int len);
 /* XXX */
 
 /* BLE Error Codes (Core v4.2 Vol 2 part D) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7efbf0ee/net/nimble/src/util.c
----------------------------------------------------------------------
diff --git a/net/nimble/src/util.c b/net/nimble/src/util.c
index 9d65002..9aef110 100644
--- a/net/nimble/src/util.c
+++ b/net/nimble/src/util.c
@@ -208,7 +208,7 @@ swap_in_place(void *buf, int len)
 
 /* swap octets */
 void
-swap_buf(uint8_t *dst, uint8_t *src, int len)
+swap_buf(uint8_t *dst, const uint8_t *src, int len)
 {
     int i;