@@ -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];
106107static uint8_t wlan_conn_recover_hostname [TCPIP_HOSTNAME_MAX_SIZE ];
107108static uint8_t wlan_conn_recover_auth ;
108109static int32_t wlan_conn_recover_timeout ;
109- static uint8_t wlan_conn_recover_ap_channel ;
110110static uint8_t wlan_conn_recover_scan_channel ;
111- static uint8_t wlan_conn_recover_antenna ;
112111static bool wlan_conn_recover_hidden = false;
113- static int32_t wlan_conn_recover_bandwidth ;
114- static uint8_t wlan_conn_recover_mode ;
112+
115113static 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 ;
117115static uint8_t wlan_prom_data_buff [2 ][MAX_WIFI_PROM_PKT_SIZE ] = {0 };
118116static wlan_internal_prom_t wlan_prom_packet [2 ];
119117
@@ -122,7 +120,10 @@ static uint8_t token = 0;
122120// Event bits
123121const 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);
155156static void wlan_timer_callback ( TimerHandle_t xTimer );
156157static void wlan_validate_country (const char * country );
157158static 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
191195void 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}
406430STATIC 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
816838STATIC 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}
14921512STATIC 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}
15001527STATIC 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}
22752304STATIC MP_DEFINE_CONST_FUN_OBJ_KW (wlan_country_obj , 1 , wlan_country );
0 commit comments