From a0b565a8e8ff2d1639fd29f81351aacb9658e1c6 Mon Sep 17 00:00:00 2001 From: yudongchen95 Date: Thu, 16 Mar 2017 15:04:44 +0800 Subject: [PATCH] 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 0000000000..d79ee486ee --- /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 66d48e0d68..0000000000 --- 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 273925f45e..0000000000 --- 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 cc4fd5dcb4..0000000000 --- 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 9dd5e939fd..0000000000 --- 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 61f8990c31..0000000000 --- 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 40b1896840..0000000000 --- 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 4972d0e2a2..0000000000 --- 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 61f8990c31..0000000000 --- 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 c6e0826582..a4ca46adb3 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 0000000000..39b0c8ee6a --- /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 0000000000..4e3ccc4f05 --- /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 887cf127c7..c17add00b6 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 0000000000..59e86a0bd1 --- /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 0000000000..ac8a745773 --- /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); +}