From b4056560c0d4843bfbe100f29483f5db03814cfd Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 20 Nov 2018 18:26:53 +0800 Subject: [PATCH] examples/wifi: use esp_event library to handle events --- .../wifi/espnow/main/espnow_example_main.c | 16 +-- .../softAP/main/softap_example_main.c | 34 +++-- .../station/main/station_example_main.c | 45 +++---- examples/wifi/iperf/main/cmd_wifi.c | 53 ++++---- examples/wifi/power_save/main/power_save.c | 48 +++---- examples/wifi/scan/main/scan.c | 36 +++--- .../main/simple_sniffer_example_main.c | 4 +- .../wifi/smart_config/main/smartconfig_main.c | 31 ++--- .../main/wpa2_enterprise_main.c | 24 ++-- examples/wifi/wps/main/wps.c | 121 +++++++++--------- 10 files changed, 192 insertions(+), 220 deletions(-) diff --git a/examples/wifi/espnow/main/espnow_example_main.c b/examples/wifi/espnow/main/espnow_example_main.c index da54bff87b..9f2d9980db 100644 --- a/examples/wifi/espnow/main/espnow_example_main.c +++ b/examples/wifi/espnow/main/espnow_example_main.c @@ -20,7 +20,7 @@ #include "freertos/semphr.h" #include "freertos/timers.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "tcpip_adapter.h" #include "esp_wifi.h" #include "esp_log.h" @@ -39,23 +39,11 @@ static uint16_t s_example_espnow_seq[EXAMPLE_ESPNOW_DATA_MAX] = { 0, 0 }; static void example_espnow_deinit(example_espnow_send_param_t *send_param); -static esp_err_t example_event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "WiFi started"); - break; - default: - break; - } - return ESP_OK; -} - /* WiFi should start before using ESPNOW */ static void example_wifi_init(void) { tcpip_adapter_init(); - ESP_ERROR_CHECK( esp_event_loop_init(example_event_handler, NULL) ); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); diff --git a/examples/wifi/getting_started/softAP/main/softap_example_main.c b/examples/wifi/getting_started/softAP/main/softap_example_main.c index e648710577..8402241f06 100644 --- a/examples/wifi/getting_started/softAP/main/softap_example_main.c +++ b/examples/wifi/getting_started/softAP/main/softap_example_main.c @@ -12,7 +12,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" @@ -33,23 +33,18 @@ static EventGroupHandle_t s_wifi_event_group; static const char *TAG = "wifi softAP"; -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch(event->event_id) { - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - default: - break; + if (event_id == WIFI_EVENT_AP_STACONNECTED) { + wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; + ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", + MAC2STR(event->mac), event->aid); + } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { + wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; + ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", + MAC2STR(event->mac), event->aid); } - return ESP_OK; } void wifi_init_softap() @@ -57,10 +52,13 @@ void wifi_init_softap() s_wifi_event_group = xEventGroupCreate(); tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + wifi_config_t wifi_config = { .ap = { .ssid = EXAMPLE_ESP_WIFI_SSID, @@ -78,7 +76,7 @@ void wifi_init_softap() ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s", + ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); } diff --git a/examples/wifi/getting_started/station/main/station_example_main.c b/examples/wifi/getting_started/station/main/station_example_main.c index 71e5297324..917f53cdbb 100644 --- a/examples/wifi/getting_started/station/main/station_example_main.c +++ b/examples/wifi/getting_started/station/main/station_example_main.c @@ -12,7 +12,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" @@ -39,33 +39,26 @@ static const char *TAG = "wifi station"; static int s_retry_num = 0; -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { + esp_wifi_connect(); + xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } + ESP_LOGI(TAG,"connect to the AP fail"); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + ip4addr_ntoa(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - { - if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { - esp_wifi_connect(); - xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); - s_retry_num++; - ESP_LOGI(TAG,"retry to connect to the AP"); - } - ESP_LOGI(TAG,"connect to the AP fail\n"); - break; - } - default: - break; } - return ESP_OK; } void wifi_init_sta() @@ -73,17 +66,21 @@ void wifi_init_sta() s_wifi_event_group = xEventGroupCreate(); tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); + wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_ESP_WIFI_SSID, .password = EXAMPLE_ESP_WIFI_PASS }, }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); ESP_ERROR_CHECK(esp_wifi_start() ); diff --git a/examples/wifi/iperf/main/cmd_wifi.c b/examples/wifi/iperf/main/cmd_wifi.c index ea130ecd9c..9ef61be48c 100644 --- a/examples/wifi/iperf/main/cmd_wifi.c +++ b/examples/wifi/iperf/main/cmd_wifi.c @@ -17,7 +17,7 @@ #include "freertos/event_groups.h" #include "esp_wifi.h" #include "tcpip_adapter.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "iperf.h" typedef struct { @@ -53,7 +53,8 @@ static EventGroupHandle_t wifi_event_group; const int CONNECTED_BIT = BIT0; const int DISCONNECTED_BIT = BIT1; -static void scan_done_handler(void) +static void scan_done_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { uint16_t sta_number = 0; uint8_t i; @@ -72,35 +73,30 @@ static void scan_done_handler(void) } } free(ap_list_buffer); + ESP_LOGI(TAG, "sta scan done"); } -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void got_ip_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch(event->event_id) { - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT); - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_SCAN_DONE: - scan_done_handler(); - ESP_LOGI(TAG, "sta scan done"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - if (reconnect) { - ESP_LOGI(TAG, "sta disconnect, reconnect..."); - esp_wifi_connect(); - } else { - ESP_LOGI(TAG, "sta disconnect"); - } - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - xEventGroupSetBits(wifi_event_group, DISCONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; + xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT); + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); } +static void disconnect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + if (reconnect) { + ESP_LOGI(TAG, "sta disconnect, reconnect..."); + esp_wifi_connect(); + } else { + ESP_LOGI(TAG, "sta disconnect"); + } + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + xEventGroupSetBits(wifi_event_group, DISCONNECTED_BIT); +} + + void initialise_wifi(void) { esp_log_level_set("wifi", ESP_LOG_WARN); @@ -112,9 +108,12 @@ void initialise_wifi(void) tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + ESP_ERROR_CHECK( esp_event_loop_create_default() ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); + ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &scan_done_handler, NULL) ); + ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, NULL) ); + ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &got_ip_handler, NULL) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) ); ESP_ERROR_CHECK( esp_wifi_start() ); diff --git a/examples/wifi/power_save/main/power_save.c b/examples/wifi/power_save/main/power_save.c index c9576a9aa6..417b4c9d8f 100644 --- a/examples/wifi/power_save/main/power_save.c +++ b/examples/wifi/power_save/main/power_save.c @@ -16,7 +16,7 @@ #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_log.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_pm.h" #include "nvs_flash.h" @@ -39,43 +39,37 @@ static const char *TAG = "power_save"; - -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:%s\n", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - default: - break; + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + esp_wifi_connect(); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip)); } - return ESP_OK; } /*init wifi as sta and set power save mode*/ static void wifi_power_save(void) { tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); - + ESP_ERROR_CHECK(esp_event_loop_create_default()); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); + wifi_config_t wifi_config = { - .sta = { - .ssid = DEFAULT_SSID, - .password = DEFAULT_PWD, - .listen_interval = DEFAULT_LISTEN_INTERVAL, - }, + .sta = { + .ssid = DEFAULT_SSID, + .password = DEFAULT_PWD, + .listen_interval = DEFAULT_LISTEN_INTERVAL, + }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); diff --git a/examples/wifi/scan/main/scan.c b/examples/wifi/scan/main/scan.c index fa27bf7f21..15235b20cb 100644 --- a/examples/wifi/scan/main/scan.c +++ b/examples/wifi/scan/main/scan.c @@ -25,7 +25,7 @@ #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_log.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "nvs_flash.h" /*Set the SSID and Password via "make menuconfig"*/ @@ -68,36 +68,32 @@ static const char *TAG = "scan"; -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "Got IP: %s\n", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - default: - break; + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + esp_wifi_connect(); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip)); } - return ESP_OK; } + /* Initialize Wi-Fi as sta and set scan method */ static void wifi_scan(void) { tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); + wifi_config_t wifi_config = { .sta = { .ssid = DEFAULT_SSID, diff --git a/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c b/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c index fcd01d8b63..c4232ae329 100644 --- a/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c +++ b/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c @@ -13,7 +13,7 @@ #include "argtable3/argtable3.h" #include "tcpip_adapter.h" #include "esp_console.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_vfs_dev.h" #include "esp_vfs_fat.h" #include "esp_wifi.h" @@ -65,7 +65,7 @@ static void initialize_nvs() static void initialize_wifi() { tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(NULL, NULL)); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); diff --git a/examples/wifi/smart_config/main/smartconfig_main.c b/examples/wifi/smart_config/main/smartconfig_main.c index 71e9365e47..3a405ff8a3 100644 --- a/examples/wifi/smart_config/main/smartconfig_main.c +++ b/examples/wifi/smart_config/main/smartconfig_main.c @@ -14,7 +14,7 @@ #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_wpa2.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" @@ -31,36 +31,33 @@ static const int CONNECTED_BIT = BIT0; static const int ESPTOUCH_DONE_BIT = BIT1; static const char *TAG = "sc"; -void smartconfig_example_task(void * parm); +static void smartconfig_example_task(void * parm); -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { esp_wifi_connect(); xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT); - break; - default: - break; + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT); } - return ESP_OK; } static void initialise_wifi(void) { tcpip_adapter_init(); s_wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); + + ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) ); + ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) ); + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_start() ); } @@ -100,7 +97,7 @@ static void sc_callback(smartconfig_status_t status, void *pdata) } } -void smartconfig_example_task(void * parm) +static void smartconfig_example_task(void * parm) { EventBits_t uxBits; ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) ); diff --git a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c index 5b3f804e17..64e8b40de1 100644 --- a/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c +++ b/examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c @@ -23,7 +23,7 @@ #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_wpa2.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" @@ -76,23 +76,17 @@ extern uint8_t client_crt_end[] asm("_binary_wpa2_client_crt_end"); extern uint8_t client_key_start[] asm("_binary_wpa2_client_key_start"); extern uint8_t client_key_end[] asm("_binary_wpa2_client_key_end"); -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { esp_wifi_connect(); xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); } - return ESP_OK; } static void initialise_wifi(void) @@ -104,9 +98,11 @@ static void initialise_wifi(void) tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); + ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) ); + ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); wifi_config_t wifi_config = { .sta = { diff --git a/examples/wifi/wps/main/wps.c b/examples/wifi/wps/main/wps.c index 50ab8de9bc..62465e2de6 100644 --- a/examples/wifi/wps/main/wps.c +++ b/examples/wifi/wps/main/wps.c @@ -8,10 +8,16 @@ */ /* - Showing how to use WPS. + This example demonstrates how to use WPS. + It supports two modes, which can be selected in menuconfig. - WPS_TYPE_PBC: Start esp32 and it will enter wps PBC mode. Then push the button of wps on router down. The esp32 will connected to the router. - WPS_TYPE_PIN: Start esp32, You'll see PIN code which is a eight-digit number showing on COM. Enter the PIN code in router and then the esp32 will connected to router. + WPS_TYPE_PBC: + Start ESP32 and it will enter WPS PBC mode. Then push WPS button on the router. + ESP32 will receive SSID and password, and connect to the router. + + WPS_TYPE_PIN: + Start ESP32, you'll see an eight-digit PIN number in log output. + Enter the PIN code on the router and then the ESP32 will get connected to router. */ #include "freertos/FreeRTOS.h" @@ -19,17 +25,17 @@ #include "esp_wifi.h" #include "esp_log.h" #include "esp_wps.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "nvs_flash.h" /*set wps mode via "make menuconfig"*/ #if CONFIG_EXAMPLE_WPS_TYPE_PBC -#define WPS_TEST_MODE WPS_TYPE_PBC +#define WPS_MODE WPS_TYPE_PBC #elif CONFIG_EXAMPLE_WPS_TYPE_PIN -#define WPS_TEST_MODE WPS_TYPE_PIN +#define WPS_MODE WPS_TYPE_PIN #else -#define WPS_TEST_MODE WPS_TYPE_DISABLE +#define WPS_MODE WPS_TYPE_DISABLE #endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/ @@ -38,72 +44,73 @@ #define PINSTR "%c%c%c%c%c%c%c%c" #endif - static const char *TAG = "example_wps"; -static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_TEST_MODE); +static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_MODE); -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:%s\n", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: - /*point: the function esp_wifi_wps_start() only get ssid & password - * so call the function esp_wifi_connect() here - * */ - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS"); - ESP_ERROR_CHECK(esp_wifi_wps_disable()); - ESP_ERROR_CHECK(esp_wifi_connect()); - break; - case SYSTEM_EVENT_STA_WPS_ER_FAILED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_FAILED"); - ESP_ERROR_CHECK(esp_wifi_wps_disable()); - ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); - ESP_ERROR_CHECK(esp_wifi_wps_start(0)); - break; - case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT"); - ESP_ERROR_CHECK(esp_wifi_wps_disable()); - ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); - ESP_ERROR_CHECK(esp_wifi_wps_start(0)); - break; - case SYSTEM_EVENT_STA_WPS_ER_PIN: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN"); - /*show the PIN code here*/ - ESP_LOGI(TAG, "WPS_PIN = "PINSTR, PIN2STR(event->event_info.sta_er_pin.pin_code)); - break; - default: - break; + switch (event_id) { + case WIFI_EVENT_STA_START: + ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); + break; + case WIFI_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); + ESP_ERROR_CHECK(esp_wifi_connect()); + break; + case WIFI_EVENT_STA_WPS_ER_SUCCESS: + ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS"); + /* esp_wifi_wps_start() only gets ssid & password, so call esp_wifi_connect() here. */ + ESP_ERROR_CHECK(esp_wifi_wps_disable()); + ESP_ERROR_CHECK(esp_wifi_connect()); + break; + case WIFI_EVENT_STA_WPS_ER_FAILED: + ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_FAILED"); + ESP_ERROR_CHECK(esp_wifi_wps_disable()); + ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); + ESP_ERROR_CHECK(esp_wifi_wps_start(0)); + break; + case WIFI_EVENT_STA_WPS_ER_TIMEOUT: + ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_TIMEOUT"); + ESP_ERROR_CHECK(esp_wifi_wps_disable()); + ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); + ESP_ERROR_CHECK(esp_wifi_wps_start(0)); + break; + case WIFI_EVENT_STA_WPS_ER_PIN: + ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_PIN"); + /* display the PIN code */ + wifi_event_sta_wps_er_pin_t* event = (wifi_event_sta_wps_er_pin_t*) event_data; + ESP_LOGI(TAG, "WPS_PIN = " PINSTR, PIN2STR(event->pin_code)); + break; + default: + break; } - return ESP_OK; +} + +static void got_ip_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip)); } /*init wifi as sta and start wps*/ static void start_wps(void) { tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &got_ip_event_handler, NULL)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); - + ESP_LOGI(TAG, "start wps..."); - - + ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); ESP_ERROR_CHECK(esp_wifi_wps_start(0)); }