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

Commit 7837d32

Browse files
author
iwahdan88
committed
esp32/modwlan: Fix Wifi connection resuming after more than 1 machine sleep cycle
Issue : #259
1 parent a004d14 commit 7837d32

File tree

3 files changed

+70
-39
lines changed

3 files changed

+70
-39
lines changed

esp32/mods/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ STATIC mp_obj_t machine_sleep (uint n_args, const mp_obj_t *arg) {
187187
bt_deinit(NULL);
188188
wlan_deinit(NULL);
189189
#if defined(FIPY) || defined(GPY)
190-
while (!lteppp_task_ready()) {
190+
while (config_get_lte_modem_enable_on_boot() && !lteppp_task_ready()) {
191191
mp_hal_delay_ms(2);
192192
}
193193
lteppp_deinit();

esp32/mods/modwlan.c

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ wlan_obj_t wlan_obj = {
8989
.enable_servers = false,
9090
.pwrsave = false,
9191
.max_tx_pwr = CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER, /*FIXME: This value is configured in sdkconfig but the max_tx_power is always init to 78 at startup by idf*/
92-
.is_promiscuous = false
92+
.is_promiscuous = false,
93+
.sta_conn_timeout = false
9394
};
9495

9596
/* TODO: Maybe we can add possibility to create IRQs for wifi events */
@@ -106,14 +107,11 @@ static uint8_t wlan_conn_recover_key[MAX_WLAN_KEY_SIZE];
106107
static uint8_t wlan_conn_recover_hostname[TCPIP_HOSTNAME_MAX_SIZE];
107108
static uint8_t wlan_conn_recover_auth;
108109
static int32_t wlan_conn_recover_timeout;
109-
static uint8_t wlan_conn_recover_ap_channel;
110110
static uint8_t wlan_conn_recover_scan_channel;
111-
static uint8_t wlan_conn_recover_antenna;
112111
static bool wlan_conn_recover_hidden = false;
113-
static int32_t wlan_conn_recover_bandwidth;
114-
static uint8_t wlan_conn_recover_mode;
112+
115113
static wlan_wpa2_ent_obj_t wlan_wpa2_ent;
116-
static TimerHandle_t wlan_conn_timeout_timer;
114+
static TimerHandle_t wlan_conn_timeout_timer = NULL;
117115
static uint8_t wlan_prom_data_buff[2][MAX_WIFI_PROM_PKT_SIZE] = {0};
118116
static wlan_internal_prom_t wlan_prom_packet[2];
119117

@@ -122,7 +120,10 @@ static uint8_t token = 0;
122120
// Event bits
123121
const int CONNECTED_BIT = BIT0;
124122

125-
STATIC bool is_inf_up = false;
123+
static bool is_inf_up = false;
124+
125+
//mutex for Timeout Counter protection
126+
SemaphoreHandle_t timeout_mutex;
126127

127128
/******************************************************************************
128129
DECLARE PUBLIC DATA
@@ -155,6 +156,7 @@ static char *wlan_read_file (const char *file_path, vstr_t *vstr);
155156
static void wlan_timer_callback( TimerHandle_t xTimer );
156157
static void wlan_validate_country(const char * country);
157158
static void wlan_validate_country_policy(uint8_t policy);
159+
STATIC void wlan_stop_sta_conn_timer();
158160
//*****************************************************************************
159161
//
160162
//! \brief The Function Handles WLAN Events
@@ -186,6 +188,8 @@ void wlan_pre_init (void) {
186188
(wlan_prom_packet[0].data) = (uint8_t*) (&(wlan_prom_data_buff[0][0]));
187189
(wlan_prom_packet[1].data) = (uint8_t*) (&(wlan_prom_data_buff[1][0]));
188190
wlan_obj.mutex = xSemaphoreCreateMutex();
191+
timeout_mutex = xSemaphoreCreateMutex();
192+
memcpy(wlan_obj.country.cc, (const char*)"NA", sizeof(wlan_obj.country.cc));
189193
}
190194

191195
void wlan_resume (bool reconnect)
@@ -199,6 +203,7 @@ void wlan_resume (bool reconnect)
199203
const char* bssid = NULL;
200204
const char* key = NULL;
201205
const char* hostname = NULL;
206+
wifi_country_t* country = NULL;
202207

203208
if(wlan_conn_recover_bssid[0] != '\0')
204209
{
@@ -212,34 +217,36 @@ void wlan_resume (bool reconnect)
212217
{
213218
hostname = (const char*)wlan_conn_recover_hostname;
214219
}
220+
if(strcmp((const char*)wlan_obj.country.cc, "NA"))
221+
{
222+
country = &(wlan_obj.country);
223+
}
215224

216225
wlan_internal_setup_t setup_config = {
217-
wlan_conn_recover_mode,
218-
(const char *)wlan_conn_recover_ssid,
219-
key,
220-
wlan_conn_recover_auth,
221-
wlan_conn_recover_ap_channel,
222-
wlan_conn_recover_antenna,
226+
wlan_obj.mode,
227+
(const char *)(wlan_obj.ssid),
228+
(const char *)(wlan_obj.key),
229+
wlan_obj.auth,
230+
wlan_obj.channel,
231+
wlan_obj.antenna,
223232
false,
224233
wlan_conn_recover_hidden,
225-
wlan_conn_recover_bandwidth,
226-
NULL,
227-
NULL
234+
wlan_obj.bandwidth,
235+
country,
236+
&(wlan_obj.max_tx_pwr)
228237
};
229238

230239
// initialize the wlan subsystem
231240
wlan_setup(&setup_config);
232-
241+
mod_network_register_nic(&wlan_obj);
233242

234243
if (mod_wlan_was_sta_disconnected) {
235244
/* re-establish connection */
236-
wlan_do_connect ((const char*)wlan_conn_recover_ssid, bssid, wlan_conn_recover_auth, (const char*)wlan_conn_recover_key,
245+
wlan_do_connect ((const char*)wlan_conn_recover_ssid, bssid, wlan_conn_recover_auth, (const char*)key,
237246
wlan_conn_recover_timeout, &wlan_wpa2_ent, hostname, wlan_conn_recover_scan_channel);
238247

239248
mod_wlan_was_sta_disconnected = false;
240249
}
241-
242-
wlan_init_wlan_recover_params();
243250
}
244251
}
245252
}
@@ -272,16 +279,21 @@ void wlan_setup (wlan_internal_setup_t *config) {
272279
{
273280
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
274281
}
282+
memcpy(&(wlan_obj.country), config->country, sizeof(wlan_obj.country));
275283
}
276284

