]> granicus.if.org Git - esp-idf/commitdiff
iperf example: add scan command to get AP rssi
authorHe Yin Ling <heyinling@espressif.com>
Thu, 11 Jan 2018 06:18:29 +0000 (14:18 +0800)
committerbot <bot@espressif.com>
Tue, 15 May 2018 03:29:31 +0000 (03:29 +0000)
examples/wifi/iperf/main/cmd_wifi.c

index 3407e99e98941198063b3b79b2ee003e5d1411a9..c2af6459492502f37e99468338eb2ee6a90a3315 100644 (file)
@@ -37,7 +37,14 @@ typedef struct {
     struct arg_str *password;
     struct arg_end *end;
 } wifi_args_t;
+
+typedef struct {
+    struct arg_str *ssid;
+    struct arg_end *end;
+} wifi_scan_arg_t;
+
 static wifi_args_t sta_args;
+static wifi_scan_arg_t scan_args;
 static wifi_args_t ap_args;
 static bool reconnect = true;
 static const char *TAG="iperf";
@@ -46,6 +53,27 @@ static EventGroupHandle_t wifi_event_group;
 const int CONNECTED_BIT = BIT0;
 const int DISCONNECTED_BIT = BIT1;
 
+static void scan_done_handler(void)
+{
+    uint16_t sta_number = 0;
+    uint8_t i;
+    wifi_ap_record_t *ap_list_buffer;
+
+    esp_wifi_scan_get_ap_num(&sta_number);
+    ap_list_buffer = malloc(sta_number * sizeof(wifi_ap_record_t));
+    if (ap_list_buffer == NULL) {
+        ESP_LOGE(TAG, "Failed to malloc buffer to print scan results");
+        return;
+    }
+
+    if (esp_wifi_scan_get_ap_records(&sta_number,(wifi_ap_record_t *)ap_list_buffer) == ESP_OK) {
+        for(i=0; i<sta_number; i++) {
+            ESP_LOGI(TAG, "[%s][rssi=%d]", ap_list_buffer[i].ssid, ap_list_buffer[i].rssi);
+        }
+    }
+    free(ap_list_buffer);
+}
+
 static esp_err_t event_handler(void *ctx, system_event_t *event)
 {
     switch(event->event_id) {
@@ -53,6 +81,10 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
         xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT);
         xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
         break;
+    case SYSTEM_EVENT_SCAN_DONE:
+        scan_done_handler();
+        ESP_LOGI(TAG, "sta scan done");
+        break;
     case SYSTEM_EVENT_STA_DISCONNECTED:
         if (reconnect) {
             ESP_LOGI(TAG, "sta disconnect, reconnect...");
@@ -131,6 +163,35 @@ static int wifi_cmd_sta(int argc, char** argv)
     return 0;
 }
 
+static bool wifi_cmd_sta_scan(const char* ssid)
+{
+    wifi_scan_config_t scan_config = { 0 };
+    scan_config.ssid = (uint8_t *) ssid;
+
+    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
+    ESP_ERROR_CHECK( esp_wifi_scan_start(&scan_config, false) );
+
+    return true;
+}
+
+static int wifi_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;
+    }
+
+    ESP_LOGI(TAG, "sta start to scan");
+    if ( scan_args.ssid->count == 1 ) {
+        wifi_cmd_sta_scan(scan_args.ssid->sval[0]);
+    } else {
+        wifi_cmd_sta_scan(NULL);
+    }
+    return 0;
+}
+
 
 static bool wifi_cmd_ap_set(const char* ssid, const char* pass)
 {
@@ -338,10 +399,24 @@ void register_wifi()
 
     ESP_ERROR_CHECK( esp_console_cmd_register(&sta_cmd) );
 
+    scan_args.ssid = arg_str0(NULL, NULL, "<ssid>", "SSID of AP want to be scanned");
+    scan_args.end = arg_end(1);
+
+    const esp_console_cmd_t scan_cmd = {
+        .command = "scan",
+        .help = "WiFi is station mode, start scan ap",
+        .hint = NULL,
+        .func = &wifi_cmd_scan,
+        .argtable = &scan_args
+    };
+
     ap_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
     ap_args.password = arg_str0(NULL, NULL, "<pass>", "password of AP");
     ap_args.end = arg_end(2);
 
+
+    ESP_ERROR_CHECK( esp_console_cmd_register(&scan_cmd) );
+
     const esp_console_cmd_t ap_cmd = {
         .command = "ap",
         .help = "AP mode, configure ssid and password",