Skip to content

Commit 7e1d461

Browse files
committed
drv/bluetooth: turn off if not connected and user program runs
This turns off Bluetooth if a user program is started without Bluetooth being already connected. Fixes: pybricks/pybricks-micropython#11 Fixes: pybricks/support#137
1 parent 184d900 commit 7e1d461

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static void init_advertising_data() {
169169
gap_scan_response_set_data(sizeof(scan_resp_data), (uint8_t *)scan_resp_data);
170170
}
171171

172+
// TODO: high-level Bluetooth management needs to be moved to pbsys/
172173
PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
173174
static struct etimer timer;
174175

@@ -187,8 +188,9 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
187188
gap_advertisements_enable(true);
188189

189190
// TODO: we should have a timeout and stop scanning eventually
191+
// TODO: allow user programs to initiate BLE connections
190192
pbsys_status_set(PBSYS_STATUS_BLE_ADVERTISING);
191-
PROCESS_WAIT_UNTIL(con_handle != HCI_CON_HANDLE_INVALID);
193+
PROCESS_WAIT_UNTIL(con_handle != HCI_CON_HANDLE_INVALID || pbsys_status_test(PBSYS_STATUS_USER_PROGRAM_RUNNING));
192194
pbsys_status_clear(PBSYS_STATUS_BLE_ADVERTISING);
193195

194196
for (;;) {
@@ -209,6 +211,7 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
209211
// reset Bluetooth chip
210212
hci_power_control(HCI_POWER_OFF);
211213
PROCESS_WAIT_UNTIL(hci_get_state() == HCI_STATE_OFF);
214+
PROCESS_WAIT_WHILE(pbsys_status_test(PBSYS_STATUS_USER_PROGRAM_RUNNING));
212215
}
213216

214217
PROCESS_END();

lib/pbio/drv/city_hub/bluetooth.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,7 @@ static PT_THREAD(uart_service_send_data(struct pt *pt))
11551155
PT_END(pt);
11561156
}
11571157

1158+
// TODO: high-level Bluetooth management needs to be moved to pbsys/
11581159
PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
11591160
static struct etimer timer;
11601161
static struct pt child_pt;
@@ -1207,8 +1208,9 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
12071208
PROCESS_PT_SPAWN(&child_pt, set_discoverable(&child_pt));
12081209

12091210
// TODO: we should have a timeout and stop scanning eventually
1211+
// TODO: allow user programs to initiate BLE connections
12101212
pbsys_status_set(PBSYS_STATUS_BLE_ADVERTISING);
1211-
PROCESS_WAIT_UNTIL(conn_handle != NO_CONNECTION);
1213+
PROCESS_WAIT_UNTIL(conn_handle != NO_CONNECTION || pbsys_status_test(PBSYS_STATUS_USER_PROGRAM_RUNNING));
12121214
pbsys_status_clear(PBSYS_STATUS_BLE_ADVERTISING);
12131215

12141216
etimer_set(&timer, clock_from_msec(500));
@@ -1231,6 +1233,7 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
12311233

12321234
// reset Bluetooth chip
12331235
bluetooth_reset(RESET_STATE_OUT_LOW);
1236+
PROCESS_WAIT_WHILE(pbsys_status_test(PBSYS_STATUS_USER_PROGRAM_RUNNING));
12341237
}
12351238

12361239
PROCESS_END();

lib/pbio/drv/move_hub/bluetooth.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ static PT_THREAD(uart_service_send_data(struct pt *pt))
639639
PT_END(pt);
640640
}
641641

642+
// TODO: high-level Bluetooth management needs to be moved to pbsys/
642643
PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
643644
static struct etimer timer;
644645
static struct pt child_pt;
@@ -664,8 +665,9 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
664665
PROCESS_PT_SPAWN(&child_pt, set_discoverable(&child_pt));
665666

666667
// TODO: we should have a timeout and stop scanning eventually
668+
// TODO: allow user programs to initiate BLE connections
667669
pbsys_status_set(PBSYS_STATUS_BLE_ADVERTISING);
668-
PROCESS_WAIT_UNTIL(conn_handle);
670+
PROCESS_WAIT_UNTIL(conn_handle || pbsys_status_test(PBSYS_STATUS_USER_PROGRAM_RUNNING));
669671
pbsys_status_clear(PBSYS_STATUS_BLE_ADVERTISING);
670672

671673
etimer_set(&timer, clock_from_msec(500));
@@ -688,6 +690,7 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
688690

689691
// reset Bluetooth chip
690692
GPIOB->BRR = GPIO_BRR_BR_6;
693+
PROCESS_WAIT_WHILE(pbsys_status_test(PBSYS_STATUS_USER_PROGRAM_RUNNING));
691694
}
692695

693696
PROCESS_END();

lib/pbio/drv/technic_hub/bluetooth.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ static PT_THREAD(uart_service_send_data(struct pt *pt))
11521152
PT_END(pt);
11531153
}
11541154

1155+
// TODO: high-level Bluetooth management needs to be moved to pbsys/
11551156
PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
11561157
static struct etimer timer;
11571158
static struct pt child_pt;
@@ -1204,8 +1205,9 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
12041205
PROCESS_PT_SPAWN(&child_pt, set_discoverable(&child_pt));
12051206

12061207
// TODO: we should have a timeout and stop scanning eventually
1208+
// TODO: allow user programs to initiate BLE connections
12071209
pbsys_status_set(PBSYS_STATUS_BLE_ADVERTISING);
1208-
PROCESS_WAIT_UNTIL(conn_handle != NO_CONNECTION);
1210+
PROCESS_WAIT_UNTIL(conn_handle != NO_CONNECTION || pbsys_status_test(PBSYS_STATUS_USER_PROGRAM_RUNNING));
12091211
pbsys_status_clear(PBSYS_STATUS_BLE_ADVERTISING);
12101212

12111213
etimer_set(&timer, clock_from_msec(500));
@@ -1228,6 +1230,7 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
12281230

12291231
// reset Bluetooth chip
12301232
bluetooth_reset(RESET_STATE_OUT_LOW);
1233+
PROCESS_WAIT_WHILE(pbsys_status_test(PBSYS_STATUS_USER_PROGRAM_RUNNING));
12311234
}
12321235

12331236
PROCESS_END();

0 commit comments

Comments
 (0)