54
54
#define BLE_AD_TYPE_FLAGS_DATA_SIZE 1
55
55
56
56
static const ble_gap_sec_params_t pairing_sec_params = {
57
- .bond = 0 ,
57
+ .bond = 1 ,
58
58
.mitm = 0 ,
59
59
.lesc = 0 ,
60
60
.keypress = 0 ,
@@ -66,6 +66,13 @@ static const ble_gap_sec_params_t pairing_sec_params = {
66
66
.kdist_peer = { .enc = 1 , .id = 1 },
67
67
};
68
68
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
+
69
76
static volatile bool m_discovery_in_process ;
70
77
static volatile bool m_discovery_successful ;
71
78
@@ -86,7 +93,10 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
86
93
87
94
switch (ble_evt -> header .evt_id ) {
88
95
case BLE_GAP_EVT_DISCONNECTED :
96
+ CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_DISCONNECTED\n" );
97
+ // Adapter.c does the work for this event.
89
98
break ;
99
+
90
100
case BLE_GAP_EVT_PHY_UPDATE_REQUEST : {
91
101
ble_gap_phys_t const phys = {
92
102
.rx_phys = BLE_GAP_PHY_AUTO ,
@@ -173,10 +183,11 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
173
183
break ;
174
184
}
175
185
case BLE_GAP_EVT_SEC_PARAMS_REQUEST : {
186
+ CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_SEC_PARAMS_REQUEST\n" );
176
187
// First time pairing.
177
188
// 1. Either we or peer initiate the process
178
189
// 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)
180
191
// 4. Connection is secured: BLE_GAP_EVT_CONN_SEC_UPDATE
181
192
// 5. Long-term Keys exchanged: BLE_GAP_EVT_AUTH_STATUS
182
193
@@ -205,43 +216,55 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
205
216
}
206
217
207
218
case BLE_GAP_EVT_LESC_DHKEY_REQUEST :
219
+ CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_LESC_DHKEY_REQUEST\n" );
208
220
// TODO for LESC pairing:
209
221
// sd_ble_gap_lesc_dhkey_reply(...);
210
222
break ;
211
223
212
224
case BLE_GAP_EVT_AUTH_STATUS : { // 0x19
225
+ CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_AUTH_STATUS\n" );
213
226
// Pairing process completed
214
227
ble_gap_evt_auth_status_t * status = & ble_evt -> evt .gap_evt .params .auth_status ;
215
228
self -> sec_status = status -> auth_status ;
216
229
if (status -> auth_status == BLE_GAP_SEC_STATUS_SUCCESS ) {
217
230
self -> ediv = self -> bonding_keys .own_enc .master_id .ediv ;
218
231
self -> pair_status = PAIR_PAIRED ;
232
+ CONNECTION_DEBUG_PRINTF ("PAIR_PAIRED\n" );
219
233
bonding_save_keys (self -> is_central , self -> conn_handle , & self -> bonding_keys );
220
234
} else {
235
+ // Inform busy-waiter pairing has failed.
221
236
self -> pair_status = PAIR_NOT_PAIRED ;
237
+ CONNECTION_DEBUG_PRINTF ("PAIR_NOT_PAIRED\n" );
222
238
}
223
239
break ;
224
240
}
225
241
226
242
case BLE_GAP_EVT_SEC_INFO_REQUEST : { // 0x14
243
+ CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_SEC_INFO_REQUEST\n" );
227
244
// Peer asks for the stored keys.
228
245
// - load key and return if bonded previously.
229
246
// - Else return NULL --> Initiate key exchange
230
247
ble_gap_evt_sec_info_request_t * sec_info_request = & ble_evt -> evt .gap_evt .params .sec_info_request ;
231
248
(void ) sec_info_request ;
232
249
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 );
237
257
self -> ediv = self -> bonding_keys .own_enc .master_id .ediv ;
238
258
} else {
259
+ CONNECTION_DEBUG_PRINTF ("keys not found\n" );
260
+ // We don't have stored keys. Ask for keys.
239
261
sd_ble_gap_sec_info_reply (self -> conn_handle , NULL , NULL , NULL );
240
262
}
241
263
break ;
242
264
}
243
265
244
266
case BLE_GAP_EVT_CONN_SEC_UPDATE : { // 0x1a
267
+ CONNECTION_DEBUG_PRINTF ("BLE_GAP_EVT_CONN_SEC_UPDATE\n" );
245
268
ble_gap_conn_sec_t * conn_sec = & ble_evt -> evt .gap_evt .params .conn_sec_update .conn_sec ;
246
269
if (conn_sec -> sec_mode .sm <= 1 && conn_sec -> sec_mode .lv <= 1 ) {
247
270
// Security setup did not succeed:
@@ -250,21 +273,24 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
250
273
// mode >=1 and/or level >=1 means encryption is set up
251
274
self -> pair_status = PAIR_NOT_PAIRED ;
252
275
} else {
253
- // Does an sd_ble_gatts_sys_attr_set() with the stored values.
254
276
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" );
257
280
} else {
258
281
// No matching bonding found, so use fresh system attributes.
282
+ CONNECTION_DEBUG_PRINTF ("bonding_load_cccd_info() failed\n" );
259
283
sd_ble_gatts_sys_attr_set (self -> conn_handle , NULL , 0 , 0 );
260
284
}
261
285
}
262
286
break ;
263
287
}
264
288
265
289
default :
290
+ CONNECTION_DEBUG_PRINTF ("Unhandled event: %04x\n" , ble_evt -> header .evt_id );
266
291
return false;
267
292
}
293
+ CONNECTION_DEBUG_PRINTF ("Handled event: %04x\n" , ble_evt -> header .evt_id );
268
294
return true;
269
295
}
270
296
0 commit comments