Skip to content

Commit 4ad004f

Browse files
committed
put bonding to-do flags into Connection objects instead of using a heap-allocated queue
1 parent 9e7f874 commit 4ad004f

File tree

5 files changed

+84
-183
lines changed

5 files changed

+84
-183
lines changed

ports/nrf/common-hal/_bleio/Connection.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "shared-bindings/_bleio/Characteristic.h"
4747
#include "shared-bindings/_bleio/Service.h"
4848
#include "shared-bindings/_bleio/UUID.h"
49+
#include "supervisor/shared/tick.h"
4950

5051
#include "common-hal/_bleio/bonding.h"
5152

@@ -93,7 +94,6 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
9394

9495
switch (ble_evt->header.evt_id) {
9596
case BLE_GAP_EVT_DISCONNECTED:
96-
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_DISCONNECTED\n");
9797
// Adapter.c does the work for this event.
9898
break;
9999

@@ -130,7 +130,12 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
130130
self->pair_status == PAIR_PAIRED &&
131131
ble_evt->evt.gatts_evt.params.write.uuid.type == BLE_UUID_TYPE_BLE &&
132132
ble_evt->evt.gatts_evt.params.write.uuid.uuid == 0x2902) {
133-
bonding_save_cccd_info(self->is_central, self->conn_handle, self->ediv);
133+
//
134+
// Save sys_attr data (CCCD state) in bonding area at
135+
// next opportunity, but also remember time of this
136+
// request, so we can consolidate closely-spaced requests.
137+
self->do_bond_cccds = true;
138+
self->do_bond_cccds_request_time = supervisor_ticks_ms64();
134139
}
135140
break;
136141

@@ -194,7 +199,6 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
194199
break;
195200
}
196201
case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
197-
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_SEC_PARAMS_REQUEST\n");
198202
// First time pairing.
199203
// 1. Either we or peer initiate the process
200204
// 2. Peer asks for security parameters using BLE_GAP_EVT_SEC_PARAMS_REQUEST.
@@ -227,47 +231,40 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
227231
}
228232

229233
case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
230-
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_LESC_DHKEY_REQUEST\n");
231234
// TODO for LESC pairing:
232235
// sd_ble_gap_lesc_dhkey_reply(...);
233236
break;
234237

235238
case BLE_GAP_EVT_AUTH_STATUS: { // 0x19
236-
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_AUTH_STATUS\n");
237239
// Key exchange completed.
238240
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
239241
self->sec_status = status->auth_status;
240242
if (status->auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
241243
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
242244
self->pair_status = PAIR_PAIRED;
243-
CONNECTION_DEBUG_PRINTF("PAIR_PAIRED\n");
244-
bonding_save_keys(self->is_central, self->conn_handle, &self->bonding_keys);
245+
// Save keys in bonding area at next opportunity.
246+
self->do_bond_keys = true;
245247
} else {
246248
// Inform busy-waiter pairing has failed.
247249
self->pair_status = PAIR_NOT_PAIRED;
248-
CONNECTION_DEBUG_PRINTF("PAIR_NOT_PAIRED\n");
249250
}
250251
break;
251252
}
252253

