@@ -69,6 +69,9 @@ static void _on_sync(void) {
6969 xTaskNotifyGive (cp_task );
7070}
7171
72+ // All examples have this. It'd make sense in a header.
73+ void ble_store_config_init (void );
74+
7275void common_hal_bleio_adapter_set_enabled (bleio_adapter_obj_t * self , bool enabled ) {
7376 const bool is_enabled = common_hal_bleio_adapter_get_enabled (self );
7477
@@ -93,6 +96,20 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
9396 ble_hs_cfg .sync_cb = _on_sync ;
9497 // ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
9598
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+
96113 ble_svc_gap_init ();
97114 ble_svc_gatt_init ();
98115 ble_svc_ans_init ();
@@ -115,6 +132,8 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
115132 connection -> conn_handle = BLEIO_HANDLE_INVALID ;
116133 }
117134
135+ ble_store_config_init ();
136+
118137 cp_task = xTaskGetCurrentTaskHandle ();
119138
120139 nimble_port_freertos_init (nimble_host_task );
@@ -277,10 +296,13 @@ static int _mtu_reply(uint16_t conn_handle,
277296 const struct ble_gatt_error * error ,
278297 uint16_t mtu , void * arg ) {
279298 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 ) {
281300 return 0 ;
282301 }
283- connection -> mtu = mtu ;
302+ if (error -> status == 0 ) {
303+ connection -> mtu = mtu ;
304+ }
305+ xTaskNotify (cp_task , conn_handle , eSetValueWithOverwrite );
284306 return 0 ;
285307}
286308
@@ -324,11 +346,11 @@ static int _connect_event(struct ble_gap_event *event, void *self_in) {
324346 switch (event -> type ) {
325347 case BLE_GAP_EVENT_CONNECT :
326348 if (event -> connect .status == 0 ) {
349+ // This triggers an MTU exchange. Its reply will unblock CP.
327350 _new_connection (event -> connect .conn_handle );
328351 // Set connections objs back to NULL since we have a new
329352 // connection and need a new tuple.
330353 self -> connection_objs = NULL ;
331- xTaskNotify (cp_task , event -> connect .conn_handle , eSetValueWithOverwrite );
332354 } else {
333355 xTaskNotify (cp_task , - event -> connect .status , eSetValueWithOverwrite );
334356 }
@@ -663,7 +685,7 @@ bool common_hal_bleio_adapter_get_advertising(bleio_adapter_obj_t *self) {
663685bool common_hal_bleio_adapter_get_connected (bleio_adapter_obj_t * self ) {
664686 for (size_t i = 0 ; i < BLEIO_TOTAL_CONNECTION_COUNT ; i ++ ) {
665687 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 ) {
667689 return true;
668690 }
669691 }
@@ -678,7 +700,7 @@ mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self) {
678700 mp_obj_t items [BLEIO_TOTAL_CONNECTION_COUNT ];
679701 for (size_t i = 0 ; i < BLEIO_TOTAL_CONNECTION_COUNT ; i ++ ) {
680702 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 ) {
682704 if (connection -> connection_obj == mp_const_none ) {
683705 connection -> connection_obj = bleio_connection_new_from_internal (connection );
684706 }
@@ -691,14 +713,13 @@ mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self) {
691713}
692714
693715void common_hal_bleio_adapter_erase_bonding (bleio_adapter_obj_t * self ) {
694- mp_raise_NotImplementedError (NULL );
695- // bonding_erase_storage();
716+ ble_store_clear ();
696717}
697718
698719bool 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 ;
702723}
703724
704725void bleio_adapter_gc_collect (bleio_adapter_obj_t * adapter ) {
0 commit comments