diff --git a/examples/performance/README.md b/examples/performance/README.md index f5f9e6e0e..d53a8de78 100644 --- a/examples/performance/README.md +++ b/examples/performance/README.md @@ -6,30 +6,28 @@ Including TCP/UDP TX/RX throughput. # tcp_perf -This example is used to test tcp throughput and delay time. First you should set options in menuconfig. +This example is used to test tcp throughput and delay time. -Steps: +Step1: Set options in `make menuconfig` like ssid, password, server ip and server port. And choose what the esp32 will work as. -* Step1: Set a AP and a STA with same SSID & PASSWORD. You can set one esp32 as AP and another esp32 as STA, also you can use Router or wifi adaptor instead one of the them. +* AP or STA. You can set one esp32 as AP and another esp32 as STA with same ssid & password, also you can use Router or wifi adapter instead of one of these. -* Step2: Set a tcp client and a sever. Make sure the client has correct sever ip & port so they can get connected. It's okay if you creat a tcp sever & client using PC since one of the wifi device is't esp32. +* Client or server. Make sure the client has correct server ip & port so they can get connected. It's okay if you create a tcp server & client using PC since one of the wifi device is't esp32. -* Step3: Set a sender and a receiver. Set one of them sending data and the other receiving. +* Send or receive. Set one of them sending data and the other receiving. -* Step4: Save your settings and make bin file downloading to flash. +Step2: Exit menuconfig, saving the new settings. Then build the app and flash it to the ESP32. -* Step5: Start test. - -Here are some things that might help you do the test easily. +Step3: Start test. And here are some things that might help you do the test easily. * You'd better turn on the AP before the STA. -* The tcp sever should be built before the tcp client. -* If you use a esp32 as AP, you'd better use it as tcp sever also. -* Once the tcp connection crashed, esp32 should be restarted to rebuild tcp connection. +* The tcp server should be started before the tcp client. +* If you use a esp32 as AP, you'd better use it as tcp server also. +* Once the tcp connection crashed, esp32 should be restarted to re-establish TCP connection. -After connection established, TCP sever and TCP client can send data to each other. The result of throughput will show by COM. +Step4: View the result. After connection established, TCP server and TCP client can send data to each other. The result of throughput will be printed in the serial log. -Explaining more in [main.c](./tcp_perf/main/main.c). +See [main.c](./tcp_perf/main/main.c) for full details. # udp_perf @@ -37,9 +35,9 @@ This example is similar to tcp_perf. Also the steps is similar to tcp_perf. There's a obvious difference between udp_perf and tcp perf: -Before formal sending & receiving, a packet will be send from client to sever. So the sever can konw the ip&port of client. Maybe it's easily to use if you set the udp sever as receiver. +Before formal sending & receiving, a packet will be send from client to server. So the server can know the ip&port of client. It is usually eaiser to set the UDP server as the receiver. -Explaining more in [main.c](./udp_perf/main/main.c). +See [main.c](./udp_perf/main/main.c) for full details. # More diff --git a/examples/performance/tcp_perf/main/Kconfig.projbuild b/examples/performance/tcp_perf/main/Kconfig.projbuild index c832f5667..92bf8387f 100644 --- a/examples/performance/tcp_perf/main/Kconfig.projbuild +++ b/examples/performance/tcp_perf/main/Kconfig.projbuild @@ -1,7 +1,7 @@ menu "Example Configuration" #choice -# prompt "TCP_PERF_MODE " +# prompt "TCP_PERF_MODE " # default MODE_TCP_SHIELDBOX # help # This option set performance mode. @@ -21,64 +21,98 @@ menu "Example Configuration" # bool "Performance in long distance" #endchoice -config TCP_PERF_WIFI_MODE_AP - bool "softap mode enable" - default n - help - yes:ESP32 is softap. no:ESP32 is station. +choice TCP_PERF_WIFI_MODE + prompt "AP or STA" + default TCP_PERF_ESP_IS_STATION + help + Whether the esp32 is softAP or station. -config TCP_PERF_SEVER - bool "TCP performance sever enable" - default n - help - yes:ESP32 is TCP sever. no:ESP32 is TCP client. +config TCP_PERF_ESP_IS_SOFTAP + bool "SoftAP" +config TCP_PERF_ESP_IS_STATION + bool "Station" +endchoice + +config TCP_PERF_WIFI_MODE_AP + bool + default y if TCP_PERF_ESP_IS_SOFTAP + default n if TCP_PERF_ESP_IS_STATION + +choice TCP_PERF_SERVER_CLIENT + prompt "server or client" + default TCP_PERF_ESP_IS_CLIENT + help + Whether the esp32 is tcp server or client. - We suggest to make this config be same with "Station mode". + We suggest to choose "client" if you choose "station" in "wifi mode". + +config TCP_PERF_ESP_IS_SERVER + bool "server" +config TCP_PERF_ESP_IS_CLIENT + bool "client" +endchoice + +config TCP_PERF_SERVER + bool + default y if TCP_PERF_ESP_IS_SERVER + default n if TCP_PERF_ESP_IS_CLIENT + +choice TCP_PERF_TX_RX + prompt "send or receive" + default TCP_PERF_ESP_RECV + help + Whether the esp32 will send or receive. + +config TCP_PERF_ESP_SEND + bool "send" +config TCP_PERF_ESP_RECV + bool "receive" +endchoice config TCP_PERF_TX - bool "TCP performance TX test enable" - default n - help - yes:TCP TX test. no:TCP RX test. + bool + default y if TCP_PERF_ESP_SEND + default n if TCP_PERF_ESP_RECV config TCP_PERF_DELAY_DEBUG bool "TCP performance delay info enable" - default n - help - Show TCP performance delay info. + depends on TCP_PERF_TX + default n + help + Show TCP performance delay info. - Ignore in TCP RX. + Ignore in TCP RX. config TCP_PERF_WIFI_SSID string "WiFi SSID" - default "esp_wifi_test1" - help - SSID (network name) for the example to connect to. + default "esp_wifi_test1" + help + SSID (network name) for the example to connect to. config TCP_PERF_WIFI_PASSWORD string "WiFi Password" - default "1234567890" - help - WiFi password (WPA or WPA2) for the example to use. + default "1234567890" + help + WiFi password (WPA or WPA2) for the example to use. -config TCP_PERF_SEVER_PORT - int "TCP sever port" - default 4567 - help - Which will the tcp sever use. +config TCP_PERF_SERVER_PORT + int "TCP server port" + default 4567 + help + Which will the tcp server use. config TCP_PERF_SERVER_IP string "TCP server ip" - default "192.168.4.1" - help - IP of TCP server. + default "192.168.4.1" + help + IP of TCP server. - Ignore in TCP sever. + Ignore in TCP server. config TCP_PERF_PKT_SIZE int "Size of TCP packet" - default 1460 - help - the data send&recv packet size. + default 1460 + help + the data send&recv packet size. endmenu diff --git a/examples/performance/tcp_perf/main/main.c b/examples/performance/tcp_perf/main/tcp_main.c similarity index 70% rename from examples/performance/tcp_perf/main/main.c rename to examples/performance/tcp_perf/main/tcp_main.c index 65811ff39..fbb65c0f0 100644 --- a/examples/performance/tcp_perf/main/main.c +++ b/examples/performance/tcp_perf/main/tcp_main.c @@ -10,13 +10,13 @@ step1: init wifi as AP/STA using config SSID/PASSWORD. step2: - creat a tcp sever/client socket using config PORT/(IP). - if sever: wating for connect. - if client connect to sever. + create a tcp server/client socket using config PORT/(IP). + if server: wating for connect. + if client connect to server. step3: send/receive data to/from each other. if the tcp connect established. esp will send or receive data. - you can see the info in com port output. + you can see the info in serial output. */ #include @@ -27,17 +27,7 @@ step3: #include "tcp_perf.h" -int connectedflag = 0; -int totle_data = 0; -#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO - -int totle_pack = 0; -int send_success = 0; -int send_fail = 0; -int delay_classify[5] = { 0 }; - -#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ //this task establish a TCP connection and receive data from TCP static void tcp_conn(void *pvParameters) @@ -52,19 +42,19 @@ static void tcp_conn(void *pvParameters) ESP_LOGI(TAG, "sta has connected to ap."); /*create tcp socket*/ - int socret; + int socket_ret; -#if ESP_TCP_MODE_SEVER +#if ESP_TCP_MODE_SERVER vTaskDelay(3000 / portTICK_RATE_MS); - ESP_LOGI(TAG, "creat_tcp_sever."); - socret=creat_tcp_sever(); -#else /*ESP_TCP_MODE_SEVER*/ + ESP_LOGI(TAG, "create_tcp_server."); + socket_ret=create_tcp_server(); +#else /*ESP_TCP_MODE_SERVER*/ vTaskDelay(20000 / portTICK_RATE_MS); - ESP_LOGI(TAG, "creat_tcp_client."); - socret = creat_tcp_client(); + ESP_LOGI(TAG, "create_tcp_client."); + socket_ret = create_tcp_client(); #endif - if(ESP_FAIL == socret) { - ESP_LOGI(TAG, "creat tcp socket error,stop."); + if(ESP_FAIL == socket_ret) { + ESP_LOGI(TAG, "create tcp socket error,stop."); vTaskDelete(NULL); } @@ -77,10 +67,10 @@ static void tcp_conn(void *pvParameters) #endif int pps; while (1) { - totle_data = 0; + total_data = 0; vTaskDelay(3000 / portTICK_RATE_MS);//every 3s - pps = totle_data / 3; - if (totle_data <= 0) { + pps = total_data / 3; + if (total_data <= 0) { int err_ret = check_socket_error_code(); if (err_ret == ECONNRESET) { ESP_LOGI(TAG, "disconnected... stop."); @@ -91,9 +81,9 @@ static void tcp_conn(void *pvParameters) #if ESP_TCP_PERF_TX ESP_LOGI(TAG, "tcp send %d byte per sec!", pps); #if ESP_TCP_DELAY_INFO - ESP_LOGI(TAG, "tcp send packet totle:%d succeed:%d failed:%d\n" + ESP_LOGI(TAG, "tcp send packet total:%d succeed:%d failed:%d\n" "time(ms):0-30:%d 30-100:%d 100-300:%d 300-1000:%d 1000+:%d\n", - totle_pack, send_success, send_fail, delay_classify[0], + total_pack, send_success, send_fail, delay_classify[0], delay_classify[1], delay_classify[2], delay_classify[3], delay_classify[4]); #endif /*ESP_TCP_DELAY_INFO*/ #else diff --git a/examples/performance/tcp_perf/main/tcp_perf.c b/examples/performance/tcp_perf/main/tcp_perf.c index 6d1e8cb76..0b8447ee9 100644 --- a/examples/performance/tcp_perf/main/tcp_perf.c +++ b/examples/performance/tcp_perf/main/tcp_perf.c @@ -1,4 +1,16 @@ - +// Copyright 2013-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include #include @@ -12,11 +24,24 @@ #include "tcp_perf.h" /*socket*/ -static int sever_socket = 0; -static struct sockaddr_in sever_addr; +static int server_socket = 0; +static struct sockaddr_in server_addr; static struct sockaddr_in client_addr; static unsigned int socklen = sizeof(client_addr); -static int connect_soc = 0; +static int connect_socket = 0; + +int connectedflag = 0; +int total_data = 0; + +#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO + +int total_pack = 0; +int send_success = 0; +int send_fail = 0; +int delay_classify[5] = { 0 }; + +#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ + static esp_err_t event_handler(void *ctx, system_event_t *event) { @@ -70,25 +95,23 @@ void send_data(void *pvParameters) while(1) { #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO - //vTaskDelay(1000/portTICK_RATE_MS); - totle_pack++; + total_pack++; gettimeofday(&tv_start, NULL); #endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ //send function - len = send(connect_soc, databuff, DEFAULT_PKTSIZE, 0); + len = send(connect_socket, databuff, DEFAULT_PKTSIZE, 0); #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO gettimeofday(&tv_finish, NULL); #endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ if(len > 0) { - totle_data += len; + total_data += len; #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO send_success++; send_delay_ms = (tv_finish.tv_sec - tv_start.tv_sec) * 1000 + (tv_finish.tv_usec - tv_start.tv_usec) / 1000; - //ESP_LOGI(TAG, "send_delay_ms=%ld",send_delay_ms); if(send_delay_ms < 30) delay_classify[0]++; else if(send_delay_ms < 100) @@ -108,10 +131,11 @@ void send_data(void *pvParameters) send_fail++; #endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ - /*for faster sending,don't show error code - *if it can't work as expectations,unnote the two lines here + /*Most of the error code will be send window full. + *So, for faster sending,don't show error code. + *if it can't work as expectations,unnote the two lines here. **/ - //perror("data_count error"); + //show_socket_error_code(connect_socket); //vTaskDelay(500/portTICK_RATE_MS); } } @@ -122,48 +146,44 @@ void recv_data(void *pvParameters) int len = 0; char databuff[DEFAULT_PKTSIZE]; while (1) { - len = recv(connect_soc, databuff, DEFAULT_PKTSIZE, 0); + len = recv(connect_socket, databuff, DEFAULT_PKTSIZE, 0); if (len > 0) { - totle_data += len; + total_data += len; } else { - show_socket_error_code(connect_soc); + show_socket_error_code(connect_socket); vTaskDelay(500 / portTICK_RATE_MS); } } } -//use this esp32 as a tcp sever. return ESP_OK:success ESP_FAIL:error -int creat_tcp_sever() +//use this esp32 as a tcp server. return ESP_OK:success ESP_FAIL:error +esp_err_t create_tcp_server() { - ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT); - sever_socket = socket(AF_INET, SOCK_STREAM, 0); - if (sever_socket < 0) { - show_socket_error_code(sever_socket); - //perror("socket() error:"); + ESP_LOGI(TAG, "server socket....port=%d\n", DEFAULT_PORT); + server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (server_socket < 0) { + show_socket_error_code(server_socket); return ESP_FAIL; } - sever_addr.sin_family = AF_INET; - sever_addr.sin_port = htons(DEFAULT_PORT); - sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(sever_socket, (struct sockaddr*)&sever_addr, sizeof(sever_addr)) < 0) { - show_socket_error_code(sever_socket); - //perror("bind() error"); - close(sever_socket); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(DEFAULT_PORT); + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { + show_socket_error_code(server_socket); + close(server_socket); return ESP_FAIL; } - if (listen(sever_socket, 5) < 0) { - show_socket_error_code(sever_socket); - //perror("listen() error"); - close(sever_socket); + if (listen(server_socket, 5) < 0) { + show_socket_error_code(server_socket); + close(server_socket); return ESP_FAIL; } - connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen); - if (connect_soc<0) { - show_socket_error_code(connect_soc); - //perror("accept() error"); - close(sever_socket); + connect_socket = accept(server_socket, (struct sockaddr*)&client_addr, &socklen); + if (connect_socket<0) { + show_socket_error_code(connect_socket); + close(server_socket); return ESP_FAIL; } /*connection established,now can send/recv*/ @@ -171,26 +191,24 @@ int creat_tcp_sever() return ESP_OK; } //use this esp32 as a tcp client. return ESP_OK:success ESP_FAIL:error -int creat_tcp_client() +esp_err_t create_tcp_client() { - ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n", - DEFAULT_SEVER_IP, DEFAULT_PORT); - connect_soc = socket(AF_INET, SOCK_STREAM, 0); - if (connect_soc < 0) { - show_socket_error_code(connect_soc); - //perror("socket failed!"); + ESP_LOGI(TAG, "client socket....serverip:port=%s:%d\n", + DEFAULT_SERVER_IP, DEFAULT_PORT); + connect_socket = socket(AF_INET, SOCK_STREAM, 0); + if (connect_socket < 0) { + show_socket_error_code(connect_socket); return ESP_FAIL; } - sever_addr.sin_family = AF_INET; - sever_addr.sin_port = htons(DEFAULT_PORT); - sever_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP); - ESP_LOGI(TAG, "connecting to sever..."); - if (connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) { - show_socket_error_code(connect_soc); - //perror("connect to sever error!"); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(DEFAULT_PORT); + server_addr.sin_addr.s_addr = inet_addr(DEFAULT_SERVER_IP); + ESP_LOGI(TAG, "connecting to server..."); + if (connect(connect_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + show_socket_error_code(connect_socket); return ESP_FAIL; } - ESP_LOGI(TAG, "connect to sever success!"); + ESP_LOGI(TAG, "connect to server success!"); return ESP_OK; } @@ -251,72 +269,71 @@ void wifi_init_softap() char* tcpip_get_reason(int err) { - switch (err) { + switch (err) { case 0: - return "reason: other reason"; + return "reason: other reason"; case ENOMEM: - return "reason: out of memory"; + return "reason: out of memory"; case ENOBUFS: - return "reason: buffer error"; + return "reason: buffer error"; case EWOULDBLOCK: - return "reason: timeout, try again"; + return "reason: timeout, try again"; case EHOSTUNREACH: - return "reason: routing problem"; + return "reason: routing problem"; case EINPROGRESS: - return "reason: operation in progress"; + return "reason: operation in progress"; case EINVAL: - return "reason: invalid value"; + return "reason: invalid value"; case EADDRINUSE: - return "reason: address in use"; + return "reason: address in use"; case EALREADY: - return "reason: conn already connected"; + return "reason: conn already connected"; case EISCONN: - return "reason: conn already established"; + return "reason: conn already established"; case ECONNABORTED: - return "reason: connection aborted"; + return "reason: connection aborted"; case ECONNRESET: - return "reason: connection is reset"; + return "reason: connection is reset"; case ENOTCONN: - return "reason: connection closed"; + return "reason: connection closed"; case EIO: - return "reason: invalid argument"; + return "reason: invalid argument"; case -1: - return "reason: low level netif error"; + return "reason: low level netif error"; default: - return "reason not found"; - } + return "reason not found"; + } } -int show_socket_error_code(int soc) +int show_socket_error_code(int socket) { - int result; + int result; u32_t optlen = sizeof(int); - getsockopt(soc, SOL_SOCKET, SO_ERROR, &result, &optlen); - ESP_LOGI(TAG, "soc error %d reason: %s", result, tcpip_get_reason(result)); + getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen); + ESP_LOGI(TAG, "socket error %d reason: %s", result, tcpip_get_reason(result)); return result; } int check_socket_error_code() { - int ret; -#if ESP_TCP_MODE_SEVER - ESP_LOGI(TAG, "check sever_socket sever_socket"); - ret = show_socket_error_code(sever_socket); - if(ret == ECONNRESET) - return ret; + int ret; +#if ESP_TCP_MODE_SERVER + ESP_LOGI(TAG, "check server_socket"); + ret = show_socket_error_code(server_socket); + if(ret == ECONNRESET) + return ret; #endif - ESP_LOGI(TAG, "check sever_socket connect_soc"); - ret = show_socket_error_code(connect_soc); - if(ret == ECONNRESET) - return ret; - return 0; + ESP_LOGI(TAG, "check connect_socket"); + ret = show_socket_error_code(connect_socket); + if(ret == ECONNRESET) + return ret; + return 0; } void close_socket() { - close(connect_soc); - close(sever_socket); + close(connect_socket); + close(server_socket); } - diff --git a/examples/performance/tcp_perf/main/tcp_perf.h b/examples/performance/tcp_perf/main/tcp_perf.h index 3b7ba0b60..e29215b5b 100644 --- a/examples/performance/tcp_perf/main/tcp_perf.h +++ b/examples/performance/tcp_perf/main/tcp_perf.h @@ -7,16 +7,16 @@ extern "C" { #endif -/*AP info and tcp_sever info*/ +/*AP info and tcp_server info*/ #define DEFAULT_SSID CONFIG_TCP_PERF_WIFI_SSID #define DEFAULT_PWD CONFIG_TCP_PERF_WIFI_PASSWORD -#define DEFAULT_PORT CONFIG_TCP_PERF_SEVER_PORT -#define DEFAULT_SEVER_IP CONFIG_TCP_PERF_SERVER_IP +#define DEFAULT_PORT CONFIG_TCP_PERF_SERVER_PORT +#define DEFAULT_SERVER_IP CONFIG_TCP_PERF_SERVER_IP #define DEFAULT_PKTSIZE CONFIG_TCP_PERF_PKT_SIZE #define MAX_STA_CONN 1 //how many sta can be connected(AP mode) /*test options*/ #define ESP_WIFI_MODE_AP CONFIG_TCP_PERF_WIFI_MODE_AP //TRUE:AP FALSE:STA -#define ESP_TCP_MODE_SEVER CONFIG_TCP_PERF_SEVER //TRUE:sever FALSE:client +#define ESP_TCP_MODE_SERVER CONFIG_TCP_PERF_SERVER //TRUE:server FALSE:client #define ESP_TCP_PERF_TX CONFIG_TCP_PERF_TX //TRUE:send FALSE:receive #define ESP_TCP_DELAY_INFO CONFIG_TCP_PERF_DELAY_DEBUG //TRUE:show delay time info @@ -26,10 +26,10 @@ extern "C" { extern int connectedflag; -extern int totle_data; +extern int total_data; #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO -extern int totle_pack; +extern int total_pack; extern int send_success; extern int send_fail; extern int delay_classify[5]; @@ -41,10 +41,10 @@ void wifi_init_sta(); //using esp as softap void wifi_init_softap(); -//creat a tcp sever socket. return 0:success -1:error -int creat_tcp_sever(); -//creat a tcp client socket. return 0:success -1:error -int creat_tcp_client(); +//create a tcp server socket. return ESP_OK:success ESP_FAIL:error +esp_err_t create_tcp_server(); +//create a tcp client socket. return ESP_OK:success ESP_FAIL:error +esp_err_t create_tcp_client(); //send data task void send_data(void *pvParameters); @@ -55,7 +55,7 @@ void recv_data(void *pvParameters); void close_socket(); //show socket error code. return: error code -int show_socket_error_code(int soc); +int show_socket_error_code(int socket); //check working socket int check_socket_error_code(); diff --git a/examples/performance/udp_perf/main/Kconfig.projbuild b/examples/performance/udp_perf/main/Kconfig.projbuild index 5567ed2cd..72d40cb98 100644 --- a/examples/performance/udp_perf/main/Kconfig.projbuild +++ b/examples/performance/udp_perf/main/Kconfig.projbuild @@ -1,8 +1,8 @@ menu "Example Configuration" #choice -# prompt "TCP_PERF_MODE " -# default MODE_TCP_SHIELDBOX +# prompt "UDP_PERF_MODE " +# default MODE_UDP_SHIELDBOX # help # This option set performance mode. # @@ -21,25 +21,60 @@ menu "Example Configuration" # bool "Performance in long distance" #endchoice # -config UDP_PERF_WIFI_MODE_AP - bool "softap mode enable" - default n - help - yes:ESP32 is softap. no:ESP32 is station. -config UDP_PERF_SEVER - bool "TCP performance sever enable" - default y - help - yes:ESP32 is UDP sever. no:ESP32 is UDP client. +choice UDP_PERF_WIFI_MODE + prompt "AP or STA" + default UDP_PERF_ESP_IS_STATION + help + Whether the esp32 is softAP or station. + +config UDP_PERF_ESP_IS_SOFTAP + bool "SoftAP" +config UDP_PERF_ESP_IS_STATION + bool "Station" +endchoice + +config UDP_PERF_WIFI_MODE_AP + bool + default y if UDP_PERF_ESP_IS_SOFTAP + default n if UDP_PERF_ESP_IS_STATION + +choice UDP_PERF_SERVER_CLIENT + prompt "server or client" + default UDP_PERF_ESP_IS_CLIENT + help + Whether the esp32 is tcp server or client. - We suggest to make this config be same with "Station mode". + We suggest to choose "client" if you choose "station" in "wifi mode". + +config UDP_PERF_ESP_IS_SERVER + bool "server" +config UDP_PERF_ESP_IS_CLIENT + bool "client" +endchoice + +config UDP_PERF_SERVER + bool + default y if UDP_PERF_ESP_IS_SERVER + default n if UDP_PERF_ESP_IS_CLIENT + +choice UDP_PERF_TX_RX + prompt "send or receive" + default UDP_PERF_ESP_RECV + help + Whether the esp32 will send or receive. + +config UDP_PERF_ESP_SEND + bool "send" +config UDP_PERF_ESP_RECV + bool "receive" +endchoice config UDP_PERF_TX - bool "UDP performance TX test enable" - default n - help - yes:UDP TX test. no:UDP RX test. + bool + default y if UDP_PERF_ESP_SEND + default n if UDP_PERF_ESP_RECV + config UDP_PERF_WIFI_SSID string "WiFi SSID" @@ -53,11 +88,11 @@ config UDP_PERF_WIFI_PASSWORD help WiFi password (WPA or WPA2) for the example to use. -config UDP_PERF_SEVER_PORT - int "UDP sever port" +config UDP_PERF_SERVER_PORT + int "UDP server port" default 4567 help - Which will the udp sever use. + Which will the udp server use. config UDP_PERF_SERVER_IP string "UDP server ip" @@ -65,7 +100,7 @@ config UDP_PERF_SERVER_IP help IP of UDP server. - Ignore in UDP sever. + Ignore in UDP server. config UDP_PERF_PKT_SIZE int "Size of UDP packet" diff --git a/examples/performance/udp_perf/main/main.c b/examples/performance/udp_perf/main/udp_main.c similarity index 65% rename from examples/performance/udp_perf/main/main.c rename to examples/performance/udp_perf/main/udp_main.c index 0318ee6a3..60f59e9b3 100644 --- a/examples/performance/udp_perf/main/main.c +++ b/examples/performance/udp_perf/main/udp_main.c @@ -10,15 +10,16 @@ step1: init wifi as AP/STA using config SSID/PASSWORD. step2: - creat a udp sever/client socket using config PORT/(IP). - if sever: wating for the first message of client. - if client: sending a packet to sever first. + create a udp server/client socket using config PORT/(IP). + if server: wating for the first message of client. + if client: sending a packet to server first. step3: send/receive data to/from each other. - you can see the info in com port output. + you can see the info in serial output. */ + #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" @@ -26,9 +27,6 @@ step3: #include "udp_perf.h" -int connectedflag = 0; -int totle_data = 0; -int success_pack = 0; //this task establish a UDP connection and receive data from UDP static void udp_conn(void *pvParameters) @@ -43,20 +41,19 @@ static void udp_conn(void *pvParameters) ESP_LOGI(TAG, "sta has connected to ap."); /*create udp socket*/ - int socret; + int socket_ret; -#if ESP_UDP_MODE_SEVER +#if ESP_UDP_MODE_SERVER vTaskDelay(3000 / portTICK_RATE_MS); - ESP_LOGI(TAG, "creat_udp_sever."); - socret=creat_udp_sever(); - //vTaskDelay(1000/portTICK_RATE_MS); -#else /*ESP_UDP_MODE_SEVER*/ + ESP_LOGI(TAG, "create_udp_server."); + socket_ret=create_udp_server(); +#else /*ESP_UDP_MODE_SERVER*/ vTaskDelay(20000 / portTICK_RATE_MS); - ESP_LOGI(TAG, "creat_udp_client."); - socret = creat_udp_client(); + ESP_LOGI(TAG, "create_udp_client."); + socket_ret = create_udp_client(); #endif - if(ESP_FAIL == socret) { - ESP_LOGI(TAG, "creat udp socket error,stop."); + if(ESP_FAIL == socket_ret) { + ESP_LOGI(TAG, "create udp socket error,stop."); vTaskDelete(NULL); } @@ -66,14 +63,14 @@ static void udp_conn(void *pvParameters) int pps; while (1) { - totle_data = 0; + total_data = 0; vTaskDelay(3000 / portTICK_RATE_MS);//every 3s - pps = totle_data / 3; + pps = total_data / 3; #if ESP_UDP_PERF_TX - ESP_LOGI(TAG, "udp send %d byte per sec! totle pack: %d \n", pps, success_pack); + ESP_LOGI(TAG, "udp send %d byte per sec! total pack: %d \n", pps, success_pack); #else - ESP_LOGI(TAG, "udp recv %d byte per sec! totle pack: %d \n", pps, success_pack); + ESP_LOGI(TAG, "udp recv %d byte per sec! total pack: %d \n", pps, success_pack); #endif /*ESP_UDP_PERF_TX*/ } close_socket(); diff --git a/examples/performance/udp_perf/main/udp_perf.c b/examples/performance/udp_perf/main/udp_perf.c index c3cb339c3..22a55ad90 100644 --- a/examples/performance/udp_perf/main/udp_perf.c +++ b/examples/performance/udp_perf/main/udp_perf.c @@ -1,3 +1,16 @@ +// Copyright 2013-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include #include @@ -17,6 +30,10 @@ static int mysocket; static struct sockaddr_in remote_addr; static unsigned int socklen; +int connectedflag = 0; +int total_data = 0; +int success_pack = 0; + static esp_err_t event_handler(void *ctx, system_event_t *event) { @@ -105,40 +122,40 @@ void wifi_init_softap() DEFAULT_SSID, DEFAULT_PWD); } -//creat a udp sever socket. return ESP_OK:success ESP_FAIL:error -int creat_udp_sever() +//create a udp server socket. return ESP_OK:success ESP_FAIL:error +esp_err_t create_udp_server() { - ESP_LOGI(TAG, "creat_udp_sever()"); + ESP_LOGI(TAG, "create_udp_server()"); mysocket = socket(AF_INET, SOCK_DGRAM, 0); if (mysocket < 0) { - perror("socket failed:"); + show_socket_error_code(mysocket); return ESP_FAIL; } - struct sockaddr_in sever_addr; - sever_addr.sin_family = AF_INET; - sever_addr.sin_port = htons(DEFAULT_PORT); - sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(mysocket, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) { - perror("bind local port error:"); + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(DEFAULT_PORT); + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind(mysocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + show_socket_error_code(mysocket); close(mysocket); return ESP_FAIL; } return ESP_OK; } -//creat a udp client socket. return ESP_OK:success ESP_FAIL:error -int creat_udp_client() +//create a udp client socket. return ESP_OK:success ESP_FAIL:error +esp_err_t create_udp_client() { - ESP_LOGI(TAG, "creat_udp_client()"); + ESP_LOGI(TAG, "create_udp_client()"); mysocket = socket(AF_INET, SOCK_DGRAM, 0); if (mysocket < 0) { - perror("socket failed:"); + show_socket_error_code(mysocket); return ESP_FAIL; } - /*for client remote_addr is also sever_addr*/ + /*for client remote_addr is also server_addr*/ remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(DEFAULT_PORT); - remote_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP); + remote_addr.sin_addr.s_addr = inet_addr(DEFAULT_SERVER_IP); return ESP_OK; } @@ -155,7 +172,7 @@ void send_recv_data(void *pvParameters) /*send&receive first packet*/ socklen = sizeof(remote_addr); memset(databuff, PACK_BYTE_IS, DEFAULT_PKTSIZE); -#if ESP_UDP_MODE_SEVER +#if ESP_UDP_MODE_SERVER ESP_LOGI(TAG, "first recvfrom:"); len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen); #else @@ -167,7 +184,7 @@ void send_recv_data(void *pvParameters) ESP_LOGI(TAG, "transfer data with %s:%u\n", inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port)); } else { - perror("first recv&send error:"); + show_socket_error_code(mysocket); close(mysocket); vTaskDelete(NULL); } @@ -185,13 +202,11 @@ void send_recv_data(void *pvParameters) #else len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen); #endif - //printf("%d\n",len); - //vTaskDelay(100/portTICK_RATE_MS); if (len > 0) { - totle_data += len; + total_data += len; success_pack++; } else { - //perror("data_count:\n"); + //show_socket_error_code(mysocket); /*you'd better turn off watch dog in menuconfig *Component config->ESP32-specific->Task watchdog. **/ @@ -201,6 +216,54 @@ void send_recv_data(void *pvParameters) } +char* tcpip_get_reason(int err) +{ + switch (err) { + case 0: + return "reason: other reason"; + case ENOMEM: + return "reason: out of memory"; + case ENOBUFS: + return "reason: buffer error"; + case EWOULDBLOCK: + return "reason: timeout, try again"; + case EHOSTUNREACH: + return "reason: routing problem"; + case EINPROGRESS: + return "reason: operation in progress"; + case EINVAL: + return "reason: invalid value"; + case EADDRINUSE: + return "reason: address in use"; + case EALREADY: + return "reason: conn already connected"; + case EISCONN: + return "reason: conn already established"; + case ECONNABORTED: + return "reason: connection aborted"; + case ECONNRESET: + return "reason: connection is reset"; + case ENOTCONN: + return "reason: connection closed"; + case EIO: + return "reason: invalid argument"; + case -1: + return "reason: low level netif error"; + default: + return "reason not found"; + } +} + +int show_socket_error_code(int socket) +{ + int result; + u32_t optlen = sizeof(int); + getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen); + ESP_LOGI(TAG, "socket error %d reason: %s", result, tcpip_get_reason(result)); + return result; +} + + void close_socket() { close(mysocket); diff --git a/examples/performance/udp_perf/main/udp_perf.h b/examples/performance/udp_perf/main/udp_perf.h index ecafcf9be..0f8221559 100644 --- a/examples/performance/udp_perf/main/udp_perf.h +++ b/examples/performance/udp_perf/main/udp_perf.h @@ -7,16 +7,16 @@ extern "C" { #endif -/*AP info and tcp_sever info*/ +/*AP info and tcp_server info*/ #define DEFAULT_SSID CONFIG_UDP_PERF_WIFI_SSID #define DEFAULT_PWD CONFIG_UDP_PERF_WIFI_PASSWORD -#define DEFAULT_PORT CONFIG_UDP_PERF_SEVER_PORT -#define DEFAULT_SEVER_IP CONFIG_UDP_PERF_SERVER_IP +#define DEFAULT_PORT CONFIG_UDP_PERF_SERVER_PORT +#define DEFAULT_SERVER_IP CONFIG_UDP_PERF_SERVER_IP #define DEFAULT_PKTSIZE CONFIG_UDP_PERF_PKT_SIZE #define MAX_STA_CONN 1 //how many sta can be connected(AP mode) /*test options*/ #define ESP_WIFI_MODE_AP CONFIG_UDP_PERF_WIFI_MODE_AP //TRUE:AP FALSE:STA -#define ESP_UDP_MODE_SEVER CONFIG_UDP_PERF_SEVER //TRUE:sever FALSE:client +#define ESP_UDP_MODE_SERVER CONFIG_UDP_PERF_SERVER //TRUE:server FALSE:client #define ESP_UDP_PERF_TX CONFIG_UDP_PERF_TX //TRUE:send FALSE:receive #define PACK_BYTE_IS 97 //'a' @@ -24,7 +24,7 @@ extern "C" { extern int connectedflag; -extern int totle_data; +extern int total_data; extern int success_pack; @@ -33,14 +33,17 @@ void wifi_init_sta(); //using esp as softap void wifi_init_softap(); -//creat a udp sever socket. return 0:success -1:error -int creat_udp_sever(); -//creat a udp client socket. return 0:success -1:error -int creat_udp_client(); +//create a udp server socket. return ESP_OK:success ESP_FAIL:error +esp_err_t create_udp_server(); +//create a udp client socket. return ESP_OK:success ESP_FAIL:error +esp_err_t create_udp_client(); //send or recv data task void send_recv_data(void *pvParameters); +//show socket error code. return: error code +int show_socket_error_code(int socket); + //close all socket void close_socket();