253254
case BLE_GAP_EVT_SEC_INFO_REQUEST: { // 0x14
254-
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_SEC_INFO_REQUEST\n");
255255
// Peer asks for the stored keys.
256256
// - load key and return if bonded previously.
257257
// - Else return NULL --> Initiate key exchange
258258
ble_gap_evt_sec_info_request_t* sec_info_request = &ble_evt->evt.gap_evt.params.sec_info_request;
259259
(void) sec_info_request;
260260
if ( bonding_load_keys(self->is_central, sec_info_request->master_id.ediv, &self->bonding_keys) ) {
261-
CONNECTION_DEBUG_PRINTF("keys found\n");
262-
uint32_t err_code = sd_ble_gap_sec_info_reply(
261+
sd_ble_gap_sec_info_reply(
263262
self->conn_handle,
264263
&self->bonding_keys.own_enc.enc_info,
265264
&self->bonding_keys.peer_id.id_info,
266265
NULL);
267-
CONNECTION_DEBUG_PRINTF("sd_ble_gap_sec_info_reply err_code: %0lx\n", err_code);
268266
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
269267
} else {
270-
CONNECTION_DEBUG_PRINTF("keys not found\n");
271268
// We don't have stored keys. Ask for keys.
272269
sd_ble_gap_sec_info_reply(self->conn_handle, NULL, NULL, NULL);
273270
}
@@ -277,8 +274,6 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
277274
case BLE_GAP_EVT_CONN_SEC_UPDATE: { // 0x1a
278275
// We get this both on first-time pairing and on subsequent pairings using stored keys.
279276
ble_gap_conn_sec_t* conn_sec = &ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec;
280-
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_CONN_SEC_UPDATE, sm: %d, lv: %d\n",
281-
conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
282277
if (conn_sec->sec_mode.sm <= 1 && conn_sec->sec_mode.lv <= 1) {
283278
// Security setup did not succeed:
284279
// mode 0, level 0 means no access
@@ -289,10 +284,8 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
289284
if (bonding_load_cccd_info(self->is_central, self->conn_handle, self->ediv)) {
290285
// Did an sd_ble_gatts_sys_attr_set() with the stored sys_attr values.
291286
// Not quite paired yet: wait for BLE_GAP_EVT_AUTH_STATUS with BLE_GAP_SEC_STATUS_SUCCESS.
292-
CONNECTION_DEBUG_PRINTF("did bonding_load_cccd_info()\n");
293287
} else {
294288
// No matching bonding found, so use fresh system attributes.
295-
CONNECTION_DEBUG_PRINTF("bonding_load_cccd_info() failed\n");
296289
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
297290
}
298291
self->pair_status = PAIR_PAIRED;
@@ -301,10 +294,8 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
301294
}
302295

303296
default:
304-
CONNECTION_DEBUG_PRINTF("Unhandled event: %04x\n", ble_evt->header.evt_id);
305297
return false;
306298
}
307-
CONNECTION_DEBUG_PRINTF("Handled event: %04x\n", ble_evt->header.evt_id);
308299
return true;
309300
}
310301

@@ -590,8 +581,7 @@ STATIC bool discovery_on_ble_evt(ble_evt_t *ble_evt, mp_obj_t payload) {
590581
break;
591582

592583
default:
593-
// For debugging.
594-
// mp_printf(&mp_plat_print, "Unhandled discovery event: 0x%04x\n", ble_evt->header.evt_id);
584+
// CONNECTION_DEBUG_PRINTF(&mp_plat_print, "Unhandled discovery event: 0x%04x\n", ble_evt->header.evt_id);
595585
return false;
596586
break;
597587
}

ports/nrf/common-hal/_bleio/Connection.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,22 @@ typedef struct {
5757
// The advertising data and scan response buffers are held by us, not by the SD, so we must
5858
// maintain them and not change it. If we need to change the contents during advertising,
5959
// there are tricks to get the SD to notice (see DevZone - TBS).
60-
// EDIV: Encrypted Diversifier: Identifies LTK during legacy pairing.
6160
bonding_keys_t bonding_keys;
61+
// EDIV: Encrypted Diversifier: Identifies LTK during legacy pairing.
6262
uint16_t ediv;
6363
volatile pair_status_t pair_status;
6464
uint8_t sec_status; // Internal security status.
6565
mp_obj_t connection_obj;
6666
ble_drv_evt_handler_entry_t handler_entry;
6767
ble_gap_conn_params_t conn_params;
6868
volatile bool conn_params_updating;
69+
// Request that CCCD info for this connection be saved.
70+
volatile bool do_bond_cccds;
71+
// Request that security key info for this connection be saved.
72+
volatile bool do_bond_keys;
73+
// Time of setting do_bond_ccds: we delay a bit to consolidate multiple CCCD changes
74+
// into one write. Time is currently in ticks_ms.
75+
uint64_t do_bond_cccds_request_time;
6976
} bleio_connection_internal_t;
7077

7178
typedef struct {

0 commit comments

Comments
 (0)