modify some text error

This commit is contained in:
chenyudong 2017-04-10 20:39:04 +08:00
parent 68bf54607a
commit 28a7a325e8
9 changed files with 392 additions and 255 deletions

View File

@ -6,30 +6,28 @@ Including TCP/UDP TX/RX throughput.
# tcp_perf # 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. Step3: Start test. And here are some things that might help you do the test easily.
Here are some things that might help you do the test easily.
* You'd better turn on the AP before the STA. * You'd better turn on the AP before the STA.
* The tcp sever should be built before the tcp client. * The tcp server should be started before the tcp client.
* If you use a esp32 as AP, you'd better use it as tcp sever also. * 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 rebuild tcp connection. * 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 # 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: 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 # More

View File

@ -1,7 +1,7 @@
menu "Example Configuration" menu "Example Configuration"
#choice #choice
# prompt "TCP_PERF_MODE " # prompt "TCP_PERF_MODE "
# default MODE_TCP_SHIELDBOX # default MODE_TCP_SHIELDBOX
# help # help
# This option set performance mode. # This option set performance mode.
@ -21,64 +21,98 @@ menu "Example Configuration"
# bool "Performance in long distance" # bool "Performance in long distance"
#endchoice #endchoice
config TCP_PERF_WIFI_MODE_AP choice TCP_PERF_WIFI_MODE
bool "softap mode enable" prompt "AP or STA"
default n default TCP_PERF_ESP_IS_STATION
help help
yes:ESP32 is softap. no:ESP32 is station. Whether the esp32 is softAP or station.
config TCP_PERF_SEVER config TCP_PERF_ESP_IS_SOFTAP
bool "TCP performance sever enable" bool "SoftAP"
default n config TCP_PERF_ESP_IS_STATION
help bool "Station"
yes:ESP32 is TCP sever. no:ESP32 is TCP client. 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 config TCP_PERF_TX
bool "TCP performance TX test enable" bool
default n default y if TCP_PERF_ESP_SEND
help default n if TCP_PERF_ESP_RECV
yes:TCP TX test. no:TCP RX test.
config TCP_PERF_DELAY_DEBUG config TCP_PERF_DELAY_DEBUG
bool "TCP performance delay info enable" bool "TCP performance delay info enable"
default n depends on TCP_PERF_TX
help default n
Show TCP performance delay info. help
Show TCP performance delay info.
Ignore in TCP RX. Ignore in TCP RX.
config TCP_PERF_WIFI_SSID config TCP_PERF_WIFI_SSID
string "WiFi SSID" string "WiFi SSID"
default "esp_wifi_test1" default "esp_wifi_test1"
help help
SSID (network name) for the example to connect to. SSID (network name) for the example to connect to.
config TCP_PERF_WIFI_PASSWORD config TCP_PERF_WIFI_PASSWORD
string "WiFi Password" string "WiFi Password"
default "1234567890" default "1234567890"
help help
WiFi password (WPA or WPA2) for the example to use. WiFi password (WPA or WPA2) for the example to use.
config TCP_PERF_SEVER_PORT config TCP_PERF_SERVER_PORT
int "TCP sever port" int "TCP server port"
default 4567 default 4567
help help
Which will the tcp sever use. Which will the tcp server use.
config TCP_PERF_SERVER_IP config TCP_PERF_SERVER_IP
string "TCP server ip" string "TCP server ip"
default "192.168.4.1" default "192.168.4.1"
help help
IP of TCP server. IP of TCP server.
Ignore in TCP sever. Ignore in TCP server.
config TCP_PERF_PKT_SIZE config TCP_PERF_PKT_SIZE
int "Size of TCP packet" int "Size of TCP packet"
default 1460 default 1460
help help
the data send&recv packet size. the data send&recv packet size.
endmenu endmenu

View File

