@@ -96,14 +96,14 @@ wlan_obj_t wlan_obj = {
96
96
.irq_enabled = false,
97
97
.pwrsave = false,
98
98
.is_promiscuous = false,
99
- .sta_conn_timeout = false
99
+ .sta_conn_timeout = false,
100
+ .country = NULL
100
101
};
101
102
102
103
/* TODO: Maybe we can add possibility to create IRQs for wifi events */
103
104
104
105
static EventGroupHandle_t wifi_event_group ;
105
106
106
- static bool mod_wlan_is_deinit = false;
107
107
static uint16_t mod_wlan_ap_number_of_connections = 0 ;
108
108
109
109
/* Variables holding wlan conn params for wakeup recovery of connections */
@@ -135,6 +135,7 @@ SemaphoreHandle_t smartConfigTimeout_mutex;
135
135
136
136
static const int ESPTOUCH_DONE_BIT = BIT1 ;
137
137
static const int ESPTOUCH_STOP_BIT = BIT2 ;
138
+ static const int ESPTOUCH_SLEEP_STOP_BIT = BIT3 ;
138
139
static bool wlan_smart_config_enabled = false;
139
140
140
141
/******************************************************************************
@@ -202,24 +203,36 @@ void wlan_pre_init (void) {
202
203
wlan_obj .mutex = xSemaphoreCreateMutex ();
203
204
timeout_mutex = xSemaphoreCreateMutex ();
204
205
smartConfigTimeout_mutex = xSemaphoreCreateMutex ();
205
- memcpy (wlan_obj .country .cc , (const char * )"NA" , sizeof (wlan_obj .country .cc ));
206
206
// create Smart Config Task
207
207
xTaskCreatePinnedToCore (TASK_SMART_CONFIG , "SmartConfig" , SMART_CONF_TASK_STACK_SIZE / sizeof (StackType_t ), NULL , SMART_CONF_TASK_PRIORITY , & SmartConfTaskHandle , 1 );
208
208
}
209
209
210
210
void wlan_resume (bool reconnect )
211
211
{
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 ;
223
236
}
224
237
225
238
wlan_internal_setup_t setup_config = {
@@ -234,11 +247,11 @@ void wlan_resume (bool reconnect)
234
247
false,
235
248
wlan_conn_recover_hidden ,
236
249
wlan_obj .bandwidth ,
237
- country ,
250
+ country_ptr ,
238
251
& (wlan_obj .max_tx_pwr )
239
252
};
240
253
241
- // initialize the wlan subsystem
254
+ // Initialize & reconnect to the previous connection
242
255
wlan_setup (& setup_config );
243
256
}
244
257
}
@@ -258,12 +271,15 @@ void wlan_setup (wlan_internal_setup_t *config) {
258
271
wlan_set_bandwidth (config -> bandwidth );
259
272
260
273
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 )
263
276
{
264
277
nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_operation_failed ));
265
278
}
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 ));
267
283
}
268
284
269
285
if (config -> max_tx_pr != NULL )
@@ -950,9 +966,8 @@ static void TASK_SMART_CONFIG (void *pvParameters) {
950
966
static uint32_t thread_notification ;
951
967
952
968
smartConf_init :
953
- wlan_smart_config_enabled = false;
954
969
connected = false;
955
- // Block task till notification is recieved
970
+ // Block task till notification is received
956
971
thread_notification = ulTaskNotifyTake (pdTRUE , portMAX_DELAY );
957
972
958
973
if (thread_notification ) {
@@ -967,30 +982,37 @@ static void TASK_SMART_CONFIG (void *pvParameters) {
967
982
}
968
983
CHECK_ESP_ERR (esp_smartconfig_set_type (SC_TYPE_ESPTOUCH ), smartConf_init )
969
984
CHECK_ESP_ERR (esp_smartconfig_start (smart_config_callback ), smartConf_init )
970
- wlan_smart_config_enabled = true;
971
985
goto smartConf_start ;
972
986
}
973
987
goto smartConf_init ;
974
988
975
989
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" );
977
992
/*create Timer */
978
993
wlan_smartConfig_timeout = xTimerCreate ("smartConfig_Timer" , 60000 / portTICK_PERIOD_MS , 0 , 0 , wlan_timer_callback );
979
994
/*start Timer */
980
995
xTimerStart (wlan_smartConfig_timeout , 0 );
981
996
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 );
983
998
if (uxBits & ESPTOUCH_STOP_BIT ) {
999
+ wlan_smart_config_enabled = false;
984
1000
esp_smartconfig_stop ();
985
1001
//mp_printf(&mp_plat_print, "\nSmart Config Aborted or Timed-out\n");
986
1002
goto smartConf_init ;
987
1003
}
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
+ }
988
1009
if (uxBits & CONNECTED_BIT ) {
989
1010
//mp_printf(&mp_plat_print, "WiFi Connected to ap\n");
990
1011
connected = true;
991
1012
}
992
1013
if (uxBits & ESPTOUCH_DONE_BIT ) {
993
1014
//mp_printf(&mp_plat_print, "smartconfig over\n");
1015
+ wlan_smart_config_enabled = false;
994
1016
esp_smartconfig_stop ();
995
1017
wlan_stop_smartConfig_timer ();
996
1018
//set event flag
@@ -1244,11 +1266,25 @@ mp_obj_t wlan_deinit(mp_obj_t self_in) {
1244
1266
1245
1267
if (wlan_obj .started )
1246
1268
{
1269
+ bool called_from_sleep = false;
1270
+
1247
1271
// 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
+ }
1252
1288
}
1253
1289
1254
1290
mod_network_deregister_nic (& wlan_obj );
@@ -1262,7 +1298,11 @@ mp_obj_t wlan_deinit(mp_obj_t self_in) {
1262
1298
1263
1299
/* stop and free wifi resource */
1264
1300
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
+ }
1266
1306
wlan_obj .started = false;
1267
1307
mod_network_deregister_nic (& wlan_obj );
1268
1308
}
@@ -2446,6 +2486,9 @@ STATIC mp_obj_t wlan_country(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
2446
2486
nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_operation_failed ));
2447
2487
}
2448
2488
2489
+ if (wlan_obj .country == NULL ) {
2490
+ wlan_obj .country = (wifi_country_t * )malloc (sizeof (wifi_country_t ));
2491
+ }
2449
2492
memcpy (& (wlan_obj .country ), & country_config , sizeof (wlan_obj .country ));
2450
2493
2451
2494
return mp_const_none ;
0 commit comments