diff --git a/components/esp32/CMakeLists.txt b/components/esp32/CMakeLists.txt index 916113b533..0a314e2ec1 100644 --- a/components/esp32/CMakeLists.txt +++ b/components/esp32/CMakeLists.txt @@ -36,7 +36,7 @@ else() # driver is a public requirement because esp_sleep.h uses gpio_num_t & touch_pad_t # app_update is added here because cpu_start.c uses esp_ota_get_app_description() function. set(priv_requires app_trace app_update bootloader_support log mbedtls nvs_flash pthread - smartconfig_ack spi_flash vfs wpa_supplicant espcoredump esp_common esp_wifi) + spi_flash vfs wpa_supplicant espcoredump esp_common esp_wifi) set(fragments linker.lf ld/esp32_fragments.lf) idf_component_register(SRCS "${srcs}" diff --git a/components/esp32/esp_adapter.c b/components/esp32/esp_adapter.c index 40888f5a53..0699bf70ca 100644 --- a/components/esp32/esp_adapter.c +++ b/components/esp32/esp_adapter.c @@ -33,6 +33,7 @@ #include "esp_intr_alloc.h" #include "esp_attr.h" #include "esp_log.h" +#include "esp_event.h" #include "esp_heap_caps.h" #include "esp_private/wifi_os_adapter.h" #include "esp_private/wifi.h" @@ -46,7 +47,6 @@ #include "nvs.h" #include "os.h" #include "esp_smartconfig.h" -#include "smartconfig_ack.h" #include "esp_coexist_internal.h" #include "esp_coexist_adapter.h" @@ -373,6 +373,15 @@ static int32_t task_get_max_priority_wrapper(void) return (int32_t)(configMAX_PRIORITIES); } +static int32_t esp_event_post_wrapper(const char* event_base, int32_t event_id, void* event_data, size_t event_data_size, uint32_t ticks_to_wait) +{ + if (ticks_to_wait == OSI_FUNCS_TIME_BLOCKING) { + return (int32_t)esp_event_post(event_base, event_id, event_data, event_data_size, portMAX_DELAY); + } else { + return (int32_t)esp_event_post(event_base, event_id, event_data, event_data_size, ticks_to_wait); + } +} + static void IRAM_ATTR timer_arm_wrapper(void *timer, uint32_t tmout, bool repeat) { ets_timer_arm(timer, tmout, repeat); @@ -427,11 +436,6 @@ static void * IRAM_ATTR zalloc_internal_wrapper(size_t size) return ptr; } -static void sc_ack_send_wrapper(void *param) -{ - return sc_ack_send((sc_ack_t *)param); -} - static uint32_t coex_status_get_wrapper(void) { #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE @@ -544,6 +548,7 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._task_get_max_priority = task_get_max_priority_wrapper, ._malloc = malloc, ._free = free, + ._event_post = esp_event_post_wrapper, ._get_free_heap_size = esp_get_free_heap_size, ._rand = esp_random, ._dport_access_stall_other_cpu_start_wrap = esp_dport_access_stall_other_cpu_start_wrap, @@ -590,8 +595,6 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._modem_sleep_exit = esp_modem_sleep_exit, ._modem_sleep_register = esp_modem_sleep_register, ._modem_sleep_deregister = esp_modem_sleep_deregister, - ._sc_ack_send = sc_ack_send_wrapper, - ._sc_ack_send_stop = sc_ack_send_stop, ._coex_status_get = coex_status_get_wrapper, ._coex_wifi_request = coex_wifi_request_wrapper, ._coex_wifi_release = coex_wifi_release_wrapper, diff --git a/components/esp_wifi/CMakeLists.txt b/components/esp_wifi/CMakeLists.txt index dd3e3d1d2d..469497f699 100644 --- a/components/esp_wifi/CMakeLists.txt +++ b/components/esp_wifi/CMakeLists.txt @@ -10,9 +10,10 @@ idf_component_register(SRCS "src/coexist.c" "src/mesh_event.c" "src/phy_init.c" "src/restore.c" + "src/smartconfig.c" "src/wifi_init.c" INCLUDE_DIRS "include" "${idf_target}/include" - PRIV_REQUIRES wpa_supplicant nvs_flash + PRIV_REQUIRES wpa_supplicant nvs_flash smartconfig_ack LDFRAGMENTS "${ldfragments}") idf_build_get_property(build_dir BUILD_DIR) diff --git a/components/esp_wifi/include/esp_private/wifi.h b/components/esp_wifi/include/esp_private/wifi.h index c8fbe90ed0..ee0654f8bf 100644 --- a/components/esp_wifi/include/esp_private/wifi.h +++ b/components/esp_wifi/include/esp_private/wifi.h @@ -36,6 +36,7 @@ #include "esp_wifi_types.h" #include "esp_event.h" #include "esp_wifi.h" +#include "esp_smartconfig.h" #ifdef __cplusplus extern "C" { @@ -181,6 +182,34 @@ esp_err_t esp_wifi_internal_set_sta_ip(void); */ esp_err_t esp_wifi_internal_set_fix_rate(wifi_interface_t ifx, bool en, wifi_phy_rate_t rate); +/** + * @brief Start SmartConfig, config ESP device to connect AP. You need to broadcast information by phone APP. + * Device sniffer special packets from the air that containing SSID and password of target AP. + * + * @attention 1. This API can be called in station or softAP-station mode. + * @attention 2. Can not call esp_smartconfig_start twice before it finish, please call + * esp_smartconfig_stop first. + * + * @param config pointer to smartconfig start configure structure + * + * @return + * - ESP_OK: succeed + * - others: fail + */ +esp_err_t esp_smartconfig_internal_start(const smartconfig_start_config_t *config); + +/** + * @brief Stop SmartConfig, free the buffer taken by esp_smartconfig_start. + * + * @attention Whether connect to AP succeed or not, this API should be called to free + * memory taken by smartconfig_start. + * + * @return + * - ESP_OK: succeed + * - others: fail + */ +esp_err_t esp_smartconfig_internal_stop(void); + /** * @brief Check the MD5 values of the OS adapter header files in IDF and WiFi library * diff --git a/components/esp_wifi/include/esp_private/wifi_os_adapter.h b/components/esp_wifi/include/esp_private/wifi_os_adapter.h index 165caa6edc..912ec3a537 100644 --- a/components/esp_wifi/include/esp_private/wifi_os_adapter.h +++ b/components/esp_wifi/include/esp_private/wifi_os_adapter.h @@ -21,7 +21,7 @@ extern "C" { #endif -#define ESP_WIFI_OS_ADAPTER_VERSION 0x00000002 +#define ESP_WIFI_OS_ADAPTER_VERSION 0x00000003 #define ESP_WIFI_OS_ADAPTER_MAGIC 0xDEADBEAF #define OSI_FUNCS_TIME_BLOCKING 0xffffffff @@ -72,6 +72,7 @@ typedef struct { int32_t (* _task_get_max_priority)(void); void *(* _malloc)(uint32_t size); void (* _free)(void *p); + int32_t (* _event_post)(const char* event_base, int32_t event_id, void* event_data, size_t event_data_size, uint32_t ticks_to_wait); uint32_t (* _get_free_heap_size)(void); uint32_t (* _rand)(void); void (* _dport_access_stall_other_cpu_start_wrap)(void); @@ -118,8 +119,6 @@ typedef struct { int32_t (* _modem_sleep_exit)(uint32_t module); int32_t (* _modem_sleep_register)(uint32_t module); int32_t (* _modem_sleep_deregister)(uint32_t module); - void (* _sc_ack_send)(void *param); - void (* _sc_ack_send_stop)(void); uint32_t (* _coex_status_get)(void); int32_t (* _coex_wifi_request)(uint32_t event, uint32_t latency, uint32_t duration); int32_t (* _coex_wifi_release)(uint32_t event); diff --git a/components/esp_wifi/include/esp_smartconfig.h b/components/esp_wifi/include/esp_smartconfig.h index 34cf8667ab..bd5c545e58 100644 --- a/components/esp_wifi/include/esp_smartconfig.h +++ b/components/esp_wifi/include/esp_smartconfig.h @@ -18,37 +18,48 @@ #include #include #include "esp_err.h" +#include "esp_event_base.h" #ifdef __cplusplus extern "C" { #endif -typedef enum { - SC_STATUS_WAIT = 0, /**< Waiting to start connect */ - SC_STATUS_FIND_CHANNEL, /**< Finding target channel */ - SC_STATUS_GETTING_SSID_PSWD, /**< Getting SSID and password of target AP */ - SC_STATUS_LINK, /**< Connecting to target AP */ - SC_STATUS_LINK_OVER, /**< Connected to AP successfully */ -} smartconfig_status_t; - typedef enum { SC_TYPE_ESPTOUCH = 0, /**< protocol: ESPTouch */ SC_TYPE_AIRKISS, /**< protocol: AirKiss */ SC_TYPE_ESPTOUCH_AIRKISS, /**< protocol: ESPTouch and AirKiss */ } smartconfig_type_t; -/** - * @brief The callback of SmartConfig, executed when smart-config status changed. - * - * @param status Status of SmartConfig: - * - SC_STATUS_GETTING_SSID_PSWD : pdata is a pointer of smartconfig_type_t, means config type. - * - SC_STATUS_LINK : pdata is a pointer to wifi_config_t. - * - SC_STATUS_LINK_OVER : pdata is a pointer of phone's IP address(4 bytes) if pdata unequal NULL. - * - otherwise : parameter void *pdata is NULL. - * @param pdata According to the different status have different values. - * - */ -typedef void (*sc_callback_t)(smartconfig_status_t status, void *pdata); +/** Smartconfig event declarations */ +typedef enum { + SC_EVENT_SCAN_DONE, /*!< ESP32 station smartconfig has finished to scan for APs */ + SC_EVENT_FOUND_CHANNEL, /*!< ESP32 station smartconfig has found the channel of the target AP */ + SC_EVENT_GOT_SSID_PSWD, /*!< ESP32 station smartconfig got the SSID and password */ + SC_EVENT_SEND_ACK_DONE, /*!< ESP32 station smartconfig has sent ACK to cellphone */ +} smartconfig_event_t; + +/** @brief smartconfig event base declaration */ +ESP_EVENT_DECLARE_BASE(SC_EVENT); + +/** Argument structure for SC_EVENT_GOT_SSID_PSWD event */ +typedef struct { + uint8_t ssid[32]; /**< SSID of the AP. Null terminated string. */ + uint8_t password[64]; /**< Password of the AP. Null terminated string. */ + bool bssid_set; /**< whether set MAC address of target AP or not. */ + uint8_t bssid[6]; /**< MAC address of target AP. */ + smartconfig_type_t type; /**< Type of smartconfig(ESPTouch or AirKiss). */ + uint8_t token; /**< Token from cellphone which is used to send ACK to cellphone. */ + uint8_t cellphone_ip[4]; /**< IP address of cellphone. */ +} smartconfig_event_got_ssid_pswd_t; + +/** Configure structure for esp_smartconfig_start */ +typedef struct { + bool enable_log; /**< Enable smartconfig logs. */ +} smartconfig_start_config_t; + +#define SMARTCONFIG_START_CONFIG_DEFAULT() { \ + .enable_log = false \ +}; /** * @brief Get the version of SmartConfig. @@ -66,14 +77,13 @@ const char *esp_smartconfig_get_version(void); * @attention 2. Can not call esp_smartconfig_start twice before it finish, please call * esp_smartconfig_stop first. * - * @param cb SmartConfig callback function. - * @param ... log 1: UART output logs; 0: UART only outputs the result. + * @param config pointer to smartconfig start configure structure * * @return * - ESP_OK: succeed * - others: fail */ -esp_err_t esp_smartconfig_start(sc_callback_t cb, ...); +esp_err_t esp_smartconfig_start(const smartconfig_start_config_t *config); /** * @brief Stop SmartConfig, free the buffer taken by esp_smartconfig_start. diff --git a/components/esp_wifi/lib_esp32 b/components/esp_wifi/lib_esp32 index 6579ef9a7e..7657dd453d 160000 --- a/components/esp_wifi/lib_esp32 +++ b/components/esp_wifi/lib_esp32 @@ -1 +1 @@ -Subproject commit 6579ef9a7ebdfa4196398056ee5337c300ca74e3 +Subproject commit 7657dd453d4349d03458b77c082acbfe6b6736c4 diff --git a/components/esp_wifi/src/smartconfig.c b/components/esp_wifi/src/smartconfig.c new file mode 100644 index 0000000000..a4138c89bb --- /dev/null +++ b/components/esp_wifi/src/smartconfig.c @@ -0,0 +1,77 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "esp_log.h" +#include "esp_event_base.h" +#include "esp_private/wifi.h" +#include "esp_smartconfig.h" +#include "smartconfig_ack.h" + +/* Smartconfig events definitions */ +ESP_EVENT_DEFINE_BASE(SC_EVENT); + +static const char *TAG = "smartconfig"; + +static void handler_got_ssid_passwd(void *arg, esp_event_base_t base, int32_t event_id, void *data) +{ + smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)data; + uint8_t ssid[33] = { 0 }; + uint8_t password[65] = { 0 }; + uint8_t cellphone_ip[4]; + esp_err_t err = ESP_OK; + + memcpy(ssid, evt->ssid, sizeof(evt->ssid)); + memcpy(password, evt->password, sizeof(evt->password)); + memcpy(cellphone_ip, evt->cellphone_ip, sizeof(evt->cellphone_ip)); + + ESP_LOGD(TAG, "SSID:%s", ssid); + ESP_LOGD(TAG, "PASSWORD:%s", password); + ESP_LOGD(TAG, "Phone ip: %d.%d.%d.%d\n", cellphone_ip[0], cellphone_ip[1], cellphone_ip[2], cellphone_ip[3]); + + err = sc_send_ack_start(evt->type, evt->token, evt->cellphone_ip); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Send smartconfig ACK error: %d", err); + } +} + +esp_err_t esp_smartconfig_start(const smartconfig_start_config_t *config) +{ + esp_err_t err = ESP_OK; + + err = esp_event_handler_register(SC_EVENT, SC_EVENT_GOT_SSID_PSWD, handler_got_ssid_passwd, NULL); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Register smartconfig default event handler fail!"); + return err; + } + + err = esp_smartconfig_internal_start(config); + if (err != ESP_OK) { + esp_event_handler_unregister(SC_EVENT, SC_EVENT_GOT_SSID_PSWD, handler_got_ssid_passwd); + } + return err; +} + +esp_err_t esp_smartconfig_stop(void) +{ + esp_err_t err = ESP_OK; + + err = esp_smartconfig_internal_stop(); + if (err == ESP_OK) { + sc_send_ack_stop(); + esp_event_handler_unregister(SC_EVENT, SC_EVENT_GOT_SSID_PSWD, handler_got_ssid_passwd); + } + return err; +} \ No newline at end of file diff --git a/components/smartconfig_ack/include/smartconfig_ack.h b/components/smartconfig_ack/include/smartconfig_ack.h index be49fd3bd1..0eb3e4b660 100644 --- a/components/smartconfig_ack/include/smartconfig_ack.h +++ b/components/smartconfig_ack/include/smartconfig_ack.h @@ -19,54 +19,24 @@ extern "C" { #endif -#define SC_ACK_TASK_PRIORITY 2 /*!< Priority of sending smartconfig ACK task */ -#define SC_ACK_TASK_STACK_SIZE 2048 /*!< Stack size of sending smartconfig ACK task */ - -#define SC_ACK_TOUCH_SERVER_PORT 18266 /*!< ESP touch UDP port of server on cellphone */ -#define SC_ACK_AIRKISS_SERVER_PORT 10000 /*!< Airkiss UDP port of server on cellphone */ - -#define SC_ACK_TOUCH_LEN 11 /*!< Length of ESP touch ACK context */ -#define SC_ACK_AIRKISS_LEN 7 /*!< Length of Airkiss ACK context */ - -#define SC_ACK_MAX_COUNT 30 /*!< Maximum count of sending smartconfig ACK */ - -/** - * @brief Smartconfig ACK type. - */ -typedef enum { - SC_ACK_TYPE_ESPTOUCH = 0, /*!< ESP touch ACK type */ - SC_ACK_TYPE_AIRKISS, /*!< Airkiss ACK type */ -} sc_ack_type_t; - -/** - * @brief Smartconfig parameters passed to sc_ack_send call. - */ -typedef struct sc_ack { - sc_ack_type_t type; /*!< Smartconfig ACK type */ - uint8_t *link_flag; /*!< Smartconfig link flag */ - sc_callback_t cb; /*!< Smartconfig callback function */ - struct { - uint8_t token; /*!< Smartconfig token to be sent */ - uint8_t mac[6]; /*!< MAC address of station */ - uint8_t ip[4]; /*!< IP address of cellphone */ - } ctx; -} sc_ack_t; - /** * @brief Send smartconfig ACK to cellphone. * - * @attention The API is only used in libsmartconfig.a. + * @attention The API can only be used when receiving SC_EVENT_GOT_SSID_PSWD event. * - * @param param: smartconfig parameters; + * @param type: smartconfig type(ESPTouch or AirKiss); + * token: token from the cellphone; + * cellphone_ip: IP address of the cellphone; + * + * @retuen ESP_OK: succeed + * others: fail */ -void sc_ack_send(sc_ack_t *param); +esp_err_t sc_send_ack_start(smartconfig_type_t type, uint8_t token, uint8_t *cellphone_ip); /** * @brief Stop sending smartconfig ACK to cellphone. - * - * @attention The API is only used in libsmartconfig.a. */ -void sc_ack_send_stop(void); +void sc_send_ack_stop(void); #ifdef __cplusplus } diff --git a/components/smartconfig_ack/smartconfig_ack.c b/components/smartconfig_ack/smartconfig_ack.c index f84aad3672..fae5e58bbc 100644 --- a/components/smartconfig_ack/smartconfig_ack.c +++ b/components/smartconfig_ack/smartconfig_ack.c @@ -24,9 +24,33 @@ #include "tcpip_adapter.h" #include "esp_log.h" #include "esp_wifi.h" +#include "esp_event.h" #include "esp_smartconfig.h" #include "smartconfig_ack.h" +#define SC_ACK_TASK_PRIORITY 2 /*!< Priority of sending smartconfig ACK task */ +#define SC_ACK_TASK_STACK_SIZE 2048 /*!< Stack size of sending smartconfig ACK task */ + +#define SC_ACK_TOUCH_SERVER_PORT 18266 /*!< ESP touch UDP port of server on cellphone */ +#define SC_ACK_AIRKISS_SERVER_PORT 10000 /*!< Airkiss UDP port of server on cellphone */ + +#define SC_ACK_TOUCH_LEN 11 /*!< Length of ESP touch ACK context */ +#define SC_ACK_AIRKISS_LEN 7 /*!< Length of Airkiss ACK context */ + +#define SC_ACK_MAX_COUNT 30 /*!< Maximum count of sending smartconfig ACK */ + +/** + * @brief Smartconfig parameters passed to sc_ack_send call. + */ +typedef struct sc_ack { + smartconfig_type_t type; /*!< Smartconfig type(ESPTouch or AirKiss) */ + struct { + uint8_t token; /*!< Smartconfig token from the cellphone */ + uint8_t mac[6]; /*!< MAC address of station */ + uint8_t ip[4]; /*!< IP address of cellphone */ + } ctx; +} sc_ack_t; + static const char *TAG = "smartconfig"; /* Flag to indicate sending smartconfig ACK or not. */ @@ -48,13 +72,13 @@ static void sc_ack_send_task(void *pvParameters) tcpip_adapter_ip_info_t local_ip; uint8_t remote_ip[4]; memcpy(remote_ip, ack->ctx.ip, sizeof(remote_ip)); - int remote_port = (ack->type == SC_ACK_TYPE_ESPTOUCH) ? SC_ACK_TOUCH_SERVER_PORT : SC_ACK_AIRKISS_SERVER_PORT; + int remote_port = (ack->type == SC_TYPE_ESPTOUCH) ? SC_ACK_TOUCH_SERVER_PORT : SC_ACK_AIRKISS_SERVER_PORT; struct sockaddr_in server_addr; socklen_t sin_size = sizeof(server_addr); int send_sock = -1; int optval = 1; int sendlen; - int ack_len = (ack->type == SC_ACK_TYPE_ESPTOUCH) ? SC_ACK_TOUCH_LEN : SC_ACK_AIRKISS_LEN; + int ack_len = (ack->type == SC_TYPE_ESPTOUCH) ? SC_ACK_TOUCH_LEN : SC_ACK_AIRKISS_LEN; uint8_t packet_count = 1; int err; int ret; @@ -73,7 +97,7 @@ static void sc_ack_send_task(void *pvParameters) ret = tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &local_ip); if ((ESP_OK == ret) && (local_ip.ip.addr != INADDR_ANY)) { /* If ESP touch, smartconfig ACK contains local IP address. */ - if (ack->type == SC_ACK_TYPE_ESPTOUCH) { + if (ack->type == SC_TYPE_ESPTOUCH) { memcpy(ack->ctx.ip, &local_ip.ip.addr, 4); } @@ -94,12 +118,7 @@ static void sc_ack_send_task(void *pvParameters) if (sendlen > 0) { /* Totally send 30 smartconfig ACKs. Then smartconfig is successful. */ if (packet_count++ >= SC_ACK_MAX_COUNT) { - if (ack->link_flag) { - *ack->link_flag = 1; - } - if (ack->cb) { - ack->cb(SC_STATUS_LINK_OVER, remote_ip); - } + esp_event_post(SC_EVENT, SC_EVENT_SEND_ACK_DONE, NULL, 0, portMAX_DELAY); goto _end; } } @@ -127,31 +146,36 @@ _end: vTaskDelete(NULL); } -void sc_ack_send(sc_ack_t *param) +esp_err_t sc_send_ack_start(smartconfig_type_t type, uint8_t token, uint8_t *cellphone_ip) { sc_ack_t *ack = NULL; - if (param == NULL) { - ESP_LOGE(TAG, "Smart config ack parameter error"); - return; + if (cellphone_ip == NULL) { + ESP_LOGE(TAG, "Cellphone IP address is NULL"); + return ESP_ERR_INVALID_ARG; } ack = malloc(sizeof(sc_ack_t)); if (ack == NULL) { - ESP_LOGE(TAG, "Smart config ack parameter malloc fail"); - return; + ESP_LOGE(TAG, "ACK parameter malloc fail"); + return ESP_ERR_NO_MEM; } - memcpy(ack, param, sizeof(sc_ack_t)); + ack->type = type; + ack->ctx.token = token; + memcpy(ack->ctx.ip, cellphone_ip, 4); s_sc_ack_send = true; if (xTaskCreate(sc_ack_send_task, "sc_ack_send_task", SC_ACK_TASK_STACK_SIZE, ack, SC_ACK_TASK_PRIORITY, NULL) != pdPASS) { ESP_LOGE(TAG, "Create sending smartconfig ACK task fail"); free(ack); + return ESP_ERR_NO_MEM; } + + return ESP_OK; } -void sc_ack_send_stop(void) +void sc_send_ack_stop(void) { s_sc_ack_send = false; } diff --git a/examples/wifi/smart_config/main/smartconfig_main.c b/examples/wifi/smart_config/main/smartconfig_main.c index 3a405ff8a3..0819f256da 100644 --- a/examples/wifi/smart_config/main/smartconfig_main.c +++ b/examples/wifi/smart_config/main/smartconfig_main.c @@ -29,7 +29,7 @@ static EventGroupHandle_t s_wifi_event_group; to the AP with an IP? */ static const int CONNECTED_BIT = BIT0; static const int ESPTOUCH_DONE_BIT = BIT1; -static const char *TAG = "sc"; +static const char *TAG = "smartconfig_example"; static void smartconfig_example_task(void * parm); @@ -43,6 +43,36 @@ static void event_handler(void* arg, esp_event_base_t event_base, xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT); + } else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) { + ESP_LOGI(TAG, "Scan done"); + } else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) { + ESP_LOGI(TAG, "Found channel"); + } else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) { + ESP_LOGI(TAG, "Got SSID and password"); + + smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data; + wifi_config_t wifi_config; + uint8_t ssid[33] = { 0 }; + uint8_t password[65] = { 0 }; + + bzero(&wifi_config, sizeof(wifi_config_t)); + memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid)); + memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password)); + wifi_config.sta.bssid_set = evt->bssid_set; + if (wifi_config.sta.bssid_set == true) { + memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid)); + } + + memcpy(ssid, evt->ssid, sizeof(evt->ssid)); + memcpy(password, evt->password, sizeof(evt->password)); + ESP_LOGI(TAG, "SSID:%s", ssid); + ESP_LOGI(TAG, "PASSWORD:%s", password); + + ESP_ERROR_CHECK( esp_wifi_disconnect() ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_connect() ); + } else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) { + xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT); } } @@ -57,51 +87,18 @@ static void initialise_wifi(void) 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_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_start() ); } -static void sc_callback(smartconfig_status_t status, void *pdata) -{ - switch (status) { - case SC_STATUS_WAIT: - ESP_LOGI(TAG, "SC_STATUS_WAIT"); - break; - case SC_STATUS_FIND_CHANNEL: - ESP_LOGI(TAG, "SC_STATUS_FINDING_CHANNEL"); - break; - case SC_STATUS_GETTING_SSID_PSWD: - ESP_LOGI(TAG, "SC_STATUS_GETTING_SSID_PSWD"); - break; - case SC_STATUS_LINK: - ESP_LOGI(TAG, "SC_STATUS_LINK"); - wifi_config_t *wifi_config = pdata; - ESP_LOGI(TAG, "SSID:%s", wifi_config->sta.ssid); - ESP_LOGI(TAG, "PASSWORD:%s", wifi_config->sta.password); - ESP_ERROR_CHECK( esp_wifi_disconnect() ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_connect() ); - break; - case SC_STATUS_LINK_OVER: - ESP_LOGI(TAG, "SC_STATUS_LINK_OVER"); - if (pdata != NULL) { - uint8_t phone_ip[4] = { 0 }; - memcpy(phone_ip, (uint8_t* )pdata, 4); - ESP_LOGI(TAG, "Phone ip: %d.%d.%d.%d\n", phone_ip[0], phone_ip[1], phone_ip[2], phone_ip[3]); - } - xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT); - break; - default: - break; - } -} - static void smartconfig_example_task(void * parm) { EventBits_t uxBits; ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) ); - ESP_ERROR_CHECK( esp_smartconfig_start(sc_callback) ); + smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT(); + ESP_ERROR_CHECK( esp_smartconfig_start(&cfg) ); while (1) { uxBits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY); if(uxBits & CONNECTED_BIT) {