@ -10,13 +10,13 @@ step1:
init wifi as AP/STA using config SSID/PASSWORD. init wifi as AP/STA using config SSID/PASSWORD.
step2: step2:
creat a tcp sever/client socket using config PORT/(IP). create a tcp server/client socket using config PORT/(IP).
if sever: wating for connect. if server: wating for connect.
if client connect to sever. if client connect to server.
step3: step3:
send/receive data to/from each other. send/receive data to/from each other.
if the tcp connect established. esp will send or receive data. 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 <errno.h> #include <errno.h>
@ -27,17 +27,7 @@ step3:
#include "tcp_perf.h" #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 //this task establish a TCP connection and receive data from TCP
static void tcp_conn(void *pvParameters) static void tcp_conn(void *pvParameters)
@ -52,19 +42,19 @@ static void tcp_conn(void *pvParameters)
ESP_LOGI(TAG, "sta has connected to ap."); ESP_LOGI(TAG, "sta has connected to ap.");
/*create tcp socket*/ /*create tcp socket*/
int socret; int socket_ret;
#if ESP_TCP_MODE_SEVER #if ESP_TCP_MODE_SERVER
vTaskDelay(3000 / portTICK_RATE_MS); vTaskDelay(3000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "creat_tcp_sever."); ESP_LOGI(TAG, "create_tcp_server.");
socret=creat_tcp_sever(); socket_ret=create_tcp_server();
#else /*ESP_TCP_MODE_SEVER*/ #else /*ESP_TCP_MODE_SERVER*/
vTaskDelay(20000 / portTICK_RATE_MS); vTaskDelay(20000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "creat_tcp_client."); ESP_LOGI(TAG, "create_tcp_client.");
socret = creat_tcp_client(); socket_ret = create_tcp_client();
#endif #endif
if(ESP_FAIL == socret) { if(ESP_FAIL == socket_ret) {
ESP_LOGI(TAG, "creat tcp socket error,stop."); ESP_LOGI(TAG, "create tcp socket error,stop.");
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@ -77,10 +67,10 @@ static void tcp_conn(void *pvParameters)
#endif #endif
int pps; int pps;
while (1) { while (1) {
totle_data = 0; total_data = 0;
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
pps = totle_data / 3; pps = total_data / 3;
if (totle_data <= 0) { if (total_data <= 0) {
int err_ret = check_socket_error_code(); int err_ret = check_socket_error_code();
if (err_ret == ECONNRESET) { if (err_ret == ECONNRESET) {
ESP_LOGI(TAG, "disconnected... stop."); ESP_LOGI(TAG, "disconnected... stop.");
@ -91,9 +81,9 @@ static void tcp_conn(void *pvParameters)
#if ESP_TCP_PERF_TX #if ESP_TCP_PERF_TX
ESP_LOGI(TAG, "tcp send %d byte per sec!", pps); ESP_LOGI(TAG, "tcp send %d byte per sec!", pps);
#if ESP_TCP_DELAY_INFO #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", "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]); delay_classify[1], delay_classify[2], delay_classify[3], delay_classify[4]);
#endif /*ESP_TCP_DELAY_INFO*/ #endif /*ESP_TCP_DELAY_INFO*/
#else #else

View File

@ -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 <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -12,11 +24,24 @@
#include "tcp_perf.h" #include "tcp_perf.h"
/*socket*/ /*socket*/
static int sever_socket = 0; static int server_socket = 0;
static struct sockaddr_in sever_addr; static struct sockaddr_in server_addr;
static struct sockaddr_in client_addr; static struct sockaddr_in client_addr;
static unsigned int socklen = sizeof(client_addr); static unsigned int socklen = sizeof(client_addr);
static int connect_soc = 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) static esp_err_t event_handler(void *ctx, system_event_t *event)
{ {
@ -70,25 +95,23 @@ void send_data(void *pvParameters)
while(1) { while(1) {
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
//vTaskDelay(1000/portTICK_RATE_MS); total_pack++;
totle_pack++;
gettimeofday(&tv_start, NULL); gettimeofday(&tv_start, NULL);
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ #endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
//send function //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 #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
gettimeofday(&tv_finish, NULL); gettimeofday(&tv_finish, NULL);
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ #endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
if(len > 0) { if(len > 0) {
totle_data += len; total_data += len;
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
send_success++; send_success++;
send_delay_ms = (tv_finish.tv_sec - tv_start.tv_sec) * 1000 send_delay_ms = (tv_finish.tv_sec - tv_start.tv_sec) * 1000
+ (tv_finish.tv_usec - tv_start.tv_usec) / 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) if(send_delay_ms < 30)
delay_classify[0]++; delay_classify[0]++;
else if(send_delay_ms < 100) else if(send_delay_ms < 100)
@ -108,10 +131,11 @@ void send_data(void *pvParameters)
send_fail++; send_fail++;
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/ #endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
/*for faster sending,don't show error code /*Most of the error code will be send window full.
*if it can't work as expectations,unnote the two lines here *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); //vTaskDelay(500/portTICK_RATE_MS);
} }
} }
@ -122,48 +146,44 @@ void recv_data(void *pvParameters)
int len = 0; int len = 0;
char databuff[DEFAULT_PKTSIZE]; char databuff[DEFAULT_PKTSIZE];
while (1) { while (1) {
len = recv(connect_soc, databuff, DEFAULT_PKTSIZE, 0); len = recv(connect_socket, databuff, DEFAULT_PKTSIZE, 0);
if (len > 0) { if (len > 0) {
totle_data += len; total_data += len;
} }
else { else {
show_socket_error_code(connect_soc); show_socket_error_code(connect_socket);
vTaskDelay(500 / portTICK_RATE_MS); vTaskDelay(500 / portTICK_RATE_MS);
} }
} }
} }
//use this esp32 as a tcp sever. return ESP_OK:success ESP_FAIL:error //use this esp32 as a tcp server. return ESP_OK:success ESP_FAIL:error
int creat_tcp_sever() esp_err_t create_tcp_server()
{ {
ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT); ESP_LOGI(TAG, "server socket....port=%d\n", DEFAULT_PORT);
sever_socket = socket(AF_INET, SOCK_STREAM, 0); server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (sever_socket < 0) { if (server_socket < 0) {
show_socket_error_code(sever_socket); show_socket_error_code(server_socket);
//perror("socket() error:");
return ESP_FAIL; return ESP_FAIL;
} }
sever_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULT_PORT); server_addr.sin_port = htons(DEFAULT_PORT);
sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sever_socket, (struct sockaddr*)&sever_addr, sizeof(sever_addr)) < 0) { if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
show_socket_error_code(sever_socket); show_socket_error_code(server_socket);
//perror("bind() error"); close(server_socket);
close(sever_socket);
return ESP_FAIL; return ESP_FAIL;
} }
if (listen(sever_socket, 5) < 0) { if (listen(server_socket, 5) < 0) {
show_socket_error_code(sever_socket); show_socket_error_code(server_socket);
//perror("listen() error"); close(server_socket);
close(sever_socket);
return ESP_FAIL; return ESP_FAIL;
} }
connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen); connect_socket = accept(server_socket, (struct sockaddr*)&client_addr, &socklen);
if (connect_soc<0) { if (connect_socket<0) {
show_socket_error_code(connect_soc); show_socket_error_code(connect_socket);
//perror("accept() error"); close(server_socket);
close(sever_socket);
return ESP_FAIL; return ESP_FAIL;
} }
/*connection establishednow can send/recv*/ /*connection establishednow can send/recv*/
@ -171,26 +191,24 @@ int creat_tcp_sever()
return ESP_OK; return ESP_OK;
} }
//use this esp32 as a tcp client. return ESP_OK:success ESP_FAIL:error //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", ESP_LOGI(TAG, "client socket....serverip:port=%s:%d\n",
DEFAULT_SEVER_IP, DEFAULT_PORT); DEFAULT_SERVER_IP, DEFAULT_PORT);
connect_soc = socket(AF_INET, SOCK_STREAM, 0); connect_socket = socket(AF_INET, SOCK_STREAM, 0);
if (connect_soc < 0) { if (connect_socket < 0) {
show_socket_error_code(connect_soc); show_socket_error_code(connect_socket);
//perror("socket failed!");
return ESP_FAIL; return ESP_FAIL;
} }
sever_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULT_PORT); server_addr.sin_port = htons(DEFAULT_PORT);
sever_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP); server_addr.sin_addr.s_addr = inet_addr(DEFAULT_SERVER_IP);
ESP_LOGI(TAG, "connecting to sever..."); ESP_LOGI(TAG, "connecting to server...");
if (connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) { if (connect(connect_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
show_socket_error_code(connect_soc); show_socket_error_code(connect_socket);
//perror("connect to sever error!");
return ESP_FAIL; return ESP_FAIL;
} }
ESP_LOGI(TAG, "connect to sever success!"); ESP_LOGI(TAG, "connect to server success!");
return ESP_OK; return ESP_OK;
} }
@ -251,72 +269,71 @@ void wifi_init_softap()
char* tcpip_get_reason(int err) char* tcpip_get_reason(int err)
{ {
switch (err) { switch (err) {
case 0: case 0:
return "reason: other reason"; return "reason: other reason";
case ENOMEM: case ENOMEM:
return "reason: out of memory"; return "reason: out of memory";
case ENOBUFS: case ENOBUFS:
return "reason: buffer error"; return "reason: buffer error";
case EWOULDBLOCK: case EWOULDBLOCK:
return "reason: timeout, try again"; return "reason: timeout, try again";
case EHOSTUNREACH: case EHOSTUNREACH:
return "reason: routing problem"; return "reason: routing problem";
case EINPROGRESS: case EINPROGRESS:
return "reason: operation in progress"; return "reason: operation in progress";
case EINVAL: case EINVAL:
return "reason: invalid value"; return "reason: invalid value";
case EADDRINUSE: case EADDRINUSE:
return "reason: address in use"; return "reason: address in use";
case EALREADY: case EALREADY:
return "reason: conn already connected"; return "reason: conn already connected";
case EISCONN: case EISCONN:
return "reason: conn already established"; return "reason: conn already established";
case ECONNABORTED: case ECONNABORTED:
return "reason: connection aborted"; return "reason: connection aborted";
case ECONNRESET: case ECONNRESET:
return "reason: connection is reset"; return "reason: connection is reset";
case ENOTCONN: case ENOTCONN:
return "reason: connection closed"; return "reason: connection closed";
case EIO: case EIO:
return "reason: invalid argument"; return "reason: invalid argument";
case -1: case -1:
return "reason: low level netif error"; return "reason: low level netif error";
default: 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); u32_t optlen = sizeof(int);
getsockopt(soc, SOL_SOCKET, SO_ERROR, &result, &optlen); getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen);
ESP_LOGI(TAG, "soc error %d reason: %s", result, tcpip_get_reason(result)); ESP_LOGI(TAG, "socket error %d reason: %s", result, tcpip_get_reason(result));
return result; return result;
} }
int check_socket_error_code() int check_socket_error_code()
{ {
int ret; int ret;
#if ESP_TCP_MODE_SEVER #if ESP_TCP_MODE_SERVER
ESP_LOGI(TAG, "check sever_socket sever_socket"); ESP_LOGI(TAG, "check server_socket");
ret = show_socket_error_code(sever_socket); ret = show_socket_error_code(server_socket);
if(ret == ECONNRESET) if(ret == ECONNRESET)
return ret; return ret;
#endif #endif
ESP_LOGI(TAG, "check sever_socket connect_soc"); ESP_LOGI(TAG, "check connect_socket");
ret = show_socket_error_code(connect_soc); ret = show_socket_error_code(connect_socket);
if(ret == ECONNRESET) if(ret == ECONNRESET)
return ret; return ret;
return 0; return 0;
} }
void close_socket() void close_socket()
{ {
close(connect_soc); close(connect_socket);
close(sever_socket); close(server_socket);
} }

View File

@ -7,16 +7,16 @@
extern "C" { extern "C" {
#endif #endif
/*AP info and tcp_sever info*/ /*AP info and tcp_server info*/
#define DEFAULT_SSID CONFIG_TCP_PERF_WIFI_SSID #define DEFAULT_SSID CONFIG_TCP_PERF_WIFI_SSID
#define DEFAULT_PWD CONFIG_TCP_PERF_WIFI_PASSWORD #define DEFAULT_PWD CONFIG_TCP_PERF_WIFI_PASSWORD
#define DEFAULT_PORT CONFIG_TCP_PERF_SEVER_PORT #define DEFAULT_PORT CONFIG_TCP_PERF_SERVER_PORT
#define DEFAULT_SEVER_IP CONFIG_TCP_PERF_SERVER_IP #define DEFAULT_SERVER_IP CONFIG_TCP_PERF_SERVER_IP
#define DEFAULT_PKTSIZE CONFIG_TCP_PERF_PKT_SIZE #define DEFAULT_PKTSIZE CONFIG_TCP_PERF_PKT_SIZE
#define MAX_STA_CONN 1 //how many sta can be connected(AP mode) #define MAX_STA_CONN 1 //how many sta can be connected(AP mode)
/*test options*/ /*test options*/
#define ESP_WIFI_MODE_AP CONFIG_TCP_PERF_WIFI_MODE_AP //TRUE:AP FALSE:STA #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_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 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 connectedflag;
extern int totle_data; extern int total_data;
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO #if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
extern int totle_pack; extern int total_pack;
extern int send_success; extern int send_success;
extern int send_fail; extern int send_fail;
extern int delay_classify[5]; extern int delay_classify[5];
@ -41,10 +41,10 @@ void wifi_init_sta();
//using esp as softap //using esp as softap
void wifi_init_softap(); void wifi_init_softap();
//creat a tcp sever socket. return 0:success -1:error //create a tcp server socket. return ESP_OK:success ESP_FAIL:error
int creat_tcp_sever(); esp_err_t create_tcp_server();
//creat a tcp client socket. return 0:success -1:error //create a tcp client socket. return ESP_OK:success ESP_FAIL:error
int creat_tcp_client(); esp_err_t create_tcp_client();
//send data task //send data task
void send_data(void *pvParameters); void send_data(void *pvParameters);
@ -55,7 +55,7 @@ void recv_data(void *pvParameters);
void close_socket(); void close_socket();
//show socket error code. return: error code //show socket error code. return: error code
int show_socket_error_code(int soc); int show_socket_error_code(int socket);
//check working socket //check working socket
int check_socket_error_code(); int check_socket_error_code();

View File

@ -1,8 +1,8 @@
menu "Example Configuration" menu "Example Configuration"
#choice #choice
# prompt "TCP_PERF_MODE " # prompt "UDP_PERF_MODE "
# default MODE_TCP_SHIELDBOX # default MODE_UDP_SHIELDBOX
# help # help
# This option set performance mode. # This option set performance mode.
# #
@ -21,25 +21,60 @@ menu "Example Configuration"
# bool "Performance in long distance" # bool "Performance in long distance"
#endchoice #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 choice UDP_PERF_WIFI_MODE
bool "TCP performance sever enable" prompt "AP or STA"
default y default UDP_PERF_ESP_IS_STATION
help help
yes:ESP32 is UDP sever. no:ESP32 is UDP client. 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 config UDP_PERF_TX
bool "UDP performance TX test enable" bool
default n default y if UDP_PERF_ESP_SEND
help default n if UDP_PERF_ESP_RECV
yes:UDP TX test. no:UDP RX test.
config UDP_PERF_WIFI_SSID config UDP_PERF_WIFI_SSID
string "WiFi SSID" string "WiFi SSID"
@ -53,11 +88,11 @@ config UDP_PERF_WIFI_PASSWORD
help help
WiFi password (WPA or WPA2) for the example to use. WiFi password (WPA or WPA2) for the example to use.
config UDP_PERF_SEVER_PORT config UDP_PERF_SERVER_PORT
int "UDP sever port" int "UDP server port"
default 4567 default 4567
help help
Which will the udp sever use. Which will the udp server use.
config UDP_PERF_SERVER_IP config UDP_PERF_SERVER_IP
string "UDP server ip" string "UDP server ip"
@ -65,7 +100,7 @@ config UDP_PERF_SERVER_IP
help help
IP of UDP server. IP of UDP server.
Ignore in UDP sever. Ignore in UDP server.
config UDP_PERF_PKT_SIZE config UDP_PERF_PKT_SIZE
int "Size of UDP packet" int "Size of UDP packet"

View File

@ -10,15 +10,16 @@ step1:
init wifi as AP/STA using config SSID/PASSWORD. init wifi as AP/STA using config SSID/PASSWORD.
step2: step2:
creat a udp sever/client socket using config PORT/(IP). create a udp server/client socket using config PORT/(IP).
if sever: wating for the first message of client. if server: wating for the first message of client.
if client: sending a packet to sever first. if client: sending a packet to server first.
step3: step3:
send/receive data to/from each other. 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/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_log.h" #include "esp_log.h"
@ -26,9 +27,6 @@ step3:
#include "udp_perf.h" #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 //this task establish a UDP connection and receive data from UDP
static void udp_conn(void *pvParameters) static void udp_conn(void *pvParameters)
@ -43,20 +41,19 @@ static void udp_conn(void *pvParameters)
ESP_LOGI(TAG, "sta has connected to ap."); ESP_LOGI(TAG, "sta has connected to ap.");
/*create udp socket*/ /*create udp socket*/
int socret; int socket_ret;
#if ESP_UDP_MODE_SEVER #if ESP_UDP_MODE_SERVER
vTaskDelay(3000 / portTICK_RATE_MS); vTaskDelay(3000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "creat_udp_sever."); ESP_LOGI(TAG, "create_udp_server.");
socret=creat_udp_sever(); socket_ret=create_udp_server();
//vTaskDelay(1000/portTICK_RATE_MS); #else /*ESP_UDP_MODE_SERVER*/
#else /*ESP_UDP_MODE_SEVER*/
vTaskDelay(20000 / portTICK_RATE_MS); vTaskDelay(20000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "creat_udp_client."); ESP_LOGI(TAG, "create_udp_client.");
socret = creat_udp_client(); socket_ret = create_udp_client();
#endif #endif
if(ESP_FAIL == socret) { if(ESP_FAIL == socket_ret) {
ESP_LOGI(TAG, "creat udp socket error,stop."); ESP_LOGI(TAG, "create udp socket error,stop.");
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@ -66,14 +63,14 @@ static void udp_conn(void *pvParameters)
int pps; int pps;
while (1) { while (1) {
totle_data = 0; total_data = 0;
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
pps = totle_data / 3; pps = total_data / 3;
#if ESP_UDP_PERF_TX #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 #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*/ #endif /*ESP_UDP_PERF_TX*/
} }
close_socket(); close_socket();

View File

@ -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 <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -17,6 +30,10 @@ static int mysocket;
static struct sockaddr_in remote_addr; static struct sockaddr_in remote_addr;
static unsigned int socklen; 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) static esp_err_t event_handler(void *ctx, system_event_t *event)
{ {
@ -105,40 +122,40 @@ void wifi_init_softap()
DEFAULT_SSID, DEFAULT_PWD); DEFAULT_SSID, DEFAULT_PWD);
} }
//creat a udp sever socket. return ESP_OK:success ESP_FAIL:error //create a udp server socket. return ESP_OK:success ESP_FAIL:error
int creat_udp_sever() 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); mysocket = socket(AF_INET, SOCK_DGRAM, 0);
if (mysocket < 0) { if (mysocket < 0) {
perror("socket failed:"); show_socket_error_code(mysocket);
return ESP_FAIL; return ESP_FAIL;
} }
struct sockaddr_in sever_addr; struct sockaddr_in server_addr;
sever_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULT_PORT); server_addr.sin_port = htons(DEFAULT_PORT);
sever_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(mysocket, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) { if (bind(mysocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind local port error:"); show_socket_error_code(mysocket);
close(mysocket); close(mysocket);
return ESP_FAIL; return ESP_FAIL;
} }
return ESP_OK; return ESP_OK;
} }
//creat a udp client socket. return ESP_OK:success ESP_FAIL:error //create a udp client socket. return ESP_OK:success ESP_FAIL:error
int creat_udp_client() 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); mysocket = socket(AF_INET, SOCK_DGRAM, 0);
if (mysocket < 0) { if (mysocket < 0) {
perror("socket failed:"); show_socket_error_code(mysocket);
return ESP_FAIL; 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_family = AF_INET;
remote_addr.sin_port = htons(DEFAULT_PORT); 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; return ESP_OK;
} }
@ -155,7 +172,7 @@ void send_recv_data(void *pvParameters)
/*send&receive first packet*/ /*send&receive first packet*/
socklen = sizeof(remote_addr); socklen = sizeof(remote_addr);
memset(databuff, PACK_BYTE_IS, DEFAULT_PKTSIZE); memset(databuff, PACK_BYTE_IS, DEFAULT_PKTSIZE);
#if ESP_UDP_MODE_SEVER #if ESP_UDP_MODE_SERVER
ESP_LOGI(TAG, "first recvfrom:"); ESP_LOGI(TAG, "first recvfrom:");
len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen); len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen);
#else #else
@ -167,7 +184,7 @@ void send_recv_data(void *pvParameters)
ESP_LOGI(TAG, "transfer data with %s:%u\n", ESP_LOGI(TAG, "transfer data with %s:%u\n",
inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port)); inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));
} else { } else {
perror("first recv&send error:"); show_socket_error_code(mysocket);
close(mysocket); close(mysocket);
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@ -185,13 +202,11 @@ void send_recv_data(void *pvParameters)
#else #else
len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen); len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen);
#endif #endif
//printf("%d\n",len);
//vTaskDelay(100/portTICK_RATE_MS);
if (len > 0) { if (len > 0) {
totle_data += len; total_data += len;
success_pack++; success_pack++;
} else { } else {
//perror("data_count:\n"); //show_socket_error_code(mysocket);
/*you'd better turn off watch dog in menuconfig /*you'd better turn off watch dog in menuconfig
*Component config->ESP32-specific->Task watchdog. *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() void close_socket()
{ {
close(mysocket); close(mysocket);

View File

@ -7,16 +7,16 @@
extern "C" { extern "C" {
#endif #endif
/*AP info and tcp_sever info*/ /*AP info and tcp_server info*/
#define DEFAULT_SSID CONFIG_UDP_PERF_WIFI_SSID #define DEFAULT_SSID CONFIG_UDP_PERF_WIFI_SSID
#define DEFAULT_PWD CONFIG_UDP_PERF_WIFI_PASSWORD #define DEFAULT_PWD CONFIG_UDP_PERF_WIFI_PASSWORD
#define DEFAULT_PORT CONFIG_UDP_PERF_SEVER_PORT #define DEFAULT_PORT CONFIG_UDP_PERF_SERVER_PORT
#define DEFAULT_SEVER_IP CONFIG_UDP_PERF_SERVER_IP #define DEFAULT_SERVER_IP CONFIG_UDP_PERF_SERVER_IP
#define DEFAULT_PKTSIZE CONFIG_UDP_PERF_PKT_SIZE #define DEFAULT_PKTSIZE CONFIG_UDP_PERF_PKT_SIZE
#define MAX_STA_CONN 1 //how many sta can be connected(AP mode) #define MAX_STA_CONN 1 //how many sta can be connected(AP mode)
/*test options*/ /*test options*/
#define ESP_WIFI_MODE_AP CONFIG_UDP_PERF_WIFI_MODE_AP //TRUE:AP FALSE:STA #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 ESP_UDP_PERF_TX CONFIG_UDP_PERF_TX //TRUE:send FALSE:receive
#define PACK_BYTE_IS 97 //'a' #define PACK_BYTE_IS 97 //'a'
@ -24,7 +24,7 @@ extern "C" {
extern int connectedflag; extern int connectedflag;
extern int totle_data; extern int total_data;
extern int success_pack; extern int success_pack;
@ -33,14 +33,17 @@ void wifi_init_sta();
//using esp as softap //using esp as softap
void wifi_init_softap(); void wifi_init_softap();
//creat a udp sever socket. return 0:success -1:error //create a udp server socket. return ESP_OK:success ESP_FAIL:error
int creat_udp_sever(); esp_err_t create_udp_server();
//creat a udp client socket. return 0:success -1:error //create a udp client socket. return ESP_OK:success ESP_FAIL:error
int creat_udp_client(); esp_err_t create_udp_client();
//send or recv data task //send or recv data task
void send_recv_data(void *pvParameters); void send_recv_data(void *pvParameters);
//show socket error code. return: error code
int show_socket_error_code(int socket);
//close all socket //close all socket
void close_socket(); void close_socket();