46
46
#include "shared-bindings/_bleio/Characteristic.h"
47
47
#include "shared-bindings/_bleio/Service.h"
48
48
#include "shared-bindings/_bleio/UUID.h"
49
+ #include "supervisor/shared/tick.h"
49
50
50
51
#include "common-hal/_bleio/bonding.h"
51
52
@@ -93,7 +94,6 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
93
94
94
95
switch (ble_evt -> header .evt_id ) {
95
96
case BLE_GAP_EVT_DISCONNECTED :
96
- CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_DISCONNECTED\n" );
97
97
// Adapter.c does the work for this event.
98
98
break ;
99
99
@@ -130,7 +130,12 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
130
130
self -> pair_status == PAIR_PAIRED &&
131
131
ble_evt -> evt .gatts_evt .params .write .uuid .type == BLE_UUID_TYPE_BLE &&
132
132
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 ();
134
139
}
135
140
break ;
136
141
@@ -194,7 +199,6 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
194
199
break ;
195
200
}
196
201
case BLE_GAP_EVT_SEC_PARAMS_REQUEST : {
197
- CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_SEC_PARAMS_REQUEST\n" );
198
202
// First time pairing.
199
203
// 1. Either we or peer initiate the process
200
204
// 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) {
227
231
}
228
232
229
233
case BLE_GAP_EVT_LESC_DHKEY_REQUEST :
230
- CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_LESC_DHKEY_REQUEST\n" );
231
234
// TODO for LESC pairing:
232
235
// sd_ble_gap_lesc_dhkey_reply(...);
233
236
break ;
234
237
235
238
case BLE_GAP_EVT_AUTH_STATUS : { // 0x19
236
- CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_AUTH_STATUS\n" );
237
239
// Key exchange completed.
238
240
ble_gap_evt_auth_status_t * status = & ble_evt -> evt .gap_evt .params .auth_status ;
239
241
self -> sec_status = status -> auth_status ;
240
242
if (status -> auth_status == BLE_GAP_SEC_STATUS_SUCCESS ) {
241
243
self -> ediv = self -> bonding_keys .own_enc .master_id .ediv ;
242
244
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 ;
245
247
} else {
246
248
// Inform busy-waiter pairing has failed.
247
249
self -> pair_status = PAIR_NOT_PAIRED ;
248
- CONNECTION_DEBUG_PRINTF ("PAIR_NOT_PAIRED\n" );
249
250
}
250
251
break ;
251
252
}
252
253
253
254
case BLE_GAP_EVT_SEC_INFO_REQUEST : { // 0x14
254
- CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_SEC_INFO_REQUEST\n" );
255
255
// Peer asks for the stored keys.
256
256
// - load key and return if bonded previously.
257
257
// - Else return NULL --> Initiate key exchange
258
258
ble_gap_evt_sec_info_request_t * sec_info_request = & ble_evt -> evt .gap_evt .params .sec_info_request ;
259
259
(void ) sec_info_request ;
260
260
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 (
263
262
self -> conn_handle ,
264
263
& self -> bonding_keys .own_enc .enc_info ,
265
264
& self -> bonding_keys .peer_id .id_info ,
266
265
NULL );
267
- CONNECTION_DEBUG_PRINTF ("sd_ble_gap_sec_info_reply err_code: %0lx\n" , err_code );
268
266
self -> ediv = self -> bonding_keys .own_enc .master_id .ediv ;
269
267
} else {
270
- CONNECTION_DEBUG_PRINTF ("keys not found\n" );
271
268
// We don't have stored keys. Ask for keys.
272
269
sd_ble_gap_sec_info_reply (self -> conn_handle , NULL , NULL , NULL );
273
270
}
@@ -277,8 +274,6 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
277
274
case BLE_GAP_EVT_CONN_SEC_UPDATE : { // 0x1a
278
275
// We get this both on first-time pairing and on subsequent pairings using stored keys.
279
276
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 );
282
277
if (conn_sec -> sec_mode .sm <= 1 && conn_sec -> sec_mode .lv <= 1 ) {
283
278
// Security setup did not succeed:
284
279
// mode 0, level 0 means no access
@@ -289,10 +284,8 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
289
284
if (bonding_load_cccd_info (self -> is_central , self -> conn_handle , self -> ediv )) {
290
285
// Did an sd_ble_gatts_sys_attr_set() with the stored sys_attr values.
291
286
// 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" );
293
287
} else {
294
288
// No matching bonding found, so use fresh system attributes.
295
- CONNECTION_DEBUG_PRINTF ("bonding_load_cccd_info() failed\n" );
296
289
sd_ble_gatts_sys_attr_set (self -> conn_handle , NULL , 0 , 0 );
297
290
}
298
291
self -> pair_status = PAIR_PAIRED ;
@@ -301,10 +294,8 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
301
294
}
302
295
303
296
default :
304
- CONNECTION_DEBUG_PRINTF ("Unhandled event: %04x\n" , ble_evt -> header .evt_id );
305
297
return false;
306
298
}
307
- CONNECTION_DEBUG_PRINTF ("Handled event: %04x\n" , ble_evt -> header .evt_id );
308
299
return true;
309
300
}
310
301
@@ -590,8 +581,7 @@ STATIC bool discovery_on_ble_evt(ble_evt_t *ble_evt, mp_obj_t payload) {
590
581
break ;
591
582
592
583
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);
595
585
return false;
596
586
break ;
597
587
}
0 commit comments