From 99578f1bd00660bf8b95624b5a6c16a4bd1cce69 Mon Sep 17 00:00:00 2001 From: yudongchen95 Date: Mon, 13 Mar 2017 19:29:24 +0800 Subject: [PATCH 1/7] wifi performance: add wifi performance examples including TCP&UDP TX/RX --- examples/performance/TCP_recv/Makefile | 9 + examples/performance/TCP_recv/main/TCP_recv.c | 153 ++++++++++++++++ .../performance/TCP_recv/main/component.mk | 8 + examples/performance/TCP_send/Makefile | 9 + examples/performance/TCP_send/main/TCP_send.c | 137 ++++++++++++++ .../performance/TCP_send/main/component.mk | 8 + examples/performance/UDP_recv/Makefile | 9 + examples/performance/UDP_recv/main/UDP_recv.c | 173 ++++++++++++++++++ .../performance/UDP_recv/main/component.mk | 8 + examples/performance/UDP_send/Makefile | 9 + examples/performance/UDP_send/main/UDP_send.c | 173 ++++++++++++++++++ .../performance/UDP_send/main/component.mk | 8 + 12 files changed, 704 insertions(+) create mode 100644 examples/performance/TCP_recv/Makefile create mode 100644 examples/performance/TCP_recv/main/TCP_recv.c create mode 100644 examples/performance/TCP_recv/main/component.mk create mode 100644 examples/performance/TCP_send/Makefile create mode 100644 examples/performance/TCP_send/main/TCP_send.c create mode 100644 examples/performance/TCP_send/main/component.mk create mode 100644 examples/performance/UDP_recv/Makefile create mode 100644 examples/performance/UDP_recv/main/UDP_recv.c create mode 100644 examples/performance/UDP_recv/main/component.mk create mode 100644 examples/performance/UDP_send/Makefile create mode 100644 examples/performance/UDP_send/main/UDP_send.c create mode 100644 examples/performance/UDP_send/main/component.mk diff --git a/examples/performance/TCP_recv/Makefile b/examples/performance/TCP_recv/Makefile new file mode 100644 index 000000000..c6e082658 --- /dev/null +++ b/examples/performance/TCP_recv/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := TCP_recv + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/performance/TCP_recv/main/TCP_recv.c b/examples/performance/TCP_recv/main/TCP_recv.c new file mode 100644 index 000000000..66d48e0d6 --- /dev/null +++ b/examples/performance/TCP_recv/main/TCP_recv.c @@ -0,0 +1,153 @@ + + +/* +TCP_recv example +this example will receive data as much as it can, +and calculate how much data it has received per second. +*/ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include +#include +#include "driver/uart.h" +#include "soc/uart_struct.h" + +/*AP info and tcp_sever info*/ +//#define DEFAULTSSID "wifi-12" +//#define DEFAULTPWD "sumof1+1=2" +#define DEFAULTSSID "tp_wifi_test" +#define DEFAULTPWD "1234567890" +#define DEFAULTPORT 4567 +#define DEFAULTSEVER "192.168.2.183" +#define BUFFSIZE 1024 +static int sizepersec=0; + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; + +/**/ +static int connectedflag = 0; +static int clientsocket; +static struct sockaddr_in serverAddr; + + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + //printf("event_handler:SYSTEM_EVENT_STA_START\n"); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); + break; + case SYSTEM_EVENT_STA_GOT_IP: + printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); + connectedflag=1; + break; + default: + break; + } + return ESP_OK; +} +//wifi_init +static void wifi_init() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULTSSID, + .password = DEFAULTPWD + }, + }; + + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_start() ); + esp_wifi_connect(); + + printf("wifi_init over.\n"); +} + + +static void recvdata(void *pvParameters) +{ + + int len=0; + char buffrev[BUFFSIZE]; + memset(buffrev,0,sizeof(buffrev)); + while(1) + { + len=recv(clientsocket, buffrev, BUFFSIZE, 0); + if(len>0) + { + sizepersec+=len; + } + } +} +//this task establish a tcp connection and recv data from tcp +static void tcp_client(void *pvParameters) +{ + TaskHandle_t tasksend; + do + { + vTaskDelay(100); + } + while(!connectedflag); + //wating for connecting to AP + + clientsocket = socket(AF_INET, SOCK_STREAM, 0); + if(clientsocket < 0) + { + printf("socket failed!\n"); + vTaskDelete(NULL); + } + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(DEFAULTPORT); + serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER); + + printf("connecting to sever...\n"); + if(connect(clientsocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) + { + printf("connect to sever error!\n"); + vTaskDelete(NULL); + } + + printf("connect succeed!now can recv&send!\n"); + + //create a new task... + send(clientsocket, "hello", 6, 0); + xTaskCreate(&recvdata,"recvdata",4096,NULL,5,&tasksend); + + while(1) + { + sizepersec=0; + //calc every 3s + vTaskDelay(3000/ portTICK_RATE_MS); + printf("tcp recv %d byte per sec!\n",sizepersec/3); + } +} + + +void app_main(void) +{ + nvs_flash_init(); + wifi_init(); + xTaskCreate(&tcp_client,"tcp_client",4096,NULL,5,NULL); +} \ No newline at end of file diff --git a/examples/performance/TCP_recv/main/component.mk b/examples/performance/TCP_recv/main/component.mk new file mode 100644 index 000000000..61f8990c3 --- /dev/null +++ b/examples/performance/TCP_recv/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# diff --git a/examples/performance/TCP_send/Makefile b/examples/performance/TCP_send/Makefile new file mode 100644 index 000000000..273925f45 --- /dev/null +++ b/examples/performance/TCP_send/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := TCP_send + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/performance/TCP_send/main/TCP_send.c b/examples/performance/TCP_send/main/TCP_send.c new file mode 100644 index 000000000..cc4fd5dcb --- /dev/null +++ b/examples/performance/TCP_send/main/TCP_send.c @@ -0,0 +1,137 @@ + + +/* +TCP_send example +this example will creat a TCP connect, +and send data constantly. +*/ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include +#include +#include "driver/uart.h" +#include "soc/uart_struct.h" + +/*AP info and tcp_sever info*/ +//#define DEFAULTSSID "wifi-12" +//#define DEFAULTPWD "sumof1+1=2" +#define DEFAULTSSID "tp_wifi_test1" +#define DEFAULTPWD "1234567890" +#define DEFAULTPORT 4567 +#define DEFAULTSEVER "192.168.0.102" +#define BUFFSIZE 1024 + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; + +/**/ +static int connectedflag = 0; +static int clientsocket; +static struct sockaddr_in serverAddr; + + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + //printf("event_handler:SYSTEM_EVENT_STA_START\n"); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); + break; + case SYSTEM_EVENT_STA_GOT_IP: + printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); + connectedflag=1; + break; + default: + break; + } + return ESP_OK; +} +//wifi_init +static void wifi_init() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULTSSID, + .password = DEFAULTPWD + }, + }; + + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_start() ); + esp_wifi_connect(); + + printf("wifi_init over.\n"); +} + + +//this task establish a tcp connection and send data +static void tcp_client(void *pvParameters) +{ + do + { + vTaskDelay(100); + } + while(!connectedflag); + //wating for connecting to AP + + clientsocket = socket(AF_INET, SOCK_STREAM, 0); + if(clientsocket < 0) + { + printf("socket failed!\n"); + vTaskDelete(NULL); + } + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(DEFAULTPORT); + serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER); + + printf("connecting to sever...\n"); + if(connect(clientsocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) + { + printf("connect to sever error!\n"); + vTaskDelete(NULL); + } + + printf("connect succeed!\n"); + + //create a new task... + //send(clientsocket, "hello", 6, 0); + + vTaskDelay(1000/portTICK_RATE_MS); + char buffsend[BUFFSIZE]; + memset(buffsend,97,BUFFSIZE);//'a'=97 + //send as much as it can + while(1) + { + send(clientsocket, buffsend, BUFFSIZE, 0); + //vTaskDelay(3000/portTICK_RATE_MS); + } +} + + +void app_main(void) +{ + nvs_flash_init(); + wifi_init(); + xTaskCreate(&tcp_client,"tcp_client",4096,NULL,5,NULL); +} \ No newline at end of file diff --git a/examples/performance/TCP_send/main/component.mk b/examples/performance/TCP_send/main/component.mk new file mode 100644 index 000000000..61f8990c3 --- /dev/null +++ b/examples/performance/TCP_send/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# diff --git a/examples/performance/UDP_recv/Makefile b/examples/performance/UDP_recv/Makefile new file mode 100644 index 000000000..887cf127c --- /dev/null +++ b/examples/performance/UDP_recv/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := UDP_recv + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/performance/UDP_recv/main/UDP_recv.c b/examples/performance/UDP_recv/main/UDP_recv.c new file mode 100644 index 000000000..9dd5e939f --- /dev/null +++ b/examples/performance/UDP_recv/main/UDP_recv.c @@ -0,0 +1,173 @@ + + +/* +UDP_Client RECV +This Demo use esp32 as station, +when esp32 start,it will connect to an AP and will create a UDP socket and receive data. +And calculate speed of receiving data. +*/ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include +#include +#include "driver/uart.h" +#include "soc/uart_struct.h" + +/*AP info and tcp_sever info*/ +//#define DEFAULTSSID "wifi-12" +//#define DEFAULTPWD "sumof1+1=2" +#define DEFAULTSSID "tp_wifi_test1" +#define DEFAULTPWD "1234567890" +#define DEFAULTPORT 4567 +#define DEFAULTSEVER "192.168.0.102" +#define DEFAULTLOCALPORT 7654 //bind local port so it can be easy to debug +#define BUFFSIZE 1024 +static int sizepersec=0; + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; + +/**/ +static int connectedflag = 0; +static int clientsocket; +static struct sockaddr_in serverAddr; + + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + //printf("event_handler:SYSTEM_EVENT_STA_START\n"); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); + break; + case SYSTEM_EVENT_STA_GOT_IP: + printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); + printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + connectedflag=1; + break; + default: + break; + } + return ESP_OK; +} +//wifi_init +static void wifi_init() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULTSSID, + .password = DEFAULTPWD + }, + }; + + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_start() ); + esp_wifi_connect(); + + printf("wifi_init over.\n"); +} + + +static void recvdata(void *pvParameters) +{ + + int len=0; + char buffrev[BUFFSIZE]; + unsigned int socklen; + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(DEFAULTPORT); + serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER); + printf("recvdata!\n"); + socklen=sizeof(serverAddr); + if(sendto(clientsocket, "hello", 6, 0,(struct sockaddr *)&serverAddr,socklen)<0) + { + perror("sendto hello:"); + } + memset(buffrev,0,sizeof(buffrev)); + while(1) + { + len=recvfrom(clientsocket, buffrev, BUFFSIZE, 0,(struct sockaddr *)&serverAddr,&socklen); + //printf("len= %d\n",len); + if(len>0) + { + sizepersec+=len; + //printf("%s\n",buffrev); + } + else if(len<0) + { + perror("recvfrom:\n"); + vTaskDelay(100/portTICK_RATE_MS); + } + } +} +//this task establish a udp connection and send/recv data +static void udp_connect(void *pvParameters) +{ + TaskHandle_t tasksend; + do + { + vTaskDelay(100); + } + while(!connectedflag); + //wating for connecting to AP + + clientsocket = socket(AF_INET, SOCK_DGRAM, 0); + if(clientsocket < 0) + { + printf("socket failed!\n"); + vTaskDelete(NULL); + } + struct sockaddr_in myaddr; + myaddr.sin_family=AF_INET; + myaddr.sin_port=htons(DEFAULTLOCALPORT); + if(bind(clientsocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0) + { + printf("bind local port error!\n"); + vTaskDelete(NULL); + } + + + vTaskDelay(2000/portTICK_RATE_MS); + + //create a new task... + + xTaskCreate(&recvdata,"recvdata",4096,NULL,5,&tasksend); + + while(1) + { + sizepersec=0; + vTaskDelay(3000/ portTICK_RATE_MS); + printf("udp recv %d byte per sec!\n",sizepersec/3); + } + + vTaskDelete(NULL); +} + + +void app_main(void) +{ + nvs_flash_init(); + wifi_init(); + xTaskCreate(&udp_connect,"udp_connect",4096,NULL,5,NULL); +} \ No newline at end of file diff --git a/examples/performance/UDP_recv/main/component.mk b/examples/performance/UDP_recv/main/component.mk new file mode 100644 index 000000000..61f8990c3 --- /dev/null +++ b/examples/performance/UDP_recv/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# diff --git a/examples/performance/UDP_send/Makefile b/examples/performance/UDP_send/Makefile new file mode 100644 index 000000000..40b189684 --- /dev/null +++ b/examples/performance/UDP_send/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := UDP_send + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/performance/UDP_send/main/UDP_send.c b/examples/performance/UDP_send/main/UDP_send.c new file mode 100644 index 000000000..4972d0e2a --- /dev/null +++ b/examples/performance/UDP_send/main/UDP_send.c @@ -0,0 +1,173 @@ + + +/* +UDP_Client send +This Demo use esp32 as station, +when esp32 start,it will connect to an AP and will create a UDP socket, +and send data constantly. +*/ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include +#include +#include "driver/uart.h" +#include "soc/uart_struct.h" + +/*AP info and tcp_sever info*/ +//#define DEFAULTSSID "wifi-12" +//#define DEFAULTPWD "sumof1+1=2" +#define DEFAULTSSID "tp_wifi_test1" +#define DEFAULTPWD "1234567890" +#define DEFAULTPORT 4567 +#define DEFAULTSEVER "192.168.0.102" +#define DEFAULTLOCALPORT 7654 //bind local port so it can be easy to debug +#define BUFFSIZE 1024 +static int sizepersec=0; + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; + +/**/ +static int connectedflag = 0; +static int clientsocket; +static struct sockaddr_in serverAddr; + + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + //printf("event_handler:SYSTEM_EVENT_STA_START\n"); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); + break; + case SYSTEM_EVENT_STA_GOT_IP: + printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); + printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + connectedflag=1; + break; + default: + break; + } + return ESP_OK; +} +//wifi_init +static void wifi_init() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULTSSID, + .password = DEFAULTPWD + }, + }; + + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_start() ); + esp_wifi_connect(); + + printf("wifi_init over.\n"); +} + + +static void senddata(void *pvParameters) +{ + + int len=0; + char buffsend[BUFFSIZE]; + unsigned int socklen; + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(DEFAULTPORT); + serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER); + printf("senddata!\n"); + socklen=sizeof(serverAddr); + if(sendto(clientsocket, "hello", 6, 0,(struct sockaddr *)&serverAddr,socklen)<0) + { + perror("sendto hello:"); + } + memset(buffsend,97,sizeof(buffsend)); + while(1) + { + len=sendto(clientsocket, buffsend, sizeof(buffsend), 0,(struct sockaddr *)&serverAddr,socklen); + //printf("len= %d\n",len); + if(len>0) + { + sizepersec+=len; + //printf("%s\n",buffrev); + } + else if(len<0)//the error code will be "not enough space" if send data constantly. + { + //perror("sendto:\n"); + vTaskDelay(1/portTICK_RATE_MS); + } + } +} +//this task establish a udp connection and send/recv data +static void udp_connect(void *pvParameters) +{ + TaskHandle_t tasksend; + do + { + vTaskDelay(100); + } + while(!connectedflag); + //wating for connecting to AP + + clientsocket = socket(AF_INET, SOCK_DGRAM, 0); + if(clientsocket < 0) + { + printf("socket failed!\n"); + vTaskDelete(NULL); + } + struct sockaddr_in myaddr; + myaddr.sin_family=AF_INET; + myaddr.sin_port=htons(DEFAULTLOCALPORT); + if(bind(clientsocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0) + { + printf("bind local port error!\n"); + vTaskDelete(NULL); + } + + + vTaskDelay(3000/portTICK_RATE_MS); + + //create a new task... + + xTaskCreate(&senddata,"senddata",4096,NULL,5,&tasksend); + + while(1) + { + sizepersec=0; + vTaskDelay(3000/ portTICK_RATE_MS); + printf("udp send %d byte per sec!\n",sizepersec/3); + } + + vTaskDelete(NULL); +} + + +void app_main(void) +{ + nvs_flash_init(); + wifi_init(); + xTaskCreate(&udp_connect,"udp_connect",4096,NULL,5,NULL); +} \ No newline at end of file diff --git a/examples/performance/UDP_send/main/component.mk b/examples/performance/UDP_send/main/component.mk new file mode 100644 index 000000000..61f8990c3 --- /dev/null +++ b/examples/performance/UDP_send/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# From a0b565a8e8ff2d1639fd29f81351aacb9658e1c6 Mon Sep 17 00:00:00 2001 From: yudongchen95 Date: Thu, 16 Mar 2017 15:04:44 +0800 Subject: [PATCH 2/7] customization parameters in menuconfig,combine codes to two project. --- examples/performance/README.md | 7 + examples/performance/TCP_recv/main/TCP_recv.c | 153 -------------- examples/performance/TCP_send/Makefile | 9 - examples/performance/TCP_send/main/TCP_send.c | 137 ------------ examples/performance/UDP_recv/main/UDP_recv.c | 173 --------------- .../performance/UDP_recv/main/component.mk | 8 - examples/performance/UDP_send/Makefile | 9 - examples/performance/UDP_send/main/UDP_send.c | 173 --------------- .../performance/UDP_send/main/component.mk | 8 - .../{TCP_recv => tcp_esp2ap}/Makefile | 2 +- .../tcp_esp2ap/main/Kconfig.projbuild | 51 +++++ .../main/component.mk | 0 .../performance/tcp_esp2ap/main/tcp_esp2ap.c | 189 +++++++++++++++++ .../{UDP_recv => udp_esp2ap}/Makefile | 2 +- .../udp_esp2ap/main/Kconfig.projbuild | 43 ++++ .../main/component.mk | 0 .../performance/udp_esp2ap/main/udp_esp2ap.c | 199 ++++++++++++++++++ 17 files changed, 491 insertions(+), 672 deletions(-) create mode 100644 examples/performance/README.md delete mode 100644 examples/performance/TCP_recv/main/TCP_recv.c delete mode 100644 examples/performance/TCP_send/Makefile delete mode 100644 examples/performance/TCP_send/main/TCP_send.c delete mode 100644 examples/performance/UDP_recv/main/UDP_recv.c delete mode 100644 examples/performance/UDP_recv/main/component.mk delete mode 100644 examples/performance/UDP_send/Makefile delete mode 100644 examples/performance/UDP_send/main/UDP_send.c delete mode 100644 examples/performance/UDP_send/main/component.mk rename examples/performance/{TCP_recv => tcp_esp2ap}/Makefile (85%) create mode 100644 examples/performance/tcp_esp2ap/main/Kconfig.projbuild rename examples/performance/{TCP_recv => tcp_esp2ap}/main/component.mk (100%) create mode 100644 examples/performance/tcp_esp2ap/main/tcp_esp2ap.c rename examples/performance/{UDP_recv => udp_esp2ap}/Makefile (85%) create mode 100644 examples/performance/udp_esp2ap/main/Kconfig.projbuild rename examples/performance/{TCP_send => udp_esp2ap}/main/component.mk (100%) create mode 100644 examples/performance/udp_esp2ap/main/udp_esp2ap.c diff --git a/examples/performance/README.md b/examples/performance/README.md new file mode 100644 index 000000000..d79ee486e --- /dev/null +++ b/examples/performance/README.md @@ -0,0 +1,7 @@ +# Wifi Performance Examples + +Some simple codes help to test the wifi performance. +Including TCP/UDP TX/RX throughput. +You may need a TCP/UDP client on PC. + +See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples. diff --git a/examples/performance/TCP_recv/main/TCP_recv.c b/examples/performance/TCP_recv/main/TCP_recv.c deleted file mode 100644 index 66d48e0d6..000000000 --- a/examples/performance/TCP_recv/main/TCP_recv.c +++ /dev/null @@ -1,153 +0,0 @@ - - -/* -TCP_recv example -this example will receive data as much as it can, -and calculate how much data it has received per second. -*/ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include -#include -#include "driver/uart.h" -#include "soc/uart_struct.h" - -/*AP info and tcp_sever info*/ -//#define DEFAULTSSID "wifi-12" -//#define DEFAULTPWD "sumof1+1=2" -#define DEFAULTSSID "tp_wifi_test" -#define DEFAULTPWD "1234567890" -#define DEFAULTPORT 4567 -#define DEFAULTSEVER "192.168.2.183" -#define BUFFSIZE 1024 -static int sizepersec=0; - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -/**/ -static int connectedflag = 0; -static int clientsocket; -static struct sockaddr_in serverAddr; - - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - //printf("event_handler:SYSTEM_EVENT_STA_START\n"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); - break; - case SYSTEM_EVENT_STA_GOT_IP: - printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); - connectedflag=1; - break; - default: - break; - } - return ESP_OK; -} -//wifi_init -static void wifi_init() -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config = { - .sta = { - .ssid = DEFAULTSSID, - .password = DEFAULTPWD - }, - }; - - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); - esp_wifi_connect(); - - printf("wifi_init over.\n"); -} - - -static void recvdata(void *pvParameters) -{ - - int len=0; - char buffrev[BUFFSIZE]; - memset(buffrev,0,sizeof(buffrev)); - while(1) - { - len=recv(clientsocket, buffrev, BUFFSIZE, 0); - if(len>0) - { - sizepersec+=len; - } - } -} -//this task establish a tcp connection and recv data from tcp -static void tcp_client(void *pvParameters) -{ - TaskHandle_t tasksend; - do - { - vTaskDelay(100); - } - while(!connectedflag); - //wating for connecting to AP - - clientsocket = socket(AF_INET, SOCK_STREAM, 0); - if(clientsocket < 0) - { - printf("socket failed!\n"); - vTaskDelete(NULL); - } - serverAddr.sin_family = AF_INET; - serverAddr.sin_port = htons(DEFAULTPORT); - serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER); - - printf("connecting to sever...\n"); - if(connect(clientsocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) - { - printf("connect to sever error!\n"); - vTaskDelete(NULL); - } - - printf("connect succeed!now can recv&send!\n"); - - //create a new task... - send(clientsocket, "hello", 6, 0); - xTaskCreate(&recvdata,"recvdata",4096,NULL,5,&tasksend); - - while(1) - { - sizepersec=0; - //calc every 3s - vTaskDelay(3000/ portTICK_RATE_MS); - printf("tcp recv %d byte per sec!\n",sizepersec/3); - } -} - - -void app_main(void) -{ - nvs_flash_init(); - wifi_init(); - xTaskCreate(&tcp_client,"tcp_client",4096,NULL,5,NULL); -} \ No newline at end of file diff --git a/examples/performance/TCP_send/Makefile b/examples/performance/TCP_send/Makefile deleted file mode 100644 index 273925f45..000000000 --- a/examples/performance/TCP_send/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# - -PROJECT_NAME := TCP_send - -include $(IDF_PATH)/make/project.mk - diff --git a/examples/performance/TCP_send/main/TCP_send.c b/examples/performance/TCP_send/main/TCP_send.c deleted file mode 100644 index cc4fd5dcb..000000000 --- a/examples/performance/TCP_send/main/TCP_send.c +++ /dev/null @@ -1,137 +0,0 @@ - - -/* -TCP_send example -this example will creat a TCP connect, -and send data constantly. -*/ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include -#include -#include "driver/uart.h" -#include "soc/uart_struct.h" - -/*AP info and tcp_sever info*/ -//#define DEFAULTSSID "wifi-12" -//#define DEFAULTPWD "sumof1+1=2" -#define DEFAULTSSID "tp_wifi_test1" -#define DEFAULTPWD "1234567890" -#define DEFAULTPORT 4567 -#define DEFAULTSEVER "192.168.0.102" -#define BUFFSIZE 1024 - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -/**/ -static int connectedflag = 0; -static int clientsocket; -static struct sockaddr_in serverAddr; - - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - //printf("event_handler:SYSTEM_EVENT_STA_START\n"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); - break; - case SYSTEM_EVENT_STA_GOT_IP: - printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); - connectedflag=1; - break; - default: - break; - } - return ESP_OK; -} -//wifi_init -static void wifi_init() -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config = { - .sta = { - .ssid = DEFAULTSSID, - .password = DEFAULTPWD - }, - }; - - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); - esp_wifi_connect(); - - printf("wifi_init over.\n"); -} - - -//this task establish a tcp connection and send data -static void tcp_client(void *pvParameters) -{ - do - { - vTaskDelay(100); - } - while(!connectedflag); - //wating for connecting to AP - - clientsocket = socket(AF_INET, SOCK_STREAM, 0); - if(clientsocket < 0) - { - printf("socket failed!\n"); - vTaskDelete(NULL); - } - serverAddr.sin_family = AF_INET; - serverAddr.sin_port = htons(DEFAULTPORT); - serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER); - - printf("connecting to sever...\n"); - if(connect(clientsocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) - { - printf("connect to sever error!\n"); - vTaskDelete(NULL); - } - - printf("connect succeed!\n"); - - //create a new task... - //send(clientsocket, "hello", 6, 0); - - vTaskDelay(1000/portTICK_RATE_MS); - char buffsend[BUFFSIZE]; - memset(buffsend,97,BUFFSIZE);//'a'=97 - //send as much as it can - while(1) - { - send(clientsocket, buffsend, BUFFSIZE, 0); - //vTaskDelay(3000/portTICK_RATE_MS); - } -} - - -void app_main(void) -{ - nvs_flash_init(); - wifi_init(); - xTaskCreate(&tcp_client,"tcp_client",4096,NULL,5,NULL); -} \ No newline at end of file diff --git a/examples/performance/UDP_recv/main/UDP_recv.c b/examples/performance/UDP_recv/main/UDP_recv.c deleted file mode 100644 index 9dd5e939f..000000000 --- a/examples/performance/UDP_recv/main/UDP_recv.c +++ /dev/null @@ -1,173 +0,0 @@ - - -/* -UDP_Client RECV -This Demo use esp32 as station, -when esp32 start,it will connect to an AP and will create a UDP socket and receive data. -And calculate speed of receiving data. -*/ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include -#include -#include "driver/uart.h" -#include "soc/uart_struct.h" - -/*AP info and tcp_sever info*/ -//#define DEFAULTSSID "wifi-12" -//#define DEFAULTPWD "sumof1+1=2" -#define DEFAULTSSID "tp_wifi_test1" -#define DEFAULTPWD "1234567890" -#define DEFAULTPORT 4567 -#define DEFAULTSEVER "192.168.0.102" -#define DEFAULTLOCALPORT 7654 //bind local port so it can be easy to debug -#define BUFFSIZE 1024 -static int sizepersec=0; - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -/**/ -static int connectedflag = 0; -static int clientsocket; -static struct sockaddr_in serverAddr; - - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - //printf("event_handler:SYSTEM_EVENT_STA_START\n"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); - break; - case SYSTEM_EVENT_STA_GOT_IP: - printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); - printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - connectedflag=1; - break; - default: - break; - } - return ESP_OK; -} -//wifi_init -static void wifi_init() -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config = { - .sta = { - .ssid = DEFAULTSSID, - .password = DEFAULTPWD - }, - }; - - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); - esp_wifi_connect(); - - printf("wifi_init over.\n"); -} - - -static void recvdata(void *pvParameters) -{ - - int len=0; - char buffrev[BUFFSIZE]; - unsigned int socklen; - serverAddr.sin_family = AF_INET; - serverAddr.sin_port = htons(DEFAULTPORT); - serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER); - printf("recvdata!\n"); - socklen=sizeof(serverAddr); - if(sendto(clientsocket, "hello", 6, 0,(struct sockaddr *)&serverAddr,socklen)<0) - { - perror("sendto hello:"); - } - memset(buffrev,0,sizeof(buffrev)); - while(1) - { - len=recvfrom(clientsocket, buffrev, BUFFSIZE, 0,(struct sockaddr *)&serverAddr,&socklen); - //printf("len= %d\n",len); - if(len>0) - { - sizepersec+=len; - //printf("%s\n",buffrev); - } - else if(len<0) - { - perror("recvfrom:\n"); - vTaskDelay(100/portTICK_RATE_MS); - } - } -} -//this task establish a udp connection and send/recv data -static void udp_connect(void *pvParameters) -{ - TaskHandle_t tasksend; - do - { - vTaskDelay(100); - } - while(!connectedflag); - //wating for connecting to AP - - clientsocket = socket(AF_INET, SOCK_DGRAM, 0); - if(clientsocket < 0) - { - printf("socket failed!\n"); - vTaskDelete(NULL); - } - struct sockaddr_in myaddr; - myaddr.sin_family=AF_INET; - myaddr.sin_port=htons(DEFAULTLOCALPORT); - if(bind(clientsocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0) - { - printf("bind local port error!\n"); - vTaskDelete(NULL); - } - - - vTaskDelay(2000/portTICK_RATE_MS); - - //create a new task... - - xTaskCreate(&recvdata,"recvdata",4096,NULL,5,&tasksend); - - while(1) - { - sizepersec=0; - vTaskDelay(3000/ portTICK_RATE_MS); - printf("udp recv %d byte per sec!\n",sizepersec/3); - } - - vTaskDelete(NULL); -} - - -void app_main(void) -{ - nvs_flash_init(); - wifi_init(); - xTaskCreate(&udp_connect,"udp_connect",4096,NULL,5,NULL); -} \ No newline at end of file diff --git a/examples/performance/UDP_recv/main/component.mk b/examples/performance/UDP_recv/main/component.mk deleted file mode 100644 index 61f8990c3..000000000 --- a/examples/performance/UDP_recv/main/component.mk +++ /dev/null @@ -1,8 +0,0 @@ -# -# Main component makefile. -# -# This Makefile can be left empty. By default, it will take the sources in the -# src/ directory, compile them and link them into lib(subdirectory_name).a -# in the build directory. This behaviour is entirely configurable, -# please read the ESP-IDF documents if you need to do this. -# diff --git a/examples/performance/UDP_send/Makefile b/examples/performance/UDP_send/Makefile deleted file mode 100644 index 40b189684..000000000 --- a/examples/performance/UDP_send/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# - -PROJECT_NAME := UDP_send - -include $(IDF_PATH)/make/project.mk - diff --git a/examples/performance/UDP_send/main/UDP_send.c b/examples/performance/UDP_send/main/UDP_send.c deleted file mode 100644 index 4972d0e2a..000000000 --- a/examples/performance/UDP_send/main/UDP_send.c +++ /dev/null @@ -1,173 +0,0 @@ - - -/* -UDP_Client send -This Demo use esp32 as station, -when esp32 start,it will connect to an AP and will create a UDP socket, -and send data constantly. -*/ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include -#include -#include "driver/uart.h" -#include "soc/uart_struct.h" - -/*AP info and tcp_sever info*/ -//#define DEFAULTSSID "wifi-12" -//#define DEFAULTPWD "sumof1+1=2" -#define DEFAULTSSID "tp_wifi_test1" -#define DEFAULTPWD "1234567890" -#define DEFAULTPORT 4567 -#define DEFAULTSEVER "192.168.0.102" -#define DEFAULTLOCALPORT 7654 //bind local port so it can be easy to debug -#define BUFFSIZE 1024 -static int sizepersec=0; - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -/**/ -static int connectedflag = 0; -static int clientsocket; -static struct sockaddr_in serverAddr; - - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - //printf("event_handler:SYSTEM_EVENT_STA_START\n"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); - break; - case SYSTEM_EVENT_STA_GOT_IP: - printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); - printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - connectedflag=1; - break; - default: - break; - } - return ESP_OK; -} -//wifi_init -static void wifi_init() -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config = { - .sta = { - .ssid = DEFAULTSSID, - .password = DEFAULTPWD - }, - }; - - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); - esp_wifi_connect(); - - printf("wifi_init over.\n"); -} - - -static void senddata(void *pvParameters) -{ - - int len=0; - char buffsend[BUFFSIZE]; - unsigned int socklen; - serverAddr.sin_family = AF_INET; - serverAddr.sin_port = htons(DEFAULTPORT); - serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER); - printf("senddata!\n"); - socklen=sizeof(serverAddr); - if(sendto(clientsocket, "hello", 6, 0,(struct sockaddr *)&serverAddr,socklen)<0) - { - perror("sendto hello:"); - } - memset(buffsend,97,sizeof(buffsend)); - while(1) - { - len=sendto(clientsocket, buffsend, sizeof(buffsend), 0,(struct sockaddr *)&serverAddr,socklen); - //printf("len= %d\n",len); - if(len>0) - { - sizepersec+=len; - //printf("%s\n",buffrev); - } - else if(len<0)//the error code will be "not enough space" if send data constantly. - { - //perror("sendto:\n"); - vTaskDelay(1/portTICK_RATE_MS); - } - } -} -//this task establish a udp connection and send/recv data -static void udp_connect(void *pvParameters) -{ - TaskHandle_t tasksend; - do - { - vTaskDelay(100); - } - while(!connectedflag); - //wating for connecting to AP - - clientsocket = socket(AF_INET, SOCK_DGRAM, 0); - if(clientsocket < 0) - { - printf("socket failed!\n"); - vTaskDelete(NULL); - } - struct sockaddr_in myaddr; - myaddr.sin_family=AF_INET; - myaddr.sin_port=htons(DEFAULTLOCALPORT); - if(bind(clientsocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0) - { - printf("bind local port error!\n"); - vTaskDelete(NULL); - } - - - vTaskDelay(3000/portTICK_RATE_MS); - - //create a new task... - - xTaskCreate(&senddata,"senddata",4096,NULL,5,&tasksend); - - while(1) - { - sizepersec=0; - vTaskDelay(3000/ portTICK_RATE_MS); - printf("udp send %d byte per sec!\n",sizepersec/3); - } - - vTaskDelete(NULL); -} - - -void app_main(void) -{ - nvs_flash_init(); - wifi_init(); - xTaskCreate(&udp_connect,"udp_connect",4096,NULL,5,NULL); -} \ No newline at end of file diff --git a/examples/performance/UDP_send/main/component.mk b/examples/performance/UDP_send/main/component.mk deleted file mode 100644 index 61f8990c3..000000000 --- a/examples/performance/UDP_send/main/component.mk +++ /dev/null @@ -1,8 +0,0 @@ -# -# Main component makefile. -# -# This Makefile can be left empty. By default, it will take the sources in the -# src/ directory, compile them and link them into lib(subdirectory_name).a -# in the build directory. This behaviour is entirely configurable, -# please read the ESP-IDF documents if you need to do this. -# diff --git a/examples/performance/TCP_recv/Makefile b/examples/performance/tcp_esp2ap/Makefile similarity index 85% rename from examples/performance/TCP_recv/Makefile rename to examples/performance/tcp_esp2ap/Makefile index c6e082658..a4ca46adb 100644 --- a/examples/performance/TCP_recv/Makefile +++ b/examples/performance/tcp_esp2ap/Makefile @@ -3,7 +3,7 @@ # project subdirectory. # -PROJECT_NAME := TCP_recv +PROJECT_NAME := tcp_esp2ap include $(IDF_PATH)/make/project.mk diff --git a/examples/performance/tcp_esp2ap/main/Kconfig.projbuild b/examples/performance/tcp_esp2ap/main/Kconfig.projbuild new file mode 100644 index 000000000..39b0c8ee6 --- /dev/null +++ b/examples/performance/tcp_esp2ap/main/Kconfig.projbuild @@ -0,0 +1,51 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "tp_wifi_test1" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "1234567890" + help + WiFi password (WPA or WPA2) for the example to use. + +config SEVER_PORT + int "tcp sever port" + default 4567 + help + which will the tcp sever use. + +config BUFF_SIZE + int "buff size" + default 1024 + help + the data send&recv buff size. + +choice + prompt "test mode" + default PERFORMANCE_MODE + help + This option performance mode. + + - for "tcp receive" setting,it will receive data by tcp. + + - for "tcp send" setting, it will send data by tcp. + +# - for "udp receive" setting,it will receive data by udp. + +# - for "udp send" setting,it will send data by udp. + +config MODE_TCP_RECV + bool "tcp receive" +config MODE_TCP_SEND + bool "tcp send" +#config MODE_UDP_RECV +# bool "udp receive" +#config MODE_UDP_SEND +# bool "udp send" +endchoice + +endmenu diff --git a/examples/performance/TCP_recv/main/component.mk b/examples/performance/tcp_esp2ap/main/component.mk similarity index 100% rename from examples/performance/TCP_recv/main/component.mk rename to examples/performance/tcp_esp2ap/main/component.mk diff --git a/examples/performance/tcp_esp2ap/main/tcp_esp2ap.c b/examples/performance/tcp_esp2ap/main/tcp_esp2ap.c new file mode 100644 index 000000000..4e3ccc4f0 --- /dev/null +++ b/examples/performance/tcp_esp2ap/main/tcp_esp2ap.c @@ -0,0 +1,189 @@ + +/* +TCP_recv example +This example use esp32 as station and TCP sever, +when esp32 start,it will connect to an AP and will create a TCP socket as a sever, +use a TCP client connect to esp32, +then it will send/receive data,(set work mode in menuconfig) +And calculate the speed of sending/receiving data. +*/ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include +#include +#include "driver/uart.h" +#include "soc/uart_struct.h" + +/*AP info and tcp_sever info*/ +#define DEFAULTSSID CONFIG_WIFI_SSID +#define DEFAULTPWD CONFIG_WIFI_PASSWORD +#define DEFAULTPORT CONFIG_SEVER_PORT +#define BUFFSIZE CONFIG_BUFF_SIZE + +static int totle_data=0; + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; + +/**/ +static int connectedflag = 0; +static int sever_socket; +static struct sockaddr_in sever_addr; +static struct sockaddr_in client_addr; +static unsigned int socklen = sizeof(client_addr); +static int connect_soc; + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + //printf("event_handler:SYSTEM_EVENT_STA_START\n"); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); + break; + case SYSTEM_EVENT_STA_GOT_IP: + printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); + printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + connectedflag=1; + break; + default: + break; + } + return ESP_OK; +} +//wifi_init +static void wifi_init() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULTSSID, + .password = DEFAULTPWD + }, + }; + + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_start() ); + esp_wifi_connect(); + + printf("wifi_init over.\n"); +} + +static void data_count(void *pvParameters) +{ + int len=0; + char databuff[BUFFSIZE]; +#ifdef CONFIG_MODE_TCP_SEND + memset(databuff,97,BUFFSIZE); +#endif + while(1) + { +#ifdef CONFIG_MODE_TCP_SEND + len=send(connect_soc, databuff, BUFFSIZE, 0); +#else + len=recv(connect_soc, databuff, BUFFSIZE, 0); +#endif + if(len>0) + { + totle_data+=len; + } + else + { + /*for faster send&receive,don't show error code. + *if it can't work as expectations,unnote the two lines here + **/ + //perror("data_count error"); + //vTaskDelay(500/portTICK_RATE_MS); + } + } +} + +//this task establish a TCP connection and receive data from TCP +static void tcp_client(void *pvParameters) +{ + do + { + vTaskDelay(100); + } + while(!connectedflag); + //wating for connecting to AP + + printf("socket....port=%d\n",DEFAULTPORT); + sever_socket = socket(AF_INET, SOCK_STREAM, 0); + if(sever_socket < 0) + { + perror("socket() error:"); + vTaskDelete(NULL); + } + sever_addr.sin_family = AF_INET; + sever_addr.sin_port = htons(DEFAULTPORT); + sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); + if(bind(sever_socket,(struct sockaddr*)&sever_addr,sizeof(sever_addr))<0) + { + perror("bind() error"); + close(sever_socket); + vTaskDelete(NULL); + } + if(listen(sever_socket,5)<0) + { + perror("listen() error"); + close(sever_socket); + vTaskDelete(NULL); + } + connect_soc = accept(sever_socket,(struct sockaddr*)&client_addr,&socklen); + if(connect_soc<0) + { + perror("accept() error"); + close(sever_socket); + vTaskDelete(NULL); + } + /*connection established,now can send/recv*/ + printf("connection established!"); + TaskHandle_t tasksend; + xTaskCreate(&data_count,"data_count",4096,NULL,5,&tasksend); + + int pps; + while(1) + { + totle_data=0; + //calc every 3s + vTaskDelay(3000/ portTICK_RATE_MS); + pps=totle_data/3; +#ifdef CONFIG_MODE_TCP_SEND + printf("tcp send %d byte per sec!\n",pps); +#else + printf("tcp recv %d byte per sec!\n",pps); +#endif + } + vTaskDelete(tasksend); + close(connect_soc); + close(sever_socket); + vTaskDelete(NULL); +} + + +void app_main(void) +{ + nvs_flash_init(); + wifi_init(); + xTaskCreate(&tcp_client,"tcp_client",4096,NULL,5,NULL); +} diff --git a/examples/performance/UDP_recv/Makefile b/examples/performance/udp_esp2ap/Makefile similarity index 85% rename from examples/performance/UDP_recv/Makefile rename to examples/performance/udp_esp2ap/Makefile index 887cf127c..c17add00b 100644 --- a/examples/performance/UDP_recv/Makefile +++ b/examples/performance/udp_esp2ap/Makefile @@ -3,7 +3,7 @@ # project subdirectory. # -PROJECT_NAME := UDP_recv +PROJECT_NAME := udp_esp2ap include $(IDF_PATH)/make/project.mk diff --git a/examples/performance/udp_esp2ap/main/Kconfig.projbuild b/examples/performance/udp_esp2ap/main/Kconfig.projbuild new file mode 100644 index 000000000..59e86a0bd --- /dev/null +++ b/examples/performance/udp_esp2ap/main/Kconfig.projbuild @@ -0,0 +1,43 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "tp_wifi_test1" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "1234567890" + help + WiFi password (WPA or WPA2) for the example to use. + +config SEVER_PORT + int "tcp sever port" + default 4567 + help + which will the udp sever use. + +config BUFF_SIZE + int "buff size" + default 1024 + help + the data send&recv buff size. + +choice + prompt "test mode" + default PERFORMANCE_MODE + help + This option performance mode. + + - for "udp receive" setting,it will receive data by udp. + + - for "udp send" setting, it will send data by udp. + +config MODE_UDP_RECV + bool "udp receive" +config MODE_UDP_SEND + bool "udp send" +endchoice + +endmenu diff --git a/examples/performance/TCP_send/main/component.mk b/examples/performance/udp_esp2ap/main/component.mk similarity index 100% rename from examples/performance/TCP_send/main/component.mk rename to examples/performance/udp_esp2ap/main/component.mk diff --git a/examples/performance/udp_esp2ap/main/udp_esp2ap.c b/examples/performance/udp_esp2ap/main/udp_esp2ap.c new file mode 100644 index 000000000..ac8a74577 --- /dev/null +++ b/examples/performance/udp_esp2ap/main/udp_esp2ap.c @@ -0,0 +1,199 @@ + + +/* +udp_esp2ap test +This example use esp32 as station, +when esp32 start,it will connect to an AP and will create a UDP socket, +use a UDP client send a message first to let it know the client IP and port, +then it will send/receive data,(set work mode in menuconfig) +And calculate the speed of sending/receiving data. +*/ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "esp_task_wdt.h" +#include "nvs_flash.h" +#include +#include +#include "driver/uart.h" +#include "soc/uart_struct.h" + +/*AP info and tcp_sever info*/ +//#define DEFAULTSSID "wifi-12" +//#define DEFAULTPWD "sumof1+1=2" +#define DEFAULTSSID CONFIG_WIFI_SSID +#define DEFAULTPWD CONFIG_WIFI_PASSWORD +#define DEFAULTPORT CONFIG_SEVER_PORT +#define BUFFSIZE CONFIG_BUFF_SIZE + +static int totle_data=0; + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; + +/**/ +static int connectedflag = 0; +static int mysocket; + + + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + //printf("event_handler:SYSTEM_EVENT_STA_START\n"); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); + break; + case SYSTEM_EVENT_STA_GOT_IP: + printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); + printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + connectedflag=1; + break; + default: + break; + } + return ESP_OK; +} +//wifi_init +static void wifi_init() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULTSSID, + .password = DEFAULTPWD + }, + }; + + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_start() ); + esp_wifi_connect(); + + printf("wifi_init over.\n"); +} + + +static void data_count(void *pvParameters) +{ + printf("task data_count start!\n"); + int len; + char databuff[BUFFSIZE]; + struct sockaddr_in client_addr; + unsigned int socklen = sizeof(client_addr); + len=recvfrom(mysocket, databuff, BUFFSIZE, 0,(struct sockaddr *)&client_addr,&socklen); + if(len<0) + { + perror("first recv error:"); + close(mysocket); + vTaskDelete(NULL); + } + else + { +#ifdef CONFIG_MODE_TCP_SEND + printf("send data to %s:%u\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + memset(databuff,97,BUFFSIZE); +#else + printf("recv data from %s:%u\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); +#endif + + } + socklen=sizeof(client_addr); + printf("start calc!\n"); + + while(1) + { +#ifdef CONFIG_MODE_UDP_SEND + len=sendto(mysocket, databuff, BUFFSIZE, 0, (struct sockaddr *)&client_addr, sizeof(client_addr)); +#else + len=recvfrom(mysocket, databuff, BUFFSIZE, 0,(struct sockaddr *)&client_addr,&socklen); +#endif + + + //printf("len= %d\n",len); + if(len>0) + { + totle_data+=len; + } + else + { + //perror("data_count:\n"); + /*you'd better turn off watch dog in menuconfig + *Component config->ESP32-specific->Task watchdog. + **/ + //vTaskDelay(1/portTICK_RATE_MS); + } + } + +} +//this task establish a udp connection and send/recv data +static void udp_connect(void *pvParameters) +{ + TaskHandle_t tasksend; + do + { + vTaskDelay(100); + } + while(!connectedflag); + //wating for connecting to AP + + + mysocket = socket(AF_INET, SOCK_DGRAM, 0); + if(mysocket < 0) + { + perror("socket failed:"); + vTaskDelete(NULL); + } + struct sockaddr_in myaddr; + myaddr.sin_family=AF_INET; + myaddr.sin_port=htons(DEFAULTPORT); + if(bind(mysocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0) + { + perror("bind local port error:"); + close(mysocket); + vTaskDelete(NULL); + } + + vTaskDelay(2000/portTICK_RATE_MS); + + //create a new task... + xTaskCreate(&data_count,"data_count",4096,NULL,5,&tasksend); + int pps; + while(1) + { + totle_data=0; + vTaskDelay(3000/ portTICK_RATE_MS); + pps = totle_data/3; +#ifdef CONFIG_MODE_UDP_SEND + printf("udp send %d byte per sec!(just reference)\n",pps); +#else + printf("udp recv %d byte per sec!\n",pps); +#endif + } + vTaskDelete(NULL); +} + + +void app_main(void) +{ + nvs_flash_init(); + wifi_init(); + xTaskCreate(&udp_connect,"udp_connect",4096,NULL,5,NULL); +} From 5ecc88cb33973d18279b7aa5222ca749dcb8b1fa Mon Sep 17 00:00:00 2001 From: chenyudong Date: Mon, 20 Mar 2017 13:43:04 +0800 Subject: [PATCH 3/7] tcp_perf:modify tcp_perf with some new options. You can set esp32 as AP/STA, client/sever, sender/recever in menuconfig. You can set whether to display delay time info in menuconfig. Now you can transfer data between esp and esp. --- examples/performance/README.md | 19 +- .../tcp_esp2ap/main/Kconfig.projbuild | 51 ---- .../performance/tcp_esp2ap/main/tcp_esp2ap.c | 189 ------------- .../{tcp_esp2ap => tcp_perf}/Makefile | 2 +- .../tcp_perf/main/Kconfig.projbuild | 84 ++++++ .../main/component.mk | 0 examples/performance/tcp_perf/main/main.c | 109 ++++++++ examples/performance/tcp_perf/main/tcp_perf.c | 251 ++++++++++++++++++ examples/performance/tcp_perf/main/tcp_perf.h | 71 +++++ 9 files changed, 534 insertions(+), 242 deletions(-) delete mode 100644 examples/performance/tcp_esp2ap/main/Kconfig.projbuild delete mode 100644 examples/performance/tcp_esp2ap/main/tcp_esp2ap.c rename examples/performance/{tcp_esp2ap => tcp_perf}/Makefile (85%) create mode 100644 examples/performance/tcp_perf/main/Kconfig.projbuild rename examples/performance/{tcp_esp2ap => tcp_perf}/main/component.mk (100%) create mode 100644 examples/performance/tcp_perf/main/main.c create mode 100644 examples/performance/tcp_perf/main/tcp_perf.c create mode 100644 examples/performance/tcp_perf/main/tcp_perf.h diff --git a/examples/performance/README.md b/examples/performance/README.md index d79ee486e..198df79b1 100644 --- a/examples/performance/README.md +++ b/examples/performance/README.md @@ -1,7 +1,24 @@ # Wifi Performance Examples Some simple codes help to test the wifi performance. + Including TCP/UDP TX/RX throughput. -You may need a TCP/UDP client on PC. + +#tcp_perf + +Using tcp. + +This example is used to test tcp throughput and delay time. + +First you should set menuconfig. + +You can set esp32 will be use as AP/STA, client/sever, sender/receiver in menuconfig. Also some config such as SSID, PASSWORD, SEVER_IP can be set in menuconfig. + +Open AP, then open STA, when they make a connect, they will send/receive data.You will see the calc result in com output. Make sure that your set can let them connect. + +Explaining more in [main.c](tcp_perf/main/main.c). + + + See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples. diff --git a/examples/performance/tcp_esp2ap/main/Kconfig.projbuild b/examples/performance/tcp_esp2ap/main/Kconfig.projbuild deleted file mode 100644 index 39b0c8ee6..000000000 --- a/examples/performance/tcp_esp2ap/main/Kconfig.projbuild +++ /dev/null @@ -1,51 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "tp_wifi_test1" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "1234567890" - help - WiFi password (WPA or WPA2) for the example to use. - -config SEVER_PORT - int "tcp sever port" - default 4567 - help - which will the tcp sever use. - -config BUFF_SIZE - int "buff size" - default 1024 - help - the data send&recv buff size. - -choice - prompt "test mode" - default PERFORMANCE_MODE - help - This option performance mode. - - - for "tcp receive" setting,it will receive data by tcp. - - - for "tcp send" setting, it will send data by tcp. - -# - for "udp receive" setting,it will receive data by udp. - -# - for "udp send" setting,it will send data by udp. - -config MODE_TCP_RECV - bool "tcp receive" -config MODE_TCP_SEND - bool "tcp send" -#config MODE_UDP_RECV -# bool "udp receive" -#config MODE_UDP_SEND -# bool "udp send" -endchoice - -endmenu diff --git a/examples/performance/tcp_esp2ap/main/tcp_esp2ap.c b/examples/performance/tcp_esp2ap/main/tcp_esp2ap.c deleted file mode 100644 index 4e3ccc4f0..000000000 --- a/examples/performance/tcp_esp2ap/main/tcp_esp2ap.c +++ /dev/null @@ -1,189 +0,0 @@ - -/* -TCP_recv example -This example use esp32 as station and TCP sever, -when esp32 start,it will connect to an AP and will create a TCP socket as a sever, -use a TCP client connect to esp32, -then it will send/receive data,(set work mode in menuconfig) -And calculate the speed of sending/receiving data. -*/ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include -#include -#include "driver/uart.h" -#include "soc/uart_struct.h" - -/*AP info and tcp_sever info*/ -#define DEFAULTSSID CONFIG_WIFI_SSID -#define DEFAULTPWD CONFIG_WIFI_PASSWORD -#define DEFAULTPORT CONFIG_SEVER_PORT -#define BUFFSIZE CONFIG_BUFF_SIZE - -static int totle_data=0; - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -/**/ -static int connectedflag = 0; -static int sever_socket; -static struct sockaddr_in sever_addr; -static struct sockaddr_in client_addr; -static unsigned int socklen = sizeof(client_addr); -static int connect_soc; - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - //printf("event_handler:SYSTEM_EVENT_STA_START\n"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); - break; - case SYSTEM_EVENT_STA_GOT_IP: - printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); - printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - connectedflag=1; - break; - default: - break; - } - return ESP_OK; -} -//wifi_init -static void wifi_init() -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config = { - .sta = { - .ssid = DEFAULTSSID, - .password = DEFAULTPWD - }, - }; - - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); - esp_wifi_connect(); - - printf("wifi_init over.\n"); -} - -static void data_count(void *pvParameters) -{ - int len=0; - char databuff[BUFFSIZE]; -#ifdef CONFIG_MODE_TCP_SEND - memset(databuff,97,BUFFSIZE); -#endif - while(1) - { -#ifdef CONFIG_MODE_TCP_SEND - len=send(connect_soc, databuff, BUFFSIZE, 0); -#else - len=recv(connect_soc, databuff, BUFFSIZE, 0); -#endif - if(len>0) - { - totle_data+=len; - } - else - { - /*for faster send&receive,don't show error code. - *if it can't work as expectations,unnote the two lines here - **/ - //perror("data_count error"); - //vTaskDelay(500/portTICK_RATE_MS); - } - } -} - -//this task establish a TCP connection and receive data from TCP -static void tcp_client(void *pvParameters) -{ - do - { - vTaskDelay(100); - } - while(!connectedflag); - //wating for connecting to AP - - printf("socket....port=%d\n",DEFAULTPORT); - sever_socket = socket(AF_INET, SOCK_STREAM, 0); - if(sever_socket < 0) - { - perror("socket() error:"); - vTaskDelete(NULL); - } - sever_addr.sin_family = AF_INET; - sever_addr.sin_port = htons(DEFAULTPORT); - sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); - if(bind(sever_socket,(struct sockaddr*)&sever_addr,sizeof(sever_addr))<0) - { - perror("bind() error"); - close(sever_socket); - vTaskDelete(NULL); - } - if(listen(sever_socket,5)<0) - { - perror("listen() error"); - close(sever_socket); - vTaskDelete(NULL); - } - connect_soc = accept(sever_socket,(struct sockaddr*)&client_addr,&socklen); - if(connect_soc<0) - { - perror("accept() error"); - close(sever_socket); - vTaskDelete(NULL); - } - /*connection established,now can send/recv*/ - printf("connection established!"); - TaskHandle_t tasksend; - xTaskCreate(&data_count,"data_count",4096,NULL,5,&tasksend); - - int pps; - while(1) - { - totle_data=0; - //calc every 3s - vTaskDelay(3000/ portTICK_RATE_MS); - pps=totle_data/3; -#ifdef CONFIG_MODE_TCP_SEND - printf("tcp send %d byte per sec!\n",pps); -#else - printf("tcp recv %d byte per sec!\n",pps); -#endif - } - vTaskDelete(tasksend); - close(connect_soc); - close(sever_socket); - vTaskDelete(NULL); -} - - -void app_main(void) -{ - nvs_flash_init(); - wifi_init(); - xTaskCreate(&tcp_client,"tcp_client",4096,NULL,5,NULL); -} diff --git a/examples/performance/tcp_esp2ap/Makefile b/examples/performance/tcp_perf/Makefile similarity index 85% rename from examples/performance/tcp_esp2ap/Makefile rename to examples/performance/tcp_perf/Makefile index a4ca46adb..03fb85528 100644 --- a/examples/performance/tcp_esp2ap/Makefile +++ b/examples/performance/tcp_perf/Makefile @@ -3,7 +3,7 @@ # project subdirectory. # -PROJECT_NAME := tcp_esp2ap +PROJECT_NAME := tcp_perf include $(IDF_PATH)/make/project.mk diff --git a/examples/performance/tcp_perf/main/Kconfig.projbuild b/examples/performance/tcp_perf/main/Kconfig.projbuild new file mode 100644 index 000000000..e5f89d981 --- /dev/null +++ b/examples/performance/tcp_perf/main/Kconfig.projbuild @@ -0,0 +1,84 @@ +menu "Example Configuration" + +choice + prompt "TCP_PERF_MODE " + default MODE_TCP_SHIELDBOX + help + This option performance mode. + + - for "Performance in shieldbox" setting,it will receive data by tcp. + + - for "Performance in air" setting, it will send data by tcp. + + - for "Performance in long distance" setting, it will send data by tcp. + + +config MODE_TCP_SHIELDBOX + bool "Performance in shieldbox" +config MODE_TCP_AIR + bool "Performance in air" +config MODE_TCP_LONG_DISTANCE + 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. + +config TCP_PERF_SEVER + bool "TCP performance sever enable" + default n + help + yes:ESP32 is TCP sever. no:ESP32 is TCP client. + + We suggest to make this config be same with "Station mode". + +config TCP_PERF_TX + bool "TCP performance TX test enable" + default n + help + yes:TCP TX test. no:TCP RX test. + +config TCP_PERF_DELAY_DEBUG + bool "TCP performance delay info enable" + default n + help + Show TCP performance delay info. + + Ignore in TCP RX. + +config TCP_PERF_WIFI_SSID + string "WiFi SSID" + default "tp_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. + +config TCP_PERF_SEVER_PORT + int "TCP sever port" + default 4567 + help + Which will the tcp sever use. + +config TCP_PERF_SERVER_IP + string "TCP server ip" + default "192.168.4.1" + help + IP of TCP server. + + Ignore in TCP sever. + +config TCP_PERF_PKT_SIZE + int "Size of TCP packet" + default 1460 + help + the data send&recv packet size. + +endmenu diff --git a/examples/performance/tcp_esp2ap/main/component.mk b/examples/performance/tcp_perf/main/component.mk similarity index 100% rename from examples/performance/tcp_esp2ap/main/component.mk rename to examples/performance/tcp_perf/main/component.mk diff --git a/examples/performance/tcp_perf/main/main.c b/examples/performance/tcp_perf/main/main.c new file mode 100644 index 000000000..052932f1f --- /dev/null +++ b/examples/performance/tcp_perf/main/main.c @@ -0,0 +1,109 @@ + + +/* +tcp_perf example + +Using this example to test tcp throughput performance. +esp<->esp or esp<->ap + +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. + +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. + +*/ + +#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 + +//this task establish a TCP connection and receive data from TCP +static void tcp_conn(void *pvParameters) +{ + ESP_LOGI(TAG, "task tcp_conn start."); + //wating for connecting to AP + do + { + vTaskDelay(100); + } + while(!connectedflag); + ESP_LOGI(TAG, "sta has connected to ap."); + + //create tcp socket + int socret; +#if ESP_TCP_MODE_SEVER + ESP_LOGI(TAG, "creat_tcp_sever."); + socret=creat_tcp_sever(); +#else + ESP_LOGI(TAG, "creat_tcp_client."); + socret=creat_tcp_client(); +#endif + if(-1==socret) + { + ESP_LOGI(TAG, "creat tcp socket error,stop."); + vTaskDelete(NULL); + } + + + //create a task to tx/rx data + TaskHandle_t tx_rx_task; +#if ESP_TCP_PERF_TX + xTaskCreate(&send_data,"send_data",4096,NULL,4,&tx_rx_task); +#else + xTaskCreate(&recv_data,"recv_data",4096,NULL,4,&tx_rx_task); +#endif + int pps; + while(1) + { + totle_data=0; + //calc every 3s + vTaskDelay(3000/ portTICK_RATE_MS); + pps=totle_data/3; +#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\n0-30:%d 30-100:%d 100-300:%d 300-1000:%d 1000+:%d\n", + totle_pack,send_success,send_fail,delay_classify[0],delay_classify[1],delay_classify[2],delay_classify[3],delay_classify[ +4]); +#endif +#else + ESP_LOGI(TAG, "tcp recv %d byte per sec!\n",pps); +#endif + } + close_socket(); + vTaskDelete(tx_rx_task); + vTaskDelete(NULL); +} + + + +void app_main(void) +{ + nvs_flash_init(); +#if ESP_WIFI_MODE_AP + ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n"); + wifi_init_softap(); +#else + ESP_LOGI(TAG, "ESP_WIFI_MODE_STA\n"); + wifi_init_sta(); +#endif + xTaskCreate(&tcp_conn,"tcp_conn",4096,NULL,5,NULL); +} diff --git a/examples/performance/tcp_perf/main/tcp_perf.c b/examples/performance/tcp_perf/main/tcp_perf.c new file mode 100644 index 000000000..99da7bef7 --- /dev/null +++ b/examples/performance/tcp_perf/main/tcp_perf.c @@ -0,0 +1,251 @@ + +#include "tcp_perf.h" + +extern int connectedflag; +extern int totle_data; + +#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO + +extern int totle_pack; +extern int send_success; +extern int send_fail; +extern int delay_classify[5]; + +#endif + +/* 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 struct sockaddr_in sever_addr; +static struct sockaddr_in client_addr; +static unsigned int socklen = sizeof(client_addr); +static int connect_soc; + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + break; + case SYSTEM_EVENT_STA_GOT_IP: + ESP_LOGI(TAG, "event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); + ESP_LOGI(TAG, "ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + 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; + 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); + break; + default: + break; + } + return ESP_OK; +} + +//send data +void send_data(void *pvParameters) +{ + int len=0; + char databuff[DEFAULT_PKTSIZE]; + memset(databuff,97,DEFAULT_PKTSIZE); + vTaskDelay(100/portTICK_RATE_MS); + ESP_LOGI(TAG,"start sending..."); + while(1) + { +#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO + //delaytime + struct timeval tv_start; + struct timeval tv_finish; + unsigned int send_delay_ms; + + totle_pack++; + gettimeofday(&tv_start,NULL); +#endif + + len=send(connect_soc, databuff, DEFAULT_PKTSIZE, 0); + +#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO + gettimeofday(&tv_finish,NULL); +#endif + if(len>0) + { + totle_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=%d",send_delay_ms); + if(send_delay_ms<30) + delay_classify[0]++; + else if(send_delay_ms<100) + delay_classify[1]++; + else if(send_delay_ms<300) + delay_classify[2]++; + else if(send_delay_ms<1000) + delay_classify[3]++; + else + delay_classify[4]++; +#endif + } + else + { +#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO + send_fail++; +#endif + /*for faster send&receive,don't show error code. + *if it can't work as expectations,unnote the two lines here + **/ + //perror("data_count error"); + //vTaskDelay(500/portTICK_RATE_MS); + } + } +} +//send data +void recv_data(void *pvParameters) +{ + int len=0; + char databuff[DEFAULT_PKTSIZE]; + while(1) + { + len=recv(connect_soc, databuff, DEFAULT_PKTSIZE, 0); + if(len>0) + { + totle_data+=len; + } + else + { + perror("data_count error"); + vTaskDelay(500/portTICK_RATE_MS); + } + } +} + +//use this esp32 as a tcp sever. return 0:success -1:error +int creat_tcp_sever() +{ + ESP_LOGI(TAG, "sever socket....port=%d\n",DEFAULTPORT); + sever_socket = socket(AF_INET, SOCK_STREAM, 0); + if(sever_socket < 0) + { + perror("socket() error:"); + return -1; + } + sever_addr.sin_family = AF_INET; + sever_addr.sin_port = htons(DEFAULTPORT); + sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); + if(bind(sever_socket,(struct sockaddr*)&sever_addr,sizeof(sever_addr))<0) + { + perror("bind() error"); + close(sever_socket); + return -1; + } + if(listen(sever_socket,5)<0) + { + 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"); + close(sever_socket); + return -1; + } + /*connection established,now can send/recv*/ + ESP_LOGI(TAG, "tcp connection established!"); + return 0; +} +//use this esp32 as a tcp client. return 0:success -1:error +int creat_tcp_client() +{ + ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n",DEFAULTSEVERIP,DEFAULTPORT); + connect_soc = socket(AF_INET, SOCK_STREAM, 0); + if(connect_soc < 0) + { + perror("socket failed!"); + return -1; + } + sever_addr.sin_family = AF_INET; + sever_addr.sin_port = htons(DEFAULTPORT); + sever_addr.sin_addr.s_addr = inet_addr(DEFAULTSEVERIP); + printf("connecting to sever..."); + if(connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) + { + perror("connect to sever error!"); + return -1; + } + ESP_LOGI(TAG,"connect to sever success!"); + return 0; +} + +//wifi_init_sta +void wifi_init_sta() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULTSSID, + .password = DEFAULTPWD + }, + }; + + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_start() ); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",DEFAULTSSID,DEFAULTPWD); +} +//wifi_init_softap +void wifi_init_softap() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .ap = { + .ssid = DEFAULTSSID, + .ssid_len=0, + .max_connection=MAXSTACONN, + .password = DEFAULTPWD, + .authmode=WIFI_AUTH_WPA_WPA2_PSK + }, + }; + + 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_start() ); + + ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",DEFAULTSSID,DEFAULTPWD); +} + +void close_socket() +{ + close(connect_soc); + close(sever_socket); +} + + + diff --git a/examples/performance/tcp_perf/main/tcp_perf.h b/examples/performance/tcp_perf/main/tcp_perf.h new file mode 100644 index 000000000..071218af6 --- /dev/null +++ b/examples/performance/tcp_perf/main/tcp_perf.h @@ -0,0 +1,71 @@ +#ifndef __TCP_PERF_H__ +#define __TCP_PERF_H__ + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include +#include +#include "driver/uart.h" +#include "soc/uart_struct.h" +#include "esp_log.h" + + + + +#ifdef __cplusplus +extern "C" { +#endif + +/*AP info and tcp_sever info*/ +#define DEFAULTSSID CONFIG_TCP_PERF_WIFI_SSID +#define DEFAULTPWD CONFIG_TCP_PERF_WIFI_PASSWORD +#define DEFAULTPORT CONFIG_TCP_PERF_SEVER_PORT +#define DEFAULTSEVERIP CONFIG_TCP_PERF_SERVER_IP +#define DEFAULT_PKTSIZE CONFIG_TCP_PERF_PKT_SIZE +#define MAXSTACONN 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_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 + + +#define TAG "tcp_perf:" + + + +//using esp as station +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(); + +//send data task +void send_data(void *pvParameters); +//receive data task +void recv_data(void *pvParameters); + +//close all socket +void close_socket(); + + + + + +#ifdef __cplusplus +} +#endif + +#endif + From 8a163e3db0d205aed2d351029b2238389c05fb25 Mon Sep 17 00:00:00 2001 From: chenyudong Date: Tue, 21 Mar 2017 20:06:32 +0800 Subject: [PATCH 4/7] eg/perf:modify udp_perf and some mistakes modify udp_perf to be similar with tcp_perf. change indentation and some mistakes of codes. edit README. --- examples/performance/README.md | 28 +- .../tcp_perf/main/Kconfig.projbuild | 106 +++---- examples/performance/tcp_perf/main/main.c | 131 ++++---- examples/performance/tcp_perf/main/tcp_perf.c | 289 +++++++++--------- examples/performance/tcp_perf/main/tcp_perf.h | 24 +- .../udp_esp2ap/main/Kconfig.projbuild | 43 --- .../performance/udp_esp2ap/main/udp_esp2ap.c | 199 ------------ .../{udp_esp2ap => udp_perf}/Makefile | 0 .../udp_perf/main/Kconfig.projbuild | 75 +++++ .../main/component.mk | 0 examples/performance/udp_perf/main/main.c | 90 ++++++ examples/performance/udp_perf/main/udp_perf.c | 198 ++++++++++++ examples/performance/udp_perf/main/udp_perf.h | 73 +++++ 13 files changed, 730 insertions(+), 526 deletions(-) delete mode 100644 examples/performance/udp_esp2ap/main/Kconfig.projbuild delete mode 100644 examples/performance/udp_esp2ap/main/udp_esp2ap.c rename examples/performance/{udp_esp2ap => udp_perf}/Makefile (100%) create mode 100644 examples/performance/udp_perf/main/Kconfig.projbuild rename examples/performance/{udp_esp2ap => udp_perf}/main/component.mk (100%) create mode 100644 examples/performance/udp_perf/main/main.c create mode 100644 examples/performance/udp_perf/main/udp_perf.c create mode 100644 examples/performance/udp_perf/main/udp_perf.h diff --git a/examples/performance/README.md b/examples/performance/README.md index 198df79b1..af7aae006 100644 --- a/examples/performance/README.md +++ b/examples/performance/README.md @@ -4,21 +4,33 @@ Some simple codes help to test the wifi performance. Including TCP/UDP TX/RX throughput. -#tcp_perf +# tcp_perf -Using tcp. +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. +You can set esp32 as AP/STA, client/sever, sender/receiver. Make sure that STAcan connect to AP with your configuration in menuconfig. -First you should set menuconfig. +So the tcp_perf can be used in following scenes: -You can set esp32 will be use as AP/STA, client/sever, sender/receiver in menuconfig. Also some config such as SSID, PASSWORD, SEVER_IP can be set in menuconfig. +* 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) -Open AP, then open STA, when they make a connect, they will send/receive data.You will see the calc result in com output. Make sure that your set can let them connect. - -Explaining more in [main.c](tcp_perf/main/main.c). +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. diff --git a/examples/performance/tcp_perf/main/Kconfig.projbuild b/examples/performance/tcp_perf/main/Kconfig.projbuild index e5f89d981..e40c52f83 100644 --- a/examples/performance/tcp_perf/main/Kconfig.projbuild +++ b/examples/performance/tcp_perf/main/Kconfig.projbuild @@ -1,84 +1,84 @@ menu "Example Configuration" -choice - prompt "TCP_PERF_MODE " - default MODE_TCP_SHIELDBOX - help - This option performance mode. - - - for "Performance in shieldbox" setting,it will receive data by tcp. - - - for "Performance in air" setting, it will send data by tcp. - - - for "Performance in long distance" setting, it will send data by tcp. - - -config MODE_TCP_SHIELDBOX - bool "Performance in shieldbox" -config MODE_TCP_AIR - bool "Performance in air" -config MODE_TCP_LONG_DISTANCE - bool "Performance in long distance" -endchoice +#choice +# prompt "TCP_PERF_MODE " +# default MODE_TCP_SHIELDBOX +# help +# This option set performance mode. +# +# - for "Performance in shieldbox" setting,it will receive data by tcp. +# +# - for "Performance in air" setting, it will send data by tcp. +# +# - for "Performance in long distance" setting, it will send data by tcp. +# +# +#config MODE_TCP_SHIELDBOX +# bool "Performance in shieldbox" +#config MODE_TCP_AIR +# bool "Performance in air" +#config MODE_TCP_LONG_DISTANCE +# 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. + default n + help + yes:ESP32 is softap. no:ESP32 is station. config TCP_PERF_SEVER bool "TCP performance sever enable" - default n - help - yes:ESP32 is TCP sever. no:ESP32 is TCP client. - - We suggest to make this config be same with "Station mode". + default n + help + yes:ESP32 is TCP sever. no:ESP32 is TCP client. + + We suggest to make this config be same with "Station mode". config TCP_PERF_TX bool "TCP performance TX test enable" - default n - help - yes:TCP TX test. no:TCP RX test. + default n + help + yes:TCP TX test. no:TCP RX test. config TCP_PERF_DELAY_DEBUG bool "TCP performance delay info enable" - default n - help - Show TCP performance delay info. - - Ignore in TCP RX. + default n + help + Show TCP performance delay info. + + Ignore in TCP RX. config TCP_PERF_WIFI_SSID string "WiFi SSID" - default "tp_wifi_test1" - help - SSID (network name) for the example to connect to. + default "tp_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" + int "TCP sever port" default 4567 help - Which will the tcp sever use. - + Which will the tcp sever use. + config TCP_PERF_SERVER_IP - string "TCP server ip" + string "TCP server ip" default "192.168.4.1" help - IP of TCP server. - - Ignore in TCP sever. + IP of TCP server. + + Ignore in TCP sever. config TCP_PERF_PKT_SIZE - int "Size of TCP packet" + int "Size of TCP packet" default 1460 help - the data send&recv packet size. - + the data send&recv packet size. + endmenu diff --git a/examples/performance/tcp_perf/main/main.c b/examples/performance/tcp_perf/main/main.c index 052932f1f..d08220f7a 100644 --- a/examples/performance/tcp_perf/main/main.c +++ b/examples/performance/tcp_perf/main/main.c @@ -7,90 +7,87 @@ Using this example to test tcp throughput performance. esp<->esp or esp<->ap step1: - init wifi as AP/STA using config SSID/PASSWORD. + 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. - + creat a tcp sever/client socket using config PORT/(IP). + if sever: wating for connect. + if client connect to sever. 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. - + 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. */ #include "tcp_perf.h" int connectedflag = 0; -int totle_data=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}; +int totle_pack = 0; +int send_success = 0; +int send_fail = 0; +int delay_classify[5] = { 0 }; -#endif +#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) { - ESP_LOGI(TAG, "task tcp_conn start."); - //wating for connecting to AP - do - { - vTaskDelay(100); - } - while(!connectedflag); - ESP_LOGI(TAG, "sta has connected to ap."); - - //create tcp socket - int socret; + ESP_LOGI(TAG, "task tcp_conn start."); + /*wating for connecting to AP*/ + do + { + vTaskDelay(100); + } + while (!connectedflag); + ESP_LOGI(TAG, "sta has connected to ap."); + + /*create tcp socket*/ + int socret; + #if ESP_TCP_MODE_SEVER - ESP_LOGI(TAG, "creat_tcp_sever."); - socret=creat_tcp_sever(); -#else - ESP_LOGI(TAG, "creat_tcp_client."); - socret=creat_tcp_client(); + ESP_LOGI(TAG, "creat_tcp_sever."); + socret=creat_tcp_sever(); +#else /*ESP_TCP_MODE_SEVER*/ + ESP_LOGI(TAG, "creat_tcp_client."); + socret = creat_tcp_client(); #endif - if(-1==socret) - { - ESP_LOGI(TAG, "creat tcp socket error,stop."); - vTaskDelete(NULL); - } - - - //create a task to tx/rx data - TaskHandle_t tx_rx_task; -#if ESP_TCP_PERF_TX - xTaskCreate(&send_data,"send_data",4096,NULL,4,&tx_rx_task); -#else - xTaskCreate(&recv_data,"recv_data",4096,NULL,4,&tx_rx_task); -#endif - int pps; - while(1) - { - totle_data=0; - //calc every 3s - vTaskDelay(3000/ portTICK_RATE_MS); - pps=totle_data/3; -#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\n0-30:%d 30-100:%d 100-300:%d 300-1000:%d 1000+:%d\n", - totle_pack,send_success,send_fail,delay_classify[0],delay_classify[1],delay_classify[2],delay_classify[3],delay_classify[ -4]); -#endif -#else - ESP_LOGI(TAG, "tcp recv %d byte per sec!\n",pps); -#endif - } - close_socket(); - vTaskDelete(tx_rx_task); + if(-1 == socret) { + ESP_LOGI(TAG, "creat tcp socket error,stop."); vTaskDelete(NULL); + } + + /*create a task to tx/rx data*/ + TaskHandle_t tx_rx_task; +#if ESP_TCP_PERF_TX + xTaskCreate(&send_data, "send_data", 4096, NULL, 4, &tx_rx_task); +#else /*ESP_TCP_PERF_TX*/ + xTaskCreate(&recv_data, "recv_data", 4096, NULL, 4, &tx_rx_task); +#endif + int pps; + while (1) { + totle_data = 0; + vTaskDelay(3000 / portTICK_RATE_MS);//every 3s + pps = totle_data / 3; + +#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" + "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], + delay_classify[1], delay_classify[2], delay_classify[3], delay_classify[4]); +#endif /*ESP_TCP_DELAY_INFO*/ +#else + ESP_LOGI(TAG, "tcp recv %d byte per sec!\n", pps); +#endif /*ESP_TCP_PERF_TX*/ + } + close_socket(); + vTaskDelete(tx_rx_task); + vTaskDelete(NULL); } @@ -101,9 +98,9 @@ void app_main(void) #if ESP_WIFI_MODE_AP ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n"); wifi_init_softap(); -#else +#else /*ESP_WIFI_MODE_AP*/ ESP_LOGI(TAG, "ESP_WIFI_MODE_STA\n"); wifi_init_sta(); #endif - xTaskCreate(&tcp_conn,"tcp_conn",4096,NULL,5,NULL); + xTaskCreate(&tcp_conn, "tcp_conn", 4096, NULL, 5, NULL); } diff --git a/examples/performance/tcp_perf/main/tcp_perf.c b/examples/performance/tcp_perf/main/tcp_perf.c index 99da7bef7..458ebfe13 100644 --- a/examples/performance/tcp_perf/main/tcp_perf.c +++ b/examples/performance/tcp_perf/main/tcp_perf.c @@ -1,18 +1,6 @@ #include "tcp_perf.h" -extern int connectedflag; -extern int totle_data; - -#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO - -extern int totle_pack; -extern int send_success; -extern int send_fail; -extern int delay_classify[5]; - -#endif - /* FreeRTOS event group to signal when we are connected & ready to make a request */ static EventGroupHandle_t wifi_event_group; /*socket*/ @@ -34,20 +22,21 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) case SYSTEM_EVENT_STA_CONNECTED: break; case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); - ESP_LOGI(TAG, "ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + 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; 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); + MAC2STR(event->event_info.sta_connected.mac), + event->event_info.sta_connected.aid); 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); + MAC2STR(event->event_info.sta_disconnected.mac), + event->event_info.sta_disconnected.aid); break; default: break; @@ -58,138 +47,136 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) //send data void send_data(void *pvParameters) { - int len=0; - char databuff[DEFAULT_PKTSIZE]; - memset(databuff,97,DEFAULT_PKTSIZE); - vTaskDelay(100/portTICK_RATE_MS); - ESP_LOGI(TAG,"start sending..."); - while(1) - { + int len = 0; + char databuff[DEFAULT_PKTSIZE]; + memset(databuff, PACK_BYTE_IS, DEFAULT_PKTSIZE); + vTaskDelay(100/portTICK_RATE_MS); + ESP_LOGI(TAG, "start sending..."); #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO - //delaytime - struct timeval tv_start; - struct timeval tv_finish; - unsigned int send_delay_ms; - - totle_pack++; - gettimeofday(&tv_start,NULL); -#endif - - len=send(connect_soc, databuff, DEFAULT_PKTSIZE, 0); + //delaytime + struct timeval tv_start; + struct timeval tv_finish; + unsigned long send_delay_ms; +#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ + while(1) { #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO - gettimeofday(&tv_finish,NULL); -#endif - if(len>0) - { - totle_data+=len; + //vTaskDelay(1000/portTICK_RATE_MS); + totle_pack++; + gettimeofday(&tv_start, NULL); +#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ + + //send function + len = send(connect_soc, databuff, DEFAULT_PKTSIZE, 0); + #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=%d",send_delay_ms); - if(send_delay_ms<30) - delay_classify[0]++; - else if(send_delay_ms<100) - delay_classify[1]++; - else if(send_delay_ms<300) - delay_classify[2]++; - else if(send_delay_ms<1000) - delay_classify[3]++; - else - delay_classify[4]++; -#endif - } - else - { + gettimeofday(&tv_finish, NULL); +#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ + if(len > 0) { + totle_data += len; + #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO - send_fail++; -#endif - /*for faster send&receive,don't show error code. - *if it can't work as expectations,unnote the two lines here - **/ - //perror("data_count error"); - //vTaskDelay(500/portTICK_RATE_MS); - } - } + 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) + delay_classify[1]++; + else if(send_delay_ms < 300) + delay_classify[2]++; + else if(send_delay_ms < 1000) + delay_classify[3]++; + else + delay_classify[4]++; +#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ + + }/*if(len > 0)*/ + else { + +#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO + 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 + **/ + //perror("data_count error"); + //vTaskDelay(500/portTICK_RATE_MS); + } + } } -//send data +//receive data void recv_data(void *pvParameters) { - int len=0; - char databuff[DEFAULT_PKTSIZE]; - while(1) - { - len=recv(connect_soc, databuff, DEFAULT_PKTSIZE, 0); - if(len>0) - { - totle_data+=len; - } - else - { - perror("data_count error"); - vTaskDelay(500/portTICK_RATE_MS); - } - } + int len = 0; + char databuff[DEFAULT_PKTSIZE]; + while (1) { + len = recv(connect_soc, databuff, DEFAULT_PKTSIZE, 0); + if (len > 0) { + totle_data += len; + } + else { + perror("recv_data error"); + vTaskDelay(500 / portTICK_RATE_MS); + } + } } + //use this esp32 as a tcp sever. return 0:success -1:error int creat_tcp_sever() { - ESP_LOGI(TAG, "sever socket....port=%d\n",DEFAULTPORT); - sever_socket = socket(AF_INET, SOCK_STREAM, 0); - if(sever_socket < 0) - { - perror("socket() error:"); - return -1; - } - sever_addr.sin_family = AF_INET; - sever_addr.sin_port = htons(DEFAULTPORT); - sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); - if(bind(sever_socket,(struct sockaddr*)&sever_addr,sizeof(sever_addr))<0) - { - perror("bind() error"); - close(sever_socket); - return -1; - } - if(listen(sever_socket,5)<0) - { - 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"); - close(sever_socket); - return -1; - } - /*connection established,now can send/recv*/ - ESP_LOGI(TAG, "tcp connection established!"); - return 0; + 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:"); + 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"); + close(sever_socket); + return -1; + } + if (listen(sever_socket, 5) < 0) { + 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"); + close(sever_socket); + return -1; + } + /*connection established,now can send/recv*/ + ESP_LOGI(TAG, "tcp connection established!"); + return 0; } //use this esp32 as a tcp client. return 0:success -1:error int creat_tcp_client() { - ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n",DEFAULTSEVERIP,DEFAULTPORT); - connect_soc = socket(AF_INET, SOCK_STREAM, 0); - if(connect_soc < 0) - { - perror("socket failed!"); - return -1; - } - sever_addr.sin_family = AF_INET; - sever_addr.sin_port = htons(DEFAULTPORT); - sever_addr.sin_addr.s_addr = inet_addr(DEFAULTSEVERIP); - printf("connecting to sever..."); - if(connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) - { - perror("connect to sever error!"); - return -1; - } - ESP_LOGI(TAG,"connect to sever success!"); - return 0; + 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) { + perror("socket failed!"); + return -1; + } + 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) { + perror("connect to sever error!"); + return -1; + } + ESP_LOGI(TAG, "connect to sever success!"); + return 0; } //wifi_init_sta @@ -197,54 +184,56 @@ void wifi_init_sta() { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_config_t wifi_config = { .sta = { - .ssid = DEFAULTSSID, - .password = DEFAULTPWD + .ssid = DEFAULT_SSID, + .password = DEFAULT_PWD }, }; - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK(esp_wifi_start() ); ESP_LOGI(TAG, "wifi_init_sta finished."); - ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",DEFAULTSSID,DEFAULTPWD); + ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n", + DEFAULT_SSID,DEFAULT_PWD); } //wifi_init_softap void wifi_init_softap() { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_config_t wifi_config = { .ap = { - .ssid = DEFAULTSSID, + .ssid = DEFAULT_SSID, .ssid_len=0, - .max_connection=MAXSTACONN, - .password = DEFAULTPWD, + .max_connection=MAX_STA_CONN, + .password = DEFAULT_PWD, .authmode=WIFI_AUTH_WPA_WPA2_PSK }, }; - 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_start() ); + 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_start()); - ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",DEFAULTSSID,DEFAULTPWD); + ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n", + DEFAULT_SSID, DEFAULT_PWD); } void close_socket() { - close(connect_soc); - close(sever_socket); + close(connect_soc); + close(sever_socket); } diff --git a/examples/performance/tcp_perf/main/tcp_perf.h b/examples/performance/tcp_perf/main/tcp_perf.h index 071218af6..2ddc17762 100644 --- a/examples/performance/tcp_perf/main/tcp_perf.h +++ b/examples/performance/tcp_perf/main/tcp_perf.h @@ -24,12 +24,12 @@ extern "C" { #endif /*AP info and tcp_sever info*/ -#define DEFAULTSSID CONFIG_TCP_PERF_WIFI_SSID -#define DEFAULTPWD CONFIG_TCP_PERF_WIFI_PASSWORD -#define DEFAULTPORT CONFIG_TCP_PERF_SEVER_PORT -#define DEFAULTSEVERIP CONFIG_TCP_PERF_SERVER_IP +#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_PKTSIZE CONFIG_TCP_PERF_PKT_SIZE -#define MAXSTACONN 1 //how many sta can be connected(AP mode) +#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 @@ -37,9 +37,20 @@ extern "C" { #define ESP_TCP_DELAY_INFO CONFIG_TCP_PERF_DELAY_DEBUG //TRUE:show delay time info +#define PACK_BYTE_IS 97 //'a' #define TAG "tcp_perf:" +extern int connectedflag; +extern int totle_data; + +#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO +extern int totle_pack; +extern int send_success; +extern int send_fail; +extern int delay_classify[5]; +#endif/*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ + //using esp as station void wifi_init_sta(); @@ -67,5 +78,6 @@ void close_socket(); } #endif -#endif + +#endif /*#ifndef __TCP_PERF_H__*/ diff --git a/examples/performance/udp_esp2ap/main/Kconfig.projbuild b/examples/performance/udp_esp2ap/main/Kconfig.projbuild deleted file mode 100644 index 59e86a0bd..000000000 --- a/examples/performance/udp_esp2ap/main/Kconfig.projbuild +++ /dev/null @@ -1,43 +0,0 @@ -menu "Example Configuration" - -config WIFI_SSID - string "WiFi SSID" - default "tp_wifi_test1" - help - SSID (network name) for the example to connect to. - -config WIFI_PASSWORD - string "WiFi Password" - default "1234567890" - help - WiFi password (WPA or WPA2) for the example to use. - -config SEVER_PORT - int "tcp sever port" - default 4567 - help - which will the udp sever use. - -config BUFF_SIZE - int "buff size" - default 1024 - help - the data send&recv buff size. - -choice - prompt "test mode" - default PERFORMANCE_MODE - help - This option performance mode. - - - for "udp receive" setting,it will receive data by udp. - - - for "udp send" setting, it will send data by udp. - -config MODE_UDP_RECV - bool "udp receive" -config MODE_UDP_SEND - bool "udp send" -endchoice - -endmenu diff --git a/examples/performance/udp_esp2ap/main/udp_esp2ap.c b/examples/performance/udp_esp2ap/main/udp_esp2ap.c deleted file mode 100644 index ac8a74577..000000000 --- a/examples/performance/udp_esp2ap/main/udp_esp2ap.c +++ /dev/null @@ -1,199 +0,0 @@ - - -/* -udp_esp2ap test -This example use esp32 as station, -when esp32 start,it will connect to an AP and will create a UDP socket, -use a UDP client send a message first to let it know the client IP and port, -then it will send/receive data,(set work mode in menuconfig) -And calculate the speed of sending/receiving data. -*/ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_system.h" -#include "esp_task_wdt.h" -#include "nvs_flash.h" -#include -#include -#include "driver/uart.h" -#include "soc/uart_struct.h" - -/*AP info and tcp_sever info*/ -//#define DEFAULTSSID "wifi-12" -//#define DEFAULTPWD "sumof1+1=2" -#define DEFAULTSSID CONFIG_WIFI_SSID -#define DEFAULTPWD CONFIG_WIFI_PASSWORD -#define DEFAULTPORT CONFIG_SEVER_PORT -#define BUFFSIZE CONFIG_BUFF_SIZE - -static int totle_data=0; - -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; - -/**/ -static int connectedflag = 0; -static int mysocket; - - - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - //printf("event_handler:SYSTEM_EVENT_STA_START\n"); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n"); - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - //printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n"); - break; - case SYSTEM_EVENT_STA_GOT_IP: - printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n"); - printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - connectedflag=1; - break; - default: - break; - } - return ESP_OK; -} -//wifi_init -static void wifi_init() -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config = { - .sta = { - .ssid = DEFAULTSSID, - .password = DEFAULTPWD - }, - }; - - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); - esp_wifi_connect(); - - printf("wifi_init over.\n"); -} - - -static void data_count(void *pvParameters) -{ - printf("task data_count start!\n"); - int len; - char databuff[BUFFSIZE]; - struct sockaddr_in client_addr; - unsigned int socklen = sizeof(client_addr); - len=recvfrom(mysocket, databuff, BUFFSIZE, 0,(struct sockaddr *)&client_addr,&socklen); - if(len<0) - { - perror("first recv error:"); - close(mysocket); - vTaskDelete(NULL); - } - else - { -#ifdef CONFIG_MODE_TCP_SEND - printf("send data to %s:%u\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); - memset(databuff,97,BUFFSIZE); -#else - printf("recv data from %s:%u\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); -#endif - - } - socklen=sizeof(client_addr); - printf("start calc!\n"); - - while(1) - { -#ifdef CONFIG_MODE_UDP_SEND - len=sendto(mysocket, databuff, BUFFSIZE, 0, (struct sockaddr *)&client_addr, sizeof(client_addr)); -#else - len=recvfrom(mysocket, databuff, BUFFSIZE, 0,(struct sockaddr *)&client_addr,&socklen); -#endif - - - //printf("len= %d\n",len); - if(len>0) - { - totle_data+=len; - } - else - { - //perror("data_count:\n"); - /*you'd better turn off watch dog in menuconfig - *Component config->ESP32-specific->Task watchdog. - **/ - //vTaskDelay(1/portTICK_RATE_MS); - } - } - -} -//this task establish a udp connection and send/recv data -static void udp_connect(void *pvParameters) -{ - TaskHandle_t tasksend; - do - { - vTaskDelay(100); - } - while(!connectedflag); - //wating for connecting to AP - - - mysocket = socket(AF_INET, SOCK_DGRAM, 0); - if(mysocket < 0) - { - perror("socket failed:"); - vTaskDelete(NULL); - } - struct sockaddr_in myaddr; - myaddr.sin_family=AF_INET; - myaddr.sin_port=htons(DEFAULTPORT); - if(bind(mysocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0) - { - perror("bind local port error:"); - close(mysocket); - vTaskDelete(NULL); - } - - vTaskDelay(2000/portTICK_RATE_MS); - - //create a new task... - xTaskCreate(&data_count,"data_count",4096,NULL,5,&tasksend); - int pps; - while(1) - { - totle_data=0; - vTaskDelay(3000/ portTICK_RATE_MS); - pps = totle_data/3; -#ifdef CONFIG_MODE_UDP_SEND - printf("udp send %d byte per sec!(just reference)\n",pps); -#else - printf("udp recv %d byte per sec!\n",pps); -#endif - } - vTaskDelete(NULL); -} - - -void app_main(void) -{ - nvs_flash_init(); - wifi_init(); - xTaskCreate(&udp_connect,"udp_connect",4096,NULL,5,NULL); -} diff --git a/examples/performance/udp_esp2ap/Makefile b/examples/performance/udp_perf/Makefile similarity index 100% rename from examples/performance/udp_esp2ap/Makefile rename to examples/performance/udp_perf/Makefile diff --git a/examples/performance/udp_perf/main/Kconfig.projbuild b/examples/performance/udp_perf/main/Kconfig.projbuild new file mode 100644 index 000000000..19917ee73 --- /dev/null +++ b/examples/performance/udp_perf/main/Kconfig.projbuild @@ -0,0 +1,75 @@ +menu "Example Configuration" + +#choice +# prompt "UCP_PERF_MODE " +# default MODE_UDP_SHIELDBOX +# help +# This option set performance mode. +# +# - for "Performance in shieldbox" setting,it will receive data by udp. +# - for "Performance in air" setting, it will send data by udp. +# +# - for "Performance in long distance" setting, it will send data by udp. +# +# +#config MODE_UDP_SHIELDBOX +# bool "Performance in shieldbox" +#config MODE_UDP_AIR +# bool "Performance in air" +#config MODE_UDP_LONG_DISTANCE +# 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 n + help + yes:ESP32 is UDP sever. no:ESP32 is UDP client. + + We suggest to make this config be same with "Station mode". + +config UDP_PERF_TX + bool "UDP performance TX test enable" + default y + help + yes:UDP TX test. no:UDP RX test. + +config UDP_PERF_WIFI_SSID + string "WiFi SSID" + default "tp_wifi_test1" + help + SSID (network name) for the example to connect to. + +config UDP_PERF_WIFI_PASSWORD + string "WiFi Password" + default "1234567890" + help + WiFi password (WPA or WPA2) for the example to use. + +config UDP_PERF_SEVER_PORT + int "UDP sever port" + default 4567 + help + Which will the udp sever use. + +config UDP_PERF_SERVER_IP + string "UDP server ip" + default "192.168.4.1" + help + IP of UDP server. + + Ignore in UDP sever. + +config UDP_PERF_PKT_SIZE + int "Size of UDP packet" + default 1460 + help + the data send&recv packet size. + +endmenu diff --git a/examples/performance/udp_esp2ap/main/component.mk b/examples/performance/udp_perf/main/component.mk similarity index 100% rename from examples/performance/udp_esp2ap/main/component.mk rename to examples/performance/udp_perf/main/component.mk diff --git a/examples/performance/udp_perf/main/main.c b/examples/performance/udp_perf/main/main.c new file mode 100644 index 000000000..545a47d55 --- /dev/null +++ b/examples/performance/udp_perf/main/main.c @@ -0,0 +1,90 @@ + + +/* +udp_perf example + +Using this example to test udp throughput performance. +esp<->esp or esp<->ap + +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. + +step3: + send/receive data to/from each other. + you can see the info in com port output. +*/ + +#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) +{ + ESP_LOGI(TAG, "task udp_conn start."); + /*wating for connecting to AP*/ + do + { + vTaskDelay(100); + } + while (!connectedflag); + ESP_LOGI(TAG, "sta has connected to ap."); + + /*create udp socket*/ + int socret; + +#if ESP_UDP_MODE_SEVER + ESP_LOGI(TAG, "creat_udp_sever."); + socret=creat_udp_sever(); + //vTaskDelay(1000/portTICK_RATE_MS); +#else /*ESP_UDP_MODE_SEVER*/ + ESP_LOGI(TAG, "creat_udp_client."); + socret = creat_udp_client(); +#endif + if(-1 == socret) { + ESP_LOGI(TAG, "creat udp socket error,stop."); + vTaskDelete(NULL); + } + + /*create a task to tx/rx data*/ + TaskHandle_t tx_rx_task; + xTaskCreate(&send_recv_data, "send_recv_data", 4096, NULL, 4, &tx_rx_task); + + int pps; + while (1) { + totle_data = 0; + vTaskDelay(3000 / portTICK_RATE_MS);//every 3s + pps = totle_data / 3; + +#if ESP_UDP_PERF_TX + ESP_LOGI(TAG, "udp send %d byte per sec! totle pack: %d \n", pps, success_pack); +#else + ESP_LOGI(TAG, "udp recv %d byte per sec! totle pack: %d \n", pps, success_pack); +#endif /*ESP_UDP_PERF_TX*/ + } + close_socket(); + vTaskDelete(tx_rx_task); + vTaskDelete(NULL); +} + + + +void app_main(void) +{ + nvs_flash_init(); +#if ESP_WIFI_MODE_AP + ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n"); + wifi_init_softap(); +#else /*ESP_WIFI_MODE_AP*/ + ESP_LOGI(TAG, "ESP_WIFI_MODE_STA\n"); + wifi_init_sta(); +#endif + xTaskCreate(&udp_conn, "udp_conn", 4096, NULL, 5, NULL); +} diff --git a/examples/performance/udp_perf/main/udp_perf.c b/examples/performance/udp_perf/main/udp_perf.c new file mode 100644 index 000000000..ebaa5e018 --- /dev/null +++ b/examples/performance/udp_perf/main/udp_perf.c @@ -0,0 +1,198 @@ + +#include "udp_perf.h" + + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; +static int mysocket; + +static struct sockaddr_in remote_addr; +static unsigned int socklen; + + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + break; + case SYSTEM_EVENT_STA_GOT_IP: + 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; + 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; + 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); + break; + default: + break; + } + return ESP_OK; +} + + +//wifi_init_sta +void wifi_init_sta() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULT_SSID, + .password = DEFAULT_PWD + }, + }; + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK(esp_wifi_start() ); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n", + DEFAULT_SSID,DEFAULT_PWD); +} +//wifi_init_softap +void wifi_init_softap() +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .ap = { + .ssid = DEFAULT_SSID, + .ssid_len=0, + .max_connection=MAX_STA_CONN, + .password = DEFAULT_PWD, + .authmode=WIFI_AUTH_WPA_WPA2_PSK + }, + }; + + 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_start()); + + ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n", + DEFAULT_SSID, DEFAULT_PWD); +} + +//creat a udp sever socket. return 0:success -1:error +int creat_udp_sever() +{ + ESP_LOGI(TAG, "creat_udp_sever()"); + mysocket = socket(AF_INET, SOCK_DGRAM, 0); + if (mysocket < 0) { + perror("socket failed:"); + return -1; + } + 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:"); + close(mysocket); + return -1; + } + return 0; +} + +//creat a udp client socket. return 0:success -1:error +int creat_udp_client() +{ + ESP_LOGI(TAG, "creat_udp_client()"); + mysocket = socket(AF_INET, SOCK_DGRAM, 0); + if (mysocket < 0) { + perror("socket failed:"); + return -1; + } + /*for client remote_addr is also sever_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); + + return 0; +} + + +//send or recv data task +void send_recv_data(void *pvParameters) +{ + ESP_LOGI(TAG, "task send_recv_data start!\n"); + + int len; + char databuff[DEFAULT_PKTSIZE]; + + /*send&receive first packet*/ + socklen = sizeof(remote_addr); + memset(databuff, PACK_BYTE_IS, DEFAULT_PKTSIZE); +#if ESP_UDP_MODE_SEVER + ESP_LOGI(TAG, "first recvfrom:"); + len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen); +#else + ESP_LOGI(TAG, "first sendto:"); + len = sendto(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr)); +#endif + + if (len > 0) { + 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:"); + close(mysocket); + vTaskDelete(NULL); + } + +#if ESP_UDP_PERF_TX + vTaskDelay(500 / portTICK_RATE_MS); +#endif + ESP_LOGI(TAG, "start count!\n"); + while(1) + { + /*you can add delay time for fixed-frequency*/ + //vTaskDelay(5 / portTICK_RATE_MS); +#if ESP_UDP_PERF_TX + len = sendto(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr)); +#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; + success_pack++; + } else { + //perror("data_count:\n"); + /*you'd better turn off watch dog in menuconfig + *Component config->ESP32-specific->Task watchdog. + **/ + //vTaskDelay(1/portTICK_RATE_MS); + } + } +} + + +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 new file mode 100644 index 000000000..64ad225db --- /dev/null +++ b/examples/performance/udp_perf/main/udp_perf.h @@ -0,0 +1,73 @@ +#ifndef __UDP_PERF_H__ +#define __UDP_PERF_H__ + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include +#include +#include "driver/uart.h" +#include "soc/uart_struct.h" +#include "esp_log.h" + + + + +#ifdef __cplusplus +extern "C" { +#endif + +/*AP info and tcp_sever 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_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_PERF_TX CONFIG_UDP_PERF_TX //TRUE:send FALSE:receive +#define PACK_BYTE_IS 97 //'a' + +#define TAG "udp_perf:" + + +extern int connectedflag; +extern int totle_data; +extern int success_pack; + + +//using esp as station +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(); + +//send or recv data task +void send_recv_data(void *pvParameters); + +//close all socket +void close_socket(); + + + + + +#ifdef __cplusplus +} +#endif + + +#endif /*#ifndef __UDP_PERF_H__*/ + From e261e16981b9f78ea5b7ef1bffbf10c917cab85a Mon Sep 17 00:00:00 2001 From: chenyudong Date: Thu, 23 Mar 2017 16:36:32 +0800 Subject: [PATCH 5/7] 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 af7aae006..f5f9e6e0e 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 d08220f7a..eafb1b371 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 458ebfe13..1384dd47d 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 2ddc17762..a6b70cf90 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 c17add00b..97d6eaa13 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 545a47d55..10213063d 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 ebaa5e018..9b37fd162 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)); From 68bf54607a80fce1ee6f47a21bb58e47584bcd3e Mon Sep 17 00:00:00 2001 From: chenyudong Date: Fri, 31 Mar 2017 16:34:12 +0800 Subject: [PATCH 6/7] tcp_udp: remove some unnecessary functions. --- .../tcp_perf/main/Kconfig.projbuild | 12 +++---- examples/performance/tcp_perf/main/main.c | 9 +++-- examples/performance/tcp_perf/main/tcp_perf.c | 34 +++++++++++-------- examples/performance/tcp_perf/main/tcp_perf.h | 16 --------- .../udp_perf/main/Kconfig.projbuild | 19 ++++++----- examples/performance/udp_perf/main/main.c | 8 +++-- examples/performance/udp_perf/main/udp_perf.c | 28 +++++++++------ examples/performance/udp_perf/main/udp_perf.h | 16 --------- 8 files changed, 66 insertions(+), 76 deletions(-) diff --git a/examples/performance/tcp_perf/main/Kconfig.projbuild b/examples/performance/tcp_perf/main/Kconfig.projbuild index e40c52f83..c832f5667 100644 --- a/examples/performance/tcp_perf/main/Kconfig.projbuild +++ b/examples/performance/tcp_perf/main/Kconfig.projbuild @@ -5,12 +5,12 @@ menu "Example Configuration" # default MODE_TCP_SHIELDBOX # help # This option set performance mode. +# +# - Testing in shieldbox for "Performance in shieldbox" setting. # -# - for "Performance in shieldbox" setting,it will receive data by tcp. -# -# - for "Performance in air" setting, it will send data by tcp. -# -# - for "Performance in long distance" setting, it will send data by tcp. +# - Testing in air for "Performance in air" setting. +# +# - Testing in long distance for "Performance in long distance" setting. # # #config MODE_TCP_SHIELDBOX @@ -51,7 +51,7 @@ config TCP_PERF_DELAY_DEBUG config TCP_PERF_WIFI_SSID string "WiFi SSID" - default "tp_wifi_test1" + default "esp_wifi_test1" help SSID (network name) for the example to connect to. diff --git a/examples/performance/tcp_perf/main/main.c b/examples/performance/tcp_perf/main/main.c index eafb1b371..65811ff39 100644 --- a/examples/performance/tcp_perf/main/main.c +++ b/examples/performance/tcp_perf/main/main.c @@ -19,6 +19,12 @@ step3: you can see the info in com port output. */ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" +#include "esp_err.h" + #include "tcp_perf.h" int connectedflag = 0; @@ -57,7 +63,7 @@ static void tcp_conn(void *pvParameters) ESP_LOGI(TAG, "creat_tcp_client."); socret = creat_tcp_client(); #endif - if(-1 == socret) { + if(ESP_FAIL == socret) { ESP_LOGI(TAG, "creat tcp socket error,stop."); vTaskDelete(NULL); } @@ -103,7 +109,6 @@ static void tcp_conn(void *pvParameters) void app_main(void) { - nvs_flash_init(); #if ESP_WIFI_MODE_AP ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n"); wifi_init_softap(); diff --git a/examples/performance/tcp_perf/main/tcp_perf.c b/examples/performance/tcp_perf/main/tcp_perf.c index 1384dd47d..6d1e8cb76 100644 --- a/examples/performance/tcp_perf/main/tcp_perf.c +++ b/examples/performance/tcp_perf/main/tcp_perf.c @@ -1,8 +1,16 @@ + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_log.h" + + #include "tcp_perf.h" -/* 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 = 0; static struct sockaddr_in sever_addr; @@ -126,7 +134,7 @@ void recv_data(void *pvParameters) } -//use this esp32 as a tcp sever. return 0:success -1:error +//use this esp32 as a tcp sever. return ESP_OK:success ESP_FAIL:error int creat_tcp_sever() { ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT); @@ -134,7 +142,7 @@ int creat_tcp_sever() if (sever_socket < 0) { show_socket_error_code(sever_socket); //perror("socket() error:"); - return -1; + return ESP_FAIL; } sever_addr.sin_family = AF_INET; sever_addr.sin_port = htons(DEFAULT_PORT); @@ -143,26 +151,26 @@ int creat_tcp_sever() show_socket_error_code(sever_socket); //perror("bind() error"); close(sever_socket); - return -1; + return ESP_FAIL; } if (listen(sever_socket, 5) < 0) { show_socket_error_code(sever_socket); //perror("listen() error"); close(sever_socket); - return -1; + 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); - return -1; + return ESP_FAIL; } /*connection established,now can send/recv*/ ESP_LOGI(TAG, "tcp connection established!"); - return 0; + return ESP_OK; } -//use this esp32 as a tcp client. return 0:success -1:error +//use this esp32 as a tcp client. return ESP_OK:success ESP_FAIL:error int creat_tcp_client() { ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n", @@ -171,7 +179,7 @@ int creat_tcp_client() if (connect_soc < 0) { show_socket_error_code(connect_soc); //perror("socket failed!"); - return -1; + return ESP_FAIL; } sever_addr.sin_family = AF_INET; sever_addr.sin_port = htons(DEFAULT_PORT); @@ -180,17 +188,16 @@ int creat_tcp_client() if (connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) { show_socket_error_code(connect_soc); //perror("connect to sever error!"); - return -1; + return ESP_FAIL; } ESP_LOGI(TAG, "connect to sever success!"); - return 0; + return ESP_OK; } //wifi_init_sta void wifi_init_sta() { tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); @@ -214,7 +221,6 @@ void wifi_init_sta() void wifi_init_softap() { tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); diff --git a/examples/performance/tcp_perf/main/tcp_perf.h b/examples/performance/tcp_perf/main/tcp_perf.h index a6b70cf90..3b7ba0b60 100644 --- a/examples/performance/tcp_perf/main/tcp_perf.h +++ b/examples/performance/tcp_perf/main/tcp_perf.h @@ -1,22 +1,6 @@ #ifndef __TCP_PERF_H__ #define __TCP_PERF_H__ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include -#include -#include "driver/uart.h" -#include "soc/uart_struct.h" -#include "esp_log.h" - - #ifdef __cplusplus diff --git a/examples/performance/udp_perf/main/Kconfig.projbuild b/examples/performance/udp_perf/main/Kconfig.projbuild index 19917ee73..5567ed2cd 100644 --- a/examples/performance/udp_perf/main/Kconfig.projbuild +++ b/examples/performance/udp_perf/main/Kconfig.projbuild @@ -1,15 +1,16 @@ menu "Example Configuration" #choice -# prompt "UCP_PERF_MODE " -# default MODE_UDP_SHIELDBOX +# prompt "TCP_PERF_MODE " +# default MODE_TCP_SHIELDBOX # help # This option set performance mode. -# -# - for "Performance in shieldbox" setting,it will receive data by udp. -# - for "Performance in air" setting, it will send data by udp. # -# - for "Performance in long distance" setting, it will send data by udp. +# - Testing in shieldbox for "Performance in shieldbox" setting. +# +# - Testing in air for "Performance in air" setting. +# +# - Testing in long distance for "Performance in long distance" setting. # # #config MODE_UDP_SHIELDBOX @@ -28,7 +29,7 @@ config UDP_PERF_WIFI_MODE_AP config UDP_PERF_SEVER bool "TCP performance sever enable" - default n + default y help yes:ESP32 is UDP sever. no:ESP32 is UDP client. @@ -36,13 +37,13 @@ config UDP_PERF_SEVER config UDP_PERF_TX bool "UDP performance TX test enable" - default y + default n help yes:UDP TX test. no:UDP RX test. config UDP_PERF_WIFI_SSID string "WiFi SSID" - default "tp_wifi_test1" + default "esp_wifi_test1" help SSID (network name) for the example to connect to. diff --git a/examples/performance/udp_perf/main/main.c b/examples/performance/udp_perf/main/main.c index 10213063d..0318ee6a3 100644 --- a/examples/performance/udp_perf/main/main.c +++ b/examples/performance/udp_perf/main/main.c @@ -19,6 +19,11 @@ step3: you can see the info in com port output. */ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" +#include "esp_err.h" + #include "udp_perf.h" int connectedflag = 0; @@ -50,7 +55,7 @@ static void udp_conn(void *pvParameters) ESP_LOGI(TAG, "creat_udp_client."); socret = creat_udp_client(); #endif - if(-1 == socret) { + if(ESP_FAIL == socret) { ESP_LOGI(TAG, "creat udp socket error,stop."); vTaskDelete(NULL); } @@ -80,7 +85,6 @@ static void udp_conn(void *pvParameters) void app_main(void) { - nvs_flash_init(); #if ESP_WIFI_MODE_AP ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n"); wifi_init_softap(); diff --git a/examples/performance/udp_perf/main/udp_perf.c b/examples/performance/udp_perf/main/udp_perf.c index 9b37fd162..c3cb339c3 100644 --- a/examples/performance/udp_perf/main/udp_perf.c +++ b/examples/performance/udp_perf/main/udp_perf.c @@ -1,9 +1,17 @@ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_log.h" + + #include "udp_perf.h" -/* FreeRTOS event group to signal when we are connected & ready to make a request */ -static EventGroupHandle_t wifi_event_group; + static int mysocket; static struct sockaddr_in remote_addr; @@ -49,7 +57,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) void wifi_init_sta() { tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); @@ -73,7 +80,6 @@ void wifi_init_sta() void wifi_init_softap() { tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); @@ -99,14 +105,14 @@ void wifi_init_softap() DEFAULT_SSID, DEFAULT_PWD); } -//creat a udp sever socket. return 0:success -1:error +//creat a udp sever socket. return ESP_OK:success ESP_FAIL:error int creat_udp_sever() { ESP_LOGI(TAG, "creat_udp_sever()"); mysocket = socket(AF_INET, SOCK_DGRAM, 0); if (mysocket < 0) { perror("socket failed:"); - return -1; + return ESP_FAIL; } struct sockaddr_in sever_addr; sever_addr.sin_family = AF_INET; @@ -115,26 +121,26 @@ int creat_udp_sever() if (bind(mysocket, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) { perror("bind local port error:"); close(mysocket); - return -1; + return ESP_FAIL; } - return 0; + return ESP_OK; } -//creat a udp client socket. return 0:success -1:error +//creat a udp client socket. return ESP_OK:success ESP_FAIL:error int creat_udp_client() { ESP_LOGI(TAG, "creat_udp_client()"); mysocket = socket(AF_INET, SOCK_DGRAM, 0); if (mysocket < 0) { perror("socket failed:"); - return -1; + return ESP_FAIL; } /*for client remote_addr is also sever_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); - return 0; + return ESP_OK; } diff --git a/examples/performance/udp_perf/main/udp_perf.h b/examples/performance/udp_perf/main/udp_perf.h index 64ad225db..ecafcf9be 100644 --- a/examples/performance/udp_perf/main/udp_perf.h +++ b/examples/performance/udp_perf/main/udp_perf.h @@ -1,22 +1,6 @@ #ifndef __UDP_PERF_H__ #define __UDP_PERF_H__ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include -#include -#include "driver/uart.h" -#include "soc/uart_struct.h" -#include "esp_log.h" - - #ifdef __cplusplus From 28a7a325e8c918d7475bcf08bb2c3f543037e5dc Mon Sep 17 00:00:00 2001 From: chenyudong Date: Mon, 10 Apr 2017 20:39:04 +0800 Subject: [PATCH 7/7] modify some text error --- examples/performance/README.md | 30 ++- .../tcp_perf/main/Kconfig.projbuild | 110 ++++++---- .../tcp_perf/main/{main.c => tcp_main.c} | 46 ++--- examples/performance/tcp_perf/main/tcp_perf.c | 195 ++++++++++-------- examples/performance/tcp_perf/main/tcp_perf.h | 22 +- .../udp_perf/main/Kconfig.projbuild | 77 +++++-- .../udp_perf/main/{main.c => udp_main.c} | 39 ++-- examples/performance/udp_perf/main/udp_perf.c | 107 ++++++++-- examples/performance/udp_perf/main/udp_perf.h | 21 +- 9 files changed, 392 insertions(+), 255 deletions(-) rename examples/performance/tcp_perf/main/{main.c => tcp_main.c} (70%) rename examples/performance/udp_perf/main/{main.c => udp_main.c} (65%) 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();