2017-09-28 08:17:06 +00:00
|
|
|
/* Scan Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-11-15 15:56:16 +00:00
|
|
|
This example shows how to scan for available set of APs.
|
2017-09-28 08:17:06 +00:00
|
|
|
*/
|
2019-10-23 06:12:32 +00:00
|
|
|
#include <string.h>
|
2017-09-28 08:17:06 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_log.h"
|
2018-11-20 10:26:53 +00:00
|
|
|
#include "esp_event.h"
|
2017-09-28 08:17:06 +00:00
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
2019-10-23 06:12:32 +00:00
|
|
|
#define DEFAULT_SCAN_LIST_SIZE CONFIG_EXAMPLE_SCAN_LIST_SIZE
|
2017-09-28 08:17:06 +00:00
|
|
|
|
|
|
|
static const char *TAG = "scan";
|
|
|
|
|
2019-10-23 06:12:32 +00:00
|
|
|
static void print_auth_mode(int authmode)
|
2017-09-28 08:17:06 +00:00
|
|
|
{
|
2019-10-23 06:12:32 +00:00
|
|
|
switch (authmode) {
|
|
|
|
case WIFI_AUTH_OPEN:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OPEN");
|
|
|
|
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;
|
2023-11-22 06:23:21 +00:00
|
|
|
case WIFI_AUTH_ENTERPRISE:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_ENTERPRISE");
|
2019-10-23 06:12:32 +00:00
|
|
|
break;
|
2020-05-15 11:54:51 +00:00
|
|
|
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;
|
2023-11-22 06:23:21 +00:00
|
|
|
case WIFI_AUTH_WPA3_ENT_192:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA3_ENT_192");
|
|
|
|
break;
|
2019-10-23 06:12:32 +00:00
|
|
|
default:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_UNKNOWN");
|
|
|
|
break;
|
2017-09-28 08:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 06:12:32 +00:00
|
|
|
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;
|
2023-04-26 08:56:49 +00:00
|
|
|
case WIFI_CIPHER_TYPE_AES_CMAC128:
|
|
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_AES_CMAC128");
|
|
|
|
break;
|
|
|
|
case WIFI_CIPHER_TYPE_SMS4:
|
|
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_SMS4");
|
|
|
|
break;
|
|
|
|
case WIFI_CIPHER_TYPE_GCMP:
|
|
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_GCMP");
|
|
|
|
break;
|
|
|
|
case WIFI_CIPHER_TYPE_GCMP256:
|
|
|
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_GCMP256");
|
|
|
|
break;
|
2019-10-23 06:12:32 +00:00
|
|
|
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;
|
2023-04-26 08:56:49 +00:00
|
|
|
case WIFI_CIPHER_TYPE_SMS4:
|
|
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_SMS4");
|
|
|
|
break;
|
|
|
|
case WIFI_CIPHER_TYPE_GCMP:
|
|
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_GCMP");
|
|
|
|
break;
|
|
|
|
case WIFI_CIPHER_TYPE_GCMP256:
|
|
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_GCMP256");
|
|
|
|
break;
|
2019-10-23 06:12:32 +00:00
|
|
|
default:
|
|
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_UNKNOWN");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 10:26:53 +00:00
|
|
|
|
2017-11-15 09:29:13 +00:00
|
|
|
/* Initialize Wi-Fi as sta and set scan method */
|
2017-09-28 08:17:06 +00:00
|
|
|
static void wifi_scan(void)
|
|
|
|
{
|
2019-11-29 09:54:02 +00:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 10:26:53 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-10-17 09:36:34 +00:00
|
|
|
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
|
assert(sta_netif);
|
2017-11-15 07:34:21 +00:00
|
|
|
|
2017-09-28 08:17:06 +00:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 10:26:53 +00:00
|
|
|
|
2019-10-23 06:12:32 +00:00
|
|
|
uint16_t number = DEFAULT_SCAN_LIST_SIZE;
|
|
|
|
wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
|
2019-11-15 15:56:16 +00:00
|
|
|
uint16_t ap_count = 0;
|
2019-10-23 06:12:32 +00:00
|
|
|
memset(ap_info, 0, sizeof(ap_info));
|
|
|
|
|
2017-09-28 08:17:06 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
2021-01-14 11:33:10 +00:00
|
|
|
esp_wifi_scan_start(NULL, true);
|
2023-10-17 09:55:54 +00:00
|
|
|
ESP_LOGI(TAG, "Max AP number ap_info can hold = %u", number);
|
2019-10-23 06:12:32 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
|
2019-11-15 15:56:16 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
|
2023-10-17 09:55:54 +00:00
|
|
|
ESP_LOGI(TAG, "Total APs scanned = %u, actual AP number ap_info holds = %u", ap_count, number);
|
|
|
|
for (int i = 0; i < number; i++) {
|
2019-10-23 06:12:32 +00:00
|
|
|
ESP_LOGI(TAG, "SSID \t\t%s", ap_info[i].ssid);
|
|
|
|
ESP_LOGI(TAG, "RSSI \t\t%d", ap_info[i].rssi);
|
|
|
|
print_auth_mode(ap_info[i].authmode);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-09-28 08:17:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
void app_main(void)
|
2017-09-28 08:17:06 +00:00
|
|
|
{
|
|
|
|
// Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 15:11:09 +00:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-09-28 08:17:06 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
|
|
|
wifi_scan();
|
|
|
|
}
|