From e261e16981b9f78ea5b7ef1bffbf10c917cab85a Mon Sep 17 00:00:00 2001 From: chenyudong Date: Thu, 23 Mar 2017 16:36:32 +0800 Subject: [PATCH] eg/perf:modify README. --- examples/performance/README.md | 82 +++++++------- examples/performance/tcp_perf/main/main.c | 9 ++ examples/performance/tcp_perf/main/tcp_perf.c | 102 +++++++++++++++--- examples/performance/tcp_perf/main/tcp_perf.h | 5 +- examples/performance/udp_perf/Makefile | 2 +- examples/performance/udp_perf/main/main.c | 2 + examples/performance/udp_perf/main/udp_perf.c | 3 + 7 files changed, 154 insertions(+), 51 deletions(-) diff --git a/examples/performance/README.md b/examples/performance/README.md index af7aae006a..f5f9e6e0e1 100644 --- a/examples/performance/README.md +++ b/examples/performance/README.md @@ -1,36 +1,46 @@ -# Wifi Performance Examples - -Some simple codes help to test the wifi performance. - -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. - -You can set esp32 as AP/STA, client/sever, sender/receiver. Make sure that STAcan connect to AP with your configuration in menuconfig. - -So the tcp_perf can be used in following scenes: - -* esp32 to Router (using esp32 as STA) -* esp32 to Wifi adaptor (using esp32 as AP) -* esp32 to esp32 (using one of them as AP, the other STA) - -After connection established, TCP sever and TCP client can send data to each other. The result of throughput will show by COM. - -Explaining more in [main.c](./tcp_perf/main/main.c). - -# udp_perf - -Similar with tcp_perf. - -There are some points need to notice. - -* A packet will be send from client to sever.So the sever can konw the ip&port of client. - -* To easy use this example, it's better to use udp sever as recviver. - - -# More - -See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples. +# Wifi Performance Examples + +Some simple codes help to test the wifi performance. + +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. + +Steps: + +* 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. + +* 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. + +* Step3: Set a sender and a receiver. Set one of them sending data and the other receiving. + +* Step4: Save your settings and make bin file downloading to flash. + +* Step5: Start test. + +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. + +After connection established, TCP sever and TCP client can send data to each other. The result of throughput will show by COM. + +Explaining more in [main.c](./tcp_perf/main/main.c). + +# udp_perf + +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. + +Explaining more in [main.c](./udp_perf/main/main.c). + +# More + +See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples. diff --git a/examples/performance/tcp_perf/main/main.c b/examples/performance/tcp_perf/main/main.c index d08220f7ac..eafb1b3714 100644 --- a/examples/performance/tcp_perf/main/main.c +++ b/examples/performance/tcp_perf/main/main.c @@ -49,9 +49,11 @@ static void tcp_conn(void *pvParameters) int socret; #if ESP_TCP_MODE_SEVER + vTaskDelay(3000 / portTICK_RATE_MS); ESP_LOGI(TAG, "creat_tcp_sever."); socret=creat_tcp_sever(); #else /*ESP_TCP_MODE_SEVER*/ + vTaskDelay(20000 / portTICK_RATE_MS); ESP_LOGI(TAG, "creat_tcp_client."); socret = creat_tcp_client(); #endif @@ -72,6 +74,13 @@ static void tcp_conn(void *pvParameters) totle_data = 0; vTaskDelay(3000 / portTICK_RATE_MS);//every 3s pps = totle_data / 3; + if (totle_data <= 0) { + int err_ret = check_socket_error_code(); + if (err_ret == ECONNRESET) { + ESP_LOGI(TAG, "disconnected... stop."); + break; + } + } #if ESP_TCP_PERF_TX ESP_LOGI(TAG, "tcp send %d byte per sec!", pps); diff --git a/examples/performance/tcp_perf/main/tcp_perf.c b/examples/performance/tcp_perf/main/tcp_perf.c index 458ebfe138..1384dd47d6 100644 --- a/examples/performance/tcp_perf/main/tcp_perf.c +++ b/examples/performance/tcp_perf/main/tcp_perf.c @@ -4,11 +4,11 @@ /* FreeRTOS event group to signal when we are connected & ready to make a request */ static EventGroupHandle_t wifi_event_group; /*socket*/ -static int sever_socket; +static int sever_socket = 0; static struct sockaddr_in sever_addr; static struct sockaddr_in client_addr; static unsigned int socklen = sizeof(client_addr); -static int connect_soc; +static int connect_soc = 0; static esp_err_t event_handler(void *ctx, system_event_t *event) { @@ -25,18 +25,19 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) ESP_LOGI(TAG, "event_handler:SYSTEM_EVENT_STA_GOT_IP!"); ESP_LOGI(TAG, "got ip:%s\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - connectedflag=1; + connectedflag = 1; break; case SYSTEM_EVENT_AP_STACONNECTED: ESP_LOGI(TAG, "station:"MACSTR" join,AID=%d\n", MAC2STR(event->event_info.sta_connected.mac), event->event_info.sta_connected.aid); - connectedflag=1; + connectedflag = 1; break; case SYSTEM_EVENT_AP_STADISCONNECTED: ESP_LOGI(TAG, "station:"MACSTR"leave,AID=%d\n", MAC2STR(event->event_info.sta_disconnected.mac), event->event_info.sta_disconnected.aid); + connectedflag = 0; break; default: break; @@ -118,7 +119,7 @@ void recv_data(void *pvParameters) totle_data += len; } else { - perror("recv_data error"); + show_socket_error_code(connect_soc); vTaskDelay(500 / portTICK_RATE_MS); } } @@ -131,25 +132,29 @@ int creat_tcp_sever() ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT); sever_socket = socket(AF_INET, SOCK_STREAM, 0); if (sever_socket < 0) { - perror("socket() error:"); + show_socket_error_code(sever_socket); + //perror("socket() error:"); return -1; } 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) { - perror("bind() error"); + show_socket_error_code(sever_socket); + //perror("bind() error"); close(sever_socket); return -1; } if (listen(sever_socket, 5) < 0) { - perror("listen() error"); + show_socket_error_code(sever_socket); + //perror("listen() error"); close(sever_socket); return -1; } connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen); if (connect_soc<0) { - perror("accept() error"); + show_socket_error_code(connect_soc); + //perror("accept() error"); close(sever_socket); return -1; } @@ -164,7 +169,8 @@ int creat_tcp_client() DEFAULT_SEVER_IP, DEFAULT_PORT); connect_soc = socket(AF_INET, SOCK_STREAM, 0); if (connect_soc < 0) { - perror("socket failed!"); + show_socket_error_code(connect_soc); + //perror("socket failed!"); return -1; } sever_addr.sin_family = AF_INET; @@ -172,7 +178,8 @@ int creat_tcp_client() 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) { - perror("connect to sever error!"); + show_socket_error_code(connect_soc); + //perror("connect to sever error!"); return -1; } ESP_LOGI(TAG, "connect to sever success!"); @@ -215,12 +222,15 @@ void wifi_init_softap() wifi_config_t wifi_config = { .ap = { .ssid = DEFAULT_SSID, - .ssid_len=0, + .ssid_len = 0, .max_connection=MAX_STA_CONN, .password = DEFAULT_PWD, - .authmode=WIFI_AUTH_WPA_WPA2_PSK + .authmode = WIFI_AUTH_WPA_WPA2_PSK }, }; + if (strlen(DEFAULT_PWD) ==0) { + wifi_config.ap.authmode = WIFI_AUTH_OPEN; + } ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); @@ -230,6 +240,72 @@ void wifi_init_softap() DEFAULT_SSID, DEFAULT_PWD); } + + + +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 soc) +{ + 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)); + 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; +#endif + ESP_LOGI(TAG, "check sever_socket connect_soc"); + ret = show_socket_error_code(connect_soc); + if(ret == ECONNRESET) + return ret; + return 0; +} + void close_socket() { close(connect_soc); diff --git a/examples/performance/tcp_perf/main/tcp_perf.h b/examples/performance/tcp_perf/main/tcp_perf.h index 2ddc177625..a6b70cf90f 100644 --- a/examples/performance/tcp_perf/main/tcp_perf.h +++ b/examples/performance/tcp_perf/main/tcp_perf.h @@ -70,8 +70,11 @@ void recv_data(void *pvParameters); //close all socket void close_socket(); +//show socket error code. return: error code +int show_socket_error_code(int soc); - +//check working socket +int check_socket_error_code(); #ifdef __cplusplus diff --git a/examples/performance/udp_perf/Makefile b/examples/performance/udp_perf/Makefile index c17add00b6..97d6eaa137 100644 --- a/examples/performance/udp_perf/Makefile +++ b/examples/performance/udp_perf/Makefile @@ -3,7 +3,7 @@ # project subdirectory. # -PROJECT_NAME := udp_esp2ap +PROJECT_NAME := udp_perf include $(IDF_PATH)/make/project.mk diff --git a/examples/performance/udp_perf/main/main.c b/examples/performance/udp_perf/main/main.c index 545a47d554..10213063db 100644 --- a/examples/performance/udp_perf/main/main.c +++ b/examples/performance/udp_perf/main/main.c @@ -41,10 +41,12 @@ static void udp_conn(void *pvParameters) int socret; #if ESP_UDP_MODE_SEVER + 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*/ + vTaskDelay(20000 / portTICK_RATE_MS); ESP_LOGI(TAG, "creat_udp_client."); socret = creat_udp_client(); #endif diff --git a/examples/performance/udp_perf/main/udp_perf.c b/examples/performance/udp_perf/main/udp_perf.c index ebaa5e018d..9b37fd1626 100644 --- a/examples/performance/udp_perf/main/udp_perf.c +++ b/examples/performance/udp_perf/main/udp_perf.c @@ -87,6 +87,9 @@ void wifi_init_softap() .authmode=WIFI_AUTH_WPA_WPA2_PSK }, }; + if (strlen(DEFAULT_PWD) ==0) { + wifi_config.ap.authmode = WIFI_AUTH_OPEN; + } ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));