From a9bffe1b4c3d4bb188aea00a3598548f447db296 Mon Sep 17 00:00:00 2001 From: FReenen Date: Thu, 20 Jun 2024 20:17:18 +0200 Subject: [PATCH] moor debuging and add wifi scan code --- rx_esp32/src/main.c | 188 +++++++++++++++++++++++++++++++++------ rx_esp32/src/wifi_scan.c | 182 +++++++++++++++++++++++++++++++++++++ rx_esp32/src/wifi_scan.h | 6 ++ 3 files changed, 348 insertions(+), 28 deletions(-) create mode 100644 rx_esp32/src/wifi_scan.c create mode 100644 rx_esp32/src/wifi_scan.h diff --git a/rx_esp32/src/main.c b/rx_esp32/src/main.c index 09060fa..623eab1 100644 --- a/rx_esp32/src/main.c +++ b/rx_esp32/src/main.c @@ -17,9 +17,9 @@ #define WIFI_PASS "SPHZHKRY" #define UDP_PORT 1234 -static const char *TAG = "FR_rx-esp32"; +static const char *TAG = "rcrf-rx_esp32"; -#define MAX_RETRY_ATTEMPTS 2 +#define MAX_RETRY_ATTEMPTS 20 // static int s_ap_creds_num = 0; static int s_retry_num = 0; @@ -54,24 +54,150 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_ { if (event_base == WIFI_EVENT) { - if (event_id == WIFI_EVENT_STA_START) + switch (event_id) { - ESP_LOGI(TAG,"wifi start (first try for connection)"); - esp_wifi_connect(); - } - else if (event_id == WIFI_EVENT_STA_DISCONNECTED) - { - ESP_LOGI(TAG,"diconnected"); - if (s_retry_num < MAX_RETRY_ATTEMPTS) - { - s_retry_num++; - ESP_LOGI(TAG, "retry num %d", s_retry_num); + case WIFI_EVENT_WIFI_READY: + ESP_LOGI(TAG, "WiFi ready"); + break; + case WIFI_EVENT_SCAN_DONE: + ESP_LOGI(TAG, "Finished scanning AP"); + break; + case WIFI_EVENT_STA_START: + ESP_LOGI(TAG, "Station start"); esp_wifi_connect(); - } - else - { - xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); - } + break; + case WIFI_EVENT_STA_STOP: + ESP_LOGI(TAG, "Station stop"); + break; + case WIFI_EVENT_STA_CONNECTED: + ESP_LOGI(TAG, "Station connected to AP"); + break; + case WIFI_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "Station disconnected from AP"); + if (s_retry_num < MAX_RETRY_ATTEMPTS) + { + s_retry_num++; + ESP_LOGI(TAG, "retry num %d", s_retry_num); + for (unsigned long i=0; i < 100000; i++) + { + printf("."); + } + esp_wifi_connect(); + } + else + { + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); + } + break; + break; + case WIFI_EVENT_STA_AUTHMODE_CHANGE: + ESP_LOGI(TAG, "the auth mode of AP connected by device's station changed"); + break; + case WIFI_EVENT_STA_WPS_ER_SUCCESS: + ESP_LOGI(TAG, "Station wps succeeds in enrollee mode"); + break; + case WIFI_EVENT_STA_WPS_ER_FAILED: + ESP_LOGI(TAG, "Station wps fails in enrollee mode"); + break; + case WIFI_EVENT_STA_WPS_ER_TIMEOUT: + ESP_LOGI(TAG, "Station wps timeout in enrollee mode"); + break; + case WIFI_EVENT_STA_WPS_ER_PIN: + ESP_LOGI(TAG, "Station wps pin code in enrollee mode"); + break; + case WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP: + ESP_LOGI(TAG, "Station wps overlap in enrollee mode"); + break; + case WIFI_EVENT_AP_START: + ESP_LOGI(TAG, "Soft-AP start"); + break; + case WIFI_EVENT_AP_STOP: + ESP_LOGI(TAG, "Soft-AP stop"); + break; + case WIFI_EVENT_AP_STACONNECTED: + ESP_LOGI(TAG, "a station connected to Soft-AP"); + break; + case WIFI_EVENT_AP_STADISCONNECTED: + ESP_LOGI(TAG, "a station disconnected from Soft-AP"); + break; + case WIFI_EVENT_AP_PROBEREQRECVED: + ESP_LOGI(TAG, "Receive probe request packet in soft-AP interface"); + break; + case WIFI_EVENT_FTM_REPORT: + ESP_LOGI(TAG, "Receive report of FTM procedure"); + break; + case WIFI_EVENT_STA_BSS_RSSI_LOW: + ESP_LOGI(TAG, "AP's RSSI crossed configured threshold"); + break; + case WIFI_EVENT_ACTION_TX_STATUS: + ESP_LOGI(TAG, "Status indication of Action Tx operation"); + break; + case WIFI_EVENT_ROC_DONE: + ESP_LOGI(TAG, "Remain-on-Channel operation complete"); + break; + case WIFI_EVENT_STA_BEACON_TIMEOUT: + ESP_LOGI(TAG, "Station beacon timeout"); + break; + case WIFI_EVENT_CONNECTIONLESS_MODULE_WAKE_INTERVAL_START: + ESP_LOGI(TAG, "Connectionless module wake interval start"); + break; + case WIFI_EVENT_AP_WPS_RG_SUCCESS: + ESP_LOGI(TAG, "Soft-AP wps succeeds in registrar mode"); + break; + case WIFI_EVENT_AP_WPS_RG_FAILED: + ESP_LOGI(TAG, "Soft-AP wps fails in registrar mode"); + break; + case WIFI_EVENT_AP_WPS_RG_TIMEOUT: + ESP_LOGI(TAG, "Soft-AP wps timeout in registrar mode"); + break; + case WIFI_EVENT_AP_WPS_RG_PIN: + ESP_LOGI(TAG, "Soft-AP wps pin code in registrar mode"); + break; + case WIFI_EVENT_AP_WPS_RG_PBC_OVERLAP: + ESP_LOGI(TAG, "Soft-AP wps overlap in registrar mode"); + break; + case WIFI_EVENT_ITWT_SETUP: + ESP_LOGI(TAG, "iTWT setup"); + break; + case WIFI_EVENT_ITWT_TEARDOWN: + ESP_LOGI(TAG, "iTWT teardown"); + break; + case WIFI_EVENT_ITWT_PROBE: + ESP_LOGI(TAG, "iTWT probe"); + break; + case WIFI_EVENT_ITWT_SUSPEND: + ESP_LOGI(TAG, "iTWT suspend"); + break; + case WIFI_EVENT_NAN_STARTED: + ESP_LOGI(TAG, "NAN Discovery has started"); + break; + case WIFI_EVENT_NAN_STOPPED: + ESP_LOGI(TAG, "NAN Discovery has stopped"); + break; + case WIFI_EVENT_NAN_SVC_MATCH: + ESP_LOGI(TAG, "NAN Service Discovery match found"); + break; + case WIFI_EVENT_NAN_REPLIED: + ESP_LOGI(TAG, "Replied to a NAN peer with Service Discovery match"); + break; + case WIFI_EVENT_NAN_RECEIVE: + ESP_LOGI(TAG, "Received a Follow-up message"); + break; + case WIFI_EVENT_NDP_INDICATION: + ESP_LOGI(TAG, "Received NDP Request from a NAN Peer"); + break; + case WIFI_EVENT_NDP_CONFIRM: + ESP_LOGI(TAG, "NDP Confirm Indication"); + break; + case WIFI_EVENT_NDP_TERMINATED: + ESP_LOGI(TAG, "NAN Datapath terminated indication"); + break; + case WIFI_EVENT_HOME_CHANNEL_CHANGE: + ESP_LOGI(TAG, "WiFi home channel change,doesn't occur when scanning"); + break; + case WIFI_EVENT_MAX: + ESP_LOGI(TAG, "Invalid WiFi event ID"); + break; } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) @@ -85,17 +211,16 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_ void wifiInit() { - // create event group - s_wifi_event_group = xEventGroupCreate(); - // init network interface ESP_ERROR_CHECK(esp_netif_init()); - // create event loop + // create event group + s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); // init wifi as default station - esp_netif_create_default_wifi_sta(); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); @@ -132,13 +257,15 @@ void app_main() { ESP_ERROR_CHECK(esp_task_wdt_deinit()); // wait so I have time to open the serial monitor - for (unsigned long i=0; i < 100000; i++){ + for (unsigned long i=0; i < 100000; i++) + { printf("."); } /* Initialize NVS — it is used to store PHY calibration data */ esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) + { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } @@ -158,11 +285,16 @@ void app_main() { /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually * happened. */ - if (bits & WIFI_CONNECTED_BIT) { + if (bits & WIFI_CONNECTED_BIT) + { ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", WIFI_SSID, WIFI_PASS); - } else if (bits & WIFI_FAIL_BIT) { + } + else if (bits & WIFI_FAIL_BIT) + { ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", WIFI_SSID, WIFI_PASS); - } else { + } + else + { ESP_LOGE(TAG, "UNEXPECTED EVENT"); } diff --git a/rx_esp32/src/wifi_scan.c b/rx_esp32/src/wifi_scan.c new file mode 100644 index 0000000..6fe270b --- /dev/null +++ b/rx_esp32/src/wifi_scan.c @@ -0,0 +1,182 @@ +#include "wifi_scan.h" + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_event.h" +#include "nvs_flash.h" +#include "regex.h" +#include "esp_task_wdt.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 const char *TAG_SCAN = "scan"; + +static void print_auth_mode(int authmode) +{ + switch (authmode) { + case WIFI_AUTH_OPEN: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_OPEN"); + break; + case WIFI_AUTH_OWE: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_OWE"); + break; + case WIFI_AUTH_WEP: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_WEP"); + break; + case WIFI_AUTH_WPA_PSK: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_WPA_PSK"); + break; + case WIFI_AUTH_WPA2_PSK: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_WPA2_PSK"); + break; + case WIFI_AUTH_WPA_WPA2_PSK: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_WPA_WPA2_PSK"); + break; + case WIFI_AUTH_ENTERPRISE: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_ENTERPRISE"); + break; + case WIFI_AUTH_WPA3_PSK: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_WPA3_PSK"); + break; + case WIFI_AUTH_WPA2_WPA3_PSK: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_WPA2_WPA3_PSK"); + break; + case WIFI_AUTH_WPA3_ENT_192: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_WPA3_ENT_192"); + break; + default: + ESP_LOGI(TAG_SCAN, "\tAuthmode: WIFI_AUTH_UNKNOWN"); + break; + } +} + +static void print_cipher_type(int pairwise_cipher, int group_cipher) +{ + switch (pairwise_cipher) { + case WIFI_CIPHER_TYPE_NONE: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_NONE"); + break; + case WIFI_CIPHER_TYPE_WEP40: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_WEP40"); + break; + case WIFI_CIPHER_TYPE_WEP104: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_WEP104"); + break; + case WIFI_CIPHER_TYPE_TKIP: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_TKIP"); + break; + case WIFI_CIPHER_TYPE_CCMP: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_CCMP"); + break; + case WIFI_CIPHER_TYPE_TKIP_CCMP: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_TKIP_CCMP"); + break; + case WIFI_CIPHER_TYPE_AES_CMAC128: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_AES_CMAC128"); + break; + case WIFI_CIPHER_TYPE_SMS4: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_SMS4"); + break; + case WIFI_CIPHER_TYPE_GCMP: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_GCMP"); + break; + case WIFI_CIPHER_TYPE_GCMP256: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_GCMP256"); + break; + default: + ESP_LOGI(TAG_SCAN, "\tPairwise Cipher: WIFI_CIPHER_TYPE_UNKNOWN"); + break; + } + + switch (group_cipher) { + case WIFI_CIPHER_TYPE_NONE: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_NONE"); + break; + case WIFI_CIPHER_TYPE_WEP40: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_WEP40"); + break; + case WIFI_CIPHER_TYPE_WEP104: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_WEP104"); + break; + case WIFI_CIPHER_TYPE_TKIP: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_TKIP"); + break; + case WIFI_CIPHER_TYPE_CCMP: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_CCMP"); + break; + case WIFI_CIPHER_TYPE_TKIP_CCMP: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_TKIP_CCMP"); + break; + case WIFI_CIPHER_TYPE_SMS4: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_SMS4"); + break; + case WIFI_CIPHER_TYPE_GCMP: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_GCMP"); + break; + case WIFI_CIPHER_TYPE_GCMP256: + ESP_LOGI(TAG_SCAN, "\tGroup Cipher: WIFI_CIPHER_TYPE_GCMP256"); + break; + default: + ESP_LOGI(TAG_SCAN, "\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 */ +static void wifi_scan(void) +{ + 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) { + ESP_LOGE(TAG_SCAN, "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*/ + + ESP_LOGI(TAG_SCAN, "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)); + ESP_LOGI(TAG_SCAN, "Total APs scanned = %u, actual AP number ap_info holds = %u", ap_count, number); + for (int i = 0; i < number; i++) { + ESP_LOGI(TAG_SCAN, "%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); + } + ESP_LOGI(TAG_SCAN, "\tChannel: %d", ap_info[i].primary); + } +} diff --git a/rx_esp32/src/wifi_scan.h b/rx_esp32/src/wifi_scan.h new file mode 100644 index 0000000..a966729 --- /dev/null +++ b/rx_esp32/src/wifi_scan.h @@ -0,0 +1,6 @@ +#ifndef WIFI_SCAN_H +#define WIFI_SCAN_H + +static void wifi_scan(void); + +#endif