nimble/sm: Update peer's identity after pairing After pairing, when Identity Address Information is received, we update peer's identity in conn and dispatch this information using new event so application knows that we now know peer's identity address and not only RPA.
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/f52185c2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/f52185c2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/f52185c2 Branch: refs/heads/develop Commit: f52185c2494d2945b6814fb4692c0ade229ae9aa Parents: 6d0e7fb Author: Andrzej Kaczmarek <[email protected]> Authored: Wed Feb 1 17:14:51 2017 +0100 Committer: Andrzej Kaczmarek <[email protected]> Committed: Wed Feb 1 22:24:57 2017 +0100 ---------------------------------------------------------------------- net/nimble/host/include/host/ble_gap.h | 13 ++++++++++++ net/nimble/host/src/ble_gap.c | 17 ++++++++++++++++ net/nimble/host/src/ble_gap_priv.h | 1 + net/nimble/host/src/ble_sm.c | 31 +++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f52185c2/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 6a4cee2..1c0c93e 100644 --- a/net/nimble/host/include/host/ble_gap.h +++ b/net/nimble/host/include/host/ble_gap.h @@ -110,6 +110,7 @@ struct hci_conn_update; #define BLE_GAP_EVENT_NOTIFY_TX 13 #define BLE_GAP_EVENT_SUBSCRIBE 14 #define BLE_GAP_EVENT_MTU 15 +#define BLE_GAP_EVENT_IDENTITY_RESOLVED 16 /*** Reason codes for the subscribe GAP event. */ @@ -504,6 +505,18 @@ struct ble_gap_event { /* The channel's new MTU. */ uint16_t value; } mtu; + + /** + * Represents a change in peer's identity. This is issued after + * successful pairing when Identity Address Information was received. + * + * Valid for the following event types: + * o BLE_GAP_EVENT_IDENTITY_RESOLVED + */ + struct { + /** The handle of the relevant connection. */ + uint16_t conn_handle; + } identity_resolved; }; }; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f52185c2/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 be9865e..c85ff62 100644 --- a/net/nimble/host/src/ble_gap.c +++ b/net/nimble/host/src/ble_gap.c @@ -3103,6 +3103,23 @@ ble_gap_enc_event(uint16_t conn_handle, int status, int security_restored) } } +void +ble_gap_identity_event(uint16_t conn_handle) +{ +#if !NIMBLE_BLE_SM + return; +#endif + + struct ble_gap_event event; + + BLE_HS_LOG(DEBUG, "send identity changed"); + + memset(&event, 0, sizeof event); + event.type = BLE_GAP_EVENT_IDENTITY_RESOLVED; + event.identity_resolved.conn_handle = conn_handle; + ble_gap_call_conn_event_cb(&event, conn_handle); +} + /***************************************************************************** * $rssi * *****************************************************************************/ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f52185c2/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 c67efe4..2e2bc75 100644 --- a/net/nimble/host/src/ble_gap_priv.h +++ b/net/nimble/host/src/ble_gap_priv.h @@ -94,6 +94,7 @@ void ble_gap_subscribe_event(uint16_t conn_handle, uint16_t attr_handle, uint8_t prev_notify, uint8_t cur_notify, uint8_t prev_indicate, uint8_t cur_indicate); void ble_gap_mtu_event(uint16_t conn_handle, uint16_t cid, uint16_t mtu); +void ble_gap_identity_event(uint16_t conn_handle); int ble_gap_master_in_progress(void); void ble_gap_conn_broken(uint16_t conn_handle, int reason); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f52185c2/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 c19c80b..fe312d4 100644 --- a/net/nimble/host/src/ble_sm.c +++ b/net/nimble/host/src/ble_sm.c @@ -505,6 +505,7 @@ ble_sm_persist_keys(struct ble_sm_proc *proc) struct ble_hs_conn *conn; ble_addr_t peer_addr; int authenticated; + int identity_ev = 0; ble_hs_lock(); @@ -515,6 +516,32 @@ ble_sm_persist_keys(struct ble_sm_proc *proc) if (proc->peer_keys.addr_valid) { peer_addr.type = proc->peer_keys.addr_type; memcpy(peer_addr.val, proc->peer_keys.addr, sizeof peer_addr); + + /* Update identity address in conn. + * If peer's address was an RPA, we store it as RPA since peer's address + * will not be an identity address. The peer's address type has to be + * set as 'ID' to allow resolve 'id' and 'ota' addresses properly in + * conn info. + */ + if (BLE_ADDR_IS_RPA(&conn->bhc_peer_addr)) { + conn->bhc_peer_rpa_addr = conn->bhc_peer_addr; + } + + conn->bhc_peer_addr = peer_addr; + + switch (peer_addr.type) { + case BLE_ADDR_PUBLIC: + case BLE_ADDR_PUBLIC_ID: + conn->bhc_peer_addr.type = BLE_ADDR_PUBLIC_ID; + break; + + case BLE_ADDR_RANDOM: + case BLE_ADDR_RANDOM_ID: + conn->bhc_peer_addr.type = BLE_ADDR_RANDOM_ID; + break; + } + + identity_ev = 1; } else { peer_addr = conn->bhc_peer_addr; peer_addr.type = ble_hs_misc_addr_type_to_id(conn->bhc_peer_addr.type); @@ -522,6 +549,10 @@ ble_sm_persist_keys(struct ble_sm_proc *proc) ble_hs_unlock(); + if (identity_ev) { + ble_gap_identity_event(proc->conn_handle); + } + authenticated = proc->flags & BLE_SM_PROC_F_AUTHENTICATED; ble_sm_fill_store_value(peer_addr.type, peer_addr.val, authenticated,
