eg/perf:modify README.

This commit is contained in:
chenyudong 2017-03-23 16:36:32 +08:00
parent 8a163e3db0
commit e261e16981
7 changed files with 154 additions and 51 deletions

View File

@ -8,13 +8,24 @@ Including TCP/UDP TX/RX throughput.
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. 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. Steps:
So the tcp_perf can be used in following scenes: * 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.
* esp32 to Router (using esp32 as STA) * 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.
* esp32 to Wifi adaptor (using esp32 as AP)
* esp32 to esp32 (using one of them as AP, the other STA) * 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. After connection established, TCP sever and TCP client can send data to each other. The result of throughput will show by COM.
@ -22,14 +33,13 @@ Explaining more in [main.c](./tcp_perf/main/main.c).
# udp_perf # udp_perf
Similar with tcp_perf. This example is similar to tcp_perf. Also the steps is similar to tcp_perf.
There are some points need to notice. There's a obvious difference between udp_perf and tcp perf:
* A packet will be send from client to sever.So the sever can konw the ip&port of client. 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.
* To easy use this example, it's better to use udp sever as recviver.
Explaining more in [main.c](./udp_perf/main/main.c).
# More # More

View File

@ -49,9 +49,11 @@ static void tcp_conn(void *pvParameters)
int socret; int socret;
#if ESP_TCP_MODE_SEVER #if ESP_TCP_MODE_SEVER
vTaskDelay(3000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "creat_tcp_sever."); ESP_LOGI(TAG, "creat_tcp_sever.");
socret=creat_tcp_sever(); socret=creat_tcp_sever();
#else /*ESP_TCP_MODE_SEVER*/ #else /*ESP_TCP_MODE_SEVER*/
vTaskDelay(20000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "creat_tcp_client."); ESP_LOGI(TAG, "creat_tcp_client.");
socret = creat_tcp_client(); socret = creat_tcp_client();
#endif #endif
@ -72,6 +74,13 @@ static void tcp_conn(void *pvParameters)
totle_data = 0; totle_data = 0;
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
pps = totle_data / 3; 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 #if ESP_TCP_PERF_TX
ESP_LOGI(TAG, "tcp send %d byte per sec!", pps); ESP_LOGI(TAG, "tcp send %d byte per sec!", pps);

View File

@ -4,11 +4,11 @@
/* FreeRTOS event group to signal when we are connected & ready to make a request */ /* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group; static EventGroupHandle_t wifi_event_group;
/*socket*/ /*socket*/
static int sever_socket; static int sever_socket = 0;
static struct sockaddr_in sever_addr; static struct sockaddr_in sever_addr;
static struct sockaddr_in client_addr; static struct sockaddr_in client_addr;
static unsigned int socklen = sizeof(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) static esp_err_t event_handler(void *ctx, system_event_t *event)
{ {
@ -37,6 +37,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
ESP_LOGI(TAG, "station:"MACSTR"leave,AID=%d\n", ESP_LOGI(TAG, "station:"MACSTR"leave,AID=%d\n",
MAC2STR(event->event_info.sta_disconnected.mac), MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid); event->event_info.sta_disconnected.aid);
connectedflag = 0;
break; break;
default: default:
break; break;
@ -118,7 +119,7 @@ void recv_data(void *pvParameters)
totle_data += len; totle_data += len;
} }
else { else {
perror("recv_data error"); show_socket_error_code(connect_soc);
vTaskDelay(500 / portTICK_RATE_MS); vTaskDelay(500 / portTICK_RATE_MS);
} }
} }
@ -131,25 +132,29 @@ int creat_tcp_sever()
ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT); ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT);
sever_socket = socket(AF_INET, SOCK_STREAM, 0); sever_socket = socket(AF_INET, SOCK_STREAM, 0);
if (sever_socket < 0) { if (sever_socket < 0) {
perror("socket() error:"); show_socket_error_code(sever_socket);
//perror("socket() error:");
return -1; return -1;
} }
sever_addr.sin_family = AF_INET; sever_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULT_PORT); sever_addr.sin_port = htons(DEFAULT_PORT);
sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); sever_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sever_socket, (struct sockaddr*)&sever_addr, sizeof(sever_addr)) < 0) { 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); close(sever_socket);
return -1; return -1;
} }
if (listen(sever_socket, 5) < 0) { if (listen(sever_socket, 5) < 0) {
perror("listen() error"); show_socket_error_code(sever_socket);
//perror("listen() error");
close(sever_socket); close(sever_socket);
return -1; return -1;
} }
connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen); connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen);
if (connect_soc<0) { if (connect_soc<0) {
perror("accept() error"); show_socket_error_code(connect_soc);
//perror("accept() error");
close(sever_socket); close(sever_socket);
return -1; return -1;
} }
@ -164,7 +169,8 @@ int creat_tcp_client()
DEFAULT_SEVER_IP, DEFAULT_PORT); DEFAULT_SEVER_IP, DEFAULT_PORT);
connect_soc = socket(AF_INET, SOCK_STREAM, 0); connect_soc = socket(AF_INET, SOCK_STREAM, 0);
if (connect_soc < 0) { if (connect_soc < 0) {
perror("socket failed!"); show_socket_error_code(connect_soc);
//perror("socket failed!");
return -1; return -1;
} }
sever_addr.sin_family = AF_INET; 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); sever_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP);
ESP_LOGI(TAG, "connecting to sever..."); ESP_LOGI(TAG, "connecting to sever...");
if (connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) { 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; return -1;
} }
ESP_LOGI(TAG, "connect to sever success!"); ESP_LOGI(TAG, "connect to sever success!");
@ -221,6 +228,9 @@ void wifi_init_softap()
.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_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); 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); 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() void close_socket()
{ {
close(connect_soc); close(connect_soc);

View File

@ -70,8 +70,11 @@ void recv_data(void *pvParameters);
//close all socket //close all socket
void close_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 #ifdef __cplusplus

View File

@ -3,7 +3,7 @@
# project subdirectory. # project subdirectory.
# #
PROJECT_NAME := udp_esp2ap PROJECT_NAME := udp_perf
include $(IDF_PATH)/make/project.mk include $(IDF_PATH)/make/project.mk

View File

@ -41,10 +41,12 @@ static void udp_conn(void *pvParameters)
int socret; int socret;
#if ESP_UDP_MODE_SEVER #if ESP_UDP_MODE_SEVER
vTaskDelay(3000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "creat_udp_sever."); ESP_LOGI(TAG, "creat_udp_sever.");
socret=creat_udp_sever(); socret=creat_udp_sever();
//vTaskDelay(1000/portTICK_RATE_MS); //vTaskDelay(1000/portTICK_RATE_MS);
#else /*ESP_UDP_MODE_SEVER*/ #else /*ESP_UDP_MODE_SEVER*/
vTaskDelay(20000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "creat_udp_client."); ESP_LOGI(TAG, "creat_udp_client.");
socret = creat_udp_client(); socret = creat_udp_client();
#endif #endif

View File

@ -87,6 +87,9 @@ void wifi_init_softap()
.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_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));