2018-09-20 07:26:14 -04:00
|
|
|
/* Console example — Ethernet 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/event_groups.h"
|
2022-07-22 11:13:04 -04:00
|
|
|
#include "sys/socket.h" // for INADDR_ANY
|
2019-09-01 05:15:11 -04:00
|
|
|
#include "esp_netif.h"
|
2018-09-20 07:26:14 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_console.h"
|
2018-11-20 05:24:49 -05:00
|
|
|
#include "esp_event.h"
|
2018-09-20 07:26:14 -04:00
|
|
|
#include "esp_eth.h"
|
2022-01-12 01:53:47 -05:00
|
|
|
#include "esp_mac.h"
|
2022-07-22 11:13:04 -04:00
|
|
|
#include "esp_bit_defs.h"
|
2018-09-20 07:26:14 -04:00
|
|
|
#include "argtable3/argtable3.h"
|
2022-07-22 11:13:04 -04:00
|
|
|
#include "ethernet_init.h"
|
2018-09-20 07:26:14 -04:00
|
|
|
#include "iperf.h"
|
|
|
|
#include "sdkconfig.h"
|
2022-07-22 11:13:04 -04:00
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
static bool started = false;
|
|
|
|
static EventGroupHandle_t eth_event_group;
|
|
|
|
static const int GOTIP_BIT = BIT0;
|
2022-07-22 11:13:04 -04:00
|
|
|
static esp_eth_handle_t *s_eth_handles = NULL;
|
|
|
|
static uint8_t s_eth_port_cnt = 0;
|
|
|
|
static esp_netif_t **s_eth_netifs = NULL;
|
2018-09-20 07:26:14 -04:00
|
|
|
|
|
|
|
/* "ethernet" command */
|
|
|
|
static struct {
|
|
|
|
struct arg_str *control;
|
|
|
|
struct arg_end *end;
|
|
|
|
} eth_control_args;
|
|
|
|
|
|
|
|
static int eth_cmd_control(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int nerrors = arg_parse(argc, argv, (void **)ð_control_args);
|
|
|
|
if (nerrors != 0) {
|
|
|
|
arg_print_errors(stderr, eth_control_args.end, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(eth_control_args.control->sval[0], "info", 4)) {
|
|
|
|
uint8_t mac_addr[6];
|
2022-07-22 11:13:04 -04:00
|
|
|
esp_netif_ip_info_t ip;
|
|
|
|
for(uint32_t i = 0; i < s_eth_port_cnt; i++) {
|
|
|
|
printf("%s:\r\n", esp_netif_get_desc(s_eth_netifs[i]));
|
|
|
|
esp_eth_ioctl(s_eth_handles[i], ETH_CMD_G_MAC_ADDR, mac_addr);
|
|
|
|
printf(" HW ADDR: " MACSTR "\r\n", MAC2STR(mac_addr));
|
|
|
|
esp_netif_get_ip_info(s_eth_netifs[i], &ip);
|
|
|
|
printf(" ETHIP: " IPSTR "\r\n", IP2STR(&ip.ip));
|
|
|
|
printf(" ETHMASK: " IPSTR "\r\n", IP2STR(&ip.netmask));
|
|
|
|
printf(" ETHGW: " IPSTR "\r\n", IP2STR(&ip.gw));
|
|
|
|
}
|
2018-09-20 07:26:14 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "iperf" command */
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct arg_str *ip;
|
|
|
|
struct arg_lit *server;
|
|
|
|
struct arg_lit *udp;
|
2021-07-30 03:55:37 -04:00
|
|
|
struct arg_lit *version;
|
2018-09-20 07:26:14 -04:00
|
|
|
struct arg_int *port;
|
2021-07-30 03:55:37 -04:00
|
|
|
struct arg_int *length;
|
2018-09-20 07:26:14 -04:00
|
|
|
struct arg_int *interval;
|
|
|
|
struct arg_int *time;
|
2022-01-13 10:01:07 -05:00
|
|
|
struct arg_int *bw_limit;
|
2018-09-20 07:26:14 -04:00
|
|
|
struct arg_lit *abort;
|
|
|
|
struct arg_end *end;
|
|
|
|
} iperf_args;
|
|
|
|
|
|
|
|
static int eth_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));
|
|
|
|
|
2021-07-30 03:55:37 -04:00
|
|
|
// ethernet iperf only support IPV4 address
|
|
|
|
cfg.type = IPERF_IP_TYPE_IPV4;
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
/* iperf -a */
|
|
|
|
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(__func__, "Wrong mode! ESP32 should run in client or server mode");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iperf -s */
|
|
|
|
if (iperf_args.ip->count == 0) {
|
|
|
|
cfg.flag |= IPERF_FLAG_SERVER;
|
|
|
|
}
|
|
|
|
/* iperf -c SERVER_ADDRESS */
|
|
|
|
else {
|
2021-07-30 03:55:37 -04:00
|
|
|
cfg.destination_ip4 = esp_ip4addr_aton(iperf_args.ip->sval[0]);
|
2018-09-20 07:26:14 -04:00
|
|
|
cfg.flag |= IPERF_FLAG_CLIENT;
|
|
|
|
}
|
|
|
|
|
2021-07-30 03:55:37 -04:00
|
|
|
if (iperf_args.length->count == 0) {
|
|
|
|
cfg.len_send_buf = 0;
|
|
|
|
} else {
|
|
|
|
cfg.len_send_buf = iperf_args.length->ival[0];
|
|
|
|
}
|
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
/* wait for ip, could blocked here */
|
2018-09-20 07:26:14 -04:00
|
|
|
xEventGroupWaitBits(eth_event_group, GOTIP_BIT, pdFALSE, pdTRUE, portMAX_DELAY);
|
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
cfg.source_ip4 = INADDR_ANY;
|
2018-09-20 07:26:14 -04:00
|
|
|
|
|
|
|
/* iperf -u */
|
|
|
|
if (iperf_args.udp->count == 0) {
|
|
|
|
cfg.flag |= IPERF_FLAG_TCP;
|
|
|
|
} else {
|
|
|
|
cfg.flag |= IPERF_FLAG_UDP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iperf -p */
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iperf -i */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iperf -t */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 10:01:07 -05:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:26:14 -04:00
|
|
|
printf("mode=%s-%s sip=%d.%d.%d.%d:%d, dip=%d.%d.%d.%d:%d, interval=%d, time=%d\r\n",
|
|
|
|
cfg.flag & IPERF_FLAG_TCP ? "tcp" : "udp",
|
|
|
|
cfg.flag & IPERF_FLAG_SERVER ? "server" : "client",
|
2021-07-30 03:55:37 -04:00
|
|
|
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,
|
2018-09-20 07:26:14 -04:00
|
|
|
cfg.interval, cfg.time);
|
|
|
|
|
|
|
|
iperf_start(&cfg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
static void event_handler(void *arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void *event_data)
|
2018-09-20 07:26:14 -04:00
|
|
|
{
|
2018-11-20 05:24:49 -05:00
|
|
|
if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_START) {
|
2018-09-20 07:26:14 -04:00
|
|
|
started = true;
|
2018-11-20 05:24:49 -05:00
|
|
|
} else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_STOP) {
|
2018-09-20 07:26:14 -04:00
|
|
|
xEventGroupClearBits(eth_event_group, GOTIP_BIT);
|
|
|
|
started = false;
|
2018-11-20 05:24:49 -05:00
|
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) {
|
|
|
|
xEventGroupSetBits(eth_event_group, GOTIP_BIT);
|
2018-09-20 07:26:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void register_ethernet(void)
|
2018-09-20 07:26:14 -04:00
|
|
|
{
|
|
|
|
eth_event_group = xEventGroupCreate();
|
2018-11-20 05:24:49 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2021-06-24 05:03:11 -04:00
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
ESP_ERROR_CHECK(example_eth_init(&s_eth_handles, &s_eth_port_cnt));
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
s_eth_netifs = calloc(s_eth_port_cnt, sizeof(esp_netif_t *));
|
|
|
|
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
|
|
|
|
esp_netif_config_t cfg_spi = {
|
|
|
|
.base = &esp_netif_config,
|
|
|
|
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
|
2021-06-24 05:03:11 -04:00
|
|
|
};
|
2022-07-22 11:13:04 -04:00
|
|
|
char if_key_str[10];
|
|
|
|
char if_desc_str[10];
|
|
|
|
char num_str[3];
|
|
|
|
for (int i = 0; i < s_eth_port_cnt; i++) {
|
|
|
|
itoa(i, num_str, 10);
|
|
|
|
strcat(strcpy(if_key_str, "ETH_"), num_str);
|
|
|
|
strcat(strcpy(if_desc_str, "eth"), num_str);
|
|
|
|
esp_netif_config.if_key = if_key_str;
|
|
|
|
esp_netif_config.if_desc = if_desc_str;
|
|
|
|
esp_netif_config.route_prio -= i*5;
|
|
|
|
s_eth_netifs[i] = esp_netif_new(&cfg_spi);
|
|
|
|
|
|
|
|
// attach Ethernet driver to TCP/IP stack
|
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(s_eth_netifs[i], esp_eth_new_netif_glue(s_eth_handles[i])));
|
|
|
|
}
|
|
|
|
|
2021-08-03 07:34:52 -04:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL));
|
2022-07-22 11:13:04 -04:00
|
|
|
for (int i = 0; i < s_eth_port_cnt; i++) {
|
|
|
|
ESP_ERROR_CHECK(esp_eth_start(s_eth_handles[i]));
|
|
|
|
}
|
2019-04-10 04:24:50 -04:00
|
|
|
|
|
|
|
eth_control_args.control = arg_str1(NULL, NULL, "<info>", "Get info of Ethernet");
|
2018-09-20 07:26:14 -04:00
|
|
|
eth_control_args.end = arg_end(1);
|
|
|
|
const esp_console_cmd_t cmd = {
|
|
|
|
.command = "ethernet",
|
2019-04-10 04:24:50 -04:00
|
|
|
.help = "Ethernet interface IO control",
|
2018-09-20 07:26:14 -04:00
|
|
|
.hint = NULL,
|
|
|
|
.func = eth_cmd_control,
|
|
|
|
.argtable = ð_control_args
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
|
|
|
|
|
|
|
iperf_args.ip = arg_str0("c", "client", "<ip>",
|
|
|
|
"run in client mode, connecting to <host>");
|
|
|
|
iperf_args.server = arg_lit0("s", "server", "run in server mode");
|
|
|
|
iperf_args.udp = arg_lit0("u", "udp", "use UDP rather than TCP");
|
2021-07-30 03:55:37 -04:00
|
|
|
iperf_args.version = arg_lit0("V", "ipv6_domain", "use IPV6 address rather than IPV4");
|
2018-09-20 07:26:14 -04:00
|
|
|
iperf_args.port = arg_int0("p", "port", "<port>",
|
|
|
|
"server port to listen on/connect to");
|
2021-07-30 03:55:37 -04:00
|
|
|
iperf_args.length = arg_int0("l", "len", "<length>", "set read/write buffer size");
|
2018-09-20 07:26:14 -04:00
|
|
|
iperf_args.interval = arg_int0("i", "interval", "<interval>",
|
|
|
|
"seconds between periodic bandwidth reports");
|
|
|
|
iperf_args.time = arg_int0("t", "time", "<time>", "time in seconds to transmit for (default 10 secs)");
|
2022-01-13 10:01:07 -05:00
|
|
|
iperf_args.bw_limit = arg_int0("b", "bandwidth", "<bandwidth>", "bandwidth to send at in Mbits/sec");
|
2018-09-20 07:26:14 -04:00
|
|
|
iperf_args.abort = arg_lit0("a", "abort", "abort running iperf");
|
|
|
|
iperf_args.end = arg_end(1);
|
|
|
|
const esp_console_cmd_t iperf_cmd = {
|
|
|
|
.command = "iperf",
|
|
|
|
.help = "iperf command",
|
|
|
|
.hint = NULL,
|
|
|
|
.func = ð_cmd_iperf,
|
|
|
|
.argtable = &iperf_args
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(esp_console_cmd_register(&iperf_cmd));
|
|
|
|
}
|