fix reset logic to not do pin ops or heap ops at bad times

This commit is contained in:
Dan Halbert 2020-08-13 17:47:35 -04:00
parent 44c9c43cd1
commit d0ffdda5bb
2 changed files with 21 additions and 15 deletions

View File

@ -268,13 +268,14 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
supervisor_disable_tick(); supervisor_disable_tick();
} }
// Stop any current activity; reset to known state. // Enabling or disabling: stop any current activity; reset to known state.
check_hci_error(hci_reset()); check_hci_error(hci_reset());
self->now_advertising = false; self->now_advertising = false;
self->extended_advertising = false; self->extended_advertising = false;
self->circuitpython_advertising = false; self->circuitpython_advertising = false;
self->advertising_timeout_msecs = 0; self->advertising_timeout_msecs = 0;
if (enabled) {
// Reset list of known attributes. // Reset list of known attributes.
// Indices into the list are handles. Handle 0x0000 designates an invalid handle, // Indices into the list are handles. Handle 0x0000 designates an invalid handle,
// so store None there to skip it. // so store None there to skip it.
@ -283,6 +284,7 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
self->last_added_service_handle = BLE_GATT_HANDLE_INVALID; self->last_added_service_handle = BLE_GATT_HANDLE_INVALID;
self->last_added_characteristic_handle = BLE_GATT_HANDLE_INVALID; self->last_added_characteristic_handle = BLE_GATT_HANDLE_INVALID;
} }
}
bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) { bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) {
return self->enabled; return self->enabled;
@ -392,10 +394,12 @@ mp_obj_t common_hal_bleio_adapter_start_scan(bleio_adapter_obj_t *self, uint8_t*
void common_hal_bleio_adapter_stop_scan(bleio_adapter_obj_t *self) { void common_hal_bleio_adapter_stop_scan(bleio_adapter_obj_t *self) {
check_enabled(self); check_enabled(self);
check_hci_error(hci_le_set_scan_enable(BT_HCI_LE_SCAN_DISABLE, BT_HCI_LE_SCAN_FILTER_DUP_DISABLE)); // If not already scanning, no problem.
if (hci_le_set_scan_enable(BT_HCI_LE_SCAN_DISABLE, BT_HCI_LE_SCAN_FILTER_DUP_DISABLE) == HCI_OK) {
shared_module_bleio_scanresults_set_done(self->scan_results, true); shared_module_bleio_scanresults_set_done(self->scan_results, true);
self->scan_results = NULL; self->scan_results = NULL;
} }
}
// typedef struct { // typedef struct {
// uint16_t conn_handle; // uint16_t conn_handle;
@ -782,14 +786,13 @@ void bleio_adapter_gc_collect(bleio_adapter_obj_t* adapter) {
} }
void bleio_adapter_reset(bleio_adapter_obj_t* adapter) { void bleio_adapter_reset(bleio_adapter_obj_t* adapter) {
if (!common_hal_bleio_adapter_get_enabled(adapter)) { if (!common_hal_bleio_adapter_get_enabled(adapter)) {
return; return;
} }
common_hal_bleio_adapter_stop_scan(adapter); // Adapter will be reset.
if (adapter->now_advertising) { common_hal_bleio_adapter_set_enabled(adapter, false);
common_hal_bleio_adapter_stop_advertising(adapter);
}
adapter->connection_objs = NULL; adapter->connection_objs = NULL;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) { for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
@ -801,6 +804,7 @@ void bleio_adapter_reset(bleio_adapter_obj_t* adapter) {
} }
connection->connection_obj = mp_const_none; connection->connection_obj = mp_const_none;
} }
} }
void bleio_adapter_background(bleio_adapter_obj_t* adapter) { void bleio_adapter_background(bleio_adapter_obj_t* adapter) {

2
main.c
View File

@ -212,6 +212,8 @@ bool maybe_run_list(const char ** filenames, pyexec_result_t* exec_result) {
} }
void cleanup_after_vm(supervisor_allocation* heap) { void cleanup_after_vm(supervisor_allocation* heap) {
// Reset port-independent devices, like CIRCUITPY_BLEIO_HCI.
reset_devices();
// Turn off the display and flush the fileystem before the heap disappears. // Turn off the display and flush the fileystem before the heap disappears.
#if CIRCUITPY_DISPLAYIO #if CIRCUITPY_DISPLAYIO
reset_displays(); reset_displays();