8000 Fix a problem when machine.sleep(resume_wifi_ble=True) drops exceptio… · pycom/pycom-micropython-sigfox@ca10cd1 · GitHub
[go: up one dir, main page]

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

Commit ca10cd1

Browse files
committed
Fix a problem when machine.sleep(resume_wifi_ble=True) drops exception when Pybytes & SmartConfig is used, but no WLAN connection has been established
1 parent 38f9019 commit ca10cd1

File tree

3 files changed

+75
-32
lines changed

3 files changed

+75
-32
lines changed

esp32/mods/modmachine.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,13 @@ STATIC mp_obj_t machine_sleep (uint n_args, const mp_obj_t *arg) {
435435
}
436436

437437
modbt_deinit(reconnect);
438-
wlan_deinit(NULL);
438+
// TRUE means wlan_deinit is called from machine_sleep
439+
wlan_deinit(mp_const_true);
439440

440441
if(ESP_OK != esp_light_sleep_start())
441442
{
442443
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Wifi or BT not stopped before sleep"));
443444
}
444-
445445
/* resume wlan */
446446
wlan_resume(reconnect);
447447
/* resume bt */

esp32/mods/modwlan.c

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ wlan_obj_t wlan_obj = {
9696
.irq_enabled = false,
9797
.pwrsave = false,
9898
.is_promiscuous = false,
99-
.sta_conn_timeout = false
99+
.sta_conn_timeout = false,
100+
.country = NULL
100101
};
101102

102103
/* TODO: Maybe we can add possibility to create IRQs for wifi events */
103104

104105
static EventGroupHandle_t wifi_event_group;
105106

106-
static bool mod_wlan_is_deinit = false;
107107
static uint16_t mod_wlan_ap_number_of_connections = 0;
108108

109109
/* Variables holding wlan conn params for wakeup recovery of connections */
@@ -135,6 +135,7 @@ SemaphoreHandle_t smartConfigTimeout_mutex;
135135

136136
static const int ESPTOUCH_DONE_BIT = BIT1;
137137
static const int ESPTOUCH_STOP_BIT = BIT2;
138+
static const int ESPTOUCH_SLEEP_STOP_BIT = BIT3;
138139
static bool wlan_smart_config_enabled = false;
139140

