#include "wifi.h" #include #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_event.h" #include "nvs_flash.h" #include "regex.h" #include "logger.h" #define SCAN_LIST_SIZE 30 #ifdef CONFIG_EXAMPLE_USE_SCAN_CHANNEL_BITMAP #define USE_CHANNEL_BTIMAP 1 #define CHANNEL_LIST_SIZE 3 static uint8_t channel_list[CHANNEL_LIST_SIZE] = {1, 6, 11}; #endif /*CONFIG_EXAMPLE_USE_SCAN_CHANNEL_BITMAP*/ wifi_ap_record_t ap_info[SCAN_LIST_SIZE]; static void print_auth_mode(int authmode) { switch (authmode) { case WIFI_AUTH_OPEN: LOG_I("\tAuthmode: WIFI_AUTH_OPEN"); break; case WIFI_AUTH_OWE: LOG_I("\tAuthmode: WIFI_AUTH_OWE"); break; case WIFI_AUTH_WEP: LOG_I("\tAuthmode: WIFI_AUTH_WEP"); break; case WIFI_AUTH_WPA_PSK: LOG_I("\tAuthmode: WIFI_AUTH_WPA_PSK"); break; case WIFI_AUTH_WPA2_PSK: LOG_I("\tAuthmode: WIFI_AUTH_WPA2_PSK"); break; case WIFI_AUTH_WPA_WPA2_PSK: LOG_I("\tAuthmode: WIFI_AUTH_WPA_WPA2_PSK"); break; case WIFI_AUTH_ENTERPRISE: LOG_I("\tAuthmode: WIFI_AUTH_ENTERPRISE"); break; case WIFI_AUTH_WPA3_PSK: LOG_I("\tAuthmode: WIFI_AUTH_WPA3_PSK"); break; case WIFI_AUTH_WPA2_WPA3_PSK: LOG_I("\tAuthmode: WIFI_AUTH_WPA2_WPA3_PSK"); break; case WIFI_AUTH_WPA3_ENT_192: LOG_I("\tAuthmode: WIFI_AUTH_WPA3_ENT_192"); break; default: LOG_I("\tAuthmode: WIFI_AUTH_UNKNOWN"); break; } } static void print_cipher_type(int pairwise_cipher, int group_cipher) { switch (pairwise_cipher) { case WIFI_CIPHER_TYPE_NONE: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_NONE"); break; case WIFI_CIPHER_TYPE_WEP40: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_WEP40"); break; case WIFI_CIPHER_TYPE_WEP104: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_WEP104"); break; case WIFI_CIPHER_TYPE_TKIP: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_TKIP"); break; case WIFI_CIPHER_TYPE_CCMP: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_CCMP"); break; case WIFI_CIPHER_TYPE_TKIP_CCMP: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_TKIP_CCMP"); break; case WIFI_CIPHER_TYPE_AES_CMAC128: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_AES_CMAC128"); break; case WIFI_CIPHER_TYPE_SMS4: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_SMS4"); break; case WIFI_CIPHER_TYPE_GCMP: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_GCMP"); break; case WIFI_CIPHER_TYPE_GCMP256: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_GCMP256"); break; default: LOG_I("\tPairwise Cipher: WIFI_CIPHER_TYPE_UNKNOWN"); break; } switch (group_cipher) { case WIFI_CIPHER_TYPE_NONE: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_NONE"); break; case WIFI_CIPHER_TYPE_WEP40: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_WEP40"); break; case WIFI_CIPHER_TYPE_WEP104: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_WEP104"); break; case WIFI_CIPHER_TYPE_TKIP: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_TKIP"); break; case WIFI_CIPHER_TYPE_CCMP: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_CCMP"); break; case WIFI_CIPHER_TYPE_TKIP_CCMP: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_TKIP_CCMP"); break; case WIFI_CIPHER_TYPE_SMS4: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_SMS4"); break; case WIFI_CIPHER_TYPE_GCMP: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_GCMP"); break; case WIFI_CIPHER_TYPE_GCMP256: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_GCMP256"); break; default: LOG_I("\tGroup Cipher: WIFI_CIPHER_TYPE_UNKNOWN"); break; } } #ifdef USE_CHANNEL_BTIMAP static void array_2_channel_bitmap(const uint8_t channel_list[], const uint8_t channel_list_size, wifi_scan_config_t *scan_config) { for(uint8_t i = 0; i < channel_list_size; i++) { uint8_t channel = channel_list[i]; scan_config->channel_bitmap.ghz_2_channels |= (1 << channel); } } #endif /*USE_CHANNEL_BTIMAP*/ /* Initialize Wi-Fi as sta and set scan method */ void wifi_scan() { uint16_t number = SCAN_LIST_SIZE; uint16_t ap_count = 0; memset(ap_info, 0, sizeof(ap_info)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); #ifdef USE_CHANNEL_BTIMAP wifi_scan_config_t *scan_config = (wifi_scan_config_t *)calloc(1,sizeof(wifi_scan_config_t)); if (!scan_config) { LOG_I("Memory Allocation for scan config failed!"); return; } array_2_channel_bitmap(channel_list, CHANNEL_LIST_SIZE, scan_config); esp_wifi_scan_start(scan_config, true); #else esp_wifi_scan_start(NULL, true); #endif /*USE_CHANNEL_BTIMAP*/ LOG_I("Max AP number ap_info can hold = %u", number); ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count)); ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info)); LOG_I("Total APs scanned = %u, actual AP number ap_info holds = %u", ap_count, number); for (int i = 0; i < number; i++) { LOG_I("%s \tRSSI: %d", ap_info[i].ssid, 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); // } LOG_I("\tChannel: %d", ap_info[i].primary); } }