@@ -44,51 +44,42 @@ static void event_handler(void* arg, esp_event_base_t event_base,
4444 int32_t event_id , void * event_data ) {
4545 wifi_radio_obj_t * radio = arg ;
4646 if (event_base == WIFI_EVENT ) {
47- if (event_id == WIFI_EVENT_SCAN_DONE ) {
48- xEventGroupSetBits (radio -> event_group_handle , WIFI_SCAN_DONE_BIT );
49- } else if (event_id == WIFI_EVENT_STA_START ) {
50- } else if (event_id == WIFI_EVENT_STA_STOP ) {
51- } else if (event_id == WIFI_EVENT_STA_CONNECTED ) {
52- ESP_EARLY_LOGW (TAG , "connected" );
53- } else if (event_id == WIFI_EVENT_STA_DISCONNECTED ) {
54- ESP_EARLY_LOGW (TAG , "disconnected" );
55- wifi_event_sta_disconnected_t * d = (wifi_event_sta_disconnected_t * ) event_data ;
56- uint8_t reason = d -> reason ;
57- ESP_EARLY_LOGW (TAG , "reason %d 0x%02x" , reason , reason );
58- if (radio -> retries_left > 0 &&
59- (reason == WIFI_REASON_AUTH_EXPIRE ||
60- reason == WIFI_REASON_ASSOC_EXPIRE ||
61- reason == WIFI_REASON_CONNECTION_FAIL ||
62- reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT )) {
63- radio -> retries_left -- ;
64- ESP_EARLY_LOGI (TAG , "Retrying connect. %d retries remaining" , radio -> retries_left );
65- esp_wifi_connect ();
66- return ;
47+ switch (event_id ) {
48+ case WIFI_EVENT_SCAN_DONE :
49+ xEventGroupSetBits (radio -> event_group_handle , WIFI_SCAN_DONE_BIT );
50+ break ;
51+ case WIFI_EVENT_STA_CONNECTED :
52+ ESP_EARLY_LOGW (TAG , "connected" );
53+ break ;
54+ case WIFI_EVENT_STA_DISCONNECTED : {
55+ ESP_EARLY_LOGW (TAG , "disconnected" );
56+ wifi_event_sta_disconnected_t * d = (wifi_event_sta_disconnected_t * ) event_data ;
57+ uint8_t reason = d -> reason ;
58+ ESP_EARLY_LOGW (TAG , "reason %d 0x%02x" , reason , reason );
59+ if (radio -> retries_left > 0 &&
60+ (reason == WIFI_REASON_AUTH_EXPIRE ||
61+ reason == WIFI_REASON_ASSOC_EXPIRE ||
62+ reason == WIFI_REASON_CONNECTION_FAIL ||
63+ reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT )) {
64+ radio -> retries_left -- ;
65+ ESP_EARLY_LOGI (TAG , "Retrying connect. %d retries remaining" , radio -> retries_left );
66+ esp_wifi_connect ();
67+ return ;
68+ }
69+
70+ radio -> last_disconnect_reason = reason ;
71+ xEventGroupSetBits (radio -> event_group_handle , WIFI_DISCONNECTED_BIT );
6772 }
6873
69- radio -> last_disconnect_reason = reason ;
70- xEventGroupSetBits (radio -> event_group_handle , WIFI_DISCONNECTED_BIT );
71-
72- // if (reason != WIFI_REASON_ASSOC_LEAVE) {
73- // // reconnect
74- // }
75- } else if (event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE ) {
74+ // Cases to handle later.
75+ // case WIFI_EVENT_STA_START:
76+ // case WIFI_EVENT_STA_STOP:
77+ // case WIFI_EVENT_STA_AUTHMODE_CHANGE:
78+ default :
79+ break ;
7680 }
7781 }
7882
79- // esp_wifi_connect();
80- // if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
81- // esp_wifi_connect();
82- // } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
83- // if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
84- // esp_wifi_connect();
85- // s_retry_num++;
86- // ESP_LOGI(TAG, "retry to connect to the AP");
87- // } else {
88- // xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
89- // }
90- // ESP_LOGI(TAG,"connect to the AP fail");
91- // } else
9283 if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP ) {
9384 ESP_EARLY_LOGW (TAG , "got ip" );
9485 radio -> retries_left = radio -> starting_retries ;
@@ -100,7 +91,7 @@ static bool wifi_inited;
10091
10192void common_hal_wifi_init (void ) {
10293 wifi_inited = true;
103- common_hal_wifi_radio_obj .base .type = & wifi_radio_type ;
94+ common_hal_wifi_radio_obj .base .type = & wifi_radio_type ;
10495
10596 ESP_ERROR_CHECK (esp_netif_init ());
10697 ESP_ERROR_CHECK (esp_event_loop_create_default ());
@@ -120,13 +111,13 @@ void common_hal_wifi_init(void) {
120111 self ,
121112 & self -> handler_instance_got_ip ));
122113
123- wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT ();
124- esp_err_t result = esp_wifi_init (& config );
125- if (result == ESP_ERR_NO_MEM ) {
126- mp_raise_msg (& mp_type_MemoryError , translate ("Failed to allocate Wifi memory" ));
127- } else if (result != ESP_OK ) {
128- // handle this
129- }
114+ wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT ();
115+ esp_err_t result = esp_wifi_init (& config );
116+ if (result == ESP_ERR_NO_MEM ) {
117+ mp_raise_msg (& mp_type_MemoryError , translate ("Failed to allocate Wifi memory" ));
118+ } else if (result != ESP_OK ) {
119+ mp_raise_RuntimeError ( translate ( "Failed to init wifi" ));
120+ }
130121 common_hal_wifi_radio_set_enabled (self , true);
131122}
132123
@@ -135,14 +126,14 @@ void wifi_reset(void) {
135126 return ;
136127 }
137128 wifi_radio_obj_t * radio = & common_hal_wifi_radio_obj ;
138- common_hal_wifi_radio_set_enabled (radio , false);
129+ common_hal_wifi_radio_set_enabled (radio , false);
139130 ESP_ERROR_CHECK (esp_event_handler_instance_unregister (WIFI_EVENT ,
140131 ESP_EVENT_ANY_ID ,
141132 radio -> handler_instance_all_wifi ));
142133 ESP_ERROR_CHECK (esp_event_handler_instance_unregister (IP_EVENT ,
143134 IP_EVENT_STA_GOT_IP ,
144135 radio -> handler_instance_got_ip ));
145- ESP_ERROR_CHECK (esp_wifi_deinit ());
136+ ESP_ERROR_CHECK (esp_wifi_deinit ());
146137 esp_netif_destroy (radio -> netif );
147138 radio -> netif = NULL ;
148139 ESP_ERROR_CHECK (esp_netif_deinit ());
0 commit comments