@@ -83,13 +83,23 @@ uint8_t circuitpython_scan_response_data[] = {
8383 #endif
8484};
8585
86- bool boot_in_discovery_mode = false;
87- bool advertising = false;
86+
87+ #if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
88+ STATIC bool boot_in_discovery_mode = false;
89+ STATIC bool advertising = false;
90+ STATIC bool ble_started = false;
91+
92+ #define WORKFLOW_UNSET 0
93+ #define WORKFLOW_ENABLED 1
94+ #define WORKFLOW_DISABLED 2
95+
96+ STATIC uint8_t workflow_state = WORKFLOW_UNSET ;
97+ STATIC bool was_connected = false;
8898
8999STATIC void supervisor_bluetooth_start_advertising (void ) {
90- # if ! CIRCUITPY_BLE_FILE_SERVICE && ! CIRCUITPY_SERIAL_BLE
91- return ;
92- #else
100+ if ( workflow_state != WORKFLOW_ENABLED ) {
101+ return ;
102+ }
93103 bool is_connected = common_hal_bleio_adapter_get_connected (& common_hal_bleio_adapter_obj );
94104 if (is_connected ) {
95105 return ;
@@ -130,16 +140,15 @@ STATIC void supervisor_bluetooth_start_advertising(void) {
130140 NULL );
131141 // This may fail if we are already advertising.
132142 advertising = status == NRF_SUCCESS ;
133- #endif
134143}
135144
145+ #endif // CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
146+
136147#define BLE_DISCOVERY_DATA_GUARD 0xbb0000bb
137148#define BLE_DISCOVERY_DATA_GUARD_MASK 0xff0000ff
138149
139150void supervisor_bluetooth_init (void ) {
140- #if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
141- return ;
142- #endif
151+ #if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
143152 uint32_t reset_state = port_get_saved_word ();
144153 uint32_t ble_mode = 0 ;
145154 if ((reset_state & BLE_DISCOVERY_DATA_GUARD_MASK ) == BLE_DISCOVERY_DATA_GUARD ) {
@@ -209,10 +218,14 @@ void supervisor_bluetooth_init(void) {
209218 status_led_deinit ();
210219 #endif
211220 port_set_saved_word (reset_state );
221+ #endif
212222}
213223
214- STATIC bool was_connected ;
215224void supervisor_bluetooth_background (void ) {
225+ #if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
226+ if (!ble_started ) {
227+ return ;
228+ }
216229 bool is_connected = common_hal_bleio_adapter_get_connected (& common_hal_bleio_adapter_obj );
217230 if (was_connected && !is_connected ) {
218231 #if CIRCUITPY_BLE_FILE_SERVICE
@@ -228,12 +241,15 @@ void supervisor_bluetooth_background(void) {
228241 #if CIRCUITPY_BLE_FILE_SERVICE
229242 supervisor_bluetooth_file_transfer_background ();
230243 #endif
244+ #endif
231245}
232246
233247void supervisor_start_bluetooth (void ) {
234- #if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
235- return ;
236- #endif
248+ #if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
249+
250+ if (workflow_state != WORKFLOW_ENABLED ) {
251+ return ;
252+ }
237253
238254 common_hal_bleio_adapter_set_enabled (& common_hal_bleio_adapter_obj , true);
239255
@@ -245,16 +261,41 @@ void supervisor_start_bluetooth(void) {
245261 supervisor_start_bluetooth_serial ();
246262 #endif
247263
248- // Kick off advertisments
264+ // Mark as started so that the background call does something.
265+ ble_started = true;
266+
267+ // Kick off advertisements
249268 supervisor_bluetooth_background ();
269+
270+ #endif
250271}
251272
252273void supervisor_stop_bluetooth (void ) {
253- #if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
254- return ;
255- #endif
274+ #if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
275+
276+ if (!ble_started && workflow_state != WORKFLOW_ENABLED ) {
277+ return ;
278+ }
256279
257280 #if CIRCUITPY_SERIAL_BLE
258281 supervisor_stop_bluetooth_serial ();
259282 #endif
283+
284+ #endif
285+ }
286+
287+ void supervisor_bluetooth_enable_workflow (void ) {
288+ #if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
289+ if (workflow_state == WORKFLOW_DISABLED ) {
290+ return ;
291+ }
292+
293+ workflow_state = WORKFLOW_ENABLED ;
294+ #endif
295+ }
296+
297+ void supervisor_bluetooth_disable_workflow (void ) {
298+ #if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
299+ workflow_state = WORKFLOW_DISABLED ;
300+ #endif
260301}
0 commit comments