/* Iperf Example - wifi commands 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. */ #include #include #include "esp_log.h" #include "esp_console.h" #include "argtable3/argtable3.h" #include "cmd_decl.h" #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_netif.h" #include "esp_event.h" #include "iperf.h" typedef struct { struct arg_str *ip; struct arg_lit *server; struct arg_lit *udp; struct arg_int *port; struct arg_int *interval; struct arg_int *time; struct arg_lit *abort; struct arg_end *end; } wifi_iperf_t; static wifi_iperf_t iperf_args; typedef struct { struct arg_str *ssid; 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="cmd_wifi"; static esp_netif_t *netif_ap = NULL; static esp_netif_t *netif_sta = NULL; static EventGroupHandle_t wifi_event_group; const int CONNECTED_BIT = BIT0; const int DISCONNECTED_BIT = BIT1; static void scan_done_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { uint16_t sta_number = 0; uint8_t i; wifi_ap_record_t *ap_list_buffer; esp_wifi_scan_get_ap_num(&sta_number); if (!sta_number) { ESP_LOGE(TAG, "No AP found"); return; } 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; isval[0]); wifi_cmd_sta_join(sta_args.ssid->sval[0], sta_args.password->sval[0]); 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_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) { wifi_config_t wifi_config = { .ap = { .ssid = "", .ssid_len = 0, .max_connection = 4, .password = "", .authmode = WIFI_AUTH_WPA_WPA2_PSK }, }; reconnect = false; strlcpy((char*) wifi_config.ap.ssid, ssid, sizeof(wifi_config.ap.ssid)); if (pass) { if (strlen(pass) != 0 && strlen(pass) < 8) { reconnect = true; ESP_LOGE(TAG, "password less than 8"); return false; } strlcpy((char*) wifi_config.ap.password, pass, sizeof(wifi_config.ap.password)); } if (strlen(pass) == 0) { wifi_config.ap.authmode = WIFI_AUTH_OPEN; } ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); return true; } static int wifi_cmd_ap(int argc, char** argv) { int nerrors = arg_parse(argc, argv, (void**) &ap_args); if (nerrors != 0) { arg_print_errors(stderr, ap_args.end, argv[0]); return 1; } wifi_cmd_ap_set(ap_args.ssid->sval[0], ap_args.password->sval[0]); ESP_LOGI(TAG, "AP mode, %s %s", ap_args.ssid->sval[0], ap_args.password->sval[0]); return 0; } static int wifi_cmd_query(int argc, char** argv) { wifi_config_t cfg; wifi_mode_t mode; esp_wifi_get_mode(&mode); if (WIFI_MODE_AP == mode) { esp_wifi_get_config(WIFI_IF_AP, &cfg); ESP_LOGI(TAG, "AP mode, %s %s", cfg.ap.ssid, cfg.ap.password); } else if (WIFI_MODE_STA == mode) { int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); if (bits & CONNECTED_BIT) { esp_wifi_get_config(WIFI_IF_STA, &cfg); ESP_LOGI(TAG, "sta mode, connected %s", cfg.ap.ssid); } else { ESP_LOGI(TAG, "sta mode, disconnected"); } } else { ESP_LOGI(TAG, "NULL mode"); return 0; } return 0; } static uint32_t wifi_get_local_ip(void) { int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); esp_netif_t * netif = netif_ap; esp_netif_ip_info_t ip_info; wifi_mode_t mode; esp_wifi_get_mode(&mode); if (WIFI_MODE_STA == mode) { bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); if (bits & CONNECTED_BIT) { netif = netif_sta; } else { ESP_LOGE(TAG, "sta has no IP"); return 0; } } esp_netif_get_ip_info(netif, &ip_info); return ip_info.ip.addr; } static int wifi_cmd_iperf(int argc, char** argv) { int nerrors = arg_parse(argc, argv, (void**) &iperf_args); iperf_cfg_t cfg; if (nerrors != 0) { arg_print_errors(stderr, iperf_args.end, argv[0]); return 0; } memset(&cfg, 0, sizeof(cfg)); if ( iperf_args.abort->count != 0) { iperf_stop(); return 0; } if ( ((iperf_args.ip->count == 0) && (iperf_args.server->count == 0)) || ((iperf_args.ip->count != 0) && (iperf_args.server->count != 0)) ) { ESP_LOGE(TAG, "should specific client/server mode"); return 0; } if (iperf_args.ip->count == 0) { cfg.flag |= IPERF_FLAG_SERVER; } else { cfg.dip = esp_ip4addr_aton(iperf_args.ip->sval[0]); cfg.flag |= IPERF_FLAG_CLIENT; } cfg.sip = wifi_get_local_ip(); if (cfg.sip == 0) { return 0; } if (iperf_args.udp->count == 0) { cfg.flag |= IPERF_FLAG_TCP; } else { cfg.flag |= IPERF_FLAG_UDP; } if (iperf_args.port->count == 0) { cfg.sport = IPERF_DEFAULT_PORT; cfg.dport = IPERF_DEFAULT_PORT; } else { if (cfg.flag & IPERF_FLAG_SERVER) { cfg.sport = iperf_args.port->ival[0]; cfg.dport = IPERF_DEFAULT_PORT; } else { cfg.sport = IPERF_DEFAULT_PORT; cfg.dport = iperf_args.port->ival[0]; } } if (iperf_args.interval->count == 0) { cfg.interval = IPERF_DEFAULT_INTERVAL; } else { cfg.interval = iperf_args.interval->ival[0]; if (cfg.interval <= 0) { cfg.interval = IPERF_DEFAULT_INTERVAL; } } if (iperf_args.time->count == 0) { cfg.time = IPERF_DEFAULT_TIME; } else { cfg.time = iperf_args.time->ival[0]; if (cfg.time <= cfg.interval) { cfg.time = cfg.interval; } } ESP_LOGI(TAG, "mode=%s-%s sip=%d.%d.%d.%d:%d, dip=%d.%d.%d.%d:%d, interval=%d, time=%d", cfg.flag&IPERF_FLAG_TCP?"tcp":"udp", cfg.flag&IPERF_FLAG_SERVER?"server":"client", cfg.sip&0xFF, (cfg.sip>>8)&0xFF, (cfg.sip>>16)&0xFF, (cfg.sip>>24)&0xFF, cfg.sport, cfg.dip&0xFF, (cfg.dip>>8)&0xFF, (cfg.dip>>16)&0xFF, (cfg.dip>>24)&0xFF, cfg.dport, cfg.interval, cfg.time); iperf_start(&cfg); return 0; } void register_wifi(void) { sta_args.ssid = arg_str1(NULL, NULL, "", "SSID of AP"); sta_args.password = arg_str0(NULL, NULL, "", "password of AP"); sta_args.end = arg_end(2); const esp_console_cmd_t sta_cmd = { .command = "sta", .help = "WiFi is station mode, join specified soft-AP", .hint = NULL, .func = &wifi_cmd_sta, .argtable = &sta_args }; ESP_ERROR_CHECK( esp_console_cmd_register(&sta_cmd) ); scan_args.ssid = arg_str0(NULL, NULL, "", "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 of AP"); ap_args.password = arg_str0(NULL, NULL, "", "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", .hint = NULL, .func = &wifi_cmd_ap, .argtable = &ap_args }; ESP_ERROR_CHECK( esp_console_cmd_register(&ap_cmd) ); const esp_console_cmd_t query_cmd = { .command = "query", .help = "query WiFi info", .hint = NULL, .func = &wifi_cmd_query, }; ESP_ERROR_CHECK( esp_console_cmd_register(&query_cmd) ); iperf_args.ip = arg_str0("c", "client", "", "run in client mode, connecting to "); iperf_args.server = arg_lit0("s", "server", "run in server mode"); iperf_args.udp = arg_lit0("u", "udp", "use UDP rather than TCP"); iperf_args.port = arg_int0("p", "port", "", "server port to listen on/connect to"); iperf_args.interval = arg_int0("i", "interval", "", "seconds between periodic bandwidth reports"); iperf_args.time = arg_int0("t", "time", "