Skip to content

Commit

Permalink
nimble/gap: Fix storing CCC for bonded devices.
Browse files Browse the repository at this point in the history
This PR make sure that ble_gatts_bonding_established is called only when
bonded.

Note: renamed not used "persist_keys" to "bonded" and make use of it

Bug can be easly reproduced when
BLE_SM_BONDING: 0
BLE_HS_DEBUG: 1

Program received signal SIGTRAP, Trace/breakpoint trap.
ble_gatts_bonding_established (conn_handle=conn_handle@entry=1) at repos/apache-mynewt-nimble/nimble/host/src/ble_gatts.c:1681
1681	    BLE_HS_DBG_ASSERT(conn->bhc_sec_state.bonded);
(gdb)
  • Loading branch information
rymanluk committed Apr 21, 2020
1 parent 6ec9b6b commit 327cbc7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
27 changes: 20 additions & 7 deletions nimble/host/src/ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -5654,7 +5654,8 @@ ble_gap_passkey_event(uint16_t conn_handle,
}

void
ble_gap_enc_event(uint16_t conn_handle, int status, int security_restored)
ble_gap_enc_event(uint16_t conn_handle, int status,
int security_restored, int bonded)
{
#if NIMBLE_BLE_SM
struct ble_gap_event event;
Expand All @@ -5667,12 +5668,24 @@ ble_gap_enc_event(uint16_t conn_handle, int status, int security_restored)
ble_gap_event_listener_call(&event);
ble_gap_call_conn_event_cb(&event, conn_handle);

if (status == 0) {
if (security_restored) {
ble_gatts_bonding_restored(conn_handle);
} else {
ble_gatts_bonding_established(conn_handle);
}
if (status != 0) {
return;
}

/* If encryption succeded and encryption has been restored for bonded device,
* notify gatt server so it has chance to send notification/indication if needed.
*/
if (security_restored) {
ble_gatts_bonding_restored(conn_handle);
return;
}

/* If this is fresh pairing and bonding has been established,
* notify gatt server about that so previous subscriptions (before bonding)
* can be stored.
*/
if (bonded) {
ble_gatts_bonding_established(conn_handle);
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion nimble/host/src/ble_gap_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int ble_gap_rx_l2cap_update_req(uint16_t conn_handle,
struct ble_gap_upd_params *params);
void ble_gap_rx_phy_update_complete(const struct ble_hci_ev_le_subev_phy_update_complete *ev);
void ble_gap_enc_event(uint16_t conn_handle, int status,
int security_restored);
int security_restored, int bonded);
void ble_gap_passkey_event(uint16_t conn_handle,
struct ble_gap_passkey_params *passkey_params);
void ble_gap_notify_rx_event(uint16_t conn_handle, uint16_t attr_handle,
Expand Down
5 changes: 3 additions & 2 deletions nimble/host/src/ble_sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res)

if (res->enc_cb) {
BLE_HS_DBG_ASSERT(proc == NULL || rm);
ble_gap_enc_event(conn_handle, res->app_status, res->restore);
ble_gap_enc_event(conn_handle, res->app_status, res->restore, res->bonded);
}

if (res->app_status == 0 &&
Expand Down Expand Up @@ -1190,6 +1190,7 @@ ble_sm_enc_event_rx(uint16_t conn_handle, uint8_t evt_status, int encrypted)

ble_hs_unlock();

res.bonded = bonded;
ble_sm_process_result(conn_handle, &res);
}

Expand Down Expand Up @@ -2425,7 +2426,7 @@ ble_sm_timer(void)
* procedures without reconnect.
*/
while ((proc = STAILQ_FIRST(&exp_list)) != NULL) {
ble_gap_enc_event(proc->conn_handle, BLE_HS_ETIMEOUT, 0);
ble_gap_enc_event(proc->conn_handle, BLE_HS_ETIMEOUT, 0, 0);

STAILQ_REMOVE_HEAD(&exp_list, next);
ble_sm_proc_free(proc);
Expand Down
2 changes: 1 addition & 1 deletion nimble/host/src/ble_sm_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ struct ble_sm_result {
void *state_arg;
unsigned execute:1;
unsigned enc_cb:1;
unsigned persist_keys:1;
unsigned bonded:1;
unsigned restore:1;
};

Expand Down

0 comments on commit 327cbc7

Please sign in to comment.