@@ -171,6 +171,15 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
171
171
break ;
172
172
}
173
173
case BLE_GAP_EVT_SEC_PARAMS_REQUEST : {
174
+ // First time pairing.
175
+ // 1. Either we or peer initiate the process
176
+ // 2. Peer asks for security parameters using BLE_GAP_EVT_SEC_PARAMS_REQUEST.
177
+ // 3. Pair Key exchange ("just works" implemented now; TODO key pairing)
178
+ // 4. Connection is secured: BLE_GAP_EVT_CONN_SEC_UPDATE
179
+ // 5. Long-term Keys exchanged: BLE_GAP_EVT_AUTH_STATUS
180
+
181
+ bonding_clear_keys (& self -> bonding_keys );
182
+ self -> ediv = EDIV_INVALID ;
174
183
ble_gap_sec_keyset_t keyset = {
175
184
.keys_own = {
176
185
.p_enc_key = & self -> bonding_keys .own_enc ,
@@ -188,7 +197,8 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
188
197
};
189
198
190
199
sd_ble_gap_sec_params_reply (self -> conn_handle , BLE_GAP_SEC_STATUS_SUCCESS ,
191
- & pairing_sec_params , & keyset );
200
+ self -> is_central ? NULL : & pairing_sec_params ,
201
+ & keyset );
192
202
break ;
193
203
}
194
204
@@ -202,8 +212,9 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
202
212
ble_gap_evt_auth_status_t * status = & ble_evt -> evt .gap_evt .params .auth_status ;
203
213
self -> sec_status = status -> auth_status ;
204
214
if (status -> auth_status == BLE_GAP_SEC_STATUS_SUCCESS ) {
205
- // TODO _ediv = bonding_keys->own_enc.master_id.ediv;
215
+ self -> ediv = bonding_keys -> own_enc .master_id .ediv ;
206
216
self -> pair_status = PAIR_PAIRED ;
217
+ bonding_save_keys (self -> is_central , self -> conn_handle , & self -> bonding_keys );
207
218
} else {
208
219
self -> pair_status = PAIR_NOT_PAIRED ;
209
220
}
@@ -216,14 +227,17 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
216
227
// - Else return NULL --> Initiate key exchange
217
228
ble_gap_evt_sec_info_request_t * sec_info_request = & ble_evt -> evt .gap_evt .params .sec_info_request ;
218
229
(void ) sec_info_request ;
219
- //if ( bond_load_keys(_role, sec_req->master_id.ediv, &bkeys) ) {
220
- //sd_ble_gap_sec_info_reply(_conn_hdl, &bkeys.own_enc.enc_info, &bkeys.peer_id.id_info, NULL);
221
- //
222
- //_ediv = bkeys.own_enc.master_id.ediv;
223
- // } else {
230
+ bond_keys bond_keys_t ;
231
+ if ( bonding_load_keys (self -> is_central , sec_info_request -> master_id .ediv , & self -> bonding_keys ) ) {
232
+ sd_ble_gap_sec_info_reply (self -> conn_handle
233
+ & self -> bonding_keys .own_enc .enc_info ,
234
+ & self -> bonding_keys .peer_id .id_info ,
235
+ NULL );
236
+ self -> ediv = bond_keys .own_enc .master_id .ediv ;
237
+ } else {
224
238
sd_ble_gap_sec_info_reply (self -> conn_handle , NULL , NULL , NULL );
225
- // }
226
- break ;
239
+ }
240
+ break ;
227
241
}
228
242
229
243
case BLE_GAP_EVT_CONN_SEC_UPDATE : { // 0x1a
@@ -235,17 +249,23 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
235
249
// mode >=1 and/or level >=1 means encryption is set up
236
250
self -> pair_status = PAIR_NOT_PAIRED ;
237
251
} else {
238
- //if ( !bond_load_cccd(_role, _conn_hdl, _ediv) ) {
239
- if (true) { // TODO: no bonding yet
240
- // Initialize system attributes fresh.
241
- sd_ble_gatts_sys_attr_set (self -> conn_handle , NULL , 0 , 0 );
242
- }
252
+ uint8_t * sys_attr ;
253
+ uint16_t sys_attr_len ;
254
+ if (bonding_load_cccd_info (self -> is_central , self -> conn_handle , self -> ediv , sys_attr , sys_attr_len )) {
255
+ sd_ble_gatts_sys_attr_set (self -> conn_handle , sys_attr , sys_attr_len , SVC_CONTEXT_FLAG );
243
256
// Not quite paired yet: wait for BLE_GAP_EVT_AUTH_STATUS SUCCESS.
244
257
self -> ediv = self -> bonding_keys .own_enc .master_id .ediv ;
258
+ } else {
259
+ // No matching bonding found, so use fresh system attributes.
260
+ sd_ble_gatts_sys_attr_set (self -> conn_handle , NULL , 0 , 0 );
261
+ }
245
262
}
246
263
break ;
247
264
}
248
265
266
+ case BLE_GATTS_EVT_WRITE : {
267
+ if (self -> pair_status == PAIR_PAIRED ) & &
268
+
249
269
250
270
default :
251
271
return false;
@@ -258,8 +278,7 @@ void bleio_connection_clear(bleio_connection_internal_t *self) {
258
278
259
279
self -> conn_handle = BLE_CONN_HANDLE_INVALID ;
260
280
self -> pair_status = PAIR_NOT_PAIRED ;
261
-
262
- memset (& self -> bonding_keys , 0 , sizeof (self -> bonding_keys ));
281
+ bonding_clear_keys (self );
263
282
}
264
283
265
284
bool common_hal_bleio_connection_get_paired (bleio_connection_obj_t * self ) {
@@ -480,7 +499,7 @@ STATIC void on_desc_discovery_rsp(ble_gattc_evt_desc_disc_rsp_t *response, bleio
480
499
default :
481
500
// TODO: sd_ble_gattc_descriptors_discover() can return things that are not descriptors,
482
501
// so ignore those.
483
- // https: //devzone.nordicsemi.com/f/nordic-q-a/49500/sd_ble_gattc_descriptors_discover-is-returning-attributes-that-are-not-descriptors
502
+ // htts:p //devzone.nordicsemi.com/f/nordic-q-a/49500/sd_ble_gattc_descriptors_discover-is-returning-attributes-that-are-not-descriptors
484
503
break ;
485
504
}
486
505
0 commit comments