Skip to content

Commit 1648303

Browse files
committed
drv/bluetooth_btstack: use PT for tasks
In MicroPython, if a task it canceled, it tries to run the protothread. In the case of starting broadcasting and observing we weren't actually creating any protothread so it caused a crash when cancellation tried to run the thread.
1 parent ac9b039 commit 1648303

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
### Fixed
1414
- Fixed BLE broadcast not working on City hub.
15+
- Fixed crash on BTStack hubs when program stopped during call to `ble.broadcast()`.
1516

1617
[support#402]: https://github.com/pybricks/support/issues/402
1718

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,14 @@ void pbdrv_bluetooth_disconnect_remote(void) {
736736
}
737737
}
738738

739-
void pbdrv_bluetooth_start_broadcasting(pbio_task_t *task, pbdrv_bluetooth_value_t *value) {
739+
static PT_THREAD(start_broadcasting_task(struct pt *pt, pbio_task_t *task)) {
740+
pbdrv_bluetooth_value_t *value = task->context;
741+
742+
PT_BEGIN(pt);
743+
740744
if (value->size > LE_ADVERTISING_DATA_SIZE) {
741745
task->status = PBIO_ERROR_INVALID_ARG;
742-
return;
746+
PT_EXIT(pt);
743747
}
744748

745749
bd_addr_t null_addr = { };
@@ -758,6 +762,12 @@ void pbdrv_bluetooth_start_broadcasting(pbio_task_t *task, pbdrv_bluetooth_value
758762

759763
// REVISIT: use callback to actually wait for start?
760764
task->status = PBIO_SUCCESS;
765+
766+
PT_END(pt);
767+
}
768+
769+
void pbdrv_bluetooth_start_broadcasting(pbio_task_t *task, pbdrv_bluetooth_value_t *value) {
770+
start_task(task, start_broadcasting_task, value);
761771
}
762772

763773
void pbdrv_bluetooth_stop_broadcasting(void) {
@@ -767,7 +777,11 @@ void pbdrv_bluetooth_stop_broadcasting(void) {
767777
}
768778
}
769779

770-
void pbdrv_bluetooth_start_observing(pbio_task_t *task, pbdrv_bluetooth_start_observing_callback_t callback) {
780+
static PT_THREAD(start_observing_task(struct pt *pt, pbio_task_t *task)) {
781+
pbdrv_bluetooth_start_observing_callback_t callback = task->context;
782+
783+
PT_BEGIN(pt);
784+
771785
observe_callback = callback;
772786

773787
if (!is_observing) {
@@ -777,6 +791,12 @@ void pbdrv_bluetooth_start_observing(pbio_task_t *task, pbdrv_bluetooth_start_ob
777791

778792
// REVISIT: use callback to actually wait for start?
779793
task->status = PBIO_SUCCESS;
794+
795+
PT_END(pt);
796+
}
797+
798+
void pbdrv_bluetooth_start_observing(pbio_task_t *task, pbdrv_bluetooth_start_observing_callback_t callback) {
799+
start_task(task, start_observing_task, callback);
780800
}
781801

782802
void pbdrv_bluetooth_stop_observing(void) {

0 commit comments

Comments
 (0)