Skip to content

Commit be6304e

Browse files
committed
pbio/drv/bluetooth: Finish implementing remote disconnect.
This was started as a task on some platforms, and waiting for completion was handled differently for each platform. This uses a task so we can cleanly finalize this task along with broadcasting clean up in the following commits. Now we could also implement an awaitable disconnect method on the Remote.
1 parent a0d063b commit be6304e

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,11 +954,22 @@ void pbdrv_bluetooth_peripheral_write(pbio_task_t *task, pbdrv_bluetooth_value_t
954954
start_task(task, peripheral_write_task, value);
955955
}
956956

957-
void pbdrv_bluetooth_peripheral_disconnect(void) {
957+
static PT_THREAD(peripheral_disconnect_task(struct pt *pt, pbio_task_t *task)) {
958+
PT_BEGIN(pt);
959+
958960
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
959961
if (peri->con_handle != HCI_CON_HANDLE_INVALID) {
960962
gap_disconnect(peri->con_handle);
961963
}
964+
965+
PT_WAIT_UNTIL(pt, peri->con_handle == HCI_CON_HANDLE_INVALID);
966+
967+
task->status = PBIO_SUCCESS;
968+
PT_END(pt);
969+
}
970+
971+
void pbdrv_bluetooth_peripheral_disconnect(pbio_task_t *task) {
972+
start_task(task, peripheral_disconnect_task, NULL);
962973
}
963974

964975
static PT_THREAD(start_broadcasting_task(struct pt *pt, pbio_task_t *task)) {

lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,8 @@ static PT_THREAD(peripheral_disconnect_task(struct pt *pt, pbio_task_t *task)) {
723723
PT_END(pt);
724724
}
725725

726-
void pbdrv_bluetooth_peripheral_disconnect(void) {
727-
static pbio_task_t task;
728-
start_task(&task, peripheral_disconnect_task, NULL);
726+
void pbdrv_bluetooth_peripheral_disconnect(pbio_task_t *task) {
727+
start_task(task, peripheral_disconnect_task, NULL);
729728
}
730729

731730
static PT_THREAD(broadcast_task(struct pt *pt, pbio_task_t *task)) {

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,16 +1052,16 @@ static PT_THREAD(peripheral_disconnect_task(struct pt *pt, pbio_task_t *task)) {
10521052
PT_WAIT_WHILE(pt, write_xfer_size);
10531053
GAP_TerminateLinkReq(peri->con_handle, 0x13);
10541054
PT_WAIT_UNTIL(pt, hci_command_status);
1055+
PT_WAIT_UNTIL(pt, peri->con_handle == NO_CONNECTION);
10551056
}
10561057

10571058
task->status = PBIO_SUCCESS;
10581059

10591060
PT_END(pt);
10601061
}
10611062

1062-
void pbdrv_bluetooth_peripheral_disconnect(void) {
1063-
static pbio_task_t task;
1064-
start_task(&task, peripheral_disconnect_task, NULL);
1063+
void pbdrv_bluetooth_peripheral_disconnect(pbio_task_t *task) {
1064+
start_task(task, peripheral_disconnect_task, NULL);
10651065
}
10661066

10671067
static PT_THREAD(broadcast_task(struct pt *pt, pbio_task_t *task)) {

lib/pbio/include/pbdrv/bluetooth.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void pbdrv_bluetooth_periperal_read_characteristic(pbio_task_t *task, pbdrv_blue
331331
// TODO: make this a generic write without response function
332332
void pbdrv_bluetooth_peripheral_write(pbio_task_t *task, pbdrv_bluetooth_value_t *value);
333333
// TODO: make this a generic disconnect
334-
void pbdrv_bluetooth_peripheral_disconnect(void);
334+
void pbdrv_bluetooth_peripheral_disconnect(pbio_task_t *task);
335335

336336
/**
337337
* Starts broadcasting undirected, non-connectable, non-scannable advertisement
@@ -430,7 +430,7 @@ static inline void pbdrv_bluetooth_peripheral_write(pbio_task_t *task, pbdrv_blu
430430
task->status = PBIO_ERROR_NOT_SUPPORTED;
431431
}
432432

433-
static inline void pbdrv_bluetooth_peripheral_disconnect(void) {
433+
static inline void pbdrv_bluetooth_peripheral_disconnect(pbio_task_t *task) {
434434
}
435435

436436
static inline void pbdrv_bluetooth_start_broadcasting(pbio_task_t *task, pbdrv_bluetooth_value_t *value) {

pybricks/iodevices/pb_type_iodevices_lwp3device.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,17 @@ STATIC void pb_lwp3device_configure_remote(void) {
315315
nlr_pop();
316316
} else {
317317
// disconnect if any setup task failed
318-
pbdrv_bluetooth_peripheral_disconnect();
318+
pbdrv_bluetooth_peripheral_disconnect(&remote->task);
319+
pb_module_tools_pbio_task_do_blocking(&remote->task, -1);
319320
nlr_jump(nlr.ret_val);
320321
}
321322
}
322323

323324
void pb_type_Remote_cleanup(void) {
324-
pbdrv_bluetooth_peripheral_disconnect();
325+
static pbio_task_t disconnect_task;
326+
pbdrv_bluetooth_peripheral_disconnect(&disconnect_task);
325327

326-
while (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL)) {
328+
while (disconnect_task.status == PBIO_ERROR_AGAIN) {
327329
MICROPY_EVENT_POLL_HOOK
328330
}
329331
}

0 commit comments

Comments
 (0)