mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
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.
This commit is contained in:
parent
a0b565a8e8
commit
5ecc88cb33
@ -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.
|
||||
|
@ -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
|
@ -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 <stdio.h>
|
||||
#include <string.h>
|
||||
#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 <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#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);
|
||||
}
|
@ -3,7 +3,7 @@
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := tcp_esp2ap
|
||||
PROJECT_NAME := tcp_perf
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
84
examples/performance/tcp_perf/main/Kconfig.projbuild
Normal file
84
examples/performance/tcp_perf/main/Kconfig.projbuild
Normal file
@ -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
|
109
examples/performance/tcp_perf/main/main.c
Normal file
109
examples/performance/tcp_perf/main/main.c
Normal file
@ -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);
|
||||
}
|
251
examples/performance/tcp_perf/main/tcp_perf.c
Normal file
251
examples/performance/tcp_perf/main/tcp_perf.c
Normal file
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
71
examples/performance/tcp_perf/main/tcp_perf.h
Normal file
71
examples/performance/tcp_perf/main/tcp_perf.h
Normal file
@ -0,0 +1,71 @@
|
||||
#ifndef __TCP_PERF_H__
|
||||
#define __TCP_PERF_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#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 <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#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
|
||||
|
Loading…
x
Reference in New Issue
Block a user