11#include "wifi.h"
22
3- #include <esp_err .h>
4- #include <esp_log .h>
3+ #include <assert .h>
4+ #include <stdint .h>
55#include <string.h>
66#pragma GCC diagnostic push
77#pragma GCC diagnostic ignored "-Wsign-compare"
2020#define WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
2121#define WIFI_H2E_IDENTIFIER "PASSWORD IDENTIFIER"
2222
23- /* FreeRTOS event group to signal when we are connected*/
23+ // FreeRTOS event group to signal when we are connected.
2424static EventGroupHandle_t s_wifi_event_group ;
2525
26- /* The event group allows multiple bits for each event, but we only care about two events:
27- * - we are connected to the AP with an IP
28- * - we failed to connect after the maximum amount of retries */
26+ static bool have_ip_info = false;
27+ static esp_netif_ip_info_t ip_info = {0 };
28+
29+ // The event group allows multiple bits for each event, but we only care about two events:
30+ // - we are connected to the AP with an IP.
31+ // - we failed to connect after the maximum amount of retries.
2932#define WIFI_CONNECTED_BIT BIT0
3033#define WIFI_FAIL_BIT BIT1
3134
3235static const char * TAG = "sta" ;
3336
3437static int s_retry_num = 0 ;
3538
39+ esp_err_t get_ip_info (esp_netif_ip_info_t * ip_info_out ) {
40+ assert (ip_info_out != NULL );
41+ if (have_ip_info ) {
42+ memcpy (ip_info_out , & ip_info , sizeof (* ip_info_out ));
43+ return ESP_OK ;
44+ }
45+ return ESP_ERR_INVALID_STATE ;
46+ }
47+
3648static void event_handler (void * arg , esp_event_base_t event_base , int32_t event_id ,
3749 void * event_data ) {
38- if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START ) {
39- esp_wifi_connect ();
40- } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED ) {
41- if (s_retry_num < CONFIG_SMALLTV_WIFI_MAXIMUM_RETRY ) {
42- esp_wifi_connect ();
43- s_retry_num ++ ;
44- ESP_LOGI (TAG , "retry to connect to the AP" );
45- } else {
46- xEventGroupSetBits (s_wifi_event_group , WIFI_FAIL_BIT );
50+ if (event_base == WIFI_EVENT ) {
51+ switch (event_id ) {
52+ case WIFI_EVENT_STA_START :
53+ ESP_LOGI (TAG , "esp_wifi_connect() initial" );
54+ esp_wifi_connect ();
55+ break ;
56+ case WIFI_EVENT_STA_DISCONNECTED :
57+ if (s_retry_num < CONFIG_SMALLTV_WIFI_MAXIMUM_RETRY ) {
58+ s_retry_num ++ ;
59+ ESP_LOGI (TAG , "esp_wifi_connect() retry=%d" , s_retry_num );
60+ esp_wifi_connect ();
61+ } else {
62+ ESP_LOGI (TAG , "esp_wifi_connect() giving up" );
63+ xEventGroupSetBits (s_wifi_event_group , WIFI_FAIL_BIT );
64+ }
65+ break ;
66+ default :
67+ ESP_LOGV (TAG , "Received unhandled WIFI_EVENT event_id=%ld" , event_id );
68+ break ;
69+ }
70+ return ;
71+ }
72+
73+ if (event_base == IP_EVENT ) {
74+ switch (event_id ) {
75+ case IP_EVENT_STA_GOT_IP : {
76+ ip_event_got_ip_t * event = (ip_event_got_ip_t * )event_data ;
77+ ESP_LOGI (TAG , "Got ip:" IPSTR , IP2STR (& event -> ip_info .ip ));
78+ memcpy (& ip_info , & event -> ip_info , sizeof (ip_info ));
79+ have_ip_info = true;
80+ xEventGroupSetBits (s_wifi_event_group , WIFI_CONNECTED_BIT );
81+ } break ;
82+ case IP_EVENT_STA_LOST_IP : {
83+ ESP_LOGE (TAG , "Lost IP, resetting" );
84+ fflush (stdout );
85+ fflush (stderr );
86+ esp_restart ();
87+ } break ;
88+ default :
89+ ESP_LOGV (TAG , "Received unhandled IP_EVENT event_id=%ld" , event_id );
90+ break ;
4791 }
48- ESP_LOGI (TAG , "connect to the AP fail" );
49- } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP ) {
50- ip_event_got_ip_t * event = (ip_event_got_ip_t * )event_data ;
51- ESP_LOGI (TAG , "got ip:" IPSTR , IP2STR (& event -> ip_info .ip ));
52- s_retry_num = 0 ;
53- xEventGroupSetBits (s_wifi_event_group , WIFI_CONNECTED_BIT );
92+ return ;
5493 }
94+
95+ ESP_LOGI (TAG , "Received unhandled event_base=%p event_id=%ld" , event_base , event_id );
5596}
5697
57- void init_wifi_sta (void ) {
98+ void init_wifi_sta () {
5899 s_wifi_event_group = xEventGroupCreate ();
59100
60101 ESP_ERROR_CHECK (esp_netif_init ());
@@ -65,8 +106,8 @@ void init_wifi_sta(void) {
65106 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT ();
66107 ESP_ERROR_CHECK (esp_wifi_init (& cfg ));
67108
68- esp_event_handler_instance_t instance_any_id ;
69- esp_event_handler_instance_t instance_got_ip ;
109+ esp_event_handler_instance_t instance_any_id = { 0 } ;
110+ esp_event_handler_instance_t instance_got_ip = { 0 } ;
70111 ESP_ERROR_CHECK (esp_event_handler_instance_register (WIFI_EVENT , ESP_EVENT_ANY_ID ,
71112 & event_handler , NULL , & instance_any_id ));
72113 ESP_ERROR_CHECK (esp_event_handler_instance_register (IP_EVENT , IP_EVENT_STA_GOT_IP ,
@@ -77,7 +118,10 @@ void init_wifi_sta(void) {
77118 {
78119 .ssid = CONFIG_SMALLTV_WIFI_SSID ,
79120 .password = CONFIG_SMALLTV_WIFI_PASSWORD ,
121+ .scan_method = WIFI_ALL_CHANNEL_SCAN ,
122+ .sort_method = WIFI_CONNECT_AP_BY_SIGNAL ,
80123 .threshold .authmode = WIFI_SCAN_AUTH_MODE_THRESHOLD ,
124+ .failure_retry_cnt = 2 ,
81125 .sae_pwe_h2e = WIFI_SAE_MODE ,
82126 .sae_h2e_identifier = WIFI_H2E_IDENTIFIER ,
83127 },
@@ -86,28 +130,19 @@ void init_wifi_sta(void) {
86130 ESP_ERROR_CHECK (esp_wifi_set_config (WIFI_IF_STA , & wifi_config ));
87131 ESP_ERROR_CHECK (esp_wifi_start ());
88132
89- ESP_LOGI (TAG , "wifi_init_sta finished." );
90-
91- /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed
92- * for the maximum number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see
93- * above) */
133+ // Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed
134+ // for the maximum number of re-tries (WIFI_FAIL_BIT).
135+ // The bits are set by event_handler().
94136 EventBits_t bits = xEventGroupWaitBits (s_wifi_event_group , WIFI_CONNECTED_BIT | WIFI_FAIL_BIT ,
95137 pdFALSE , pdFALSE , portMAX_DELAY );
96138
97- /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which
98- * event actually happened. */
139+ // xEventGroupWaitBits() returns the bits before the call returned, hence we can test which
140+ // event actually happened.
99141 if (bits & WIFI_CONNECTED_BIT ) {
100- ESP_LOGI (TAG , "connected to ap SSID:%s password:%s" , CONFIG_SMALLTV_WIFI_SSID ,
101- CONFIG_SMALLTV_WIFI_PASSWORD );
142+ ESP_LOGI (TAG , "Connected to AP SSID: %s" , CONFIG_SMALLTV_WIFI_SSID );
102143 } else if (bits & WIFI_FAIL_BIT ) {
103- ESP_LOGI (TAG , "Failed to connect to SSID:%s, password:%s" , CONFIG_SMALLTV_WIFI_SSID ,
104- CONFIG_SMALLTV_WIFI_PASSWORD );
144+ ESP_LOGW (TAG , "Failed to connect to SSID:%s " , CONFIG_SMALLTV_WIFI_SSID );
105145 } else {
106- ESP_LOGE (TAG , "UNEXPECTED EVENT " );
146+ ESP_LOGE (TAG , "Unexpected event " );
107147 }
108148}
109-
110- void init_wifi (void ) {
111- ESP_LOGI (TAG , "ESP_WIFI_MODE_STA" );
112- init_wifi_sta ();
113- }
0 commit comments