Skip to content

Commit

Permalink
pbio/sys/bluetooth: Selectively compile bluetooth toggle.
Browse files Browse the repository at this point in the history
This ensures it is always enabled on platforms without a toggle, such as Move Hub or the pbio tests.
  • Loading branch information
laurensvalk committed Jun 7, 2024
1 parent 72bdc3a commit fe54637
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/pbio/sys/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ bool pbsys_bluetooth_tx_is_idle(void) {
return !send_busy && lwrb_get_full(&stdout_ring_buf) == 0;
}

#if PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
static pbsys_storage_settings_t *settings;

bool pbsys_bluetooth_is_user_enabled(void) {
Expand Down Expand Up @@ -233,6 +234,13 @@ void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
pbsys_storage_request_write();
pbsys_bluetooth_process_poll();
}
#else // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
bool pbsys_bluetooth_is_user_enabled(void) {
return true;
}
void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
}
#endif // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE

// Contiki process

Expand Down Expand Up @@ -321,7 +329,9 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {

PROCESS_BEGIN();

#if PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
PROCESS_WAIT_EVENT_UNTIL(pbsys_storage_get_settings(&settings) == PBIO_SUCCESS);
#endif // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE

pbdrv_bluetooth_set_on_event(pbsys_bluetooth_process_poll);
pbdrv_bluetooth_set_receive_handler(handle_receive);
Expand All @@ -330,7 +340,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {

// Show inactive status only if user requested Bluetooth as disabled to
// avoid always flashing red in between program runs when disconnected.
if (!settings->bluetooth_ble_user_enabled) {
if (!pbsys_bluetooth_is_user_enabled()) {
pbsys_status_light_bluetooth_set_color(PBIO_COLOR_RED);
}

Expand All @@ -339,7 +349,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));

// Wait until Bluetooth enabled requested by user, but stop waiting on shutdown.
PROCESS_WAIT_UNTIL(settings->bluetooth_ble_user_enabled || pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN));
PROCESS_WAIT_UNTIL(pbsys_bluetooth_is_user_enabled() || pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN));
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {
break;
}
Expand All @@ -360,7 +370,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)
|| !settings->bluetooth_ble_user_enabled);
|| !pbsys_bluetooth_is_user_enabled());

// Now change the state depending on which of the above was triggered.

Expand All @@ -379,7 +389,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
// The Bluetooth enabled flag can only change while disconnected and
// while no program is running. So here it just serves to skip the
// Bluetooth loop below and go directly to the disable step below it.
while (settings->bluetooth_ble_user_enabled
while (pbsys_bluetooth_is_user_enabled()
&& pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
&& !pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {

Expand Down

0 comments on commit fe54637

Please sign in to comment.