2222 *
2323 */
2424
25- #include <stdlib.h>
2625#include <stdio.h>
26+ #include <stdlib.h>
2727#include <string.h>
28+
2829#include "freertos/FreeRTOS.h"
29- #include "freertos/task.h"
3030#include "freertos/queue.h"
31- #include "soc/soc .h"
31+ #include "freertos/task .h"
3232#include "soc/rtc_cntl_reg.h"
3333#include "soc/sens_reg.h"
34- #include "esp_wifi.h"
34+ #include "soc/soc.h"
35+
3536#include "esp_event_loop.h"
3637#include "esp_log.h"
3738#include "esp_smartconfig.h"
3839#include "esp_system.h"
40+ #include "esp_wifi.h"
41+
3942#include "board.h"
40- #include "periph_wifi.h"
4143#include "esp_peripherals.h"
44+ #include "periph_wifi.h"
4245#include "wifibleconfig.h"
4346
47+ #if __has_include ("esp_idf_version.h" )
48+ #include "esp_idf_version.h"
49+ #endif
50+
4451static const char * TAG = "PERIPH_WIFI" ;
4552
46- #define VALIDATE_WIFI (periph , ret ) if (!(periph && esp_periph_get_id(periph) == PERIPH_ID_WIFI)) { \
47- ESP_LOGE(TAG, "Invalid WIFI periph, at line %d", __LINE__);\
48- return ret;\
49- }
53+ #define VALIDATE_WIFI (periph , ret ) \
54+ if (!(periph && esp_periph_get_id(periph) == PERIPH_ID_WIFI)) { \
55+ ESP_LOGE(TAG, "Invalid WIFI periph, at line %d", __LINE__); \
56+ return ret; \
57+ }
5058
5159#define DEFAULT_RECONNECT_TIMEOUT_MS (1000)
5260
@@ -85,7 +93,7 @@ esp_err_t periph_wifi_wait_for_connected(esp_periph_handle_t periph, TickType_t
8593 return ESP_OK ;
8694 }
8795#ifdef CONFIG_BLUEDROID_ENABLED
88- if (periph_wifi -> config_mode == WIFI_CONFIG_BLUEFI ) {
96+ if (periph_wifi -> config_mode == WIFI_CONFIG_BLUEFI ) {
8997 ble_config_stop ();
9098 }
9199#endif
@@ -99,6 +107,60 @@ periph_wifi_state_t periph_wifi_is_connected(esp_periph_handle_t periph)
99107 return wifi -> wifi_state ;
100108}
101109
110+ #if defined(ESP_IDF_VERSION )
111+ #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (4 , 0 , 0 ))
112+ static void _wifi_smartconfig_event_callback (void * arg , esp_event_base_t event_base ,
113+ int32_t event_id , void * event_data )
114+ {
115+ wifi_config_t sta_conf ;
116+ periph_wifi_handle_t periph_wifi = (periph_wifi_handle_t )esp_periph_get_data (g_periph );
117+ switch (event_id ) {
118+ case SC_EVENT_SCAN_DONE :
119+ ESP_LOGD (TAG , "SC_EVENT_SCAN_DONE" );
120+ break ;
121+
122+ case SC_EVENT_FOUND_CHANNEL :
123+ ESP_LOGD (TAG , "SC_EVENT_FOUND_CHANNEL" );
124+ break ;
125+
126+ case SC_EVENT_GOT_SSID_PSWD :
127+ ESP_LOGE (TAG , "SC_EVENT_GOT_SSID_PSWD" );
128+ smartconfig_event_got_ssid_pswd_t * evt = (smartconfig_event_got_ssid_pswd_t * )event_data ;
129+ memset (& sta_conf , 0x00 , sizeof (sta_conf ));
130+ memcpy (sta_conf .sta .ssid , evt -> ssid , sizeof (sta_conf .sta .ssid ));
131+ memcpy (sta_conf .sta .password , evt -> password , sizeof (sta_conf .sta .password ));
132+ sta_conf .sta .bssid_set = evt -> bssid_set ;
133+ if (sta_conf .sta .bssid_set == true) {
134+ memcpy (sta_conf .sta .bssid , evt -> bssid , sizeof (sta_conf .sta .bssid ));
135+ }
136+ ESP_LOGE (TAG , "SSID=%s, PASS=%s" , sta_conf .sta .ssid , sta_conf .sta .password );
137+
138+ esp_wifi_disconnect ();
139+
140+ if (esp_wifi_set_config (WIFI_IF_STA , & sta_conf ) != ESP_OK ) {
141+ periph_wifi -> wifi_state = PERIPH_WIFI_CONFIG_ERROR ;
142+ xEventGroupSetBits (periph_wifi -> state_event , SMARTCONFIG_ERROR_BIT );
143+ }
144+ if (esp_wifi_connect () != ESP_OK ) {
145+ periph_wifi -> wifi_state = PERIPH_WIFI_CONFIG_ERROR ;
146+ xEventGroupSetBits (periph_wifi -> state_event , SMARTCONFIG_ERROR_BIT );
147+ esp_periph_send_event (g_periph , PERIPH_WIFI_CONFIG_ERROR , NULL , 0 );
148+ break ;
149+ }
150+ break ;
151+
152+ case SC_EVENT_SEND_ACK_DONE :
153+ ESP_LOGE (TAG , "SC_EVENT_SEND_ACK_DONE" );
154+
155+ periph_wifi -> wifi_state = PERIPH_WIFI_CONFIG_DONE ;
156+ esp_periph_send_event (g_periph , PERIPH_WIFI_CONFIG_DONE , NULL , 0 );
157+ xEventGroupSetBits (periph_wifi -> state_event , SMARTCONFIG_DONE_BIT );
158+ esp_smartconfig_stop ();
159+ break ;
160+ }
161+ }
162+ #endif
163+ #else
102164static void _wifi_smartconfig_event_callback (smartconfig_status_t status , void * pdata )
103165{
104166 wifi_config_t sta_conf ;
@@ -141,7 +203,7 @@ static void _wifi_smartconfig_event_callback(smartconfig_status_t status, void *
141203 ESP_LOGE (TAG , "SC_STATUS_LINK_OVER" );
142204
143205 if (pdata != NULL ) {
144- char phone_ip [4 ] = {0 };
206+ char phone_ip [4 ] = { 0 };
145207 memcpy (phone_ip , (const void * )pdata , 4 );
146208 ESP_LOGD (TAG , "Phone ip: %d.%d.%d.%d" , phone_ip [0 ], phone_ip [1 ], phone_ip [2 ], phone_ip [3 ]);
147209 periph_wifi -> wifi_state = PERIPH_WIFI_CONFIG_DONE ;
@@ -156,6 +218,7 @@ static void _wifi_smartconfig_event_callback(smartconfig_status_t status, void *
156218 break ;
157219 }
158220}
221+ #endif
159222
160223esp_err_t periph_wifi_wait_for_disconnected (esp_periph_handle_t periph , TickType_t tick_to_wait )
161224{
@@ -168,7 +231,6 @@ esp_err_t periph_wifi_wait_for_disconnected(esp_periph_handle_t periph, TickType
168231 return ESP_FAIL ;
169232}
170233
171-
172234esp_err_t periph_wifi_config_start (esp_periph_handle_t periph , periph_wifi_config_mode_t mode )
173235{
174236 VALIDATE_WIFI (periph , ESP_FAIL );
@@ -187,7 +249,15 @@ esp_err_t periph_wifi_config_start(esp_periph_handle_t periph, periph_wifi_confi
187249 // esp_wifi_start();
188250 err |= esp_smartconfig_set_type (mode );
189251 err |= esp_smartconfig_fast_mode (true);
252+ #if defined(ESP_IDF_VERSION )
253+ #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (4 , 0 , 0 ))
254+ smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT ();
255+ err |= esp_smartconfig_start (& cfg );
256+ esp_event_handler_register (SC_EVENT , ESP_EVENT_ANY_ID , & _wifi_smartconfig_event_callback , NULL );
257+ #endif
258+ #else
190259 err |= esp_smartconfig_start (_wifi_smartconfig_event_callback , 0 );
260+ #endif
191261 xEventGroupClearBits (periph_wifi -> state_event , SMARTCONFIG_DONE_BIT );
192262 xEventGroupClearBits (periph_wifi -> state_event , SMARTCONFIG_ERROR_BIT );
193263
@@ -209,7 +279,7 @@ esp_err_t periph_wifi_config_wait_done(esp_periph_handle_t periph, TickType_t ti
209279 VALIDATE_WIFI (periph , ESP_FAIL );
210280 periph_wifi_handle_t periph_wifi = (periph_wifi_handle_t )esp_periph_get_data (periph );
211281 EventBits_t wificonfig_bit = xEventGroupWaitBits (periph_wifi -> state_event ,
212- SMARTCONFIG_DONE_BIT | SMARTCONFIG_ERROR_BIT , false, true, tick_to_wait );
282+ SMARTCONFIG_DONE_BIT | SMARTCONFIG_ERROR_BIT , false, true, tick_to_wait );
213283
214284 if (wificonfig_bit & SMARTCONFIG_DONE_BIT ) {
215285 return ESP_OK ;
@@ -223,11 +293,55 @@ esp_err_t periph_wifi_config_wait_done(esp_periph_handle_t periph, TickType_t ti
223293
224294static void wifi_reconnect_timer (xTimerHandle tmr )
225295{
226- esp_periph_handle_t periph = (esp_periph_handle_t ) pvTimerGetTimerID (tmr );
296+ esp_periph_handle_t periph = (esp_periph_handle_t )pvTimerGetTimerID (tmr );
227297 esp_periph_stop_timer (periph );
228298 esp_wifi_connect ();
229299}
230300
301+ #if defined(ESP_IDF_VERSION )
302+ #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (4 , 0 , 0 ))
303+ static void _wifi_event_callback (void * arg , esp_event_base_t event_base ,
304+ int32_t event_id , void * event_data )
305+ {
306+ esp_periph_handle_t self = (esp_periph_handle_t )arg ;
307+ periph_wifi_handle_t periph_wifi = (periph_wifi_handle_t )esp_periph_get_data (self );
308+
309+ switch (event_id ) {
310+ case WIFI_EVENT_STA_START :
311+ periph_wifi -> wifi_state = PERIPH_WIFI_CONNECTING ;
312+ esp_wifi_connect ();
313+ break ;
314+ case IP_EVENT_STA_GOT_IP :
315+ periph_wifi -> wifi_state = PERIPH_WIFI_CONNECTED ;
316+ xEventGroupClearBits (periph_wifi -> state_event , DISCONNECTED_BIT );
317+ esp_periph_send_event (self , PERIPH_WIFI_CONNECTED , NULL , 0 );
318+ xEventGroupSetBits (periph_wifi -> state_event , CONNECTED_BIT );
319+ wifi_config_t w_config ;
320+ memset (& w_config , 0x00 , sizeof (wifi_config_t ));
321+ esp_wifi_get_config (WIFI_IF_STA , & w_config );
322+ strcpy (periph_wifi -> ssid , (char * )w_config .sta .ssid );
323+ break ;
324+ case WIFI_EVENT_STA_DISCONNECTED :
325+ periph_wifi -> wifi_state = PERIPH_WIFI_DISCONNECTED ;
326+ xEventGroupClearBits (periph_wifi -> state_event , CONNECTED_BIT );
327+ xEventGroupSetBits (periph_wifi -> state_event , DISCONNECTED_BIT );
328+ esp_periph_send_event (self , PERIPH_WIFI_DISCONNECTED , NULL , 0 );
329+
330+ ESP_LOGW (TAG , "Wi-Fi disconnected from SSID %s, auto-reconnect %s, reconnect after %d ms" ,
331+ periph_wifi -> ssid ,
332+ periph_wifi -> disable_auto_reconnect == 0 ? "enabled" : "disabled" ,
333+ periph_wifi -> reconnect_timeout_ms );
334+ if (periph_wifi -> disable_auto_reconnect ) {
335+ break ;
336+ }
337+ esp_periph_start_timer (self , periph_wifi -> reconnect_timeout_ms / portTICK_RATE_MS , wifi_reconnect_timer );
338+ break ;
339+ default :
340+ break ;
341+ }
342+ }
343+ #endif
344+ #else
231345static esp_err_t _wifi_event_callback (void * ctx , system_event_t * event )
232346{
233347 esp_periph_handle_t self = (esp_periph_handle_t )ctx ;
@@ -257,9 +371,9 @@ static esp_err_t _wifi_event_callback(void *ctx, system_event_t *event)
257371 esp_periph_send_event (self , PERIPH_WIFI_DISCONNECTED , NULL , 0 );
258372
259373 ESP_LOGW (TAG , "Wi-Fi disconnected from SSID %s, auto-reconnect %s, reconnect after %d ms" ,
260- periph_wifi -> ssid ,
261- periph_wifi -> disable_auto_reconnect == 0 ? "enabled" : "disabled" ,
262- periph_wifi -> reconnect_timeout_ms );
374+ periph_wifi -> ssid ,
375+ periph_wifi -> disable_auto_reconnect == 0 ? "enabled" : "disabled" ,
376+ periph_wifi -> reconnect_timeout_ms );
263377 if (periph_wifi -> disable_auto_reconnect ) {
264378 break ;
265379 }
@@ -270,6 +384,7 @@ static esp_err_t _wifi_event_callback(void *ctx, system_event_t *event)
270384 }
271385 return ESP_OK ;
272386}
387+ #endif
273388
274389static esp_err_t _wifi_run (esp_periph_handle_t self , audio_event_iface_msg_t * msg )
275390{
@@ -287,14 +402,22 @@ static esp_err_t _wifi_init(esp_periph_handle_t self)
287402 return ESP_FAIL ;
288403 }
289404
290- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT ();
291- ESP_ERROR_CHECK (esp_wifi_init (& cfg ));
292-
405+ #if defined(ESP_IDF_VERSION )
406+ #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (4 , 0 , 0 ))
407+ ESP_ERROR_CHECK (esp_event_loop_create_default ());
408+ ESP_ERROR_CHECK (esp_event_handler_register (WIFI_EVENT , ESP_EVENT_ANY_ID , & _wifi_event_callback , self ));
409+ ESP_ERROR_CHECK (esp_event_handler_register (IP_EVENT , IP_EVENT_STA_GOT_IP , & _wifi_event_callback , self ));
410+ #endif
411+ #else
293412 if (esp_event_loop_get_queue () == NULL ) {
294413 ESP_ERROR_CHECK (esp_event_loop_init (_wifi_event_callback , self ));
295414 } else {
296415 esp_event_loop_set_cb (_wifi_event_callback , self );
297416 }
417+ #endif
418+
419+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT ();
420+ ESP_ERROR_CHECK (esp_wifi_init (& cfg ));
298421
299422 memset (& wifi_config , 0x00 , sizeof (wifi_config_t ));
300423 if (periph_wifi -> ssid ) {
@@ -338,14 +461,7 @@ esp_periph_handle_t periph_wifi_init(periph_wifi_cfg_t *config)
338461{
339462 esp_periph_handle_t periph = NULL ;
340463 periph_wifi_handle_t periph_wifi = NULL ;
341- bool _success =
342- (
343- (periph = esp_periph_create (PERIPH_ID_WIFI , "periph_wifi" )) &&
344- (periph_wifi = calloc (1 , sizeof (struct periph_wifi ))) &&
345- (periph_wifi -> state_event = xEventGroupCreate ()) &&
346- (config -> ssid ? (bool )(periph_wifi -> ssid = strdup (config -> ssid )) : true) &&
347- (config -> password ? (bool )(periph_wifi -> password = strdup (config -> password )) : true)
348- );
464+ bool _success = ((periph = esp_periph_create (PERIPH_ID_WIFI , "periph_wifi" )) && (periph_wifi = calloc (1 , sizeof (struct periph_wifi ))) && (periph_wifi -> state_event = xEventGroupCreate ()) && (config -> ssid ? (bool )(periph_wifi -> ssid = strdup (config -> ssid )) : true) && (config -> password ? (bool )(periph_wifi -> password = strdup (config -> password )) : true));
349465
350466 AUDIO_MEM_CHECK (TAG , _success , goto _periph_wifi_init_failed );
351467
0 commit comments