2017-09-28 04:17:06 -04: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 10:56:16 -05:00
|
|
|
This example shows how to scan for available set of APs.
|
2017-09-28 04:17:06 -04:00
|
|
|
*/
|
2019-10-23 02:12:32 -04:00
|
|
|
#include <string.h>
|
2017-09-28 04:17:06 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_log.h"
|
2018-11-20 05:26:53 -05:00
|
|
|
#include "esp_event.h"
|
2017-09-28 04:17:06 -04:00
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
2019-10-23 02:12:32 -04:00
|
|
|
#define DEFAULT_SCAN_LIST_SIZE CONFIG_EXAMPLE_SCAN_LIST_SIZE
|
2017-09-28 04:17:06 -04:00
|
|
|
|
|
|
|
static const char *TAG = "scan";
|
|
|
|
|
2019-10-23 02:12:32 -04:00
|
|
|
static void print_auth_mode(int authmode)
|
2017-09-28 04:17:06 -04:00
|
|
|
{
|
2019-10-23 02:12:32 -04:00
|
|
|
switch (authmode) {
|
|
|
|
case WIFI_AUTH_OPEN:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OPEN");
|
|
|
|
break;
|
2022-06-14 02:01:52 -04:00
|
|
|
case WIFI_AUTH_OWE:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OWE");
|
|
|
|
break;
|
2019-10-23 02:12:32 -04:00
|
|
|
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-09-11 01:42:59 -04:00
|
|
|
case WIFI_AUTH_ENTERPRISE:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_ENTERPRISE");
|
2019-10-23 02:12:32 -04:00
|
|
|
break;
|
2020-05-15 07:54:51 -04: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-09-11 01:42:59 -04:00
|
|
|
case WIFI_AUTH_WPA3_ENT_192:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA3_ENT_192");
|
|
|
|
break;
|
2019-10-23 02:12:32 -04:00
|
|
|
default:
|
|
|
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_UNKNOWN");
|
|
|
|
break;
|
2017-09-28 04:17:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 02:12:32 -04: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 04:56:49 -04: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 02:12:32 -04: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 04:56:49 -04: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 02:12:32 -04:00
|
|
|
default:
|
|
|
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_UNKNOWN");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2017-11-15 04:29:13 -05:00
|
|
|
/* Initialize Wi-Fi as sta and set scan method */
|
2017-09-28 04:17:06 -04:00
|
|
|
static void wifi_scan(void)
|
|
|
|
{
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 05:26:53 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-10-17 05:36:34 -04:00
|
|
|
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
|
assert(sta_netif);
|
2017-11-15 02:34:21 -05:00
|
|
|
|
2017-09-28 04:17:06 -04:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2019-10-23 02:12:32 -04:00
|
|
|
uint16_t number = DEFAULT_SCAN_LIST_SIZE;
|
|
|
|
wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
|
2019-11-15 10:56:16 -05:00
|
|
|
uint16_t ap_count = 0;
|
2019-10-23 02:12:32 -04:00
|
|
|
memset(ap_info, 0, sizeof(ap_info));
|
|
|
|
|
2017-09-28 04:17:06 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
2021-01-14 06:33:10 -05:00
|
|
|
esp_wifi_scan_start(NULL, true);
|
2023-10-17 05:55:54 -04:00
|
|
|
ESP_LOGI(TAG, "Max AP number ap_info can hold = %u", number);
|
2019-10-23 02:12:32 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
|
2019-11-15 10:56:16 -05:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
|
2023-10-17 05:55:54 -04: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 02:12:32 -04: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);
|
|
|
|
}
|
2023-06-08 14:56:11 -04:00
|
|
|
ESP_LOGI(TAG, "Channel \t\t%d", ap_info[i].primary);
|
2019-10-23 02:12:32 -04:00
|
|
|
}
|
|
|
|
|
2017-09-28 04:17:06 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-09-28 04:17:06 -04:00
|
|
|
{
|
|
|
|
// Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 11:11:09 -04:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-09-28 04:17:06 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
|
|
|
wifi_scan();
|
|
|
|
}
|