nimble/host: Add support for privacy modes

With this patch application can set privacy mode for peer devices
as per Bluetooth 5.0 specification Vol 6, Part B, chapter 4.7


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

Branch: refs/heads/bluetooth5
Commit: 674af9341189c3996bfbf966605045a1d05e59fe
Parents: 0536012
Author: Łukasz Rymanowski <[email protected]>
Authored: Wed Apr 5 15:34:59 2017 +0200
Committer: Łukasz Rymanowski <[email protected]>
Committed: Thu Apr 6 09:46:45 2017 +0200

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gap.h |  4 +++
 net/nimble/host/src/ble_gap.c          |  6 ++++
 net/nimble/host/src/ble_hs_hci_cmd.c   | 46 +++++++++++++++++++++++++++++
 net/nimble/host/src/ble_hs_hci_priv.h  |  3 ++
 net/nimble/host/src/ble_hs_pvcy.c      | 15 ++++++++++
 net/nimble/host/src/ble_hs_pvcy_priv.h |  1 +
 6 files changed, 75 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/674af934/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 189244a..f40789a 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -566,6 +566,10 @@ int ble_gap_encryption_initiate(uint16_t conn_handle, 
const uint8_t *ltk,
                                 uint16_t ediv, uint64_t rand_val, int auth);
 int ble_gap_conn_rssi(uint16_t conn_handle, int8_t *out_rssi);
 
+#define BLE_GAP_PRIVATE_MODE_NETWORK        0
+#define BLE_GAP_PRIVATE_MODE_DEVICE         1
+int ble_gap_set_priv_mode(const ble_addr_t *peer_addr, uint8_t priv_mode);
+
 #ifdef __cplusplus
 }
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/674af934/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 80bfa7a..6318a9e 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -431,6 +431,12 @@ ble_gap_extract_conn_cb(uint16_t conn_handle,
     }
 }
 
+int
+ble_gap_set_priv_mode(const ble_addr_t *peer_addr, uint8_t priv_mode)
+{
+    return ble_hs_pvcy_set_mode(peer_addr, priv_mode);
+}
+
 /*****************************************************************************
  * $misc                                                                     *
  *****************************************************************************/

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/674af934/net/nimble/host/src/ble_hs_hci_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_cmd.c 
b/net/nimble/host/src/ble_hs_hci_cmd.c
index bbef1a0..6de599f 100644
--- a/net/nimble/host/src/ble_hs_hci_cmd.c
+++ b/net/nimble/host/src/ble_hs_hci_cmd.c
@@ -1336,6 +1336,52 @@ ble_hs_hci_cmd_build_set_resolv_priv_addr_timeout(
         timeout, dst + BLE_HCI_CMD_HDR_LEN);
 }
 
+
+static int
+ble_hs_hci_cmd_body_le_set_priv_mode(const uint8_t *addr, uint8_t addr_type,
+                                     uint8_t priv_mode, uint8_t *dst)
+{
+    /* In this command addr type can be either:
+     *  - public identity (0x00)
+     *  - random (static) identity (0x01)
+     */
+    if (addr_type > BLE_ADDR_RANDOM) {
+        return BLE_ERR_INV_HCI_CMD_PARMS;
+    }
+
+    dst[0] = addr_type;
+    memcpy(dst + 1, addr, BLE_DEV_ADDR_LEN);
+    dst[7] = priv_mode;
+
+    return 0;
+}
+
+/*
+ *  OGF=0x08 OCF=0x004e
+ */
+int
+ble_hs_hci_build_le_set_priv_mode(const uint8_t *addr, uint8_t addr_type,
+                                  uint8_t priv_mode, uint8_t *dst,
+                                  uint16_t dst_len)
+{
+    int rc;
+
+    BLE_HS_DBG_ASSERT(
+        dst_len >= BLE_HCI_CMD_HDR_LEN + BLE_HCI_LE_SET_PRIVACY_MODE_LEN);
+
+    ble_hs_hci_cmd_write_hdr(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_PRIVACY_MODE,
+                             BLE_HCI_LE_SET_PRIVACY_MODE_LEN, dst);
+    dst += BLE_HCI_CMD_HDR_LEN;
+
+    rc = ble_hs_hci_cmd_body_le_set_priv_mode(addr, addr_type, priv_mode, dst);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+
+
+}
 static int
 ble_hs_hci_cmd_body_set_random_addr(const struct hci_rand_addr *paddr,
                                     uint8_t *dst)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/674af934/net/nimble/host/src/ble_hs_hci_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_priv.h 
b/net/nimble/host/src/ble_hs_hci_priv.h
index bdc5457..b3be0bc 100644
--- a/net/nimble/host/src/ble_hs_hci_priv.h
+++ b/net/nimble/host/src/ble_hs_hci_priv.h
@@ -159,6 +159,9 @@ int ble_hs_hci_cmd_build_set_resolv_priv_addr_timeout(
 int ble_hs_hci_cmd_build_set_random_addr(const uint8_t *addr,
                                          uint8_t *dst, int dst_len);
 
+int ble_hs_hci_build_le_set_priv_mode(const uint8_t *addr, uint8_t addr_type,
+                                      uint8_t priv_mode, uint8_t *dst,
+                                      uint16_t dst_len);
 #ifdef __cplusplus
 }
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/674af934/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 3a6d974..be3369c 100644
--- a/net/nimble/host/src/ble_hs_pvcy.c
+++ b/net/nimble/host/src/ble_hs_pvcy.c
@@ -205,3 +205,18 @@ ble_hs_pvcy_our_irk(const uint8_t **out_irk)
     *out_irk = ble_hs_pvcy_irk;
     return 0;
 }
+
+int
+ble_hs_pvcy_set_mode(const ble_addr_t *addr, uint8_t priv_mode)
+{
+    uint8_t buf[BLE_HCI_CMD_HDR_LEN + BLE_HCI_LE_SET_PRIVACY_MODE_LEN];
+    int rc;
+
+    rc = ble_hs_hci_build_le_set_priv_mode(addr->val, addr->type, priv_mode,
+                                           buf, sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    return ble_hs_hci_cmd_tx(buf, NULL, 0, NULL);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/674af934/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 62689d3..2a72b0b 100644
--- a/net/nimble/host/src/ble_hs_pvcy_priv.h
+++ b/net/nimble/host/src/ble_hs_pvcy_priv.h
@@ -31,6 +31,7 @@ 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);
+int ble_hs_pvcy_set_mode(const ble_addr_t *addr, uint8_t priv_mode);
 
 #ifdef __cplusplus
 }

Reply via email to