Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit af3c618

Browse files
author
iwahdan88
committed
Fix crash safebooting device after ble connection
# Conflicts: # esp32/hal/esp32_mphal.c
1 parent f6d1555 commit af3c618

File tree

4 files changed

+71
-35
lines changed

4 files changed

+71
-35
lines changed

esp32/hal/esp32_mphal.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "bootloader.h"
3636
#include "modwlan.h"
3737
#include "modbt.h"
38+
#include "machtimer.h"
39+
#include "mpirq.h"
3840

3941
#include "driver/timer.h"
4042

@@ -220,8 +222,13 @@ void mp_hal_reset_safe_and_boot(bool reset) {
220222
boot_info_t boot_info;
221223
uint32_t boot_info_offset;
222224
/* Disable Wifi/BT to avoid cache region being accessed since it will be disabled when updating Safe boot flag in flash */
225+
machtimer_deinit();
226+
#if MICROPY_PY_THREAD
227+
mp_irq_kill();
228+
mp_thread_deinit();
229+
#endif
223230
wlan_deinit(NULL);
224-
bt_deinit(NULL);
231+
modbt_deinit(false);
225232
if (updater_read_boot_info (&boot_info, &boot_info_offset)) {
226233
boot_info.safeboot = SAFE_BOOT_SW;
227234
updater_write_boot_info (&boot_info, boot_info_offset);

esp32/mods/modbt.c

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#define MOD_BT_GATTS_SUBSCRIBE_EVT (0x0080)
7575
#define MOD_BT_GATTC_MTU_EVT (0x0100)
7676
#define MOD_BT_GATTS_MTU_EVT (0x0200)
77+
#define MOD_BT_GATTS_CLOSE_EVT (0x0400)
7778

7879
/******************************************************************************
7980
DEFINE PRIVATE TYPES
@@ -245,7 +246,7 @@ static esp_ble_adv_params_t bt_adv_params_sec = {
245246
};
246247

247248

248-
static bool mod_bt_is_deinit;
249+
static bool mod_bt_allow_resume_deinit;
249250
static uint16_t mod_bt_gatts_mtu_restore = 0;
250251
static bool mod_bt_is_conn_restore_available;
251252

@@ -288,8 +289,8 @@ void modbt_init0(void) {
288289
}
289290
else
290291
{
291-
//Using only MTU event in group for now
292-
xEventGroupClearBits(bt_event_group, MOD_BT_GATTC_MTU_EVT | MOD_BT_GATTS_MTU_EVT);
292+
//Using only specific events in group for now
293+
xEventGroupClearBits(bt_event_group, MOD_BT_GATTC_MTU_EVT | MOD_BT_GATTS_MTU_EVT | MOD_BT_GATTS_DISCONN_EVT | MOD_BT_GATTS_CLOSE_EVT);
293294
}
294295
bt_event_group = xEventGroupCreate();
295296

@@ -304,13 +305,56 @@ void modbt_init0(void) {
304305

305306
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
306307

307-
mod_bt_is_deinit = false;
308+
mod_bt_allow_resume_deinit = false;
308309
mod_bt_is_conn_restore_available = false;
309310
}
310311

312+
void modbt_deinit(bool allow_reconnect)
313+
{
314+
uint16_t timeout = 0;
315+
if (bt_obj.init)
316+
{
317+
if (bt_obj.scanning) {
318+
esp_ble_gap_stop_scanning();
319+
bt_obj.scanning = false;
320+
}
321+
/* Allow reconnection flag */
322+
mod_bt_allow_resume_deinit = allow_reconnect;
323+
324+
bt_connection_obj_t *connection_obj;
325+
326+
for (mp_uint_t i = 0; i < MP_STATE_PORT(btc_conn_list).len; i++)
327+
{
328+
// loop through the connections
329+
connection_obj = ((bt_connection_obj_t *)(MP_STATE_PORT(btc_conn_list).items[i]));
330+
//close connections
331+
modbt_conn_disconnect(connection_obj);
332+
}
333+
while ((MP_STATE_PORT(btc_conn_list).len > 0) && (timeout < 20) && !mod_bt_allow_resume_deinit)
334+
{
335+
vTaskDelay (50 / portTICK_PERIOD_MS);
336+
timeout++;
337+
}
338+
339+
if (bt_obj.gatts_conn_id >= 0)
340+
{
341+
bt_obj.advertising = false;
342+
esp_ble_gatts_close(bt_obj.gatts_if, bt_obj.gatts_conn_id);
343+
xEventGroupWaitBits(bt_event_group, MOD_BT_GATTS_DISCONN_EVT | MOD_BT_GATTS_CLOSE_EVT, true, true, 1000/portTICK_PERIOD_MS);
344+
}
345+
346+
esp_bluedroid_disable();
347+
esp_bluedroid_deinit();
348+
esp_bt_controller_disable();
349+
bt_obj.init = false;
350+
mod_bt_is_conn_restore_available = false;
351+
xEventGroupClearBits(bt_event_group, MOD_BT_GATTC_MTU_EVT | MOD_BT_GATTS_MTU_EVT | MOD_BT_GATTS_DISCONN_EVT | MOD_BT_GATTS_CLOSE_EVT);
352+
}
353+
}
354+
311355
void bt_resume(bool reconnect)
312356
{
313-
if(mod_bt_is_deinit && !bt_obj.init)
357+
if(mod_bt_allow_resume_deinit && !bt_obj.init)
314358
{
315359
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
316360
esp_bt_controller_init(&bt_cfg);
@@ -367,7 +411,7 @@ void bt_resume(bool reconnect)
367411
}
368412

369413
bt_obj.init = true;
370-
mod_bt_is_deinit = false;
414+
mod_bt_allow_resume_deinit = false;
371415
}
372416
}
373417

