8000 Merge pull request #6389 from tannewt/add_ble_user_reset · tannewt/circuitpython@395b550 · GitHub
[go: up one dir, main page]

Skip to content

Commit 395b550

Browse files
authored
Merge pull request micropython#6389 from tannewt/add_ble_user_reset
Add two stage reset for BLE
2 parents 9028bf2 + 3e85cfe commit 395b550

File tree

14 files changed

+89
-16
lines changed

14 files changed

+89
-16
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ bool vm_used_ble;
5757
// }
5858
// }
5959

60+
void bleio_user_reset() {
61+
// HCI doesn't support the BLE workflow so just do a full reset.
62+
bleio_reset();
63+
}
64+
6065
// Turn off BLE on a reset or reload.
6166
void bleio_reset() {
6267
// Create a UUID object for all CCCD's.

main.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ STATIC void cleanup_after_vm(supervisor_allocation *heap, mp_obj_t exception) {
277277
memorymonitor_reset();
278278
#endif
279279

280-
filesystem_flush();
281-
stop_mp();
282-
free_memory(heap);
283-
supervisor_move_memory();
280+
// Disable user related BLE state that uses the micropython heap.
281+
#if CIRCUITPY_BLEIO
282+
bleio_user_reset();
283+
#endif
284284

285285
#if CIRCUITPY_CANIO
286286
common_hal_canio_reset();
@@ -297,6 +297,12 @@ STATIC void cleanup_after_vm(supervisor_allocation *heap, mp_obj_t exception) {
297297
#endif
298298
reset_port();
299299
reset_board();
300+
301+
// Free the heap last because other modules may reference heap memory and need to shut down.
302+
filesystem_flush();
303+
stop_mp();
304+
free_memory(heap);
305+
supervisor_move_memory();
300306
}
301307

302308
STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
@@ -645,6 +651,12 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
645651

646652
// Done waiting, start the board back up.
647653

654+
// We delay resetting BLE until after the wait in case we're transferring
655+
// more files over.
656+
#if CIRCUITPY_BLEIO
657+
bleio_reset();
658+
#endif
659+
648660
// free code allocation if unused
649661
if ((next_code_options & next_code_stickiness_situation) == 0) {
650662
free_memory(next_code_allocation);
@@ -888,6 +900,7 @@ int __attribute__((used)) main(void) {
888900
serial_init();
889901

890902
#if CIRCUITPY_BLEIO
903+
bleio_reset();
891904
supervisor_bluetooth_enable_workflow();
892905
supervisor_start_bluetooth();
893906
#endif

ports/espressif/common-hal/_bleio/Adapter.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool
574574
}
575575

576576
void common_hal_bleio_adapter_stop_advertising(bleio_adapter_obj_t *self) {
577+
if (!common_hal_bleio_adapter_get_advertising(self)) {
578+
return;
579+
}
577580
int err_code = ble_gap_ext_adv_stop(0);
578581
self->user_advertising = false;
579582

ports/espressif/common-hal/_bleio/__init__.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242
// #include "common-hal/_bleio/bonding.h"
4343
#include "common-hal/_bleio/ble_events.h"
4444

45+
void bleio_user_reset() {
46+
// Stop any user scanning or advertising.
47+
common_hal_bleio_adapter_stop_scan(&common_hal_bleio_adapter_obj);
48+
common_hal_bleio_adapter_stop_advertising(&common_hal_bleio_adapter_obj);
49+
50+
ble_event_remove_heap_handlers();
51+
52+
// Maybe start advertising the BLE workflow.
53+
supervisor_bluetooth_background();
54+
}
55+
4556
// Turn off BLE on a reset or reload.
4657
void bleio_reset() {
4758
// Set this explicitly to save data.

ports/espressif/common-hal/_bleio/ble_events.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdbool.h>
3232
#include <stdio.h>
3333

34+
#include "py/gc.h"
3435
#include "py/misc.h"
3536
#include "py/mpstate.h"
3637
#include "py/runtime.h"
@@ -44,6 +45,17 @@ void ble_event_reset(void) {
4445
MP_STATE_VM(ble_event_handler_entries) = NULL;
4546
}
4647

48+
void ble_event_remove_heap_handlers(void) {
49+
ble_event_handler_entry_t *it = MP_STATE_VM(ble_event_handler_entries);
50+
while (it != NULL) {
51+
// If the param is on the heap, then delete the handler.
52+
if (HEAP_PTR(it->param)) {
53+
ble_event_remove_handler(it->func, it->param);
54+
}
55+
it = it->next;
56+
}
57+
}
58+
4759
void ble_event_add_handler_entry(ble_event_handler_entry_t *entry,
4860
ble_gap_event_fn *func, void *param) {
4961
ble_event_handler_entry_t *it = MP_STATE_VM(ble_event_handler_entries);

ports/espressif/common-hal/_bleio/ble_events.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct ble_event_handler_entry {
4040
} ble_event_handler_entry_t;
4141

4242
void ble_event_reset(void);
43+
void ble_event_remove_heap_handlers(void);
4344
void ble_event_add_handler(ble_gap_event_fn *func, void *param);
4445
void ble_event_remove_handler(ble_gap_event_fn *func, void *param);
4546

ports/espressif/supervisor/port.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,6 @@ void reset_port(void) {
283283
watchdog_reset();
284284
#endif
285285

286-
#if CIRCUITPY_BLEIO
287-
bleio_reset();
288-
#endif
289-
290286
#if CIRCUITPY_WIFI
291287
wifi_reset();
292288
#endif

ports/nrf/bluetooth/ble_drv.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "nrf_sdm.h"
3636
#include "nrf_soc.h"
3737
#include "nrfx_power.h"
38+
#include "py/gc.h"
3839
#include "py/misc.h"
3940
#include "py/mpstate.h"
4041

@@ -56,6 +57,17 @@ void ble_drv_reset() {
5657
sd_flash_operation_status = SD_FLASH_OPERATION_DONE;
5758
}
5859

60+
void ble_drv_remove_heap_handlers(void) {
61+
ble_drv_evt_handler_entry_t *it = MP_STATE_VM(ble_drv_evt_handler_entries);
62+
while (it != NULL) {
63+
// If the param is on the heap, then delete the handler.
64+
if (HEAP_PTR(it->param)) {
65+
ble_drv_remove_event_handler(it->func, it->param);
66+
}
67+
it = it->next;
68+
}
69+
}
70+
5971
void ble_drv_add_event_handler_entry(ble_drv_evt_handler_entry_t *entry, ble_drv_evt_handler_t func, void *param) {
6072
ble_drv_evt_handler_entry_t *it = MP_STATE_VM(ble_drv_evt_handler_entries);
6173
while (it != NULL) {

ports/nrf/bluetooth/ble_drv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ typedef struct ble_drv_evt_handler_entry {
6767
} ble_drv_evt_handler_entry_t;
6868

6969
void ble_drv_reset(void);
70+
void ble_drv_remove_heap_handlers(void);
7071
void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param);
7172
void ble_drv_remove_event_handler(ble_drv_evt_handler_t func, void *param);
7273

ports/nrf/common-hal/_bleio/PacketBuffer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,11 @@ mp_int_t common_hal_bleio_packet_buffer_write(bleio_packet_buffer_obj_t *self, c
380380
!mp_hal_is_interrupted()) {
381381
RUN_BACKGROUND_TASKS;
382382
}
383+
if (mp_hal_is_interrupted()) {
384+
return -1;
385+
}
383386
}
384-
if (self->conn_handle == BLE_CONN_HANDLE_INVALID ||
385-
mp_hal_is_interrupted()) {
387+
if (self->conn_handle == BLE_CONN_HANDLE_INVALID) {
386388
return -1;
387389
}
388390

0 commit comments

Comments
 (0)
0