1 /* OpenThread Command Line Example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 
10 #include "esp_check.h"
11 #include "esp_log.h"
12 #include "esp_ot_iperf.h"
13 #include "iperf.h"
14 #include "openthread/cli.h"
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 
19 #define TAG "ot-iperf"
20 
esp_ot_process_iperf(void * aContext,uint8_t aArgsLength,char * aArgs[])21 void esp_ot_process_iperf(void *aContext, uint8_t aArgsLength, char *aArgs[])
22 {
23     (void)(aContext);
24     (void)(aArgsLength);
25     iperf_cfg_t cfg;
26     memset(&cfg, 0, sizeof(cfg));
27     // set iperf default flag: tcp server
28     IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_TCP);
29     IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_SERVER);
30     cfg.time = IPERF_DEFAULT_TIME;
31     cfg.type = IPERF_IP_TYPE_IPV4;
32     if (aArgsLength == 0) {
33         otCliOutputFormat("---iperf parameter---\n");
34         otCliOutputFormat("-V                  :     use IPV6 address\n");
35         otCliOutputFormat("-s                  :     server mode, only receive\n");
36         otCliOutputFormat("-u                  :     upd mode\n");
37         otCliOutputFormat("-c <addr>           :     client mode, only transmit\n");
38         otCliOutputFormat("-i <interval>       :     seconds between periodic bandwidth reports\n");
39         otCliOutputFormat("-t <time>           :     time in seconds to transmit for (default 30 secs)\n");
40         otCliOutputFormat("-p <port>           :     server port to listen on/connect to\n");
41         otCliOutputFormat("-l <len_send_buf>   :     the lenth of send buffer\n");
42         otCliOutputFormat("---example---\n");
43         otCliOutputFormat("create a tcp server :     iperf -s -i 3 -p 5001 -t 60\n");
44         otCliOutputFormat("create a udp client :     iperf -c <addr> -u -i 3 -t 60 -p 5001 -l 512\n");
45     } else {
46         for (int i = 0; i < aArgsLength; i++) {
47             if (strcmp(aArgs[i], "-c") == 0) {
48                 IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_CLIENT);
49                 IPERF_FLAG_CLR(cfg.flag, IPERF_FLAG_SERVER);
50                 i++;
51                 cfg.destination_ip6 = aArgs[i];
52                 otCliOutputFormat("ip:%s\n", cfg.destination_ip6);
53             } else if (strcmp(aArgs[i], "-s") == 0) {
54                 IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_SERVER);
55                 IPERF_FLAG_CLR(cfg.flag, IPERF_FLAG_CLIENT);
56             } else if (strcmp(aArgs[i], "-V") == 0) {
57                 cfg.type = IPERF_IP_TYPE_IPV6;
58             } else if (strcmp(aArgs[i], "-u") == 0) {
59                 IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_UDP);
60                 IPERF_FLAG_CLR(cfg.flag, IPERF_FLAG_TCP);
61             } else if (strcmp(aArgs[i], "-p") == 0) {
62                 i++;
63                 if (cfg.flag & IPERF_FLAG_SERVER) {
64                     cfg.sport = atoi(aArgs[i]);
65                     cfg.dport = IPERF_DEFAULT_PORT;
66                 } else {
67                     cfg.sport = IPERF_DEFAULT_PORT;
68                     cfg.dport = atoi(aArgs[i]);
69                 }
70                 otCliOutputFormat("dp:%d\n", cfg.dport);
71                 otCliOutputFormat("sp:%d\n", cfg.sport);
72             } else if (strcmp(aArgs[i], "-i") == 0) {
73                 i++;
74                 if (atoi(aArgs[i]) < 0) {
75                     cfg.interval = IPERF_DEFAULT_INTERVAL;
76                 } else {
77                     cfg.interval = atoi(aArgs[i]);
78                 }
79                 otCliOutputFormat("i:%d\n", cfg.interval);
80             } else if (strcmp(aArgs[i], "-t") == 0) {
81                 i++;
82                 cfg.time = atoi(aArgs[i]);
83                 if (cfg.time <= cfg.interval) {
84                     cfg.time = cfg.interval;
85                 }
86                 otCliOutputFormat("t:%d\n", cfg.time);
87             } else if (strcmp(aArgs[i], "-l") == 0) {
88                 i++;
89                 if (atoi(aArgs[i]) <= 0) {
90                     ESP_LOGE(TAG, "Invalid arguments.");
91                 } else {
92                     cfg.len_send_buf = atoi(aArgs[i]);
93                 }
94             } else if (strcmp(aArgs[i], "-a") == 0) {
95                 iperf_stop();
96                 return;
97             }
98         }
99         iperf_start(&cfg);
100     }
101 }
102