Skip to content

Commit d0ffdda

Browse files
committed
fix reset logic to not do pin ops or heap ops at bad times
1 parent 44c9c43 commit d0ffdda

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

devices/ble_hci/common-hal/_bleio/Adapter.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,20 +268,22 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
268268
supervisor_disable_tick();
269269
}
270270

271-
// Stop any current activity; reset to known state.
271+
// Enabling or disabling: stop any current activity; reset to known state.
272272
check_hci_error(hci_reset());
273273
self->now_advertising = false;
274274
self->extended_advertising = false;
275275
self->circuitpython_advertising = false;
276276
self->advertising_timeout_msecs = 0;
277277

278-
// Reset list of known attributes.
279-
// Indices into the list are handles. Handle 0x0000 designates an invalid handle,
280-
// so store None there to skip it.
281-
self->attributes = mp_obj_new_list(0, NULL);
282-
bleio_adapter_add_attribute(self, mp_const_none);
283-
self->last_added_service_handle = BLE_GATT_HANDLE_INVALID;
284-
self->last_added_characteristic_handle = BLE_GATT_HANDLE_INVALID;
278+
if (enabled) {
279+
// Reset list of known attributes.
280+
// Indices into the list are handles. Handle 0x0000 designates an invalid handle,
281+
// so store None there to skip it.
282+
self->attributes = mp_obj_new_list(0, NULL);
283+
bleio_adapter_add_attribute(self, mp_const_none);
284+
self->last_added_service_handle = BLE_GATT_HANDLE_INVALID;
285+
self->last_added_characteristic_handle = BLE_GATT_HANDLE_INVALID;
286+
}
285287
}
286288

287289
bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) {
@@ -392,9 +394,11 @@ mp_obj_t common_hal_bleio_adapter_start_scan(bleio_adapter_obj_t *self, uint8_t*
392394
void common_hal_bleio_adapter_stop_scan(bleio_adapter_obj_t *self) {
393395
check_enabled(self);
394396

395-
check_hci_error(hci_le_set_scan_enable(BT_HCI_LE_SCAN_DISABLE, BT_HCI_LE_SCAN_FILTER_DUP_DISABLE));
396-
shared_module_bleio_scanresults_set_done(self->scan_results, true);
397-
self->scan_results = NULL;
397+
// If not already scanning, no problem.
398+
if (hci_le_set_scan_enable(BT_HCI_LE_SCAN_DISABLE, BT_HCI_LE_SCAN_FILTER_DUP_DISABLE) == HCI_OK) {
399+
shared_module_bleio_scanresults_set_done(self->scan_results, true);
400+
self->scan_results = NULL;
401+
}
398402
}
399403

400404
// typedef struct {
@@ -782,14 +786,13 @@ void bleio_adapter_gc_collect(bleio_adapter_obj_t* adapter) {
782786
}
783787

784788
void bleio_adapter_reset(bleio_adapter_obj_t* adapter) {
789+
785790
if (!common_hal_bleio_adapter_get_enabled(adapter)) {
786791
return;
787792
}
788793

789-
common_hal_bleio_adapter_stop_scan(adapter);
790-
if (adapter->now_advertising) {
791-
common_hal_bleio_adapter_stop_advertising(adapter);
792-
}
794+
// Adapter will be reset.
795+
common_hal_bleio_adapter_set_enabled(adapter, false);
793796

794797
adapter->connection_objs = NULL;
795798
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
@@ -801,6 +804,7 @@ void bleio_adapter_reset(bleio_adapter_obj_t* adapter) {
801804
}
802805
connection->connection_obj = mp_const_none;
803806
}
807+
804808
}
805809

806810
void bleio_adapter_background(bleio_adapter_obj_t* adapter) {

main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ bool maybe_run_list(const char ** filenames, pyexec_result_t* exec_result) {
212212
}
213213

214214
void cleanup_after_vm(supervisor_allocation* heap) {
215+
// Reset port-independent devices, like CIRCUITPY_BLEIO_HCI.
216+
reset_devices();
215217
// Turn off the display and flush the fileystem before the heap disappears.
216218
#if CIRCUITPY_DISPLAYIO
217219
reset_displays();

0 commit comments

Comments
 (0)