285+
277286
esp_wifi_start();
278287

279288
MP_THREAD_GIL_ENTER();
280289

281290
if(config->max_tx_pr != NULL)
282291
{
283292
// set the max_tx_power
284-
esp_wifi_set_max_tx_power(*(config->max_tx_pr));
293+
if(ESP_OK == esp_wifi_set_max_tx_power(*(config->max_tx_pr)))
294+
{
295+
wlan_obj.max_tx_pwr = *(config->max_tx_pr);
296+
}
285297
}
286298

287299
wlan_obj.started = true;
@@ -327,6 +339,8 @@ STATIC esp_err_t wlan_event_handler(void *ctx, system_event_t *event) {
327339
wlan_obj.channel = _event->channel;
328340
wlan_obj.auth = _event->authmode;
329341
wlan_obj.disconnected = false;
342+
/* Stop Conn timeout counter*/
343+
wlan_stop_sta_conn_timer();
330344
}
331345
break;
332346
case SYSTEM_EVENT_STA_GOT_IP: /**< ESP32 station got IP from connected AP */
@@ -397,11 +411,21 @@ STATIC void wlan_timer_callback( TimerHandle_t xTimer )
397411
if (wlan_obj.disconnected) {
398412
/* Terminate connection */
399413
esp_wifi_disconnect();
414+
wlan_obj.sta_conn_timeout = true;
400415
}
401-
402416
/* Stop Timer*/
403-
xTimerStop(wlan_conn_timeout_timer, 0);
404-
xTimerDelete(wlan_conn_timeout_timer, 0);
417+
wlan_stop_sta_conn_timer();
418+
}
419+
STATIC void wlan_stop_sta_conn_timer()
420+
{
421+
xSemaphoreTake(timeout_mutex, portMAX_DELAY);
422+
/* Clear Connection timeout counter if applicable*/
423+
if (wlan_conn_timeout_timer != NULL) {
424+
xTimerStop(wlan_conn_timeout_timer, 0);
425+
xTimerDelete(wlan_conn_timeout_timer, 0);
426+
wlan_conn_timeout_timer = NULL;
427+
}
428+
xSemaphoreGive(timeout_mutex);
405429
}
406430
STATIC void wlan_servers_start (void) {
407431
// start the servers if they were enabled before
@@ -711,6 +735,8 @@ STATIC void wlan_do_connect (const char* ssid, const char* bssid, const wifi_aut
711735
wlan_conn_recover_timeout = timeout;
712736
/*create Timer */
713737
wlan_conn_timeout_timer = xTimerCreate("Wlan_Timer", timeout / portTICK_PERIOD_MS, 0, 0, wlan_timer_callback);
738+
/* reset timeout Flag */
739+
wlan_obj.sta_conn_timeout = false;
714740
/*start Timer */
715741
xTimerStart(wlan_conn_timeout_timer, 0);
716742
}
@@ -805,12 +831,8 @@ static void wlan_init_wlan_recover_params(void)
805831
wlan_conn_recover_hostname[0] = '\0';
806832
wlan_conn_recover_auth = WIFI_AUTH_MAX;
807833
wlan_conn_recover_timeout = -1;
808-
wlan_conn_recover_ap_channel = 0;
809834
wlan_conn_recover_scan_channel = 0;
810-
wlan_conn_recover_antenna = ANTENNA_TYPE_INTERNAL;
811835
wlan_conn_recover_hidden = false;
812-
wlan_conn_recover_bandwidth = 0;
813-
wlan_conn_recover_mode = WIFI_MODE_NULL;
814836
}
815837

