260 lines
8.4 KiB
C
260 lines
8.4 KiB
C
#include <stdio.h>
|
|
#include "FluxWifi.h"
|
|
|
|
#include "ui.h"
|
|
|
|
wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
|
|
int number = 0, ap_count = 0;
|
|
|
|
static const char* TAG = "wifi_scan";
|
|
/**
|
|
* @brief Initialize Wi-Fi as STA and set up the scanning method.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void wifi_scanInit(void)
|
|
{
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
|
|
|
esp_netif_create_default_wifi_sta();
|
|
|
|
/* Wi-Fi configuration initialization */
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
memset(ap_info, 0, sizeof(ap_info));
|
|
|
|
/* Set Wi-Fi to STA mode */
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
/* Register Wi-Fi response events */
|
|
esp_event_handler_instance_t instance_any_id;
|
|
esp_event_handler_instance_t instance_got_ip;
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
|
ESP_EVENT_ANY_ID,
|
|
&event_handler,
|
|
NULL,
|
|
&instance_any_id));
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
|
IP_EVENT_STA_GOT_IP,
|
|
&event_handler,
|
|
NULL,
|
|
&instance_got_ip));
|
|
return;
|
|
|
|
wifi_config_t wifi_config = {
|
|
.sta = {
|
|
.ssid = "yuwell",
|
|
.password = "yuwell123456",
|
|
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (password len => 8).
|
|
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
|
|
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
|
|
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
|
|
*/
|
|
.threshold.authmode = WIFI_AUTH_WPA_PSK,
|
|
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
|
|
.sae_h2e_identifier = "",
|
|
},
|
|
};
|
|
}
|
|
|
|
|
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
|
int32_t event_id, void* event_data)
|
|
{
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
|
{
|
|
|
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
|
{
|
|
ESP_LOGI(TAG, "Disconnected from Wi-Fi");
|
|
lv_label_set_text_fmt(ui_pageWifiConnect_labelHeaderTitle, "%s", "WIFI-Disconnected");
|
|
|
|
lv_obj_clear_state(ui_pageHome_switchWifiShow, LV_STATE_CHECKED);
|
|
|
|
} 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:" IPSTR, IP2STR(&event->ip_info.ip));
|
|
|
|
lv_label_set_text_fmt(ui_pageWifiConnect_labelHeaderTitle, "IP:"IPSTR, IP2STR(&event->ip_info.ip));
|
|
|
|
lv_obj_add_state(ui_pageHome_switchWifiShow, LV_STATE_CHECKED);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void wifi_scan(void)
|
|
{
|
|
/* Start Wi-Fi */
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
|
|
|
/* Start scanning nearby Wi-Fi networks and wait for the scan to finish */
|
|
esp_wifi_scan_start(NULL, true);
|
|
|
|
/* Get the scan results */
|
|
get_wifi_scan_result();
|
|
|
|
/* Stop scanning */
|
|
esp_wifi_scan_stop();
|
|
esp_wifi_stop();
|
|
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* @brief Get Wi-Fi scan results.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void get_wifi_scan_result(void)
|
|
{
|
|
number = DEFAULT_SCAN_LIST_SIZE;
|
|
ap_count = 0;
|
|
|
|
/* Get the number of APs found in the last scan */
|
|
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
|
|
/* Get the list of APs found in the last scan */
|
|
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
|
|
|
|
/* Print Wi-Fi scan results */
|
|
print_wifi_scan_result(ap_count, number);
|
|
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* @brief Authentication mode.
|
|
* @param authmode : Authentication mode
|
|
* @retval None
|
|
*/
|
|
static void print_auth_mode(int authmode)
|
|
{
|
|
switch (authmode) {
|
|
case WIFI_AUTH_OPEN:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OPEN");
|
|
break;
|
|
case WIFI_AUTH_OWE:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OWE");
|
|
break;
|
|
case WIFI_AUTH_WEP:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WEP");
|
|
break;
|
|
case WIFI_AUTH_WPA_PSK:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA_PSK");
|
|
break;
|
|
case WIFI_AUTH_WPA2_PSK:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_PSK");
|
|
break;
|
|
case WIFI_AUTH_WPA_WPA2_PSK:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA_WPA2_PSK");
|
|
break;
|
|
case WIFI_AUTH_WPA2_ENTERPRISE:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_ENTERPRISE");
|
|
break;
|
|
case WIFI_AUTH_WPA3_PSK:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA3_PSK");
|
|
break;
|
|
case WIFI_AUTH_WPA2_WPA3_PSK:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_WPA3_PSK");
|
|
break;
|
|
default:
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_UNKNOWN");
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Print Wi-Fi password type.
|
|
* @param pairwise_cipher : Password type
|
|
* @param group_cipher : Group password type
|
|
* @retval None
|
|
*/
|
|
static void print_cipher_type(int pairwise_cipher, int group_cipher)
|
|
{
|
|
switch (pairwise_cipher) {
|
|
case WIFI_CIPHER_TYPE_NONE:
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_NONE");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_WEP40:
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_WEP40");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_WEP104:
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_WEP104");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_TKIP:
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_TKIP");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_CCMP:
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_CCMP");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_TKIP_CCMP:
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_TKIP_CCMP");
|
|
break;
|
|
default:
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_UNKNOWN");
|
|
break;
|
|
}
|
|
|
|
switch (group_cipher) {
|
|
case WIFI_CIPHER_TYPE_NONE:
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_NONE");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_WEP40:
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_WEP40");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_WEP104:
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_WEP104");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_TKIP:
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_TKIP");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_CCMP:
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_CCMP");
|
|
break;
|
|
case WIFI_CIPHER_TYPE_TKIP_CCMP:
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_TKIP_CCMP");
|
|
break;
|
|
default:
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_UNKNOWN");
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Print Wi-Fi scan results.
|
|
* @param ap_count : Actual number of APs scanned
|
|
* @param number : Number of AP information that can be printed
|
|
* @retval None
|
|
*/
|
|
static void print_wifi_scan_result(uint16_t ap_count, uint16_t number)
|
|
{
|
|
/* Add scan results to the dropDown list */
|
|
lv_dropdown_clear_options(ui_pageWifiConnect_dropdownWifiName);
|
|
|
|
/* Print nearby Wi-Fi information */
|
|
ESP_LOGI(TAG, "wifi scan result, ap_count = %u, number = %u", ap_count, number);
|
|
|
|
for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i++) {
|
|
ESP_LOGI(TAG, "SSID \t\t%s", ap_info[i].ssid);
|
|
|
|
ESP_LOGI(TAG, "RSSI \t\t%d", ap_info[i].rssi);
|
|
|
|
lv_dropdown_add_option(ui_pageWifiConnect_dropdownWifiName, (char*)ap_info[i].ssid, i);
|
|
|
|
/* Encryption method */
|
|
print_auth_mode(ap_info[i].authmode);
|
|
/* Password type */
|
|
if (ap_info[i].authmode != WIFI_AUTH_WEP) {
|
|
print_cipher_type(ap_info[i].pairwise_cipher, ap_info[i].group_cipher);
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Channel \t\t%d\n", ap_info[i].primary);
|
|
}
|
|
|
|
return;
|
|
} |