@@ -171,6 +171,15 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
171171 break ;
172172 }
173173 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 ;
174183 ble_gap_sec_keyset_t keyset = {
175184 .keys_own = {
176185 .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) {
188197 };
189198
190199 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 );
192202 break ;
193203 }
194204
@@ -202,8 +212,9 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
202212 ble_gap_evt_auth_status_t * status = & ble_evt -> evt .gap_evt .params .auth_status ;
203213 self -> sec_status = status -> auth_status ;
204214 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 ;
206216 self -> pair_status = PAIR_PAIRED ;
217+ bonding_save_keys (self -> is_central , self -> conn_handle , & self -> bonding_keys );
207218 } else {
208219 self -> pair_status = PAIR_NOT_PAIRED ;
209220 }
@@ -216,14 +227,17 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
216227 // - Else return NULL --> Initiate key exchange
217228 ble_gap_evt_sec_info_request_t * sec_info_request = & ble_evt -> evt .gap_evt .params .sec_info_request ;
218229 (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 {
224238 sd_ble_gap_sec_info_reply (self -> conn_handle , NULL , NULL , NULL );
225- // }
226- break ;
239+ }
240+ break ;
227241 }
228242
229243 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) {
235249 // mode >=1 and/or level >=1 means encryption is set up
236250 self -> pair_status = PAIR_NOT_PAIRED ;
237251 } 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 );
243256 // Not quite paired yet: wait for BLE_GAP_EVT_AUTH_STATUS SUCCESS.
244257 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+ }
245262 }
246263 break ;
247264 }
248265
266+ case BLE_GATTS_EVT_WRITE : {
267+ if (self -> pair_status == PAIR_PAIRED ) & &
268+
249269
250270 default :
251271 return false;
@@ -258,8 +278,7 @@ void bleio_connection_clear(bleio_connection_internal_t *self) {
258278
259279 self -> conn_handle = BLE_CONN_HANDLE_INVALID ;
260280 self -> pair_status = PAIR_NOT_PAIRED ;
261-
262- memset (& self -> bonding_keys , 0 , sizeof (self -> bonding_keys ));
281+ bonding_clear_keys (self );
263282}
264283
265284bool 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
480499 default :
481500 // TODO: sd_ble_gattc_descriptors_discover() can return things that are not descriptors,
482501 // 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
484503 break ;
485504 }
486505
0 commit comments