2424#include <pbsys/storage.h>
2525
2626#include "light.h"
27+ #include "storage.h"
2728
2829// REVISIT: this can be the negotiated MTU - 3 to allow for better throughput
2930#define MAX_CHAR_SIZE 20
@@ -46,7 +47,7 @@ static bool send_busy;
4647
4748PROCESS (pbsys_bluetooth_process , "Bluetooth" );
4849
49- static void pbsys_bluetooth_process_poll (void ) {
50+ void pbsys_bluetooth_process_poll (void ) {
5051 process_poll (& pbsys_bluetooth_process );
5152}
5253
@@ -203,45 +204,6 @@ bool pbsys_bluetooth_tx_is_idle(void) {
203204 return !send_busy && lwrb_get_full (& stdout_ring_buf ) == 0 ;
204205}
205206
206- #if PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
207- static pbsys_storage_settings_t * settings ;
208-
209- bool pbsys_bluetooth_is_user_enabled (void ) {
210- if (!settings ) {
211- return false;
212- }
213- return settings -> bluetooth_ble_user_enabled ;
214- }
215-
216- void pbsys_bluetooth_is_user_enabled_request_toggle (void ) {
217-
218- // Ignore toggle request in all but idle system status.
219- if (pbsys_status_test (PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING )
220- || pbsys_status_test (PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED )
221- || pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN )
222- || pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST )
223- // Ignore toggle is Bluetooth is currently being used in a connection.
224- || pbdrv_bluetooth_is_connected (PBDRV_BLUETOOTH_CONNECTION_LE )
225- || pbdrv_bluetooth_is_connected (PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL )
226- // Ignore if last request not yet finished processing.
227- || settings -> bluetooth_ble_user_enabled != pbdrv_bluetooth_is_ready ()
228- ) {
229- return ;
230- }
231-
232- // Toggle the user enabled state and poll process to take action.
233- settings -> bluetooth_ble_user_enabled = !settings -> bluetooth_ble_user_enabled ;
234- pbsys_storage_request_write ();
235- pbsys_bluetooth_process_poll ();
236- }
237- #else // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
238- bool pbsys_bluetooth_is_user_enabled (void ) {
239- return true;
240- }
241- void pbsys_bluetooth_is_user_enabled_request_toggle (void ) {
242- }
243- #endif // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
244-
245207// Contiki process
246208
247209static pbio_pybricks_error_t handle_receive (pbdrv_bluetooth_connection_t connection , const uint8_t * data , uint32_t size ) {
@@ -329,8 +291,9 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
329291
330292 PROCESS_BEGIN ();
331293
294+ // Ensures Bluetooth preferences are loaded before we read them.
332295 #if PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
333- PROCESS_WAIT_EVENT_UNTIL (pbsys_storage_get_settings (& settings ) == PBIO_SUCCESS );
296+ PROCESS_WAIT_EVENT_UNTIL (pbsys_storage_get_settings () != NULL );
334297 #endif // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
335298
336299 pbdrv_bluetooth_set_on_event (pbsys_bluetooth_process_poll );
@@ -340,7 +303,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
340303
341304 // Show inactive status only if user requested Bluetooth as disabled to
342305 // avoid always flashing red in between program runs when disconnected.
343- if (!pbsys_bluetooth_is_user_enabled ()) {
306+ if (!pbsys_storage_settings_bluetooth_enabled ()) {
344307 pbsys_status_light_bluetooth_set_color (PBIO_COLOR_RED );
345308 }
346309
@@ -349,7 +312,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
349312 PROCESS_WAIT_EVENT_UNTIL (ev == PROCESS_EVENT_TIMER && etimer_expired (& timer ));
350313
351314 // Wait until Bluetooth enabled requested by user, but stop waiting on shutdown.
352- PROCESS_WAIT_UNTIL (pbsys_bluetooth_is_user_enabled () || pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN ));
315+ PROCESS_WAIT_UNTIL (pbsys_storage_settings_bluetooth_enabled () || pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN ));
353316 if (pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN )) {
354317 break ;
355318 }
@@ -370,7 +333,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
370333 pbdrv_bluetooth_is_connected (PBDRV_BLUETOOTH_CONNECTION_LE )
371334 || pbsys_status_test (PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING )
372335 || pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN )
373- || !pbsys_bluetooth_is_user_enabled ());
336+ || !pbsys_storage_settings_bluetooth_enabled ());
374337
375338 // Now change the state depending on which of the above was triggered.
376339
@@ -389,7 +352,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
389352 // The Bluetooth enabled flag can only change while disconnected and
390353 // while no program is running. So here it just serves to skip the
391354 // Bluetooth loop below and go directly to the disable step below it.
392- while (pbsys_bluetooth_is_user_enabled ()
355+ while (pbsys_storage_settings_bluetooth_enabled ()
393356 && pbdrv_bluetooth_is_connected (PBDRV_BLUETOOTH_CONNECTION_LE )
394357 && !pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN )) {
395358
0 commit comments