39
39
#include "common-hal/_bleio/Connection.h"
40
40
41
41
#include "esp_bt.h"
42
+ #include "esp_mac.h"
42
43
#include "esp_nimble_hci.h"
43
44
#include "nvs_flash.h"
44
45
48
49
49
50
bleio_connection_internal_t bleio_connections [BLEIO_TOTAL_CONNECTION_COUNT ];
50
51
51
- // static void bluetooth_adapter_background(void *data) {
52
- // supervisor_bluetooth_background();
53
- // bleio_background();
54
- // }
55
-
56
52
bool ble_active = false;
57
53
58
54
static void nimble_host_task (void * param ) {
@@ -63,7 +59,7 @@ static void nimble_host_task(void *param) {
63
59
static TaskHandle_t cp_task = NULL ;
64
60
65
61
static void _on_sync (void ) {
66
- int rc = ble_hs_util_ensure_addr (0 );
62
+ int rc = ble_hs_util_ensure_addr (false );
67
63
assert (rc == 0 );
68
64
69
65
xTaskNotifyGive (cp_task );
@@ -72,6 +68,8 @@ static void _on_sync(void) {
72
68
// All examples have this. It'd make sense in a header.
73
69
void ble_store_config_init (void );
74
70
71
+ char default_ble_name [] = { 'C' , 'I' , 'R' , 'C' , 'U' , 'I' , 'T' , 'P' , 'Y' , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
72
+
75
73
void common_hal_bleio_adapter_set_enabled (bleio_adapter_obj_t * self , bool enabled ) {
76
74
const bool is_enabled = common_hal_bleio_adapter_get_enabled (self );
77
75
@@ -81,15 +79,6 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
81
79
}
82
80
83
81
if (enabled ) {
84
- esp_err_t err = nvs_flash_init ();
85
- if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND ) {
86
- // NVS partition was truncated and needs to be erased
87
- // Retry nvs_flash_init
88
- ESP_ERROR_CHECK (nvs_flash_erase ());
89
- err = nvs_flash_init ();
90
- }
91
- ESP_ERROR_CHECK (err );
92
-
93
82
CHECK_ESP_RESULT (nimble_port_init ());
94
83
95
84
// ble_hs_cfg.reset_cb = blecent_on_reset;
@@ -104,8 +93,8 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
104
93
ble_hs_cfg .sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC ;
105
94
ble_hs_cfg .sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC ;
106
95
107
- ble_hs_cfg .sm_mitm = 1 ;
108
- ble_hs_cfg .sm_sc = 1 ;
96
+ ble_hs_cfg .sm_mitm = 0 ;
97
+ ble_hs_cfg .sm_sc = 0 ;
109
98
/* Stores the IRK */
110
99
ble_hs_cfg .sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ID ;
111
100
ble_hs_cfg .sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ID ;
@@ -122,7 +111,17 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
122
111
} else
123
112
#endif
124
113
{
125
- ble_svc_gap_device_name_set ("CIRCUITPY" );
114
+ uint8_t mac [6 ];
115
+ esp_read_mac (mac , ESP_MAC_BT );
116
+ mp_int_t len = sizeof (default_ble_name ) - 1 ;
117
+ default_ble_name [len - 6 ] = nibble_to_hex_lower [mac [3 ] >> 4 & 0xf ];
118
+ default_ble_name [len - 5 ] = nibble_to_hex_lower [mac [3 ] & 0xf ];
119
+ default_ble_name [len - 4 ] = nibble_to_hex_lower [mac [4 ] >> 4 & 0xf ];
120
+ default_ble_name [len - 3 ] = nibble_to_hex_lower [mac [4 ] & 0xf ];
121
+ default_ble_name [len - 2 ] = nibble_to_hex_lower [mac [5 ] >> 4 & 0xf ];
122
+ default_ble_name [len - 1 ] = nibble_to_hex_lower [mac [5 ] & 0xf ];
123
+ default_ble_name [len ] = '\0' ; // for now we add null for compatibility with C ASCIIZ strings
124
+ ble_svc_gap_device_name_set (default_ble_name );
126
125
}
127
126
128
127
// Clear all of the internal connection objects.
@@ -180,6 +179,14 @@ bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_addre
180
179
return result == 0 ;
181
180
}
182
181
182
+ uint16_t bleio_adapter_get_name (char * buf , uint16_t len ) {
183
+ const char * name = ble_svc_gap_device_name ();
184
+ uint16_t full_len = strlen (name );
185
+ memcpy (buf , name , MIN (full_len , len ));
186
+
187
+ return full_len ;
188
+ }
189
+
183
190
mp_obj_str_t * common_hal_bleio_adapter_get_name (bleio_adapter_obj_t * self ) {
184
191
const char * name = ble_svc_gap_device_name ();
185
192
@@ -401,7 +408,8 @@ mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_addre
401
408
_connect_event , self ));
402
409
403
410
int error_code ;
404
- CHECK_NOTIFY (xTaskNotifyWait (0 , 0 , (uint32_t * )& error_code , 200 ));
411
+ // Wait an extra 50 ms to give the connect method the opportunity to time out.
412
+ CHECK_NOTIFY (xTaskNotifyWait (0 , 0 , (uint32_t * )& error_code , pdMS_TO_TICKS (timeout * 1000 + 50 )));
405
413
// Negative values are error codes, connection handle otherwise.
406
414
if (error_code < 0 ) {
407
415
CHECK_BLE_ERROR (- error_code );
@@ -487,6 +495,7 @@ static int _advertising_event(struct ble_gap_event *event, void *self_in) {
487
495
#endif
488
496
break ;
489
497
}
498
+ background_callback_add_core (& bleio_background_callback );
490
499
return 0 ;
491
500
}
492
501
@@ -499,6 +508,8 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
499
508
if (ble_gap_adv_active () && !self -> user_advertising ) {
500
509
return BLE_HS_EBUSY ;
501
510
}
511
+ // Override anonymous because it isn't working with the ESP-IDF.
512
+ anonymous = false;
502
513
503
514
uint32_t rc ;
504
515
@@ -521,12 +532,19 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
521
532
bool extended = advertising_data_len > BLE_ADV_LEGACY_DATA_MAX_LEN ||
522
533
scan_response_data_len > BLE_ADV_LEGACY_DATA_MAX_LEN ;
523
534
535
+ bool scannable = scan_response_data_len > 0 ;
536
+ bool legacy_pdu = !extended && !anonymous ;
537
+ if (legacy_pdu && connectable ) {
538
+ // Connectable legacy advertisements are always scannable too.
539
+ scannable = true;
540
+ }
541
+
524
542
struct ble_gap_ext_adv_params adv_params = {
525
543
.connectable = connectable ,
526
- .scannable = scan_response_data_len > 0 ,
544
+ .scannable = scannable ,
527
545
.directed = directed_to != NULL ,
528
546
.high_duty_directed = high_duty_directed ,
529
- .legacy_pdu = ! extended ,
547
+ .legacy_pdu = legacy_pdu ,
530
548
.anonymous = anonymous ,
531
549
.include_tx_power = extended ,
532
550
.scan_req_notif = false,
@@ -712,14 +730,46 @@ mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self) {
712
730
return self -> connection_objs ;
713
731
}
714
732
733
+ #define NIMBLE_NVS_PEER_SEC_KEY "peer_sec"
734
+ #define NIMBLE_NVS_OUR_SEC_KEY "our_sec"
735
+ #define NIMBLE_NVS_CCCD_SEC_KEY "cccd_sec"
736
+ #define NIMBLE_NVS_PEER_RECORDS_KEY "p_dev_rec"
737
+ #define NIMBLE_NVS_NAMESPACE "nimble_bond"
738
+
739
+ // Implement bonding control ourselves when the adapter isn't enabled so that it
740
+ // can run when BLE is off.
715
741
void common_hal_bleio_adapter_erase_bonding (bleio_adapter_obj_t * self ) {
716
- ble_store_clear ();
742
+ if (common_hal_bleio_adapter_get_enabled (self )) {
743
+ ble_store_clear ();
744
+ } else {
745
+ nvs_handle_t nimble_handle ;
746
+ esp_err_t err = nvs_open (NIMBLE_NVS_NAMESPACE , NVS_READWRITE , & nimble_handle );
747
+ if (err != ESP_OK ) {
748
+ return ;
749
+ }
750
+ nvs_erase_all (nimble_handle );
751
+ nvs_commit (nimble_handle );
752
+ nvs_close (nimble_handle );
753
+ }
717
754
}
718
755
719
756
bool common_hal_bleio_adapter_is_bonded_to_central (bleio_adapter_obj_t * self ) {
720
- int count ;
721
- ble_store_util_count (BLE_STORE_OBJ_TYPE_PEER_SEC , & count );
722
- return count > 0 ;
757
+ if (common_hal_bleio_adapter_get_enabled (self )) {
758
+ int count ;
759
+ ble_store_util_count (BLE_STORE_OBJ_TYPE_PEER_SEC , & count );
760
+ return count > 0 ;
761
+ }
762
+ nvs_handle_t nimble_handle ;
763
+ esp_err_t err = nvs_open (NIMBLE_NVS_NAMESPACE , NVS_READONLY , & nimble_handle );
764
+ if (err != ESP_OK ) {
765
+ return false;
766
+ }
767
+ err = nvs_find_key (nimble_handle , "peer_sec_1" , NULL );
768
+ nvs_close (nimble_handle );
769
+ if (err == ESP_OK ) {
770
+ return true;
771
+ }
772
+ return false;
723
773
}
724
774
725
775
void bleio_adapter_gc_collect (bleio_adapter_obj_t * adapter ) {
0 commit comments