140141
/******************************************************************************
@@ -202,24 +203,36 @@ void wlan_pre_init (void) {
202203
wlan_obj.mutex = xSemaphoreCreateMutex();
203204
timeout_mutex = xSemaphoreCreateMutex();
204205
smartConfigTimeout_mutex = xSemaphoreCreateMutex();
205-
memcpy(wlan_obj.country.cc, (const char*)"NA", sizeof(wlan_obj.country.cc));
206206
// create Smart Config Task
207207
xTaskCreatePinnedToCore(TASK_SMART_CONFIG, "SmartConfig", SMART_CONF_TASK_STACK_SIZE / sizeof(StackType_t), NULL, SMART_CONF_TASK_PRIORITY, &SmartConfTaskHandle, 1);
208208
}
209209

210210
void wlan_resume (bool reconnect)
211211
{
212-
if (!wlan_obj.started && mod_wlan_is_deinit) {
213-
214-
mod_wlan_is_deinit = false;
215-
216-
if(reconnect)
217-
{
218-
wifi_country_t* country = NULL;
219-
220-
if(strcmp((const char*)wlan_obj.country.cc, "NA"))
221-
{
222-
country = &(wlan_obj.country);
212+
// Configure back WLAN as it was before if reconnect is TRUE
213+
if(reconnect) {
214+
// If SmartConfig enabled then re-start it
215+
if(wlan_smart_config_enabled) {
216+
// Do initial configuration as at this point the Wifi Driver is not initialized
217+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
218+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
219+
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
220+
wlan_set_antenna(wlan_obj.antenna);
221+
wlan_set_mode(wlan_obj.mode);
222+
wlan_set_bandwidth(wlan_obj.bandwidth);
223+
if(wlan_obj.country != NULL) {
224+
esp_wifi_set_country(wlan_obj.country);
225+
}
226+
xTaskNotifyGive(SmartConfTaskHandle);
227+
}
228+
// Otherwise set up WLAN with the same parameters as it was before
229 6DB6 +
else {
230+
// In wlan_setup the wlan_obj.country is overwritten with the value coming from setup_config, need to save it out
231+
wifi_country_t country;
232+
wifi_country_t* country_ptr = NULL;
233+
if(wlan_obj.country != NULL) {
234+
memcpy(&country, wlan_obj.country, sizeof(wifi_country_t));
235+
country_ptr = &country;
223236
}
224237

225238
wlan_internal_setup_t setup_config = {
@@ -234,11 +247,11 @@ void wlan_resume (bool reconnect)
234247
false,
235248
wlan_conn_recover_hidden,
236249
wlan_obj.bandwidth,
237-
country,
250+
country_ptr,
238251
&(wlan_obj.max_tx_pwr)
239252
};
240253

241-
// initialize the wlan subsystem
254+
// Initialize & reconnect to the previous connection
242255
wlan_setup(&setup_config);
243256
}
244257
}
@@ -258,12 +271,15 @@ void wlan_setup (wlan_internal_setup_t *config) {
258271
wlan_set_bandwidth(config->bandwidth);
259272

260273
if (config->country != NULL) {
261-
262-
if(ESP_OK != esp_wifi_set_country(config->country))
274+
esp_err_t ret = esp_wifi_set_country(config->country);
275+
if(ESP_OK != ret)
263276
{
264277
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
265278
}
266-
memcpy(&(wlan_obj.country), config->country, sizeof(wlan_obj.country));
279+
if(wlan_obj.country == NULL) {
280+
wlan_obj.country = (wifi_country_t*)malloc(sizeof(wifi_country_t));
281+
}
282+
memcpy(wlan_obj.country, config->country, sizeof(wifi_country_t));
267283
}
268284

269285
if(config->max_tx_pr != NULL)
@@ -950,9 +966,8 @@ static void TASK_SMART_CONFIG (void *pvParameters) {
950966
static uint32_t thread_notification;
951967

952968
smartConf_init:
953-
wlan_smart_config_enabled = false;
954969
connected = false;
955-
// Block task till notification is recieved
970+
// Block task till notification is received
956971
thread_notification = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
957972

958973
if (thread_notification) {
@@ -967,30 +982,37 @@ static void TASK_SMART_CONFIG (void *pvParameters) {
967982
}
968983
CHECK_ESP_ERR(esp_smartconfig_set_type(SC_TYPE_ESPTOUCH), smartConf_init)
969984
CHECK_ESP_ERR(esp_smartconfig_start(smart_config_callback), smartConf_init)
970-
wlan_smart_config_enabled = true;
971985
goto smartConf_start;
972986
}
973987
goto smartConf_init;
974988

975989
smartConf_start:
976-
//mp_printf(&mp_plat_print, "\n-------SmartConfig Started-------\n");
990+
wlan_smart_config_enabled = true;
991+
mp_printf(&mp_plat_print, "\n-------SmartConfig Started-------\n");
977992
/*create Timer */
978993
wlan_smartConfig_timeout = xTimerCreate("smartConfig_Timer", 60000 / portTICK_PERIOD_MS, 0, 0, wlan_timer_callback);
979994
/*start Timer */
980995
xTimerStart(wlan_smartConfig_timeout, 0);
981996
while (1) {
982-
uxBits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT | ESPTOUCH_STOP_BIT, true, false, portMAX_DELAY);
997+
uxBits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT | ESPTOUCH_STOP_BIT | ESPTOUCH_SLEEP_STOP_BIT, true, false, portMAX_DELAY);
983998
if(uxBits & ESPTOUCH_STOP_BIT) {
999+
wlan_smart_config_enabled = false;
9841000
esp_smartconfig_stop();
9851001
//mp_printf(&mp_plat_print, "\nSmart Config Aborted or Timed-out\n");
9861002
goto smartConf_init;
9871003
}
1004+
if(uxBits & ESPTOUCH_SLEEP_STOP_BIT) {
1005+
esp_smartconfig_stop();
1006+
//mp_printf(&mp_plat_print, "\nSmart Config Aborted because sleep operation has been requested\n");
1007+
goto smartConf_init;
1008+
}
9881009
if(uxBits & CONNECTED_BIT) {
9891010
//mp_printf(&mp_plat_print, "WiFi Connected to ap\n");
9901011
connected = true;
9911012
}
9921013
if(uxBits & ESPTOUCH_DONE_BIT) {
9931014
//mp_printf(&mp_plat_print, "smartconfig over\n");
1015+
wlan_smart_config_enabled = false;
9941016
esp_smartconfig_stop();
9951017
wlan_stop_smartConfig_timer();
9961018
//set event flag
@@ -1244,11 +1266,25 @@ mp_obj_t wlan_deinit(mp_obj_t self_in) {
12441266

12451267
if (wlan_obj.started)
12461268
{
1269+
bool called_from_sleep = false;
1270+
12471271
// stop smart config if enabled
1248-
if(wlan_smart_config_enabled)
1249-
{
1250-
xEventGroupSetBits(wifi_event_group, ESPTOUCH_STOP_BIT);
1251-
vTaskDelay(100/portTICK_PERIOD_MS);
1272+
if(wlan_smart_config_enabled) {
1273+
// If the input parameter is not the object itself but a simple boolean, it is interpreted that
1274+
// wlan_deinit is called from machine_sleep()
1275+
if(mp_obj_get_type(self_in) == &mp_type_bool) {
1276+
called_from_sleep = (bool)mp_obj_get_int(self_in);
1277+
if(called_from_sleep == true) {
1278+
// stop smart config with special event
1279+
xEventGroupSetBits(wifi_event_group, ESPTOUCH_SLEEP_STOP_BIT);
1280+
vTaskDelay(100/portTICK_PERIOD_MS);
1281+
}
1282+
}
1283+
else {
1284+
// stop smart config with original STOP event
1285+
xEventGroupSetBits(wifi_event_group, ESPTOUCH_STOP_BIT);
1286+
vTaskDelay(100/portTICK_PERIOD_MS);
1287+
}
12521288
}
12531289

12541290
mod_network_deregister_nic(&wlan_obj);
@@ -1262,7 +1298,11 @@ mp_obj_t wlan_deinit(mp_obj_t self_in) {
12621298

12631299
/* stop and free wifi resource */
12641300
esp_wifi_deinit();
1265-
mod_wlan_is_deinit = true;
1301+
// Only free up memory area of country information if this deinit is not called from machine.sleep()
1302+
if(called_from_sleep == false) {
1303+
free(wlan_obj.country);
1304+
wlan_obj.country = NULL;
1305+
}
12661306
wlan_obj.started = false;
12671307
mod_network_deregister_nic(&wlan_obj);
12681308
}
@@ -2446,6 +2486,9 @@ STATIC mp_obj_t wlan_country(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
24462486
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
24472487
}
24482488

2489+
if(wlan_obj.country == NULL) {
2490+
wlan_obj.country = (wifi_country_t*)malloc(sizeof(wifi_country_t));
2491+
}
24492492
memcpy(&(wlan_obj.country), &country_config, sizeof(wlan_obj.country));
24502493

24512494
return mp_const_none;

esp32/mods/modwlan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ typedef struct _wlan_obj_t {
7373
uint8_t channel;
7474
uint8_t antenna;
7575
int8_t max_tx_pwr;
76-
wifi_country_t country;
76+
wifi_country_t* country;
7777

7878
// my own ssid, key and mac
7979
uint8_t ssid[(MODWLAN_SSID_LEN_MAX + 1)];

0 commit comments

Comments
 (0)
0