Skip to content

Commit 346ce3b

Browse files
committed
wip: HID bonding works!
1 parent 9c167af commit 346ce3b

File tree

7 files changed

+170
-91
lines changed

7 files changed

+170
-91
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ STATIC bool adapter_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
224224
}
225225
ble_drv_remove_event_handler(connection_on_ble_evt, connection);
226226
connection->conn_handle = BLE_CONN_HANDLE_INVALID;
227+
connection->pair_status = PAIR_NOT_PAIRED;
227228
if (connection->connection_obj != mp_const_none) {
228229
bleio_connection_obj_t* obj = connection->connection_obj;
229230
obj->connection = NULL;
@@ -657,6 +658,10 @@ mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self) {
657658
return self->connection_objs;
658659
}
659660

661+
void common_hal_bleio_adapter_erase_bonding(bleio_adapter_obj_t *self) {
662+
bonding_erase_storage();
663+
}
664+
660665
void bleio_adapter_gc_collect(bleio_adapter_obj_t* adapter) {
661666
gc_collect_root((void**)adapter, sizeof(bleio_adapter_obj_t) / sizeof(size_t));
662667
gc_collect_root((void**)connections, sizeof(connections) / sizeof(size_t));

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

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#define BLE_AD_TYPE_FLAGS_DATA_SIZE 1
5555

5656
static const ble_gap_sec_params_t pairing_sec_params = {
57-
.bond = 0,
57+
.bond = 1,
5858
.mitm = 0,
5959
.lesc = 0,
6060
.keypress = 0,
@@ -66,6 +66,13 @@ static const ble_gap_sec_params_t pairing_sec_params = {
6666
.kdist_peer = { .enc = 1, .id = 1},
6767
};
6868

69+
#define CONNECTION_DEBUG (1)
70+
#if CONNECTION_DEBUG
71+
#define CONNECTION_DEBUG_PRINTF(...) printf(__VA_ARGS__)
72+
#else
73+
#define CONNECTION_DEBUG_PRINTF(...)
74+
#endif
75+
6976
static volatile bool m_discovery_in_process;
7077
static volatile bool m_discovery_successful;
7178

@@ -86,7 +93,10 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
8693

8794
switch (ble_evt->header.evt_id) {
8895
case BLE_GAP_EVT_DISCONNECTED:
96+
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_DISCONNECTED\n");
97+
// Adapter.c does the work for this event.
8998
break;
99+
90100
case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
91101
ble_gap_phys_t const phys = {
92102
.rx_phys = BLE_GAP_PHY_AUTO,
@@ -173,10 +183,11 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
173183
break;
174184
}
175185
case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
186+
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_SEC_PARAMS_REQUEST\n");
176187
// First time pairing.
177188
// 1. Either we or peer initiate the process
178189
// 2. Peer asks for security parameters using BLE_GAP_EVT_SEC_PARAMS_REQUEST.
179-
// 3. Pair Key exchange ("just works" implemented now; TODO key pairing)
190+
// 3. Pair Key exchange ("just works" implemented now; TODO: out-of-band key pairing)
180191
// 4. Connection is secured: BLE_GAP_EVT_CONN_SEC_UPDATE
181192
// 5. Long-term Keys exchanged: BLE_GAP_EVT_AUTH_STATUS
182193

@@ -205,43 +216,55 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
205216
}
206217

207218
case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
219+
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_LESC_DHKEY_REQUEST\n");
208220
// TODO for LESC pairing:
209221
// sd_ble_gap_lesc_dhkey_reply(...);
210222
break;
211223

212224
case BLE_GAP_EVT_AUTH_STATUS: { // 0x19
225+
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_AUTH_STATUS\n");
213226
// Pairing process completed
214227
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
215228
self->sec_status = status->auth_status;
216229
if (status->auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
217230
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
218231
self->pair_status = PAIR_PAIRED;
232+
CONNECTION_DEBUG_PRINTF("PAIR_PAIRED\n");
219233
bonding_save_keys(self->is_central, self->conn_handle, &self->bonding_keys);
220234
} else {
235+
// Inform busy-waiter pairing has failed.
221236
self->pair_status = PAIR_NOT_PAIRED;
237+
CONNECTION_DEBUG_PRINTF("PAIR_NOT_PAIRED\n");
222238
}
223239
break;
224240
}
225241

226242
case BLE_GAP_EVT_SEC_INFO_REQUEST: { // 0x14
243+
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_SEC_INFO_REQUEST\n");
227244
// Peer asks for the stored keys.
228245
// - load key and return if bonded previously.
229246
// - Else return NULL --> Initiate key exchange
230247
ble_gap_evt_sec_info_request_t* sec_info_request = &ble_evt->evt.gap_evt.params.sec_info_request;
231248
(void) sec_info_request;
232249
if ( bonding_load_keys(self->is_central, sec_info_request->master_id.ediv, &self->bonding_keys) ) {
233-
sd_ble_gap_sec_info_reply(self->conn_handle,
234-
&self->bonding_keys.own_enc.enc_info,
235-
&self->bonding_keys.peer_id.id_info,
236-
NULL);
250+
CONNECTION_DEBUG_PRINTF("keys found\n");
251+
uint32_t err_code = sd_ble_gap_sec_info_reply(
252+
self->conn_handle,
253+
&self->bonding_keys.own_enc.enc_info,
254+
&self->bonding_keys.peer_id.id_info,
255+
NULL);
256+
CONNECTION_DEBUG_PRINTF("sd_ble_gap_sec_info_reply err_code: %0lx\n", err_code);
237257
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
238258
} else {
259+
CONNECTION_DEBUG_PRINTF("keys not found\n");
260+
// We don't have stored keys. Ask for keys.
239261
sd_ble_gap_sec_info_reply(self->conn_handle, NULL, NULL, NULL);
240262
}
241263
break;
242264
}
243265

244266
case BLE_GAP_EVT_CONN_SEC_UPDATE: { // 0x1a
267+
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_CONN_SEC_UPDATE\n");
245268
ble_gap_conn_sec_t* conn_sec = &ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec;
246269
if (conn_sec->sec_mode.sm <= 1 && conn_sec->sec_mode.lv <= 1) {
247270
// Security setup did not succeed:
@@ -250,21 +273,24 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
250273
// mode >=1 and/or level >=1 means encryption is set up
251274
self->pair_status = PAIR_NOT_PAIRED;
252275
} else {
253-
// Does an sd_ble_gatts_sys_attr_set() with the stored values.
254276
if (bonding_load_cccd_info(self->is_central, self->conn_handle, self->ediv)) {
255-
// Not quite paired yet: wait for BLE_GAP_EVT_AUTH_STATUS SUCCESS.
256-
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
277+
// Did an sd_ble_gatts_sys_attr_set() with the stored sys_attr values.
278+
// Not quite paired yet: wait for BLE_GAP_EVT_AUTH_STATUS with BLE_GAP_SEC_STATUS_SUCCESS.
279+
CONNECTION_DEBUG_PRINTF("did bonding_load_cccd_info()\n");
257280
} else {
258281
// No matching bonding found, so use fresh system attributes.
282+
CONNECTION_DEBUG_PRINTF("bonding_load_cccd_info() failed\n");
259283
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
260284
}
261285
}
262286
break;
263287
}
264288

265289
default:
290+
CONNECTION_DEBUG_PRINTF("Unhandled event: %04x\n", ble_evt->header.evt_id);
266291
return false;
267292
}
293+
CONNECTION_DEBUG_PRINTF("Handled event: %04x\n", ble_evt->header.evt_id);
268294
return true;
269295
}
270296

0 commit comments

Comments
 (0)