@@ -404,7 +448,7 @@ static void close_connection (int32_t conn_id) {
404448
for (mp_uint_t i = 0; i < MP_STATE_PORT(btc_conn_list).len; i++) {
405449
bt_connection_obj_t *connection_obj = ((bt_connection_obj_t *)(MP_STATE_PORT(btc_conn_list).items[i]));
406450
/* Only reset Conn Id if it is a normal disconnect not module de-init to mark conn obj to be restored */
407-
if (connection_obj->conn_id == conn_id && (!mod_bt_is_deinit)) {
451+
if (connection_obj->conn_id == conn_id && (!mod_bt_allow_resume_deinit)) {
408452
connection_obj->conn_id = -1;
409453
mp_obj_list_remove((void *)&MP_STATE_PORT(btc_conn_list), connection_obj);
410454
}
@@ -821,13 +865,16 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
821865
}
822866
}
823867
bt_obj.events |= MOD_BT_GATTS_DISCONN_EVT;
868+
xEventGroupSetBits(bt_event_group, MOD_BT_GATTS_DISCONN_EVT);
824869
if (bt_obj.trigger & MOD_BT_GATTS_DISCONN_EVT) {
825870
mp_irq_queue_interrupt(bluetooth_callback_handler, (void *)&bt_obj);
826871
}
827872
break;
873+
case ESP_GATTS_CLOSE_EVT:
874+
xEventGroupSetBits(bt_event_group, MOD_BT_GATTS_CLOSE_EVT);
875+
break;
828876
case ESP_GATTS_OPEN_EVT:
829877
case ESP_GATTS_CANCEL_OPEN_EVT:
830-
case ESP_GATTS_CLOSE_EVT:
831878
case ESP_GATTS_LISTEN_EVT:
832879
case ESP_GATTS_CONGEST_EVT:
833880
default:
@@ -972,29 +1019,8 @@ STATIC mp_obj_t bt_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
9721019
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bt_init_obj, 1, bt_init);
9731020

9741021
mp_obj_t bt_deinit(mp_obj_t self_in) {
975-
if (bt_obj.init) {
976-
if (bt_obj.scanning) {
977-
esp_ble_gap_stop_scanning();
978-
bt_obj.scanning = false;
979-
}
980-
/* Indicate module is de-initializing */
981-
mod_bt_is_deinit = true;
982-
983-
bt_connection_obj_t *connection_obj;
9841022

985-
for (mp_uint_t i = 0; i < MP_STATE_PORT(btc_conn_list).len; i++)
986-
{
987-
// loop through the connections
988-
connection_obj = ((bt_connection_obj_t *)(MP_STATE_PORT(btc_conn_list).items[i]));
989-
//close connections
990-
modbt_conn_disconnect(connection_obj);
991-
}
992-
esp_bluedroid_disable();
993-
esp_bluedroid_deinit();
994-
esp_bt_controller_disable();
995-
bt_obj.init = false;
996-
mod_bt_is_conn_restore_available = false;
997-
}
1023+
modbt_deinit(false);
9981024
return mp_const_none;
9991025
}
10001026
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bt_deinit_obj, bt_deinit);
@@ -2041,7 +2067,7 @@ STATIC mp_obj_t bt_conn_disconnect(mp_obj_t self_in) {
20412067
esp_ble_gattc_close(bt_obj.gattc_if, self->conn_id);
20422068
esp_ble_gap_disconnect(self->srv_bda);
20432069
/* Only reset Conn Id if it is a normal disconnect not module de-init to mark conn obj to be restored */
2044-
if(!mod_bt_is_deinit)
2070+
if(!mod_bt_allow_resume_deinit)
20452071
{
20462072
self->conn_id = -1;
20472073
}

esp32/mods/modbt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
extern void modbt_init0(void);
2929
extern mp_obj_t bt_deinit(mp_obj_t self_in);
3030
extern void bt_resume(bool reconnect);
31+
void modbt_deinit(bool allow_reconnect);
3132
#endif // MODBT_H_

esp32/mods/modmachine.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle);
184184
STATIC mp_obj_t machine_sleep (uint n_args, const mp_obj_t *arg) {
185185

186186
bool reconnect = false;
187-
bt_deinit(NULL);
188-
wlan_deinit(NULL);
187+
189188
#if defined(FIPY) || defined(GPY)
190189
if (lteppp_modem_state() < E_LTE_MODEM_DISCONNECTED) {
191190
lteppp_deinit();
@@ -223,6 +222,9 @@ STATIC mp_obj_t machine_sleep (uint n_args, const mp_obj_t *arg) {
223222
}
224223
}
225224

225+
modbt_deinit(reconnect);
226+
wlan_deinit(NULL);
227+
226228
if(ESP_OK != esp_light_sleep_start())
227229
{
228230
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Wifi or BT not stopped before sleep"));
@@ -241,7 +243,7 @@ STATIC mp_obj_t machine_deepsleep (uint n_args, const mp_obj_t *arg) {
241243
#ifndef RGB_LED_DISABLE
242244
mperror_enable_heartbeat(false);
243245
#endif
244-
bt_deinit(NULL);
246+
modbt_deinit(false);
245247
wlan_deinit(NULL);
246248
#if defined(FIPY) || defined(GPY)
247249
if (lteppp_modem_state() < E_LTE_MODEM_DISCONNECTED) {

0 commit comments

Comments
 (0)