@@ -69,6 +69,9 @@ static void _on_sync(void) {
69
69
xTaskNotifyGive (cp_task );
70
70
}
71
71
72
+ // All examples have this. It'd make sense in a header.
73
+ void ble_store_config_init (void );
74
+
72
75
void common_hal_bleio_adapter_set_enabled (bleio_adapter_obj_t * self , bool enabled ) {
73
76
const bool is_enabled = common_hal_bleio_adapter_get_enabled (self );
74
77
@@ -93,6 +96,20 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
93
96
ble_hs_cfg .sync_cb = _on_sync ;
94
97
// ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
95
98
99
+ ble_hs_cfg .sm_io_cap = BLE_SM_IO_CAP_NO_IO ;
100
+ ble_hs_cfg .sm_bonding = 1 ;
101
+ /* Enable the appropriate bit masks to make sure the keys
102
+ * that are needed are exchanged
103
+ */
104
+ ble_hs_cfg .sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC ;
105
+ ble_hs_cfg .sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC ;
106
+
107
+ ble_hs_cfg .sm_mitm = 1 ;
108
+ ble_hs_cfg .sm_sc = 1 ;
109
+ /* Stores the IRK */
110
+ ble_hs_cfg .sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ID ;
111
+ ble_hs_cfg .sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ID ;
112
+
96
113
ble_svc_gap_init ();
97
114
ble_svc_gatt_init ();
98
115
ble_svc_ans_init ();
@@ -115,6 +132,8 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
115
132
connection -> conn_handle = BLEIO_HANDLE_INVALID ;
116
133
}
117
134
135
+ ble_store_config_init ();
136
+
118
137
cp_task = xTaskGetCurrentTaskHandle ();
119
138
120
139
nimble_port_freertos_init (nimble_host_task );
@@ -277,10 +296,13 @@ static int _mtu_reply(uint16_t conn_handle,
277
296
const struct ble_gatt_error * error ,
278
297
uint16_t mtu , void * arg ) {
279
298
bleio_connection_internal_t * connection = (bleio_connection_internal_t * )arg ;
280
- if (conn_handle != connection -> conn_handle || error -> status != 0 ) {
299
+ if (conn_handle != connection -> conn_handle ) {
281
300
return 0 ;
282
301
}
283
- connection -> mtu = mtu ;
302
+ if (error -> status == 0 ) {
303
+ connection -> mtu = mtu ;
304
+ }
305
+ xTaskNotify (cp_task , conn_handle , eSetValueWithOverwrite );
284
306
return 0 ;
285
307
}
286
308
@@ -324,11 +346,11 @@ static int _connect_event(struct ble_gap_event *event, void *self_in) {
324
346
switch (event -> type ) {
325
347
case BLE_GAP_EVENT_CONNECT :
326
348
if (event -> connect .status == 0 ) {
349
+ // This triggers an MTU exchange. Its reply will unblock CP.
327
350
_new_connection (event -> connect .conn_handle );
328
351
// Set connections objs back to NULL since we have a new
329
352
// connection and need a new tuple.
330
353
self -> connection_objs = NULL ;
331
- xTaskNotify (cp_task , event -> connect .conn_handle , eSetValueWithOverwrite );
332
354
} else {
333
355
xTaskNotify (cp_task , - event -> connect .status , eSetValueWithOverwrite );
334
356
}
@@ -663,7 +685,7 @@ bool common_hal_bleio_adapter_get_advertising(bleio_adapter_obj_t *self) {
663
685
bool common_hal_bleio_adapter_get_connected (bleio_adapter_obj_t * self ) {
664
686
for (size_t i = 0 ; i < BLEIO_TOTAL_CONNECTION_COUNT ; i ++ ) {
665
687
bleio_connection_internal_t * connection = & bleio_connections [i ];
666
- if (connection -> conn_handle != BLEIO_HANDLE_INVALID ) {
688
+ if (connection -> conn_handle != BLEIO_HANDLE_INVALID && connection -> mtu != 0 ) {
667
689
return true;
668
690
}
669
691
}
@@ -678,7 +700,7 @@ mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self) {
678
700
mp_obj_t items [BLEIO_TOTAL_CONNECTION_COUNT ];
679
701
for (size_t i = 0 ; i < BLEIO_TOTAL_CONNECTION_COUNT ; i ++ ) {
680
702
bleio_connection_internal_t * connection = & bleio_connections [i ];
681
- if (connection -> conn_handle != BLEIO_HANDLE_INVALID ) {
703
+ if (connection -> conn_handle != BLEIO_HANDLE_INVALID && connection -> mtu != 0 ) {
682
704
if (connection -> connection_obj == mp_const_none ) {
683
705
connection -> connection_obj = bleio_connection_new_from_internal (connection );
684
706
}
@@ -691,14 +713,13 @@ mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self) {
691
713
}
692
714
693
715
void common_hal_bleio_adapter_erase_bonding (bleio_adapter_obj_t * self ) {
694
- mp_raise_NotImplementedError (NULL );
695
- // bonding_erase_storage();
716
+ ble_store_clear ();
696
717
}
697
718
698
719
bool common_hal_bleio_adapter_is_bonded_to_central (bleio_adapter_obj_t * self ) {
699
- mp_raise_NotImplementedError ( NULL ) ;
700
- // return bonding_peripheral_bond_count() > 0 ;
701
- return false ;
720
+ int count ;
721
+ ble_store_util_count ( BLE_STORE_OBJ_TYPE_PEER_SEC , & count ) ;
722
+ return count > 0 ;
702
723
}
703
724
704
725
void bleio_adapter_gc_collect (bleio_adapter_obj_t * adapter ) {
0 commit comments