39
39
40
40
#include "common-hal/_bleio/__init__.h"
41
41
42
+ #include "supervisor/serial.h"
42
43
#include "supervisor/shared/status_leds.h"
43
44
#include "supervisor/shared/tick.h"
45
+ #include "supervisor/shared/title_bar.h"
46
+ #include "supervisor/shared/translate/translate.h"
44
47
45
48
#include "py/mpstate.h"
46
49
@@ -75,18 +78,13 @@ const uint8_t public_advertising_data[] = { 0x02, 0x01, 0x06, // 0-2 Flags
75
78
const uint8_t private_advertising_data [] = { 0x02 , 0x01 , 0x06 , // 0-2 Flags
76
79
0x02 , 0x0a , 0x00 // 3-5 TX power level 0
77
80
};
78
- // This scan response advertises the full CIRCPYXXXX device name.
79
- uint8_t circuitpython_scan_response_data [] = {
80
- 0x0a , 0x09 , 0x43 , 0x49 , 0x52 , 0x50 , 0x59 , 0x00 , 0x00 , 0x00 , 0x00 ,
81
- #if CIRCUITPY_SERIAL_BLE
82
- 0x11 , 0x06 , 0x6e , 0x68 , 0x74 , 0x79 , 0x50 , 0x74 , 0x69 , 0x75 , 0x63 , 0x72 , 0x69 , 0x43 , 0x01 , 0x00 , 0xaf , 0xad
83
- #endif
84
- };
85
-
81
+ // This scan response advertises the full device name (if it fits.)
82
+ uint8_t circuitpython_scan_response_data [31 ];
86
83
87
84
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
88
85
STATIC bool boot_in_discovery_mode = false;
89
86
STATIC bool advertising = false;
87
+ STATIC bool _private_advertising = false;
90
88
STATIC bool ble_started = false;
91
89
92
90
#define WORKFLOW_UNSET 0
@@ -96,6 +94,36 @@ STATIC bool ble_started = false;
96
94
STATIC uint8_t workflow_state = WORKFLOW_UNSET ;
97
95
STATIC bool was_connected = false;
98
96
97
+ // To detect when the title bar changes.
98
+ STATIC bool _last_connected = false;
99
+ STATIC bool _last_advertising = false;
100
+
101
+ // Title bar status
102
+ bool supervisor_bluetooth_status_dirty (void ) {
103
+ return _last_advertising != advertising ||
104
+ _last_connected != was_connected ;
105
+ }
106
+
107
+ void supervisor_bluetooth_status (void ) {
108
+ serial_write ("BLE:" );
109
+ if (advertising ) {
110
+ if (_private_advertising ) {
111
+ serial_write_compressed (translate ("Reconnecting" ));
112
+ } else {
113
+ const char * name = (char * )circuitpython_scan_response_data + 2 ;
114
+ int len = MIN (strlen (name ), sizeof (circuitpython_scan_response_data ) - 2 );
115
+ serial_write_substring (name , len );
116
+ }
117
+ } else if (was_connected ) {
118
+ serial_write_compressed (translate ("Ok" ));
119
+ } else {
120
+ serial_write_compressed (translate ("Off" ));
121
+ }
122
+
123
+ _last_connected = was_connected ;
124
+ _last_advertising = advertising ;
125
+ }
126
+
99
127
STATIC void supervisor_bluetooth_start_advertising (void ) {
100
128
if (workflow_state != WORKFLOW_ENABLED ) {
101
129
return ;
@@ -118,6 +146,7 @@ STATIC void supervisor_bluetooth_start_advertising(void) {
118
146
size_t adv_len = sizeof (private_advertising_data );
119
147
const uint8_t * scan_response = NULL ;
120
148
size_t scan_response_len = 0 ;
149
+ _private_advertising = true;
121
150
// Advertise with less power when doing so publicly to reduce who can hear us. This will make it
122
151
// harder for someone with bad intentions to pair from a distance.
123
152
if (!bonded ) {
@@ -126,6 +155,20 @@ STATIC void supervisor_bluetooth_start_advertising(void) {
126
155
adv_len = sizeof (public_advertising_data );
127
156
scan_response = circuitpython_scan_response_data ;
128
157
scan_response_len = sizeof (circuitpython_scan_response_data );
158
+ uint16_t max_name_len = sizeof (circuitpython_scan_response_data ) - 2 ;
159
+ uint16_t name_len = bleio_adapter_get_name ((char * )circuitpython_scan_response_data + 2 ,
160
+ max_name_len );
161
+ if (name_len > max_name_len ) {
162
+ circuitpython_scan_response_data [0 ] = max_name_len + 1 ;
163
+ circuitpython_scan_response_data [1 ] = 0x8 ;
164
+ } else {
165
+ circuitpython_scan_response_data [0 ] = name_len + 1 ;
166
+ circuitpython_scan_response_data [1 ] = 0x9 ;
167
+ }
168
+ scan_response_len = circuitpython_scan_response_data [0 ] + 1 ;
169
+ assert (scan_response_len < 32 );
170
+ mp_printf (& mp_plat_print , "sr len %d\n" , scan_response_len );
171
+ _private_advertising = false;
129
172
}
130
173
uint32_t status = _common_hal_bleio_adapter_start_advertising (& common_hal_bleio_adapter_obj ,
131
174
true,
@@ -232,6 +275,9 @@ void supervisor_bluetooth_background(void) {
232
275
supervisor_bluetooth_file_transfer_disconnected ();
233
276
#endif
234
277
}
278
+ if (was_connected != is_connected ) {
279
+ supervisor_title_bar_request_update (false);
280
+ }
235
281
was_connected = is_connected ;
236
282
if (!is_connected ) {
237
283
supervisor_bluetooth_start_advertising ();
@@ -266,6 +312,7 @@ void supervisor_start_bluetooth(void) {
266
312
267
313
// Kick off advertisements
268
314
supervisor_bluetooth_background ();
315
+ supervisor_title_bar_request_update (false);
269
316
270
317
#endif
271
318
}
0 commit comments