diff --git a/examples/bluetooth/esp_ble_mesh/wifi_coexist/main/idf_component.yml b/examples/bluetooth/esp_ble_mesh/wifi_coexist/main/idf_component.yml index 9f4c98345f..9fe802f328 100644 --- a/examples/bluetooth/esp_ble_mesh/wifi_coexist/main/idf_component.yml +++ b/examples/bluetooth/esp_ble_mesh/wifi_coexist/main/idf_component.yml @@ -3,5 +3,5 @@ dependencies: path: ${IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/fast_prov example_init: path: ${IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init - iperf: - path: ${IDF_PATH}/examples/common_components/iperf + espressif/iperf: + version: "~0.1.1" diff --git a/examples/common_components/iperf/CMakeLists.txt b/examples/common_components/iperf/CMakeLists.txt deleted file mode 100644 index 85a30c1257..0000000000 --- a/examples/common_components/iperf/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -idf_component_register(SRCS "iperf.c" - "wifi_stats.c" - "wifi_twt.c" - "wifi_cmd.c" - INCLUDE_DIRS "include" - REQUIRES lwip - PRIV_REQUIRES esp_timer) - -if(CONFIG_SOC_WIFI_HE_SUPPORT) - idf_component_optional_requires(PRIVATE esp_wifi console) -endif() diff --git a/examples/common_components/iperf/Kconfig.projbuild b/examples/common_components/iperf/Kconfig.projbuild deleted file mode 100644 index 0a1fb3d458..0000000000 --- a/examples/common_components/iperf/Kconfig.projbuild +++ /dev/null @@ -1,31 +0,0 @@ -menu "Iperf Configuration" - - config IPERF_SOCKET_RX_TIMEOUT - int "iperf socket TCP/UDP rx timeout in seconds" - default 10 - help - The value is used for iperf socket TCP/UDP rx timeout, iperf will be aborted - and socket will be closed and shutdown. - - config IPERF_SOCKET_TCP_TX_TIMEOUT - int "iperf socket TCP tx timeout in seconds" - default 10 - help - The value is used for iperf socket TCP tx timeout, iperf will be aborted - and socket will be closed and shutdown. - - config IPERF_TRAFFIC_TASK_PRIORITY - int "iperf traffic task priority" - default 4 - range 1 24 - help - The value is used for iperf traffic task priority. - - config IPERF_REPORT_TASK_PRIORITY - int "iperf result report task priority" - default 6 - range 1 24 - help - The value is used for iperf result report task priority. - -endmenu diff --git a/examples/common_components/iperf/README.md b/examples/common_components/iperf/README.md deleted file mode 100644 index 3608369883..0000000000 --- a/examples/common_components/iperf/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# iperf Component - -This directory contains an implementation for iperf network performance tester - -Note that it's incompatible with `iperf3` - -This component is used as part of the following ESP-IDF examples: -- [esp_wifi](../../wifi/iperf). -- [wifi_coexist](../../bluetooth/esp_ble_mesh/wifi_coexist). -- [ethernet](../../ethernet/iperf) - -To learn more about how to use this component, please check API Documentation from header file [iperf.h](./include/iperf.h). - -Please note that this component is not considered to be a part of ESP-IDF stable API, and only for example use for now. diff --git a/examples/common_components/iperf/include/iperf.h b/examples/common_components/iperf/include/iperf.h deleted file mode 100644 index 9ac7627d3a..0000000000 --- a/examples/common_components/iperf/include/iperf.h +++ /dev/null @@ -1,89 +0,0 @@ -/* Iperf Example - iperf declaration - - 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. -*/ - -#ifndef __IPERF_H_ -#define __IPERF_H_ - -#include "esp_err.h" -#include "esp_types.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define IPERF_IP_TYPE_IPV4 0 -#define IPERF_IP_TYPE_IPV6 1 -#define IPERF_TRANS_TYPE_TCP 0 -#define IPERF_TRANS_TYPE_UDP 1 - -#define IPERF_FLAG_SET(cfg, flag) ((cfg) |= (flag)) -#define IPERF_FLAG_CLR(cfg, flag) ((cfg) &= (~(flag))) - -#define IPERF_FLAG_CLIENT (1) -#define IPERF_FLAG_SERVER (1 << 1) -#define IPERF_FLAG_TCP (1 << 2) -#define IPERF_FLAG_UDP (1 << 3) - -#define IPERF_DEFAULT_PORT 5001 -#define IPERF_DEFAULT_INTERVAL 3 -#define IPERF_DEFAULT_TIME 30 -#define IPERF_DEFAULT_NO_BW_LIMIT -1 - -#define IPERF_TRAFFIC_TASK_NAME "iperf_traffic" -#define IPERF_TRAFFIC_TASK_PRIORITY CONFIG_IPERF_TRAFFIC_TASK_PRIORITY -#define IPERF_TRAFFIC_TASK_STACK 4096 -#define IPERF_REPORT_TASK_NAME "iperf_report" -#define IPERF_REPORT_TASK_PRIORITY CONFIG_IPERF_REPORT_TASK_PRIORITY -#define IPERF_REPORT_TASK_STACK 4096 - -#define IPERF_DEFAULT_IPV4_UDP_TX_LEN (1470) -#define IPERF_DEFAULT_IPV6_UDP_TX_LEN (1450) -#define IPERF_DEFAULT_UDP_RX_LEN (16 << 10) -#define IPERF_DEFAULT_TCP_TX_LEN (16 << 10) -#define IPERF_DEFAULT_TCP_RX_LEN (16 << 10) - -#define IPERF_MAX_DELAY 64 - -#define IPERF_SOCKET_RX_TIMEOUT CONFIG_IPERF_SOCKET_RX_TIMEOUT -#define IPERF_SOCKET_TCP_TX_TIMEOUT CONFIG_IPERF_SOCKET_TCP_TX_TIMEOUT -#define IPERF_SOCKET_ACCEPT_TIMEOUT 5 - -typedef enum { - MBITS_PER_SEC, KBITS_PER_SEC, BITS_PER_SEC -} iperf_output_format; - -typedef struct { - uint32_t flag; - union { - uint32_t destination_ip4; - char *destination_ip6; - }; - union { - uint32_t source_ip4; - char *source_ip6; - }; - uint8_t type; - uint16_t dport; - uint16_t sport; - uint32_t interval; - uint32_t time; - uint16_t len_send_buf; - int32_t bw_lim; - iperf_output_format format; -} iperf_cfg_t; - -esp_err_t iperf_start(iperf_cfg_t *cfg); - -esp_err_t iperf_stop(void); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/examples/common_components/iperf/include/wifi_stats.h b/examples/common_components/iperf/include/wifi_stats.h deleted file mode 100644 index 64b5863a09..0000000000 --- a/examples/common_components/iperf/include/wifi_stats.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - - -#if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS || CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS - -int wifi_cmd_get_tx_statistics(int argc, char **argv); -int wifi_cmd_clr_tx_statistics(int argc, char **argv); - -int wifi_cmd_get_rx_statistics(int argc, char **argv); -int wifi_cmd_clr_rx_statistics(int argc, char **argv); - -#endif - -#ifdef __cplusplus -} -#endif diff --git a/examples/common_components/iperf/iperf.c b/examples/common_components/iperf/iperf.c deleted file mode 100644 index e93d832bb9..0000000000 --- a/examples/common_components/iperf/iperf.c +++ /dev/null @@ -1,609 +0,0 @@ -/* Iperf Example - iperf implementation - - 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 "sdkconfig.h" -#include -#include -#include -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_check.h" -#include "esp_log.h" -#include "esp_rom_sys.h" -#include "esp_timer.h" -#include "iperf.h" -#include "wifi_stats.h" - - -typedef struct { - iperf_cfg_t cfg; - bool finish; - uint32_t actual_len; - uint32_t buffer_len; - uint8_t *buffer; - uint32_t sockfd; -} iperf_ctrl_t; - -static bool s_iperf_is_running = false; -static iperf_ctrl_t s_iperf_ctrl; -static const char *TAG = "iperf"; - -inline static bool iperf_is_udp_client(void) -{ - return ((s_iperf_ctrl.cfg.flag & IPERF_FLAG_CLIENT) && (s_iperf_ctrl.cfg.flag & IPERF_FLAG_UDP)); -} - -inline static bool iperf_is_udp_server(void) -{ - return ((s_iperf_ctrl.cfg.flag & IPERF_FLAG_SERVER) && (s_iperf_ctrl.cfg.flag & IPERF_FLAG_UDP)); -} - -inline static bool iperf_is_tcp_client(void) -{ - return ((s_iperf_ctrl.cfg.flag & IPERF_FLAG_CLIENT) && (s_iperf_ctrl.cfg.flag & IPERF_FLAG_TCP)); -} - -inline static bool iperf_is_tcp_server(void) -{ - return ((s_iperf_ctrl.cfg.flag & IPERF_FLAG_SERVER) && (s_iperf_ctrl.cfg.flag & IPERF_FLAG_TCP)); -} - -static int iperf_get_socket_error_code(int sockfd) -{ - return errno; -} - -static int iperf_show_socket_error_reason(const char *str, int sockfd) -{ - int err = errno; - if (err != 0) { - ESP_LOGW(TAG, "%s error, error code: %d, reason: %s", str, err, strerror(err)); - } - - return err; -} - -static void iperf_report_task(void *arg) -{ - uint32_t interval = s_iperf_ctrl.cfg.interval; - uint32_t time = s_iperf_ctrl.cfg.time; - TickType_t delay_interval = (interval * 1000) / portTICK_PERIOD_MS; - uint32_t cur = 0; - double average = 0; - double actual_bandwidth = 0; - int k = 1; - const double coefficient[3] = {1048576.0, 1024.0, 1.0}; - const char unit[3] = {'M', 'K', '\0'}; - iperf_output_format format = s_iperf_ctrl.cfg.format; - - printf("\n%16s %s\n", "Interval", "Bandwidth"); - while (!s_iperf_ctrl.finish) { - vTaskDelay(delay_interval); - actual_bandwidth = (s_iperf_ctrl.actual_len / coefficient[format] * 8) / interval; - printf("%4" PRIi32 "-%4" PRIi32 " sec %.2f %cbits/sec\n", cur, cur + interval, - actual_bandwidth, unit[format]); - cur += interval; - average = ((average * (k - 1) / k) + (actual_bandwidth / k)); - k++; - s_iperf_ctrl.actual_len = 0; - if (cur >= time) { - printf("%4d-%4" PRIu32 " sec %.2f %cbits/sec\n", 0, time, - average, unit[format]); - break; - } - } - - s_iperf_ctrl.finish = true; - vTaskDelete(NULL); -} - -static esp_err_t iperf_start_report(void) -{ - int ret; - - ret = xTaskCreatePinnedToCore(iperf_report_task, IPERF_REPORT_TASK_NAME, IPERF_REPORT_TASK_STACK, NULL, IPERF_REPORT_TASK_PRIORITY, NULL, CONFIG_FREERTOS_NUMBER_OF_CORES - 1); - - if (ret != pdPASS) { - ESP_LOGE(TAG, "create task %s failed", IPERF_REPORT_TASK_NAME); - return ESP_FAIL; - } - - return ESP_OK; -} - -static void IRAM_ATTR socket_recv(int recv_socket, struct sockaddr_storage listen_addr, uint8_t type) -{ - bool iperf_recv_start = true; - uint8_t *buffer; - int want_recv = 0; - int actual_recv = 0; -#ifdef CONFIG_LWIP_IPV6 - socklen_t socklen = (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); -#else - socklen_t socklen = sizeof(struct sockaddr_in); -#endif - const char *error_log = (type == IPERF_TRANS_TYPE_TCP) ? "tcp server recv" : "udp server recv"; - - buffer = s_iperf_ctrl.buffer; - want_recv = s_iperf_ctrl.buffer_len; - while (!s_iperf_ctrl.finish) { - actual_recv = recvfrom(recv_socket, buffer, want_recv, 0, (struct sockaddr *)&listen_addr, &socklen); - if (actual_recv < 0) { - iperf_show_socket_error_reason(error_log, recv_socket); - s_iperf_ctrl.finish = true; - break; - } else { - if (iperf_recv_start) { - iperf_start_report(); - iperf_recv_start = false; - } - s_iperf_ctrl.actual_len += actual_recv; - } - } -} - -static void IRAM_ATTR socket_send(int send_socket, struct sockaddr_storage dest_addr, uint8_t type, int bw_lim) -{ - uint8_t *buffer; - uint32_t *pkt_id_p; - uint32_t pkt_cnt = 0; - int actual_send = 0; - int want_send = 0; - int period_us = -1; - int delay_us = 0; - int64_t prev_time = 0; - int64_t send_time = 0; - int err = 0; -#ifdef CONFIG_LWIP_IPV6 - const socklen_t socklen = (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); -#else - const socklen_t socklen = sizeof(struct sockaddr_in); -#endif - const char *error_log = (type == IPERF_TRANS_TYPE_TCP) ? "tcp client send" : "udp client send"; - - buffer = s_iperf_ctrl.buffer; - pkt_id_p = (uint32_t *)s_iperf_ctrl.buffer; - want_send = s_iperf_ctrl.buffer_len; - iperf_start_report(); - - if (bw_lim > 0) { - period_us = want_send * 8 / bw_lim; - } - - while (!s_iperf_ctrl.finish) { - if (period_us > 0) { - send_time = esp_timer_get_time(); - if (actual_send > 0){ - // Last packet "send" was successful, check how much off the previous loop duration was to the ideal send period. Result will adjust the - // next send delay. - delay_us += period_us + (int32_t)(prev_time - send_time); - } else { - // Last packet "send" was not successful. Ideally we should try to catch up the whole previous loop duration (e.g. prev_time - send_time). - // However, that's not possible since the most probable reason why the send was unsuccessful is the HW was not able to process the packet. - // Hence, we cannot queue more packets with shorter (or no) delay to catch up since we are already at the performance edge. The best we - // can do is to reset the send delay (which is probably big negative number) and start all over again. - delay_us = 0; - } - prev_time = send_time; - } - *pkt_id_p = htonl(pkt_cnt++); // datagrams need to be sequentially numbered - actual_send = sendto(send_socket, buffer, want_send, 0, (struct sockaddr *)&dest_addr, socklen); - if (actual_send != want_send) { - if (type == IPERF_TRANS_TYPE_UDP) { - err = iperf_get_socket_error_code(send_socket); - // ENOMEM is expected under heavy load => do not print it - if (err != ENOMEM) { - iperf_show_socket_error_reason(error_log, send_socket); - } - } else if (type == IPERF_TRANS_TYPE_TCP) { - iperf_show_socket_error_reason(error_log, send_socket); - break; - } - } else { - s_iperf_ctrl.actual_len += actual_send; - } - // The send delay may be negative, it indicates we are trying to catch up and hence to not delay the loop at all. - if (delay_us > 0) { - esp_rom_delay_us(delay_us); - } - } -} - -static esp_err_t iperf_run_tcp_server(void) -{ - int listen_socket = -1; - int client_socket = -1; - int opt = 1; - int err = 0; - esp_err_t ret = ESP_OK; - struct sockaddr_in remote_addr; - struct timeval timeout = { 0 }; - socklen_t addr_len = sizeof(struct sockaddr); - struct sockaddr_storage listen_addr = { 0 }; - - struct sockaddr_in listen_addr4 = { 0 }; -#ifdef CONFIG_LWIP_IPV6 - struct sockaddr_in6 listen_addr6 = { 0 }; - ESP_GOTO_ON_FALSE((s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6 || s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4), ESP_FAIL, exit, TAG, "Ivalid AF types"); -#else - ESP_GOTO_ON_FALSE((s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4), ESP_FAIL, exit, TAG, "Invalid AF types"); -#endif -#ifdef CONFIG_LWIP_IPV6 - if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) { - // The TCP server listen at the address "::", which means all addresses can be listened to. - inet6_aton("::", &listen_addr6.sin6_addr); - listen_addr6.sin6_family = AF_INET6; - listen_addr6.sin6_port = htons(s_iperf_ctrl.cfg.sport); - - listen_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_IPV6); - ESP_GOTO_ON_FALSE((listen_socket >= 0), ESP_FAIL, exit, TAG, "Unable to create socket: errno %d", errno); - - setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - setsockopt(listen_socket, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); - - ESP_LOGI(TAG, "Socket created"); - - err = bind(listen_socket, (struct sockaddr *)&listen_addr6, sizeof(listen_addr6)); - ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to bind: errno %d, IPPROTO: %d", errno, AF_INET6); - err = listen(listen_socket, 1); - ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Error occurred during listen: errno %d", errno); - - memcpy(&listen_addr, &listen_addr6, sizeof(listen_addr6)); - } else -#endif - if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4) { - listen_addr4.sin_family = AF_INET; - listen_addr4.sin_port = htons(s_iperf_ctrl.cfg.sport); - listen_addr4.sin_addr.s_addr = s_iperf_ctrl.cfg.source_ip4; - - listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - ESP_GOTO_ON_FALSE((listen_socket >= 0), ESP_FAIL, exit, TAG, "Unable to create socket: errno %d", errno); - - setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - - ESP_LOGI(TAG, "Socket created"); - - err = bind(listen_socket, (struct sockaddr *)&listen_addr4, sizeof(listen_addr4)); - ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to bind: errno %d, IPPROTO: %d", errno, AF_INET); - - err = listen(listen_socket, 5); - ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Error occurred during listen: errno %d", errno); - memcpy(&listen_addr, &listen_addr4, sizeof(listen_addr4)); - } - - timeout.tv_sec = IPERF_SOCKET_RX_TIMEOUT; - setsockopt(listen_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - client_socket = accept(listen_socket, (struct sockaddr *)&remote_addr, &addr_len); - ESP_GOTO_ON_FALSE((client_socket >= 0), ESP_FAIL, exit, TAG, "Unable to accept connection: errno %d", errno); - ESP_LOGI(TAG, "accept: %s,%d", inet_ntoa(remote_addr.sin_addr), htons(remote_addr.sin_port)); - - timeout.tv_sec = IPERF_SOCKET_RX_TIMEOUT; - setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - -#if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS - wifi_cmd_clr_tx_statistics(0, NULL); -#endif -#if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS - wifi_cmd_clr_rx_statistics(0, NULL); -#endif - socket_recv(client_socket, listen_addr, IPERF_TRANS_TYPE_TCP); -#if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS - wifi_cmd_get_rx_statistics(0, NULL); -#endif -#if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS - wifi_cmd_get_tx_statistics(0, NULL); -#endif - -exit: - if (client_socket != -1) { - close(client_socket); - } - - if (listen_socket != -1) { - shutdown(listen_socket, 0); - close(listen_socket); - ESP_LOGI(TAG, "TCP Socket server is closed."); - } - s_iperf_ctrl.finish = true; - return ret; -} - -static esp_err_t iperf_run_tcp_client(void) -{ - int client_socket = -1; - int err = 0; - esp_err_t ret = ESP_OK; - struct sockaddr_storage dest_addr = { 0 }; - struct sockaddr_in dest_addr4 = { 0 }; - struct timeval timeout = { 0 }; -#ifdef CONFIG_LWIP_IPV6 - struct sockaddr_in6 dest_addr6 = { 0 }; - ESP_GOTO_ON_FALSE((s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6 || s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4), ESP_FAIL, exit, TAG, "Ivalid AF types"); -#else - ESP_GOTO_ON_FALSE((s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4), ESP_FAIL, exit, TAG, "Invalid AF types"); -#endif -#ifdef CONFIG_LWIP_IPV6 - if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) { - client_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_IPV6); - ESP_GOTO_ON_FALSE((client_socket >= 0), ESP_FAIL, exit, TAG, "Unable to create socket: errno %d", errno); - - inet6_aton(s_iperf_ctrl.cfg.destination_ip6, &dest_addr6.sin6_addr); - dest_addr6.sin6_family = AF_INET6; - dest_addr6.sin6_port = htons(s_iperf_ctrl.cfg.dport); - - err = connect(client_socket, (struct sockaddr *)&dest_addr6, sizeof(struct sockaddr_in6)); - ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to connect: errno %d", errno); - ESP_LOGI(TAG, "Successfully connected"); - memcpy(&dest_addr, &dest_addr6, sizeof(dest_addr6)); - } else -#endif - if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4) { - client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - ESP_GOTO_ON_FALSE((client_socket >= 0), ESP_FAIL, exit, TAG, "Unable to create socket: errno %d", errno); - - dest_addr4.sin_family = AF_INET; - dest_addr4.sin_port = htons(s_iperf_ctrl.cfg.dport); - dest_addr4.sin_addr.s_addr = s_iperf_ctrl.cfg.destination_ip4; - err = connect(client_socket, (struct sockaddr *)&dest_addr4, sizeof(struct sockaddr_in)); - ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to connect: errno %d", errno); - ESP_LOGI(TAG, "Successfully connected"); - memcpy(&dest_addr, &dest_addr4, sizeof(dest_addr4)); - } - timeout.tv_sec = IPERF_SOCKET_TCP_TX_TIMEOUT; - setsockopt(client_socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); - -#if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS - wifi_cmd_clr_rx_statistics(0, NULL); -#endif -#if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS - wifi_cmd_clr_tx_statistics(0, NULL); -#endif - socket_send(client_socket, dest_addr, IPERF_TRANS_TYPE_TCP, s_iperf_ctrl.cfg.bw_lim); -#if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS - wifi_cmd_get_rx_statistics(0, NULL); -#endif -#if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS - wifi_cmd_get_tx_statistics(0, NULL); -#endif - -exit: - if (client_socket != -1) { - shutdown(client_socket, 0); - close(client_socket); - ESP_LOGI(TAG, "TCP Socket client is closed."); - } - s_iperf_ctrl.finish = true; - return ret; -} - -static esp_err_t iperf_run_udp_server(void) -{ - int listen_socket = -1; - int opt = 1; - int err = 0; - esp_err_t ret = ESP_OK; - struct timeval timeout = { 0 }; - struct sockaddr_storage listen_addr = { 0 }; - struct sockaddr_in listen_addr4 = { 0 }; -#ifdef CONFIG_LWIP_IPV6 - struct sockaddr_in6 listen_addr6 = { 0 }; - ESP_GOTO_ON_FALSE((s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6 || s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4), ESP_FAIL, exit, TAG, "Ivalid AF types"); -#else - ESP_GOTO_ON_FALSE((s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4), ESP_FAIL, exit, TAG, "Ivalid AF types"); -#endif -#ifdef CONFIG_LWIP_IPV6 - if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) { - // The UDP server listen at the address "::", which means all addresses can be listened to. - inet6_aton("::", &listen_addr6.sin6_addr); - listen_addr6.sin6_family = AF_INET6; - listen_addr6.sin6_port = htons(s_iperf_ctrl.cfg.sport); - - listen_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); - ESP_GOTO_ON_FALSE((listen_socket >= 0), ESP_FAIL, exit, TAG, "Unable to create socket: errno %d", errno); - ESP_LOGI(TAG, "Socket created"); - - setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - - err = bind(listen_socket, (struct sockaddr *)&listen_addr6, sizeof(struct sockaddr_in6)); - ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to bind: errno %d", errno); - ESP_LOGI(TAG, "Socket bound, port %" PRIu16, listen_addr6.sin6_port); - - memcpy(&listen_addr, &listen_addr6, sizeof(listen_addr6)); - } else -#endif - if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4) { - listen_addr4.sin_family = AF_INET; - listen_addr4.sin_port = htons(s_iperf_ctrl.cfg.sport); - listen_addr4.sin_addr.s_addr = s_iperf_ctrl.cfg.source_ip4; - - listen_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - ESP_GOTO_ON_FALSE((listen_socket >= 0), ESP_FAIL, exit, TAG, "Unable to create socket: errno %d", errno); - ESP_LOGI(TAG, "Socket created"); - - setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - - err = bind(listen_socket, (struct sockaddr *)&listen_addr4, sizeof(struct sockaddr_in)); - ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to bind: errno %d", errno); - ESP_LOGI(TAG, "Socket bound, port %d", listen_addr4.sin_port); - memcpy(&listen_addr, &listen_addr4, sizeof(listen_addr4)); - } - - timeout.tv_sec = IPERF_SOCKET_RX_TIMEOUT; - setsockopt(listen_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); -#if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS - wifi_cmd_clr_rx_statistics(0, NULL); -#endif - socket_recv(listen_socket, listen_addr, IPERF_TRANS_TYPE_UDP); -#if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS - wifi_cmd_get_rx_statistics(0, NULL); -#endif -exit: - if (listen_socket != -1) { - shutdown(listen_socket, 0); - close(listen_socket); - } - ESP_LOGI(TAG, "Udp socket server is closed."); - s_iperf_ctrl.finish = true; - return ret; -} - -static esp_err_t iperf_run_udp_client(void) -{ - int client_socket = -1; - int opt = 1; - esp_err_t ret = ESP_OK; - struct sockaddr_storage dest_addr = { 0 }; - struct sockaddr_in dest_addr4 = { 0 }; -#ifdef CONFIG_LWIP_IPV6 - struct sockaddr_in6 dest_addr6 = { 0 }; - ESP_GOTO_ON_FALSE((s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6 || s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4), ESP_FAIL, exit, TAG, "Ivalid AF types"); -#else - ESP_GOTO_ON_FALSE((s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4), ESP_FAIL, exit, TAG, "Ivalid AF types"); -#endif -#ifdef CONFIG_LWIP_IPV6 - if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) { - inet6_aton(s_iperf_ctrl.cfg.destination_ip6, &dest_addr6.sin6_addr); - dest_addr6.sin6_family = AF_INET6; - dest_addr6.sin6_port = htons(s_iperf_ctrl.cfg.dport); - - client_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IPV6); - ESP_GOTO_ON_FALSE((client_socket >= 0), ESP_FAIL, exit, TAG, "Unable to create socket: errno %d", errno); - ESP_LOGI(TAG, "Socket created, sending to %s:%" PRIu16, s_iperf_ctrl.cfg.destination_ip6, s_iperf_ctrl.cfg.dport); - - setsockopt(client_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - memcpy(&dest_addr, &dest_addr6, sizeof(dest_addr6)); - } else -#endif - if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV4) { - dest_addr4.sin_family = AF_INET; - dest_addr4.sin_port = htons(s_iperf_ctrl.cfg.dport); - dest_addr4.sin_addr.s_addr = s_iperf_ctrl.cfg.destination_ip4; - - client_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - ESP_GOTO_ON_FALSE((client_socket >= 0), ESP_FAIL, exit, TAG, "Unable to create socket: errno %d", errno); - ESP_LOGI(TAG, "Socket created, sending to %d.%d.%d.%d:%" PRIu16, - (uint16_t) s_iperf_ctrl.cfg.destination_ip4 & 0xFF, - (uint16_t) (s_iperf_ctrl.cfg.destination_ip4 >> 8) & 0xFF, - (uint16_t) (s_iperf_ctrl.cfg.destination_ip4 >> 16) & 0xFF, - (uint16_t) (s_iperf_ctrl.cfg.destination_ip4 >> 24) & 0xFF, - s_iperf_ctrl.cfg.dport); - - setsockopt(client_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - memcpy(&dest_addr, &dest_addr4, sizeof(dest_addr4)); - } -#if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS - wifi_cmd_clr_tx_statistics(0, NULL); -#endif - socket_send(client_socket, dest_addr, IPERF_TRANS_TYPE_UDP, s_iperf_ctrl.cfg.bw_lim); -#if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS - wifi_cmd_get_tx_statistics(0, NULL); -#endif -exit: - if (client_socket != -1) { - shutdown(client_socket, 0); - close(client_socket); - } - s_iperf_ctrl.finish = true; - ESP_LOGI(TAG, "UDP Socket client is closed"); - return ret; -} - -static void iperf_task_traffic(void *arg) -{ - if (iperf_is_udp_client()) { - iperf_run_udp_client(); - } else if (iperf_is_udp_server()) { - iperf_run_udp_server(); - } else if (iperf_is_tcp_client()) { - iperf_run_tcp_client(); - } else { - iperf_run_tcp_server(); - } - - if (s_iperf_ctrl.buffer) { - free(s_iperf_ctrl.buffer); - s_iperf_ctrl.buffer = NULL; - } - ESP_LOGI(TAG, "iperf exit"); - s_iperf_is_running = false; - vTaskDelete(NULL); -} - -static uint32_t iperf_get_buffer_len(void) -{ - if (iperf_is_udp_client()) { -#ifdef CONFIG_LWIP_IPV6 - if (s_iperf_ctrl.cfg.len_send_buf) { - return s_iperf_ctrl.cfg.len_send_buf; - } else if (s_iperf_ctrl.cfg.type == IPERF_IP_TYPE_IPV6) { - return IPERF_DEFAULT_IPV6_UDP_TX_LEN; - } else { - return IPERF_DEFAULT_IPV4_UDP_TX_LEN; - } -#else - return (s_iperf_ctrl.cfg.len_send_buf == 0 ? IPERF_DEFAULT_IPV4_UDP_TX_LEN : s_iperf_ctrl.cfg.len_send_buf); -#endif - } else if (iperf_is_udp_server()) { - return IPERF_DEFAULT_UDP_RX_LEN; - } else if (iperf_is_tcp_client()) { - return (s_iperf_ctrl.cfg.len_send_buf == 0 ? IPERF_DEFAULT_TCP_TX_LEN : s_iperf_ctrl.cfg.len_send_buf); - } else { - return IPERF_DEFAULT_TCP_RX_LEN; - } - return 0; -} - -esp_err_t iperf_start(iperf_cfg_t *cfg) -{ - BaseType_t ret; - - if (!cfg) { - return ESP_FAIL; - } - - if (s_iperf_is_running) { - ESP_LOGW(TAG, "iperf is running"); - return ESP_FAIL; - } - - memset(&s_iperf_ctrl, 0, sizeof(s_iperf_ctrl)); - memcpy(&s_iperf_ctrl.cfg, cfg, sizeof(*cfg)); - s_iperf_is_running = true; - s_iperf_ctrl.finish = false; - s_iperf_ctrl.buffer_len = iperf_get_buffer_len(); - s_iperf_ctrl.buffer = (uint8_t *)malloc(s_iperf_ctrl.buffer_len); - if (!s_iperf_ctrl.buffer) { - ESP_LOGE(TAG, "create buffer: not enough memory"); - return ESP_FAIL; - } - memset(s_iperf_ctrl.buffer, 0, s_iperf_ctrl.buffer_len); - ret = xTaskCreatePinnedToCore(iperf_task_traffic, IPERF_TRAFFIC_TASK_NAME, IPERF_TRAFFIC_TASK_STACK, NULL, IPERF_TRAFFIC_TASK_PRIORITY, NULL, CONFIG_FREERTOS_NUMBER_OF_CORES - 1); - if (ret != pdPASS) { - ESP_LOGE(TAG, "create task %s failed", IPERF_TRAFFIC_TASK_NAME); - free(s_iperf_ctrl.buffer); - s_iperf_ctrl.buffer = NULL; - return ESP_FAIL; - } - return ESP_OK; -} - -esp_err_t iperf_stop(void) -{ - if (s_iperf_is_running) { - s_iperf_ctrl.finish = true; - } - - while (s_iperf_is_running) { - ESP_LOGI(TAG, "wait current iperf to stop ..."); - vTaskDelay(300 / portTICK_PERIOD_MS); - } - return ESP_OK; -} diff --git a/examples/common_components/iperf/wifi_cmd.c b/examples/common_components/iperf/wifi_cmd.c deleted file mode 100644 index 7c4fb277ef..0000000000 --- a/examples/common_components/iperf/wifi_cmd.c +++ /dev/null @@ -1,1095 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include "esp_log.h" -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" -#include "ping/ping_sock.h" -#include "lwip/inet.h" -#include "lwip/netdb.h" -#include "esp_mac.h" -#include "wifi_cmd.h" - -#if CONFIG_SOC_WIFI_HE_SUPPORT - -#include "esp_console.h" -#include "argtable3/argtable3.h" -#include "esp_netif.h" -#include "esp_event.h" -#include "esp_wifi.h" -#include "esp_wifi_types.h" -#include "esp_wifi_he.h" -#include "esp_private/esp_wifi_he_private.h" - - -/******************************************************* - * Macros - *******************************************************/ -/* - * enable/disable rx/tx statistics after wifi started: - * (1) esp_wifi_enable_rx_statistics(true, true); - * (2) esp_wifi_enable_tx_statistics(ESP_WIFI_ACI_BE, true); - */ -#define VAR2IPSTR(var) (uint16_t) var & 0xFF, \ - (uint16_t) (var >> 8) & 0xFF, \ - (uint16_t) (var >> 16) & 0xFF, \ - (uint16_t) (var >> 24) & 0xFF - - -/******************************************************* - * Constants - *******************************************************/ -static const char *TAG = "cmd"; - -/******************************************************* - * Structures - *******************************************************/ -typedef struct { - struct arg_dbl *read; - struct arg_dbl *write; - struct arg_dbl *value; - struct arg_end *end; -} reg_rw_args_t; - -typedef struct { - struct arg_dbl *disable; - struct arg_end *end; -} wifi_stbc_args_t; - -typedef struct { - struct arg_dbl *disable; - struct arg_end *end; -} wifi_bmfmee_args_t; - -typedef struct { - struct arg_dbl *ul_mu_disable; - struct arg_dbl *ul_mu_data_disable; - struct arg_dbl *ersu_disable; - struct arg_dbl *report; - struct arg_dbl *resounding; - struct arg_end *end; -} wifi_omctrl_args_t; - -typedef struct { - struct arg_dbl *tf_padding; - struct arg_end *end; -} wifi_tf_padding_args_t; - -typedef struct { - struct arg_int *enable; - struct arg_int *txq; - struct arg_end *end; -} wifi_edca_args_t; - -typedef struct { - struct arg_int *reset_timer; - struct arg_int *read_timer; - struct arg_end *end; -} wifi_muedca_args_t; - -typedef struct { - struct arg_dbl *enable; - struct arg_end *end; -} wifi_cca_ignore_args_t; - -typedef struct { - struct arg_dbl *timeout; - struct arg_dbl *interval; - struct arg_int *data_size; - struct arg_int *count; - struct arg_int *tos; - struct arg_str *host; - struct arg_lit *abort; - struct arg_end *end; -} wifi_ping_args_t; - -typedef struct { - struct arg_str *ip; - struct arg_str *gw; - struct arg_str *netmask; - struct arg_end *end; -} static_ip_args_t; - -typedef struct { - struct arg_str *proto; - struct arg_end *end; -} wifi_proto_args_t; - -typedef struct { - struct arg_int *val; - struct arg_end *end; -} wifi_inactive_time_args_t; - -typedef struct { - struct arg_int *format; - struct arg_int *rate; - struct arg_end *end; -} wifi_sounding_rate_t; - -typedef struct { - struct arg_int *mcs; - struct arg_int *power; - struct arg_end *end; -} wifi_tx_pwr_t; - -/******************************************************* - * Variable Definitions - *******************************************************/ -static reg_rw_args_t reg_rw_args; -static wifi_stbc_args_t stbc_args; -static wifi_bmfmee_args_t bmfmee_args; -static wifi_omctrl_args_t omctrl_args; -static wifi_tf_padding_args_t tf_padding_args; -static wifi_edca_args_t edca_args; -static wifi_cca_ignore_args_t cca_args; -static wifi_ping_args_t ping_args; -static static_ip_args_t static_ip_args; -static wifi_proto_args_t proto_args; -static wifi_inactive_time_args_t inactive_time_args; -static wifi_sounding_rate_t wifi_sounding_rate_args; -static wifi_muedca_args_t muedca_args; -static wifi_tx_pwr_t tx_pwr_args; - - -extern esp_netif_t *netif_ap; -extern esp_netif_t *netif_sta; -extern EventGroupHandle_t wifi_event_group; -extern const int CONNECTED_BIT; - -/******************************************************* - * Function Declarations - *******************************************************/ - -/******************************************************* - * Function Definitions - *******************************************************/ -static int wifi_cmd_get_mac(int argc, char **argv) -{ - uint8_t mac[6] = { 0, }; - if (esp_wifi_get_mac(WIFI_IF_STA, mac) == ESP_OK) { - ESP_LOGW(TAG, "sta mac: " MACSTR "", MAC2STR(mac)); - } - if (esp_wifi_get_mac(WIFI_IF_AP, mac) == ESP_OK) { - ESP_LOGW(TAG, "ap mac: " MACSTR "", MAC2STR(mac)); - } - return 0; -} - -static int wifi_cmd_set_omc(int argc, char **argv) -{ - //TODO ER-SU - esp_wifi_htc_omc_t omc = { 0, }; - esp_err_t err = ESP_OK; - int nerrors = arg_parse(argc, argv, (void **) &omctrl_args); - if (nerrors != 0) { - arg_print_errors(stderr, omctrl_args.end, argv[0]); - return 1; - } - do { - if (!omctrl_args.ul_mu_disable->count && !omctrl_args.ul_mu_data_disable->count) { - omc.ul_mu_disable = 1; - omc.ul_mu_data_disable = 0; - break; - } - /* parse inputs */ - if (omctrl_args.ul_mu_disable->count) { - omc.ul_mu_disable = omctrl_args.ul_mu_disable->dval[0]; - } - if (omctrl_args.ul_mu_data_disable->count) { - omc.ul_mu_data_disable = omctrl_args.ul_mu_data_disable->dval[0]; - } - if (omctrl_args.ersu_disable->count) { - omc.er_su_disable = omctrl_args.ersu_disable->dval[0]; - } - if (omctrl_args.resounding->count) { - omc.dl_mu_mimo_resounding_recommendation = omctrl_args.resounding->dval[0]; - } - } while (0); - - if (omctrl_args.report->count && omctrl_args.report->dval[0] == 0) { - /* not report to ap the om control */ - hal_he_set_ul_mu(omc.ul_mu_disable, omc.ul_mu_data_disable); - ESP_LOGW(TAG, "(omc)(internal)disable ul mu(%d, data:%d) successfully", omc.ul_mu_disable, - omc.ul_mu_data_disable); - } else { - err = esp_wifi_set_htc_omc(&omc); - if (err != ESP_OK) { - ESP_LOGW(TAG, "(omc)disable ul mu(%d, data:%d) failed, err:0x%x", omc.ul_mu_disable, omc.ul_mu_data_disable, - err); - } else { - ESP_LOGW(TAG, "(omc)disable ul mu(%d, data:%d) successfully", omc.ul_mu_disable, omc.ul_mu_data_disable); - } - } - return 0; -} - -static int wifi_cmd_edca_tx(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **) &edca_args); - if (nerrors != 0) { - arg_print_errors(stderr, edca_args.end, argv[0]); - return 1; - } - - int txq = 2; - if (!edca_args.enable->count && !edca_args.txq->count) { - esp_test_disable_edca_tx(txq); - ESP_LOGW(TAG, "(tx)disable edca, txq[%d]", txq); - return 0; - } - txq = edca_args.txq->count ? edca_args.txq->ival[0] : txq; - if (edca_args.enable->ival[0] == 0) { - esp_test_disable_edca_tx(txq); - ESP_LOGW(TAG, "(tx)disable edca, txq[%d]", txq); - } else { - esp_test_enable_edca_tx(txq); - ESP_LOGW(TAG, "(tx)enable edca, txq[%d]", txq); - } - return 0; -} - -static int wifi_cmd_reg_rw(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **) ®_rw_args); - uint32_t addr; - if (nerrors != 0) { - arg_print_errors(stderr, reg_rw_args.end, argv[0]); - return 1; - } - if (reg_rw_args.read->count) { - addr = (uint32_t) reg_rw_args.read->dval[0]; - ESP_LOGW(TAG, "reg read 0x%08lx : 0x%08lx", addr, REG_READ(addr)); - } else if (reg_rw_args.write->count && (uint32_t) reg_rw_args.value->count) { - addr = (uint32_t) reg_rw_args.write->dval[0]; - ESP_LOGW(TAG, "reg write 0x%8lx : 0x%8lx", addr, (uint32_t) reg_rw_args.value->dval[0]); - REG_WRITE(addr, (uint32_t ) reg_rw_args.value->dval[0]); - ESP_LOGW(TAG, "reg read 0x%08lx : 0x%08lx", addr, REG_READ(addr)); - } else { - printf("Input Error\n"); - } - return 0; -} - -static int wifi_cmd_set_tf_padding(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **) &tf_padding_args); - if (nerrors != 0) { - arg_print_errors(stderr, tf_padding_args.end, argv[0]); - return 1; - } - if (tf_padding_args.tf_padding->count) { - esp_wifi_set_tf_padding_duration((int)tf_padding_args.tf_padding->dval[0]); - ESP_LOGW(TAG, "(test)set trigger frame mac padding duration:%d", (int)tf_padding_args.tf_padding->dval[0]); - } else { - printf("Input Error\n"); - } - return 0; -} - -static int wifi_cmd_tb(int argc, char **argv) -{ - dbg_read_axtb_diag(); - dbg_read_ax_diag(1); - return 0; -} - -static int wifi_cmd_stbc(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **) &stbc_args); - if (nerrors != 0) { - arg_print_errors(stderr, stbc_args.end, argv[0]); - return 1; - } - if (stbc_args.disable->count) { - esp_wifi_enable_rx_stbc(0); - ESP_LOGI(TAG, "(cfg)disable he stbc"); - } else { - esp_wifi_enable_rx_stbc(1); - ESP_LOGI(TAG, "(cfg)enable he stbc"); - } - return 0; -} - -static int wifi_cmd_su_bmfmee(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **) &bmfmee_args); - if (nerrors != 0) { - arg_print_errors(stderr, bmfmee_args.end, argv[0]); - return 1; - } - if (bmfmee_args.disable->count) { - esp_wifi_enable_su_bmfmee(0); - ESP_LOGI(TAG, "(cfg)disable he su bmfmee"); - } else { - esp_wifi_enable_su_bmfmee(1); - ESP_LOGI(TAG, "(cfg)enable he su bmfmee"); - } - return 0; -} - -static int wifi_cmd_ignore_cca(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **) &cca_args); - if (nerrors != 0) { - arg_print_errors(stderr, cca_args.end, argv[0]); - return 1; - } - if (cca_args.enable->count) { - dbg_tb_ignore_cca_enable(1); - } else { - dbg_tb_ignore_cca_enable(0); - } - return 0; -} - -static int wifi_cmd_set_ps_type(int argc, char **argv) -{ - ESP_LOGW(TAG, "set to WIFI_PS_MIN_MODEM"); - ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM)); - return 0; -} - -static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args) -{ - uint8_t ttl; - uint16_t seqno; - uint32_t elapsed_time, recv_len; - ip_addr_t target_addr; - esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); - esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl)); - esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); - esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len)); - esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time)); - -#ifdef CONFIG_LWIP_IPV6 - printf("%" PRIu32 " bytes from %s icmp_seq=%d ttl=%d time=%" PRIu32 " ms\n", - recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time); -#else - printf("%" PRIu32 " bytes from %s icmp_seq=%d ttl=%d time=%" PRIu32 " ms\n", - recv_len, inet_ntoa(target_addr.addr), seqno, ttl, elapsed_time); -#endif - -} - -static void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl, void *args) -{ - uint16_t seqno; - ip_addr_t target_addr; - esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); - esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); -#ifdef CONFIG_LWIP_IPV6 - printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno); -#else - printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.addr), seqno); -#endif -} - -static void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args) -{ - ip_addr_t target_addr; - uint32_t transmitted; - uint32_t received; - uint32_t total_time_ms; - esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted)); - esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received)); - esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); - esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms)); - uint32_t loss = (uint32_t)((1 - ((float)received) / transmitted) * 100); - if (IP_IS_V4(&target_addr)) { - printf("\n--- %s ping statistics ---\n", inet_ntoa(*ip_2_ip4(&target_addr))); - } -#ifdef CONFIG_LWIP_IPV6 - else { - printf("\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&target_addr))); - } -#endif - printf("%" PRIu32 " packets transmitted, %" PRIu32 " received, %" PRIu32 "%% packet loss, time %" PRIu32 "ms\n", - transmitted, received, loss, total_time_ms); - // delete the ping sessions, so that we clean up all resources and can create a new ping session - // we don't have to call delete function in the callback, instead we can call delete function from other tasks - esp_ping_delete_session(hdl); -} - -static int do_ping_cmd(int argc, char **argv) -{ - esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG(); - static esp_ping_handle_t ping; - int nerrors = arg_parse(argc, argv, (void **)&ping_args); - if (nerrors != 0) { - arg_print_errors(stderr, ping_args.end, argv[0]); - return 1; - } - - if (ping_args.timeout->count > 0) { - config.timeout_ms = (uint32_t)(ping_args.timeout->dval[0] * 1000); - } - - if (ping_args.interval->count > 0) { - config.interval_ms = (uint32_t)(ping_args.interval->dval[0] * 1000); - } - - if (ping_args.data_size->count > 0) { - config.data_size = (uint32_t)(ping_args.data_size->ival[0]); - } - - if (ping_args.count->count > 0) { - config.count = (uint32_t)(ping_args.count->ival[0]); - } - - if (ping_args.tos->count > 0) { - config.tos = (uint32_t)(ping_args.tos->ival[0]); - } - - if (ping_args.abort->count) { - esp_ping_stop(ping); - return 0; - } - - // parse IP address - ip_addr_t target_addr; - struct addrinfo hint; - struct addrinfo *res = NULL; - memset(&hint, 0, sizeof(hint)); - memset(&target_addr, 0, sizeof(target_addr)); - /* convert domain name to IP address */ - if (getaddrinfo(ping_args.host->sval[0], NULL, &hint, &res) != 0) { - printf("ping: unknown host %s\n", ping_args.host->sval[0]); - return 1; - } - if (res->ai_family == AF_INET) { - struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr; - inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); - } -#ifdef CONFIG_LWIP_IPV6 - else { - struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr; - inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6); - } -#endif - freeaddrinfo(res); - config.target_addr = target_addr; - - /* set callback functions */ - esp_ping_callbacks_t cbs = { - .on_ping_success = cmd_ping_on_ping_success, - .on_ping_timeout = cmd_ping_on_ping_timeout, - .on_ping_end = cmd_ping_on_ping_end, - .cb_args = NULL - }; - - esp_ping_new_session(&config, &cbs, &ping); - esp_ping_start(ping); - return 0; -} - -extern bool pm_is_waked(void); -extern bool pm_is_sleeping(void); -extern bool pm_is_dream(void); -static int wifi_cmd_get_ps_state(int argc, char **argv) -{ - ESP_LOGW(TAG, "ps: awake:%d, sleep:%d, dream:%d", pm_is_waked(), pm_is_sleeping(), pm_is_dream()); - return 0; -} - -esp_err_t esp_netif_set_static_ip(esp_netif_t *netif_sta, uint32_t ip, uint32_t gw, - uint32_t netmask) -{ - esp_netif_dhcpc_stop(netif_sta); - esp_netif_ip_info_t ip_info; - esp_netif_set_ip4_addr(&ip_info.ip, ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, - (ip >> 24) & 0xFF); - esp_netif_set_ip4_addr(&ip_info.gw, gw & 0xFF, (gw >> 8) & 0xFF, (gw >> 16) & 0xFF, - (gw >> 24) & 0xFF); - esp_netif_set_ip4_addr(&ip_info.netmask, netmask & 0xFF, (netmask >> 8) & 0xFF, - (netmask >> 16) & 0xFF, (netmask >> 24) & 0xFF); - esp_netif_set_ip_info(netif_sta, &ip_info); - return ESP_OK; -} - -static int wifi_cmd_set_ip(int argc, char **argv) -{ - uint32_t ip = 0, gw = 0, netmask = 0; - int nerrors = arg_parse(argc, argv, (void **) &static_ip_args); - if (nerrors != 0) { - arg_print_errors(stderr, static_ip_args.end, argv[0]); - return 0; - } - if (static_ip_args.ip->count != 0) { - ip = esp_ip4addr_aton(static_ip_args.ip->sval[0]); - } - if (static_ip_args.gw->count != 0) { - gw = esp_ip4addr_aton(static_ip_args.gw->sval[0]); - } - if (static_ip_args.netmask->count != 0) { - netmask = esp_ip4addr_aton(static_ip_args.netmask->sval[0]); - } - if (!ip || !netmask) { - return 0; - } - /* set static IP settings */ - esp_netif_set_static_ip(netif_sta, ip, gw, netmask); - - ESP_LOGD(TAG, "ip:%d.%d.%d.%d, gateway:%d.%d.%d.%d, netmask:%d.%d.%d.%d,", - VAR2IPSTR(ip), VAR2IPSTR(gw), VAR2IPSTR(netmask)); - return 0; -} - -void wifi_get_local_ip(esp_netif_ip_info_t *ip_info) -{ - int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); - esp_netif_t *netif = netif_ap; - 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"); - } - } - esp_netif_get_ip_info(netif, ip_info); -} - -static int wifi_cmd_query(int argc, char **argv) -{ - wifi_config_t cfg; - wifi_bandwidth_t cbw; - uint8_t mac[6]; - esp_netif_ip_info_t ip_info = { 0, }; - - wifi_mode_t mode; - esp_wifi_get_mode(&mode); - wifi_get_local_ip(&ip_info); - bool is_sta_disconnect = false; - char temp_ssid[33] = { 0 }; - printf("Wireless info:"); - if (WIFI_MODE_AP == mode) { - esp_wifi_get_config(WIFI_IF_AP, &cfg); - esp_wifi_get_bandwidth(WIFI_IF_AP, &cbw); - - printf("\n"); - printf("\tmode: ap\n"); - strncpy(temp_ssid, (char *) cfg.ap.ssid, 32); - printf("\tssid: %s\n", temp_ssid); - printf("\tpassword: %s\n", cfg.ap.password); - printf("\tchannel: %d\n", cfg.ap.channel); - if (cbw == WIFI_BW_HT20) { - printf("\tcbw: 20 MHz\n"); - } else if (cbw == WIFI_BW_HT40) { - printf("\tcbw: 40 MHz\n"); - } - - if (esp_wifi_get_mac(WIFI_IF_AP, mac) == ESP_OK) { - printf("\tap mac: "MACSTR, MAC2STR(mac)); - printf("\n"); - } - printf("\tip: %d.%d.%d.%d\n", VAR2IPSTR(ip_info.ip.addr)); - printf("\tnetmask: %d.%d.%d.%d\n", VAR2IPSTR(ip_info.netmask.addr)); - printf("\tgateway: %d.%d.%d.%d\n", VAR2IPSTR(ip_info.gw.addr)); - printf("\n"); - } else if (WIFI_MODE_STA == mode) { - int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); - if (bits & CONNECTED_BIT) { - is_sta_disconnect = false; - esp_wifi_get_config(WIFI_IF_STA, &cfg); - esp_wifi_get_bandwidth(WIFI_IF_STA, &cbw); - printf("\n"); - printf("\tmode: station\n"); - printf("\tstatus: connected\n"); - strncpy(temp_ssid, (char *) cfg.sta.ssid, 32); - printf("\tssid: %s\n", temp_ssid); - printf("\tbssid: "MACSTR, MAC2STR(cfg.sta.bssid)); - printf("\n"); - printf("\tchannel: %d\n", cfg.sta.channel); - uint16_t aid; - esp_wifi_sta_get_aid(&aid); - printf("\taid: %d\n", aid); - if (cfg.sta.pmf_cfg.capable) { - if (cfg.sta.pmf_cfg.required) { - printf("\tpmf: required\n"); - } else { - printf("\tpmf: optional\n"); - } - } else { - printf("\tpmf: disabled\n"); - } - if (cbw == WIFI_BW_HT20) { - printf("\tcbw: 20 MHz\n"); - } else if (cbw == WIFI_BW_HT40) { - printf("\tcbw: 40 MHz\n"); - } - - if (esp_wifi_get_mac(WIFI_IF_STA, mac) == ESP_OK) { - printf("\tsta mac: "MACSTR, MAC2STR(mac)); - printf("\n"); - } - printf("\tip: %d.%d.%d.%d\n", VAR2IPSTR(ip_info.ip.addr)); - printf("\tnetmask: %d.%d.%d.%d\n", VAR2IPSTR(ip_info.netmask.addr)); - printf("\tgateway: %d.%d.%d.%d\n", VAR2IPSTR(ip_info.gw.addr)); - printf("\n"); - - } else { - printf("\n"); - printf("\tmode: disconnected\n"); - is_sta_disconnect = true; - } - } - if (WIFI_MODE_NULL == mode || is_sta_disconnect) { - printf("\n"); - if (WIFI_MODE_NULL == mode) { - printf("\tmode: null\n"); - } - if (esp_wifi_get_mac(WIFI_IF_AP, mac) == ESP_OK) { - printf("\tap mac: "MACSTR, MAC2STR(mac)); - printf("\n"); - } - - if (esp_wifi_get_mac(WIFI_IF_STA, mac) == ESP_OK) { - printf("\tsta mac: "MACSTR, MAC2STR(mac)); - printf("\n"); - } - return 0; - } - - return 0; -} - -static int wifi_cmd_proto(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **)&proto_args); - if (nerrors != 0) { - arg_print_errors(stderr, proto_args.end, argv[0]); - return 1; - } - wifi_mode_t mode; - esp_wifi_get_mode(&mode); - if(WIFI_MODE_NULL == mode) { - ESP_LOGI(TAG, "current wifi mode is null"); - return 1; - } - int ifx = (WIFI_MODE_STA == mode) ? 0 : 1; - if (proto_args.proto->count) { - if (!strcmp(proto_args.proto->sval[0], "ax")) { - ESP_ERROR_CHECK(esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AX)); - printf("(%s)set to 11ax\n", (ifx == WIFI_IF_STA) ? "sta" : "ap"); - } else if (!strcmp(proto_args.proto->sval[0], "bgn")) { - ESP_ERROR_CHECK(esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N)); - printf("(%s)set to bgn\n", (ifx == WIFI_IF_STA) ? "sta" : "ap"); - } else if (!strcmp(proto_args.proto->sval[0], "bg")) { - ESP_ERROR_CHECK(esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G)); - printf("(%s)set to bg\n", (ifx == WIFI_IF_STA) ? "sta" : "ap"); - } else { - ESP_ERROR_CHECK(esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B)); - printf("(%s)set to b\n", (ifx == WIFI_IF_STA) ? "sta" : "ap"); - } - } - uint8_t protocol_bitmap = 0; - ESP_ERROR_CHECK(esp_wifi_get_protocol(ifx, &protocol_bitmap) ); - if (protocol_bitmap & WIFI_PROTOCOL_11AX) { - printf("(%s)11ax\n", (ifx == WIFI_IF_STA) ? "sta" : "ap"); - } else if (protocol_bitmap & WIFI_PROTOCOL_11N) { - printf("(%s)bgn\n", (ifx == WIFI_IF_STA) ? "sta" : "ap"); - } else if (protocol_bitmap & WIFI_PROTOCOL_11G) { - printf("(%s)bg\n", (ifx == WIFI_IF_STA) ? "sta" : "ap"); - } else if (protocol_bitmap & WIFI_PROTOCOL_11B) { - printf("(%s)b\n", (ifx == WIFI_IF_STA) ? "sta" : "ap"); - } - return 0; -} - -static int wifi_disconnect(int argc, char **argv) -{ - wifi_mode_t mode; - esp_wifi_get_mode(&mode); - if (WIFI_MODE_AP == mode) { - // TODO - ESP_LOGI(TAG, "WIFI_MODE_AP, not support"); - } else if (WIFI_MODE_STA == mode) { - esp_wifi_disconnect(); - printf("disconnect\n"); - } else { - ESP_LOGI(TAG, "NULL mode"); - } - return 0; -} - -static int wifi_cmd_inactive_time(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **)&inactive_time_args); - if (nerrors != 0) { - arg_print_errors(stderr, inactive_time_args.end, argv[0]); - return 1; - } - - esp_err_t err = ESP_OK; - wifi_mode_t mode; - esp_wifi_get_mode(&mode); - if ((mode & WIFI_MODE_AP) && inactive_time_args.val->count) { - err = esp_wifi_set_inactive_time(ESP_IF_WIFI_AP, inactive_time_args.val->ival[0]); - if (err != ESP_OK) { - ESP_LOGW(TAG, "set softAP inactive time to %d seconds, err:0x%x", inactive_time_args.val->ival[0], err); - } else { - ESP_LOGI(TAG, "set softAP inactive time to %d seconds", inactive_time_args.val->ival[0]); - } - } - //WIFI_MODE_STA or WIFI_MODE_APSTA - if ((mode & WIFI_MODE_STA) && inactive_time_args.val->count) { - err = esp_wifi_set_inactive_time(ESP_IF_WIFI_STA, inactive_time_args.val->ival[0]); - if (err != ESP_OK) { - ESP_LOGW(TAG, "set STA inactive time to %d seconds, err:0x%x", inactive_time_args.val->ival[0], err); - } else { - ESP_LOGI(TAG, "set STA inactive time to %d seconds", inactive_time_args.val->ival[0]); - } - } - uint16_t secs = 0; - esp_wifi_get_inactive_time(ESP_IF_WIFI_STA, &secs); - printf("inactive time: %d seconds\n", secs); - return 0; -} - -static int wifi_cmd_sounding_rate(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **)&wifi_sounding_rate_args); - if (nerrors != 0) { - arg_print_errors(stderr, wifi_sounding_rate_args.end, argv[0]); - return 1; - } - - if (wifi_sounding_rate_args.format->count && wifi_sounding_rate_args.rate->count) { - if (wifi_sounding_rate_args.format->ival[0] == SIG_MODE_LEGACY) { - if (wifi_sounding_rate_args.rate->ival[0] < WIFI_PHY_RATE_MCS0_LGI && - wifi_sounding_rate_args.rate->ival[0] >= 0) { - hal_he_set_bf_report_rate(SIG_MODE_LEGACY, wifi_sounding_rate_args.rate->ival[0]); - } else { - ESP_LOGW(TAG, "need correct legacy rate(0-%d)", WIFI_PHY_RATE_9M); - } - } else { - if (wifi_sounding_rate_args.rate->ival[0] >= WIFI_PHY_RATE_MCS0_LGI && - wifi_sounding_rate_args.rate->ival[0] <= WIFI_PHY_RATE_MCS9_SGI) { - hal_he_set_bf_report_rate(wifi_sounding_rate_args.format->ival[0], - wifi_sounding_rate_args.rate->ival[0]); - } else { - ESP_LOGW(TAG, "need correct mcs(%d-%d)", WIFI_PHY_RATE_MCS0_LGI, WIFI_PHY_RATE_MCS9_SGI); - } - } - } else { - ESP_LOGW(TAG, "set rate fail"); - } - return 0; -} - -static int wifi_cmd_muedca(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **)&muedca_args); - if (nerrors != 0) { - arg_print_errors(stderr, muedca_args.end, argv[0]); - return 1; - } - - if (muedca_args.reset_timer->count) { - esp_wifi_sta_reset_muedca_timer(muedca_args.reset_timer->ival[0]); - } - - uint8_t aci_bitmap = 0; - if (muedca_args.read_timer->count) { - aci_bitmap = muedca_args.read_timer->ival[0]; - if (aci_bitmap & BIT(0)) { - dbg_read_muedca_timer(3); - } - if (aci_bitmap & BIT(1)) { - dbg_read_muedca_timer(2); - } - if (aci_bitmap & BIT(2)) { - dbg_read_muedca_timer(1); - } - if (aci_bitmap & BIT(3)) { - dbg_read_muedca_timer(0); - } - } - return 0; -} - -static int wifi_cmd_set_tx_pwr(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **)&tx_pwr_args); - if (nerrors != 0) { - arg_print_errors(stderr, tx_pwr_args.end, argv[0]); - return 1; - } - if (tx_pwr_args.mcs->count && tx_pwr_args.power->count) { - if (tx_pwr_args.mcs->ival[0] <= 9 && tx_pwr_args.mcs->ival[0] >= 0) { - if (tx_pwr_args.power->ival[0] >= -13 && - tx_pwr_args.power->ival[0] <= 20) { - esp_test_set_tx_mcs_pwr(tx_pwr_args.mcs->ival[0] + WIFI_PHY_RATE_MCS0_LGI, tx_pwr_args.power->ival[0]); - ESP_LOGW(TAG, "set MCS%d TX PWR to %d", tx_pwr_args.mcs->ival[0], tx_pwr_args.power->ival[0]); - } else if (tx_pwr_args.power->ival[0] == 0xff) { - esp_test_set_tx_mcs_pwr(tx_pwr_args.mcs->ival[0] + WIFI_PHY_RATE_MCS0_LGI, tx_pwr_args.power->ival[0]); - ESP_LOGW(TAG, "set MCS%d TX PWR to default value", tx_pwr_args.mcs->ival[0]); - } - } else { - ESP_LOGW(TAG, "Set TX power fail, MCS should in range [0,9], power should in range [-13, 30] or set 0xFF for default"); - } - } - return 0; -} - -static int wifi_read_avgsnr(int argc, char **argv) -{ - wifi_mode_t mode; - esp_wifi_get_mode(&mode); - if (WIFI_MODE_AP == mode) { - // TODO - ESP_LOGI(TAG, "WIFI_MODE_AP, not support"); - } else if (WIFI_MODE_STA == mode || WIFI_MODE_APSTA == mode) { - printf("%.2f\n", esp_test_get_bfr_avgsnr()); - } else { - ESP_LOGI(TAG, "NULL mode"); - } - return 0; -} - -void register_wifi_cmd(void) -{ - /* mac */ - const esp_console_cmd_t maccmd = { - .command = "mac", - .help = "get mac", - .hint = NULL, - .func = &wifi_cmd_get_mac, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&maccmd)); - /* disable edca */ - edca_args.enable = arg_int0("e", "enable", "[enable]", "enable edca tx"); - edca_args.txq = arg_int0("q", "txq", "[txq]", "enable edca txq"); - edca_args.end = arg_end(1); - const esp_console_cmd_t edca_cmd = { - .command = "edca", - .help = "enable/disable edca", - .hint = NULL, - .func = &wifi_cmd_edca_tx, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&edca_cmd)); - /* read/write hw registers */ - reg_rw_args.read = arg_dbl0("r", NULL, "", "read register address"); - reg_rw_args.write = arg_dbl0("w", NULL, "", "write register address"); - reg_rw_args.value = arg_dbl0("v", NULL, "", "write value"); - reg_rw_args.end = arg_end(2); - const esp_console_cmd_t reg_rw_cmd = { - .command = "reg", - .help = "r/w hw register", - .hint = NULL, - .func = &wifi_cmd_reg_rw, - .argtable = ®_rw_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(®_rw_cmd)); - /* om control */ - omctrl_args.ul_mu_disable = arg_dbl0("u", "ulmu", "[ulmu]", "disable ul mu"); - omctrl_args.ul_mu_data_disable = arg_dbl0("d", "uldata", "[uldata]", "disable ul mu data"); - omctrl_args.ersu_disable = arg_dbl0("e", "ersu", "[ersu]", "disable ersu"); - omctrl_args.report = arg_dbl0("r", "report", "[report]", "report om control to ap"); - omctrl_args.resounding = arg_dbl0("s", "resounding", "[resounding]", "DL MU-MIMO resound Recoummendation"); - omctrl_args.end = arg_end(1); - const esp_console_cmd_t omctrl_cmd = { - .command = "omc", - .help = "om control", - .hint = NULL, - .func = &wifi_cmd_set_omc, - .argtable = &omctrl_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&omctrl_cmd)); - /* stbc */ - stbc_args.disable = arg_dbl0("d", "disable", "[disable]", "disable stbc"); - stbc_args.end = arg_end(1); - const esp_console_cmd_t stbc_cmd = { - .command = "stbc", - .help = "configure stbc", - .hint = NULL, - .func = &wifi_cmd_stbc, - .argtable = &stbc_args - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&stbc_cmd)); - /* su bmfmee */ - bmfmee_args.disable = arg_dbl0("d", "disable", "[disable]", "disable bmfmee"); - bmfmee_args.end = arg_end(1); - const esp_console_cmd_t bmfmee_cmd = { - .command = "bmfmee", - .help = "configure su bmfmee", - .hint = NULL, - .func = &wifi_cmd_su_bmfmee, - .argtable = &bmfmee_args - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&bmfmee_cmd)); - /* set trigger frame mac padding duration */ - tf_padding_args.tf_padding = arg_dbl0("p", "padding", "[padding]", "set trigger frame mac padding duration"); - tf_padding_args.end = arg_end(1); - const esp_console_cmd_t tf_padding_cmd = { - .command = "tf", - .help = "set padding", - .hint = NULL, - .func = &wifi_cmd_set_tf_padding, - .argtable = &tf_padding_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&tf_padding_cmd)); - /* ignore cca */ - cca_args.enable = arg_dbl0("e", "enable", "[enable]", "enable ignore cca"); - cca_args.end = arg_end(1); - const esp_console_cmd_t cca_cmd = { - .command = "cca", - .help = "ignore cca", - .hint = NULL, - .func = &wifi_cmd_ignore_cca, - .argtable = &cca_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&cca_cmd)); - /* dump tx tb ppdu */ - const esp_console_cmd_t tb_cmd = { - .command = "tb", - .help = "dump tx tb ppdu", - .hint = NULL, - .func = &wifi_cmd_tb, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&tb_cmd)); - /* set ps type */ - const esp_console_cmd_t ps_cmd = { - .command = "ps", - .help = "set ps type", - .hint = NULL, - .func = &wifi_cmd_set_ps_type, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&ps_cmd)); - /* ping test */ - ping_args.timeout = arg_dbl0("W", "timeout", "", "Time to wait for a response, in seconds"); - ping_args.interval = arg_dbl0("i", "interval", "", "Wait interval seconds between sending each packet"); - ping_args.data_size = arg_int0("s", "size", "", "Specify the number of data bytes to be sent"); - ping_args.count = arg_int0("c", "count", "", "Stop after sending count packets"); - ping_args.tos = arg_int0("Q", "tos", "", "Set Type of Service related bits in IP datagrams"); - ping_args.host = arg_str0(NULL, NULL, "[host]", "Host address"); - ping_args.abort = arg_lit0("a", "abort", "abort"); - ping_args.end = arg_end(1); - const esp_console_cmd_t ping_cmd = { - .command = "ping", - .help = "send ICMP ECHO_REQUEST to network hosts", - .hint = NULL, - .func = &do_ping_cmd, - .argtable = &ping_args - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&ping_cmd)); - /* get ps state */ - const esp_console_cmd_t pss_cmd = { - .command = "pss", - .help = "get ps state", - .hint = NULL, - .func = &wifi_cmd_get_ps_state, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&pss_cmd)); - /* ip */ - static_ip_args.ip = arg_str0("i", "ip", "", "ip address"); - static_ip_args.gw = arg_str0("g", "gateway", "", "gateway address"); - static_ip_args.netmask = arg_str0("n", "netmask", "", "netmask addess"); - static_ip_args.end = arg_end(1); - const esp_console_cmd_t static_ip_cmd = { - .command = "ip", - .help = "ip settings", - .hint = NULL, - .func = &wifi_cmd_set_ip, - .argtable = &static_ip_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&static_ip_cmd)); - /* query */ - 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)); - /* proto */ - proto_args.proto = arg_str0(NULL, NULL, "", "proto [ax,bgn,bg,b]"); - proto_args.end = arg_end(1); - const esp_console_cmd_t proto_cmd = { - .command = "proto", - .help = "set wifi protocol", - .hint = NULL, - .func = &wifi_cmd_proto, - .argtable = &proto_args - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&proto_cmd)); - /* disconnect */ - const esp_console_cmd_t disconnect_cmd = { - .command = "disconnect", - .help = "disconnect", - .hint = NULL, - .func = &wifi_disconnect, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&disconnect_cmd)); - /* inactive time */ - inactive_time_args.val = arg_int0("t", "time", "time", "set inactive time, in seconds"); - inactive_time_args.end = arg_end(1); - const esp_console_cmd_t inactive_cmd = { - .command = "inactive", - .help = "inactive time, unit: seconds", - .hint = NULL, - .func = &wifi_cmd_inactive_time, - .argtable = &inactive_time_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&inactive_cmd)); - /* set beamforming report rate */ - wifi_sounding_rate_args.format = arg_int0("f", "format", "format", "set format"); - wifi_sounding_rate_args.rate = arg_int0("r", "rate", "rate", "set rate"); - wifi_sounding_rate_args.end = arg_end(1); - const esp_console_cmd_t sounding_rate_cmd = { - .command = "sounding", - .help = "set beamforming report rate", - .hint = NULL, - .func = &wifi_cmd_sounding_rate, - .argtable = &wifi_sounding_rate_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&sounding_rate_cmd)); - /* muedca */ - muedca_args.reset_timer = arg_int0("r", NULL, "reset timer", "reset muedca timer"); - muedca_args.read_timer = arg_int0("d", NULL, "read timer", "read muedca timer"); - muedca_args.end = arg_end(1); - const esp_console_cmd_t reg_muedca_cmd = { - .command = "muedca", - .help = "Reset/Read muedca timer", - .hint = NULL, - .func = &wifi_cmd_muedca, - .argtable = &muedca_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(®_muedca_cmd)); - /* tx_pwr */ - tx_pwr_args.mcs = arg_int0("m", NULL, "[0, 9]", "force tx power on MCSX"); - tx_pwr_args.power = arg_int0("p", NULL, "[-13, 20]", "set max power, set 0xFF for default"); - tx_pwr_args.end = arg_end(1); - const esp_console_cmd_t reg_tx_pwr_cmd = { - .command = "txpwr", - .help = "force tx power on MCSX", - .hint = NULL, - .func = &wifi_cmd_set_tx_pwr, - .argtable = &tx_pwr_args, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(®_tx_pwr_cmd)); - /* avgSNR */ - const esp_console_cmd_t avgsnr_cmd = { - .command = "avgsnr", - .help = "show avgSnr in beamforming memory", - .hint = NULL, - .func = &wifi_read_avgsnr, - }; - ESP_ERROR_CHECK(esp_console_cmd_register(&avgsnr_cmd)); -} -#else - -void register_wifi_cmd(void) -{ - ; -} - -#endif /* CONFIG_SOC_WIFI_HE_SUPPORT */ diff --git a/examples/ethernet/.build-test-rules.yml b/examples/ethernet/.build-test-rules.yml index b891fb5b34..bb7ee1b6af 100644 --- a/examples/ethernet/.build-test-rules.yml +++ b/examples/ethernet/.build-test-rules.yml @@ -38,7 +38,6 @@ examples/ethernet/iperf: - esp_driver_gpio - esp_driver_spi depends_filepatterns: - - examples/common_components/iperf/**/* - examples/common_components/protocol_examples_common/**/* - examples/system/console/advanced/components/cmd_system/**/* - examples/ethernet/basic/components/ethernet_init/**/* diff --git a/examples/ethernet/iperf/main/idf_component.yml b/examples/ethernet/iperf/main/idf_component.yml index 8b9ecd9116..db29a6b932 100644 --- a/examples/ethernet/iperf/main/idf_component.yml +++ b/examples/ethernet/iperf/main/idf_component.yml @@ -3,5 +3,5 @@ dependencies: path: ${IDF_PATH}/examples/system/console/advanced/components/cmd_system ethernet_init: path: ${IDF_PATH}/examples/ethernet/basic/components/ethernet_init - iperf: - path: ${IDF_PATH}/examples/common_components/iperf + espressif/iperf: + version: "~0.1.1" diff --git a/examples/openthread/.build-test-rules.yml b/examples/openthread/.build-test-rules.yml index 51217281f9..d431fd6861 100644 --- a/examples/openthread/.build-test-rules.yml +++ b/examples/openthread/.build-test-rules.yml @@ -11,8 +11,6 @@ - lwip - openthread depends_filepatterns: - - examples/common_components/iperf/* - - examples/common_components/iperf/**/* - examples/openthread/* - examples/openthread/**/* diff --git a/examples/openthread/ot_br/main/idf_component.yml b/examples/openthread/ot_br/main/idf_component.yml index 124501cc61..10a25935ff 100644 --- a/examples/openthread/ot_br/main/idf_component.yml +++ b/examples/openthread/ot_br/main/idf_component.yml @@ -8,7 +8,7 @@ dependencies: version: ">=5.0" protocol_examples_common: path: ${IDF_PATH}/examples/common_components/protocol_examples_common - iperf: - path: ${IDF_PATH}/examples/common_components/iperf ot_led: path: ${IDF_PATH}/examples/openthread/ot_common_components/ot_led + espressif/iperf: + version: "~0.1.1" diff --git a/examples/openthread/ot_cli/main/idf_component.yml b/examples/openthread/ot_cli/main/idf_component.yml index 2ed997286a..6b1234251f 100644 --- a/examples/openthread/ot_cli/main/idf_component.yml +++ b/examples/openthread/ot_cli/main/idf_component.yml @@ -4,7 +4,7 @@ dependencies: version: "~1.0.0" idf: version: ">=4.1.0" - iperf: - path: ${IDF_PATH}/examples/common_components/iperf ot_led: path: ${IDF_PATH}/examples/openthread/ot_common_components/ot_led + espressif/iperf: + version: "~0.1.1" diff --git a/examples/wifi/iperf/main/CMakeLists.txt b/examples/wifi/iperf/main/CMakeLists.txt index ab497c35d2..156c3bfc7b 100644 --- a/examples/wifi/iperf/main/CMakeLists.txt +++ b/examples/wifi/iperf/main/CMakeLists.txt @@ -1,3 +1,2 @@ -idf_component_register(SRCS "cmd_wifi.c" - "iperf_example_main.c" +idf_component_register(SRCS "iperf_example_main.c" INCLUDE_DIRS ".") diff --git a/examples/wifi/iperf/main/cmd_decl.h b/examples/wifi/iperf/main/cmd_decl.h deleted file mode 100644 index 4fbc479491..0000000000 --- a/examples/wifi/iperf/main/cmd_decl.h +++ /dev/null @@ -1,20 +0,0 @@ -/* Iperf example — declarations of command registration functions. - - 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. -*/ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include "cmd_system.h" -#include "cmd_wifi.h" - -#ifdef __cplusplus -} -#endif diff --git a/examples/wifi/iperf/main/cmd_wifi.c b/examples/wifi/iperf/main/cmd_wifi.c deleted file mode 100644 index e33f27c75a..0000000000 --- a/examples/wifi/iperf/main/cmd_wifi.c +++ /dev/null @@ -1,616 +0,0 @@ -/* 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 -#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 "esp_mac.h" -#include "iperf.h" -#include "esp_coexist.h" -#include "wifi_cmd.h" - -typedef struct { - struct arg_str *ip; - struct arg_lit *server; - struct arg_lit *udp; - struct arg_lit *version; - struct arg_int *port; - struct arg_int *length; - struct arg_int *interval; - struct arg_int *time; - struct arg_int *bw_limit; - struct arg_lit *abort; - struct arg_end *end; -} wifi_iperf_t; - -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_iperf_t iperf_args; -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"; -esp_netif_t *netif_ap = NULL; -esp_netif_t *netif_sta = NULL; - -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"); - esp_wifi_clear_ap_list(); - 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++) { -#if CONFIG_SOC_WIFI_HE_SUPPORT - char ssid_rssi[46] = { 0, }; - sprintf(ssid_rssi, "[%s][rssi=%d]", ap_list_buffer[i].ssid, ap_list_buffer[i].rssi); - if (ap_list_buffer[i].phy_11ax) { - ESP_LOGW(TAG, - "[%2d]%45s authmode:0x%x, channel:%2d[%d], phymode:%4s, "MACSTR", bssid-index:%d, bss_color:%d, disabled:%d", - i, ssid_rssi, ap_list_buffer[i].authmode, - ap_list_buffer[i].primary, ap_list_buffer[i].second, - ap_list_buffer[i].phy_11ax ? "11ax" : (ap_list_buffer[i].phy_11n ? "11n" : - (ap_list_buffer[i].phy_11g ? "11g" : (ap_list_buffer[i].phy_11b ? "11b" : ""))), - MAC2STR(ap_list_buffer[i].bssid), ap_list_buffer[i].he_ap.bssid_index, - ap_list_buffer[i].he_ap.bss_color, ap_list_buffer[i].he_ap.bss_color_disabled); - } else { - ESP_LOGI(TAG, - "[%2d]%45s authmode:0x%x, channel:%2d[%d], phymode:%4s, "MACSTR"", - i, ssid_rssi, ap_list_buffer[i].authmode, - ap_list_buffer[i].primary, ap_list_buffer[i].second, - ap_list_buffer[i].phy_11ax ? "11ax" : (ap_list_buffer[i].phy_11n ? "11n" : - (ap_list_buffer[i].phy_11g ? "11g" : (ap_list_buffer[i].phy_11b ? "11b" : ""))), - MAC2STR(ap_list_buffer[i].bssid)); - } -#else - ESP_LOGI(TAG, "[%s][rssi=%d]", ap_list_buffer[i].ssid, ap_list_buffer[i].rssi); -#endif - } - } - free(ap_list_buffer); - ESP_LOGI(TAG, "sta scan done"); -} - -static void got_ip_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT); - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); -} - -static void disconnect_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - if (reconnect) { - ESP_LOGI(TAG, "sta disconnect, reconnect..."); - esp_wifi_connect(); - } else { - ESP_LOGI(TAG, "sta disconnect"); - } - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - xEventGroupSetBits(wifi_event_group, DISCONNECTED_BIT); -} - -void initialise_wifi(void) -{ - esp_log_level_set("wifi", ESP_LOG_WARN); - static bool initialized = false; - - if (initialized) { - return; - } - - ESP_ERROR_CHECK(esp_netif_init()); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_create_default() ); - netif_ap = esp_netif_create_default_wifi_ap(); - assert(netif_ap); - netif_sta = esp_netif_create_default_wifi_sta(); - assert(netif_sta); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, - WIFI_EVENT_SCAN_DONE, - &scan_done_handler, - NULL, - NULL)); - ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, - WIFI_EVENT_STA_DISCONNECTED, - &disconnect_handler, - NULL, - NULL)); - ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, - IP_EVENT_STA_GOT_IP, - &got_ip_handler, - NULL, - NULL)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM) ); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - -#if CONFIG_EXTERNAL_COEX_ENABLE - esp_external_coex_gpio_set_t gpio_pin; - gpio_pin.request = 1; - gpio_pin.priority = 2; - gpio_pin.grant = 3; -#if SOC_EXTERNAL_COEX_LEADER_TX_LINE - gpio_pin.tx_line = 4; -#endif - - esp_external_coex_set_work_mode(EXTERNAL_COEX_LEADER_ROLE); -#if SOC_EXTERNAL_COEX_LEADER_TX_LINE - ESP_ERROR_CHECK(esp_enable_extern_coex_gpio_pin(EXTERN_COEX_WIRE_4, gpio_pin)); -#else - ESP_ERROR_CHECK(esp_enable_extern_coex_gpio_pin(EXTERN_COEX_WIRE_3, gpio_pin)); -#endif /* SOC_EXTERNAL_COEX_LEADER_TX_LINE */ -#endif /* CONFIG_EXTERNAL_COEX_ENABLE */ - -#if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS -#if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_MU_STATS - esp_wifi_enable_rx_statistics(true, true); -#else - esp_wifi_enable_rx_statistics(true, false); -#endif -#endif -#if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS - esp_wifi_enable_tx_statistics(ESP_WIFI_ACI_BE, true); -#endif - initialized = true; -} - -static bool wifi_cmd_sta_join(const char *ssid, const char *pass, bool enable_he_mcs9) -{ - int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 0); - - wifi_config_t wifi_config = { 0 }; - - strlcpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid)); - if (pass) { - strlcpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password)); - } - - if (enable_he_mcs9 == true) { - wifi_config.sta.he_mcs9_enabled = 1; - } - - if (bits & CONNECTED_BIT) { - reconnect = false; - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - ESP_ERROR_CHECK( esp_wifi_disconnect() ); - xEventGroupWaitBits(wifi_event_group, DISCONNECTED_BIT, 0, 1, portTICK_PERIOD_MS); - } - - reconnect = true; - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); - esp_wifi_connect(); - - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, 0, 1, 5000 / portTICK_PERIOD_MS); - - return true; -} - -static int wifi_cmd_sta(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **) &sta_args); - - if (nerrors != 0) { - arg_print_errors(stderr, sta_args.end, argv[0]); - return 1; - } - - ESP_LOGI(TAG, "sta connecting to '%s'", sta_args.ssid->sval[0]); - ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); - wifi_cmd_sta_join(sta_args.ssid->sval[0], sta_args.password->sval[0], false); - return 0; -} - -static int wifi_cmd_sta40(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **) &sta_args); - - if (nerrors != 0) { - arg_print_errors(stderr, sta_args.end, argv[0]); - return 1; - } - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_protocol(0, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N)); - ESP_ERROR_CHECK(esp_wifi_set_bandwidth(0, WIFI_BW_HT40)); - ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); - - ESP_LOGI(TAG, "sta connecting to '%s'", sta_args.ssid->sval[0]); - wifi_cmd_sta_join(sta_args.ssid->sval[0], sta_args.password->sval[0], false); - return 0; -} - -static int wifi_cmd_sta_mcs89(int argc, char **argv) -{ -#if CONFIG_SOC_WIFI_HE_SUPPORT - int nerrors = arg_parse(argc, argv, (void **) &sta_args); - if (nerrors != 0) { - arg_print_errors(stderr, sta_args.end, argv[0]); - return 1; - } - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_protocol(0, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AX)); - ESP_ERROR_CHECK(esp_wifi_set_bandwidth(0, WIFI_BW_HT20)); - ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); - - ESP_LOGI(TAG, "sta connecting to '%s'", sta_args.ssid->sval[0]); - wifi_cmd_sta_join(sta_args.ssid->sval[0], sta_args.password->sval[0], true); -#else - ESP_LOGW(TAG, "HE-MCS[0, 9] is not supported"); -#endif - 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(WIFI_IF_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)); - - // now wifi iperf only support IPV4 address - cfg.type = IPERF_IP_TYPE_IPV4; - - 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.destination_ip4 = esp_ip4addr_aton(iperf_args.ip->sval[0]); - cfg.flag |= IPERF_FLAG_CLIENT; - } - - cfg.source_ip4 = wifi_get_local_ip(); - if (cfg.source_ip4 == 0) { - return 0; - } - - if (iperf_args.udp->count == 0) { - cfg.flag |= IPERF_FLAG_TCP; - } else { - cfg.flag |= IPERF_FLAG_UDP; - } - - if (iperf_args.length->count == 0) { - cfg.len_send_buf = 0; - } else { - cfg.len_send_buf = iperf_args.length->ival[0]; - } - - 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; - } - } - - /* iperf -b */ - if (iperf_args.bw_limit->count == 0) { - cfg.bw_lim = IPERF_DEFAULT_NO_BW_LIMIT; - } else { - cfg.bw_lim = iperf_args.bw_limit->ival[0]; - if (cfg.bw_lim <= 0) { - cfg.bw_lim = IPERF_DEFAULT_NO_BW_LIMIT; - } - } - - - ESP_LOGI(TAG, "mode=%s-%s sip=%" PRId32 ".%" PRId32 ".%" PRId32 ".%" PRId32 ":%d,\ - dip=%" PRId32 ".%" PRId32 ".%" PRId32 ".%" PRId32 ":%d,\ - interval=%" PRId32 ", time=%" PRId32 "", - cfg.flag & IPERF_FLAG_TCP ? "tcp" : "udp", - cfg.flag & IPERF_FLAG_SERVER ? "server" : "client", - cfg.source_ip4 & 0xFF, (cfg.source_ip4 >> 8) & 0xFF, (cfg.source_ip4 >> 16) & 0xFF, - (cfg.source_ip4 >> 24) & 0xFF, cfg.sport, - cfg.destination_ip4 & 0xFF, (cfg.destination_ip4 >> 8) & 0xFF, - (cfg.destination_ip4 >> 16) & 0xFF, (cfg.destination_ip4 >> 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) ); - - const esp_console_cmd_t sta40_cmd = { - .command = "sta40", - .help = "WiFi is station mode, set protocol to bgn and cbw to 40MHz, join a specified AP", - .hint = NULL, - .func = &wifi_cmd_sta40, - .argtable = &sta_args - }; - - ESP_ERROR_CHECK( esp_console_cmd_register(&sta40_cmd) ); - - const esp_console_cmd_t stamcs89_cmd = { - .command = "stamcs89", - .help = "WiFi is station mode, set protocol to ax and mcs set to HE-MCS[0,9], join a specified AP", - .hint = NULL, - .func = &wifi_cmd_sta_mcs89, - .argtable = &sta_args - }; - - ESP_ERROR_CHECK( esp_console_cmd_register(&stamcs89_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.version = arg_lit0("V", "ipv6_domain", "use IPV6 address rather than IPV4"); - iperf_args.port = arg_int0("p", "port", "", "server port to listen on/connect to"); - iperf_args.length = arg_int0("l", "len", "", "Set read/write buffer size"); - iperf_args.interval = arg_int0("i", "interval", "", "seconds between periodic bandwidth reports"); - iperf_args.time = arg_int0("t", "time", "