816838
STATIC void wlan_callback_handler(void* arg)
@@ -1055,11 +1077,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) {
10551077
ptrcountry_info = &country_info;
10561078
}
10571079

1058-
wlan_conn_recover_ap_channel = channel;
1059-
wlan_conn_recover_antenna = antenna;
10601080
wlan_conn_recover_hidden = hidden;
1061-
wlan_conn_recover_bandwidth = bandwidth;
1062-
wlan_conn_recover_mode = mode;
10631081

10641082
wlan_internal_setup_t setup = {
10651083
mode,
@@ -1487,6 +1505,8 @@ STATIC mp_obj_t wlan_disconnect(mp_obj_t self_in) {
14871505
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
14881506
}
14891507
esp_wifi_disconnect();
1508+
/* Stop Conn timeout counter*/
1509+
wlan_stop_sta_conn_timer();
14901510
return mp_const_none;
14911511
}
14921512
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_disconnect_obj, wlan_disconnect);
@@ -1495,6 +1515,13 @@ STATIC mp_obj_t wlan_isconnected(mp_obj_t self_in) {
14951515
if (xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT) {
14961516
return mp_const_true;
14971517
}
1518+
1519+
if(wlan_obj.sta_conn_timeout)
1520+
{
1521+
wlan_obj.sta_conn_timeout = false;
1522+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "Connection to AP Timeout!"));
1523+
}
1524+
14981525
return mp_const_false;
14991526
}
15001527
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_isconnected_obj, wlan_isconnected);
@@ -2158,7 +2185,7 @@ STATIC mp_obj_t wlan_max_tx_power (mp_uint_t n_args, const mp_obj_t *args) {
21582185
{
21592186
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
21602187
}
2161-
2188+
wlan_obj.max_tx_pwr = pwr;
21622189
return mp_const_none;
21632190
}
21642191
else
@@ -2270,6 +2297,8 @@ STATIC mp_obj_t wlan_country(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
22702297
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
22712298
}
22722299

2300+
memcpy(&(wlan_obj.country), &country_config, sizeof(wlan_obj.country));
2301+
22732302
return mp_const_none;
22742303
}
22752304
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_country_obj, 1, wlan_country);

esp32/mods/modwlan.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ typedef struct _wlan_obj_t {
6969
uint8_t auth;
7070
uint8_t channel;
7171
uint8_t antenna;
72-
int8_t max_tx_pwr;
72+
int8_t max_tx_pwr;
73+
wifi_country_t country;
7374

7475
// my own ssid, key and mac
7576
uint8_t ssid[(MODWLAN_SSID_LEN_MAX + 1)];
@@ -86,16 +87,17 @@ typedef struct _wlan_obj_t {
8687
bool irq_enabled;
8788
bool enable_servers;
8889
bool disconnected;
90+
bool sta_conn_timeout;
8991
bool soft_ap_stopped;
9092
bool sta_stopped;
9193
bool pwrsave;
9294
bool started;
93-
bool is_promiscuous;
94-
uint32_t trigger;
95-
int32_t events;
95+
bool is_promiscuous;
96+
uint32_t trigger;
97+
int32_t events;
9698
mp_obj_t handler;
9799
mp_obj_t handler_arg;
98-
SemaphoreHandle_t mutex;
100+
SemaphoreHandle_t mutex;
99101
} wlan_obj_t;
100102

101103
typedef struct wlan_internal_prom_t

0 commit comments

Comments
 (0)