From 2a41edf491f514fe00e8b0e0dec378272ec29245 Mon Sep 17 00:00:00 2001 From: Robert McMahon Date: Fri, 19 Dec 2025 18:10:02 -0800 Subject: [PATCH] add bssid to scan output --- components/app_console/app_console.c | 91 +++++++++++++++++----------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/components/app_console/app_console.c b/components/app_console/app_console.c index b37fae9..2e2b50b 100644 --- a/components/app_console/app_console.c +++ b/components/app_console/app_console.c @@ -430,45 +430,72 @@ static struct { } scan_args; static int cmd_scan(int argc, char **argv) { - int nerrors = arg_parse(argc, argv, (void **)&scan_args); - if (nerrors > 0) { - arg_print_errors(stderr, scan_args.end, argv[0]); - return 1; - } + wifi_scan_config_t scan_config = {0}; + scan_config.show_hidden = true; - if (scan_args.help->count > 0) { - printf("Usage: scan\n"); - return 0; - } - - printf("Starting WiFi Scan...\n"); - - // Force STA mode to allow scanning - wifi_ctl_switch_to_sta(WIFI_BW_HT20); - - wifi_scan_config_t scan_config = { .show_hidden = true }; + printf("Starting Wi-Fi Scan...\n"); esp_err_t err = esp_wifi_scan_start(&scan_config, true); + + // --- SMART RETRY LOGIC --- + if (err == ESP_ERR_WIFI_STATE) { + printf("WARN: WiFi is busy connecting. Forcing disconnect to allow scan...\n"); + esp_wifi_disconnect(); // Stop the connection attempt + vTaskDelay(pdMS_TO_TICKS(100)); // Give the driver a moment to reset state + + // Retry scan + err = esp_wifi_scan_start(&scan_config, true); + } + // ------------------------- + if (err != ESP_OK) { - printf("Scan failed: %s\n", esp_err_to_name(err)); + printf("Error starting scan: %s\n", esp_err_to_name(err)); return 1; } uint16_t ap_count = 0; esp_wifi_scan_get_ap_num(&ap_count); - printf("Found %d APs:\n", ap_count); - - if (ap_count > 0) { - wifi_ap_record_t *ap_list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * ap_count); - if (ap_list) { - esp_wifi_scan_get_ap_records(&ap_count, ap_list); - printf("%-32s | %-4s | %-4s | %-3s\n", "SSID", "RSSI", "CH", "Auth"); - printf("----------------------------------------------------------\n"); - for (int i = 0; i < ap_count; i++) { - printf("%-32s | %-4d | %-4d | %d\n", (char *)ap_list[i].ssid, ap_list[i].rssi, ap_list[i].primary, ap_list[i].authmode); - } - free(ap_list); - } + if (ap_count == 0) { + printf("No APs found.\n"); + return 0; } + + wifi_ap_record_t *ap_info = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * ap_count); + if (!ap_info) { + printf("Failed to allocate memory for scan results.\n"); + return 1; + } + + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); + printf("Found %u unique BSSIDs\n", ap_count); + printf("------------------------------------------------------------------------------------------------\n"); + printf("SSID | BSSID | RSSI | Channel | Auth Mode\n"); + printf("------------------------------------------------------------------------------------------------\n"); + + for (int i = 0; i < ap_count; i++) { + char *authmode; + switch (ap_info[i].authmode) { + case WIFI_AUTH_OPEN: authmode = "OPEN"; break; + case WIFI_AUTH_WEP: authmode = "WEP"; break; + case WIFI_AUTH_WPA_PSK: authmode = "WPA-PSK"; break; + case WIFI_AUTH_WPA2_PSK: authmode = "WPA2-PSK"; break; + case WIFI_AUTH_WPA_WPA2_PSK: authmode = "WPA/WPA2"; break; + case WIFI_AUTH_WPA2_ENTERPRISE: authmode = "WPA2-ENT"; break; + case WIFI_AUTH_WPA3_PSK: authmode = "WPA3-PSK"; break; + case WIFI_AUTH_WPA2_WPA3_PSK: authmode = "WPA2/WPA3"; break; + default: authmode = "UNKNOWN"; break; + } + + printf("%-32.32s | %02x:%02x:%02x:%02x:%02x:%02x | %4d | %7d | %s\n", + ap_info[i].ssid, + ap_info[i].bssid[0], ap_info[i].bssid[1], ap_info[i].bssid[2], + ap_info[i].bssid[3], ap_info[i].bssid[4], ap_info[i].bssid[5], + ap_info[i].rssi, + ap_info[i].primary, + authmode); + } + printf("------------------------------------------------------------------------------------------------\n"); + + free(ap_info); return 0; } @@ -555,10 +582,6 @@ static void register_wifi_cmd(void) { ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } -#include "ping/ping_sock.h" // Add this include - -// ... [Existing Includes] ... - static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args) { uint8_t ttl; uint16_t seqno;