mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
example: remove performance example
Remove performance example since we have iperf example close github issue: https://github.com/espressif/esp-idf/issues/1515
This commit is contained in:
parent
63ddae5087
commit
9fb2d45980
@ -1,44 +0,0 @@
|
|||||||
# Wifi Performance Examples
|
|
||||||
|
|
||||||
Some simple codes help to test the wifi performance.
|
|
||||||
|
|
||||||
Including TCP/UDP TX/RX throughput.
|
|
||||||
|
|
||||||
# tcp_perf
|
|
||||||
|
|
||||||
This example is used to test tcp throughput and delay time.
|
|
||||||
|
|
||||||
Step1: Set options in `make menuconfig` like ssid, password, server ip and server port. And choose what the esp32 will work as.
|
|
||||||
|
|
||||||
* 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.
|
|
||||||
|
|
||||||
* 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.
|
|
||||||
|
|
||||||
* Send or receive. Set one of them sending data and the other receiving.
|
|
||||||
|
|
||||||
Step2: Exit menuconfig, saving the new settings. Then build the app and flash it to the ESP32.
|
|
||||||
|
|
||||||
Step3: Start test. And here are some things that might help you do the test easily.
|
|
||||||
|
|
||||||
* You'd better turn on the AP before the STA.
|
|
||||||
* The tcp server should be started before the tcp client.
|
|
||||||
* If you use a esp32 as AP, you'd better use it as tcp server also.
|
|
||||||
* Once the tcp connection crashed, esp32 should be restarted to re-establish TCP connection.
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
See [main.c](./tcp_perf/main/main.c) for full details.
|
|
||||||
|
|
||||||
# udp_perf
|
|
||||||
|
|
||||||
This example is similar to tcp_perf. Also the steps is similar to tcp_perf.
|
|
||||||
|
|
||||||
There's a obvious difference between udp_perf and tcp perf:
|
|
||||||
|
|
||||||
Before formal sending & receiving, a packet will be send from client to server. So the server can know the ip&port of client. It is usually eaiser to set the UDP server as the receiver.
|
|
||||||
|
|
||||||
See [main.c](./udp_perf/main/main.c) for full details.
|
|
||||||
|
|
||||||
# More
|
|
||||||
|
|
||||||
See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples.
|
|
@ -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_perf
|
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
|
||||||
|
|
@ -1,119 +0,0 @@
|
|||||||
menu "Example Configuration"
|
|
||||||
|
|
||||||
#choice
|
|
||||||
# prompt "TCP_PERF_MODE "
|
|
||||||
# default MODE_TCP_SHIELDBOX
|
|
||||||
# help
|
|
||||||
# This option set performance mode.
|
|
||||||
#
|
|
||||||
# - Testing in shieldbox for "Performance in shieldbox" setting.
|
|
||||||
#
|
|
||||||
# - Testing in air for "Performance in air" setting.
|
|
||||||
#
|
|
||||||
# - Testing in long distance for "Performance in long distance" setting.
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#config MODE_TCP_SHIELDBOX
|
|
||||||
# bool "Performance in shieldbox"
|
|
||||||
#config MODE_TCP_AIR
|
|
||||||
# bool "Performance in air"
|
|
||||||
#config MODE_TCP_LONG_DISTANCE
|
|
||||||
# bool "Performance in long distance"
|
|
||||||
#endchoice
|
|
||||||
|
|
||||||
choice TCP_PERF_WIFI_MODE
|
|
||||||
prompt "AP or STA"
|
|
||||||
default TCP_PERF_ESP_IS_STATION
|
|
||||||
help
|
|
||||||
Whether the esp32 is softAP or station.
|
|
||||||
|
|
||||||
config TCP_PERF_ESP_IS_SOFTAP
|
|
||||||
bool "SoftAP"
|
|
||||||
config TCP_PERF_ESP_IS_STATION
|
|
||||||
bool "Station"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
config TCP_PERF_WIFI_MODE_AP
|
|
||||||
bool
|
|
||||||
default y if TCP_PERF_ESP_IS_SOFTAP
|
|
||||||
default n if TCP_PERF_ESP_IS_STATION
|
|
||||||
|
|
||||||
choice TCP_PERF_SERVER_CLIENT
|
|
||||||
prompt "server or client"
|
|
||||||
default TCP_PERF_ESP_IS_CLIENT
|
|
||||||
help
|
|
||||||
Whether the esp32 is tcp server or client.
|
|
||||||
|
|
||||||
We suggest to choose "client" if you choose "station" in "wifi mode".
|
|
||||||
|
|
||||||
config TCP_PERF_ESP_IS_SERVER
|
|
||||||
bool "server"
|
|
||||||
config TCP_PERF_ESP_IS_CLIENT
|
|
||||||
bool "client"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
config TCP_PERF_SERVER
|
|
||||||
bool
|
|
||||||
default y if TCP_PERF_ESP_IS_SERVER
|
|
||||||
default n if TCP_PERF_ESP_IS_CLIENT
|
|
||||||
|
|
||||||
choice TCP_PERF_TX_RX
|
|
||||||
prompt "send or receive"
|
|
||||||
default TCP_PERF_ESP_RECV
|
|
||||||
help
|
|
||||||
Whether the esp32 will send or receive.
|
|
||||||
|
|
||||||
config TCP_PERF_ESP_SEND
|
|
||||||
bool "send"
|
|
||||||
config TCP_PERF_ESP_RECV
|
|
||||||
bool "receive"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
config TCP_PERF_TX
|
|
||||||
bool
|
|
||||||
default y if TCP_PERF_ESP_SEND
|
|
||||||
default n if TCP_PERF_ESP_RECV
|
|
||||||
|
|
||||||
config TCP_PERF_DELAY_DEBUG
|
|
||||||
bool "TCP performance delay info enable"
|
|
||||||
depends on TCP_PERF_TX
|
|
||||||
default n
|
|
||||||
help
|
|
||||||
Show TCP performance delay info.
|
|
||||||
|
|
||||||
Ignore in TCP RX.
|
|
||||||
|
|
||||||
config TCP_PERF_WIFI_SSID
|
|
||||||
string "WiFi SSID"
|
|
||||||
default "esp_wifi_test1"
|
|
||||||
help
|
|
||||||
SSID (network name) for the example to connect to.
|
|
||||||
|
|
||||||
config TCP_PERF_WIFI_PASSWORD
|
|
||||||
string "WiFi Password"
|
|
||||||
default "1234567890"
|
|
||||||
help
|
|
||||||
WiFi password (WPA or WPA2) for the example to use.
|
|
||||||
|
|
||||||
config TCP_PERF_SERVER_PORT
|
|
||||||
int "TCP server port"
|
|
||||||
default 4567
|
|
||||||
help
|
|
||||||
Which will the tcp server use.
|
|
||||||
|
|
||||||
config TCP_PERF_SERVER_IP
|
|
||||||
string "TCP server ip"
|
|
||||||
depends on TCP_PERF_ESP_IS_CLIENT
|
|
||||||
default "192.168.4.1"
|
|
||||||
help
|
|
||||||
IP of TCP server.
|
|
||||||
|
|
||||||
Ignore in TCP server.
|
|
||||||
|
|
||||||
config TCP_PERF_PKT_SIZE
|
|
||||||
int "Size of TCP packet"
|
|
||||||
default 1460
|
|
||||||
help
|
|
||||||
the data send&recv packet size.
|
|
||||||
|
|
||||||
endmenu
|
|
@ -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.
|
|
||||||
#
|
|
@ -1,142 +0,0 @@
|
|||||||
/* tcp_perf Example
|
|
||||||
|
|
||||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, this
|
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
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:
|
|
||||||
create a tcp server/client socket using config PORT/(IP).
|
|
||||||
if server: wating for connect.
|
|
||||||
if client connect to server.
|
|
||||||
step3:
|
|
||||||
send/receive data to/from each other.
|
|
||||||
if the tcp connect established. esp will send or receive data.
|
|
||||||
you can see the info in serial output.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
#include "freertos/FreeRTOS.h"
|
|
||||||
#include "freertos/task.h"
|
|
||||||
#include "freertos/event_groups.h"
|
|
||||||
#include "esp_log.h"
|
|
||||||
#include "esp_err.h"
|
|
||||||
#include "nvs_flash.h"
|
|
||||||
|
|
||||||
#include "tcp_perf.h"
|
|
||||||
|
|
||||||
//this task establish a TCP connection and receive data from TCP
|
|
||||||
static void tcp_conn(void *pvParameters)
|
|
||||||
{
|
|
||||||
while (1) {
|
|
||||||
|
|
||||||
g_rxtx_need_restart = false;
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "task tcp_conn.");
|
|
||||||
|
|
||||||
/*wating for connecting to AP*/
|
|
||||||
xEventGroupWaitBits(tcp_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "sta has connected to ap.");
|
|
||||||
|
|
||||||
int socket_ret = ESP_FAIL;
|
|
||||||
|
|
||||||
TaskHandle_t tx_rx_task = NULL;
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_TCP_MODE_SERVER
|
|
||||||
if (socket_ret == ESP_FAIL) {
|
|
||||||
/*create tcp socket*/
|
|
||||||
ESP_LOGI(TAG, "tcp_server will start after 3s...");
|
|
||||||
vTaskDelay(3000 / portTICK_RATE_MS);
|
|
||||||
ESP_LOGI(TAG, "create_tcp_server.");
|
|
||||||
socket_ret = create_tcp_server();
|
|
||||||
}
|
|
||||||
#else /*EXAMPLE_ESP_TCP_MODE_SERVER*/
|
|
||||||
if (socket_ret == ESP_FAIL) {
|
|
||||||
ESP_LOGI(TAG, "tcp_client will start after 20s...");
|
|
||||||
vTaskDelay(20000 / portTICK_RATE_MS);
|
|
||||||
ESP_LOGI(TAG, "create_tcp_client.");
|
|
||||||
socket_ret = create_tcp_client();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (socket_ret == ESP_FAIL) {
|
|
||||||
ESP_LOGI(TAG, "create tcp socket error,stop.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*create a task to tx/rx data*/
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX
|
|
||||||
if (tx_rx_task == NULL) {
|
|
||||||
if (pdPASS != xTaskCreate(&send_data, "send_data", 4096, NULL, 4, &tx_rx_task)) {
|
|
||||||
ESP_LOGE(TAG, "Send task create fail!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else /*EXAMPLE_ESP_TCP_PERF_TX*/
|
|
||||||
if (tx_rx_task == NULL) {
|
|
||||||
if (pdPASS != xTaskCreate(&recv_data, "recv_data", 4096, NULL, 4, &tx_rx_task)) {
|
|
||||||
ESP_LOGE(TAG, "Recv task create fail!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
double bps;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
g_total_data = 0;
|
|
||||||
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
|
|
||||||
bps = (g_total_data * 8.0 / 3.0) / 1000000.0;
|
|
||||||
|
|
||||||
if (g_rxtx_need_restart) {
|
|
||||||
printf("send or receive task encoutner error, need to restart\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX
|
|
||||||
ESP_LOGI(TAG, "tcp send %.2f Mbits per sec!\n", bps);
|
|
||||||
#if EXAMPLE_ESP_TCP_DELAY_INFO
|
|
||||||
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",
|
|
||||||
g_total_pack, g_send_success, g_send_fail, g_delay_classify[0],
|
|
||||||
g_delay_classify[1], g_delay_classify[2], g_delay_classify[3], g_delay_classify[4]);
|
|
||||||
#endif /*EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
||||||
#else
|
|
||||||
ESP_LOGI(TAG, "tcp recv %.2f Mbits per sec!\n", bps);
|
|
||||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX*/
|
|
||||||
}
|
|
||||||
|
|
||||||
close_socket();
|
|
||||||
}
|
|
||||||
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void app_main(void)
|
|
||||||
{
|
|
||||||
esp_err_t ret = nvs_flash_init();
|
|
||||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
|
|
||||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
||||||
ret = nvs_flash_init();
|
|
||||||
}
|
|
||||||
ESP_ERROR_CHECK( ret );
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_WIFI_MODE_AP
|
|
||||||
ESP_LOGI(TAG, "EXAMPLE_ESP_WIFI_MODE_AP");
|
|
||||||
wifi_init_softap();
|
|
||||||
#else
|
|
||||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
|
||||||
wifi_init_sta();
|
|
||||||
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/
|
|
||||||
xTaskCreate(&tcp_conn, "tcp_conn", 4096, NULL, 5, NULL);
|
|
||||||
}
|
|
@ -1,349 +0,0 @@
|
|||||||
/* tcp_perf Example
|
|
||||||
|
|
||||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, this
|
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/socket.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_log.h"
|
|
||||||
|
|
||||||
|
|
||||||
#include "tcp_perf.h"
|
|
||||||
|
|
||||||
|
|
||||||
/* FreeRTOS event group to signal when we are connected to wifi */
|
|
||||||
EventGroupHandle_t tcp_event_group;
|
|
||||||
|
|
||||||
/*socket*/
|
|
||||||
static int server_socket = 0;
|
|
||||||
static struct sockaddr_in server_addr;
|
|
||||||
static struct sockaddr_in client_addr;
|
|
||||||
static unsigned int socklen = sizeof(client_addr);
|
|
||||||
static int connect_socket = 0;
|
|
||||||
bool g_rxtx_need_restart = false;
|
|
||||||
|
|
||||||
int g_total_data = 0;
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
|
||||||
|
|
||||||
int g_total_pack = 0;
|
|
||||||
int g_send_success = 0;
|
|
||||||
int g_send_fail = 0;
|
|
||||||
int g_delay_classify[5] = { 0 };
|
|
||||||
|
|
||||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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();
|
|
||||||
xEventGroupClearBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_CONNECTED:
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
|
||||||
ESP_LOGI(TAG, "got ip:%s\n",
|
|
||||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
|
||||||
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
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);
|
|
||||||
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
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);
|
|
||||||
xEventGroupClearBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
//send data
|
|
||||||
void send_data(void *pvParameters)
|
|
||||||
{
|
|
||||||
int len = 0;
|
|
||||||
char *databuff = (char *)malloc(EXAMPLE_DEFAULT_PKTSIZE * sizeof(char));
|
|
||||||
memset(databuff, EXAMPLE_PACK_BYTE_IS, EXAMPLE_DEFAULT_PKTSIZE);
|
|
||||||
vTaskDelay(100 / portTICK_RATE_MS);
|
|
||||||
ESP_LOGI(TAG, "start sending...");
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
|
||||||
//delaytime
|
|
||||||
struct timeval tv_start;
|
|
||||||
struct timeval tv_finish;
|
|
||||||
unsigned long send_delay_ms;
|
|
||||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
||||||
while (1) {
|
|
||||||
int to_write = EXAMPLE_DEFAULT_PKTSIZE;
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
|
||||||
g_total_pack++;
|
|
||||||
gettimeofday(&tv_start, NULL);
|
|
||||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
||||||
|
|
||||||
//send function
|
|
||||||
while (to_write > 0) {
|
|
||||||
len = send(connect_socket, databuff + (EXAMPLE_DEFAULT_PKTSIZE - to_write), to_write, 0);
|
|
||||||
if (len > 0) {
|
|
||||||
g_total_data += len;
|
|
||||||
to_write -= len;
|
|
||||||
} else {
|
|
||||||
int err = get_socket_error_code(connect_socket);
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
|
||||||
g_send_fail++;
|
|
||||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
||||||
|
|
||||||
if (err != ENOMEM) {
|
|
||||||
show_socket_error_reason("send_data", connect_socket);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
|
||||||
gettimeofday(&tv_finish, NULL);
|
|
||||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
||||||
if (g_total_data > 0) {
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
|
||||||
g_send_success++;
|
|
||||||
send_delay_ms = (tv_finish.tv_sec - tv_start.tv_sec) * 1000
|
|
||||||
+ (tv_finish.tv_usec - tv_start.tv_usec) / 1000;
|
|
||||||
if (send_delay_ms < 30) {
|
|
||||||
g_delay_classify[0]++;
|
|
||||||
} else if (send_delay_ms < 100) {
|
|
||||||
g_delay_classify[1]++;
|
|
||||||
} else if (send_delay_ms < 300) {
|
|
||||||
g_delay_classify[2]++;
|
|
||||||
} else if (send_delay_ms < 1000) {
|
|
||||||
g_delay_classify[3]++;
|
|
||||||
} else {
|
|
||||||
g_delay_classify[4]++;
|
|
||||||
}
|
|
||||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g_rxtx_need_restart = true;
|
|
||||||
free(databuff);
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
//receive data
|
|
||||||
void recv_data(void *pvParameters)
|
|
||||||
{
|
|
||||||
int len = 0;
|
|
||||||
char *databuff = (char *)malloc(EXAMPLE_DEFAULT_PKTSIZE * sizeof(char));
|
|
||||||
while (1) {
|
|
||||||
int to_recv = EXAMPLE_DEFAULT_PKTSIZE;
|
|
||||||
while (to_recv > 0) {
|
|
||||||
len = recv(connect_socket, databuff + (EXAMPLE_DEFAULT_PKTSIZE - to_recv), to_recv, 0);
|
|
||||||
if (len > 0) {
|
|
||||||
g_total_data += len;
|
|
||||||
to_recv -= len;
|
|
||||||
} else {
|
|
||||||
show_socket_error_reason("recv_data", connect_socket);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (g_total_data > 0) {
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
g_rxtx_need_restart = true;
|
|
||||||
free(databuff);
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//use this esp32 as a tcp server. return ESP_OK:success ESP_FAIL:error
|
|
||||||
esp_err_t create_tcp_server()
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "server socket....port=%d\n", EXAMPLE_DEFAULT_PORT);
|
|
||||||
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (server_socket < 0) {
|
|
||||||
show_socket_error_reason("create_server", server_socket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_port = htons(EXAMPLE_DEFAULT_PORT);
|
|
||||||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
||||||
show_socket_error_reason("bind_server", server_socket);
|
|
||||||
close(server_socket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
if (listen(server_socket, 5) < 0) {
|
|
||||||
show_socket_error_reason("listen_server", server_socket);
|
|
||||||
close(server_socket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
connect_socket = accept(server_socket, (struct sockaddr *)&client_addr, &socklen);
|
|
||||||
if (connect_socket < 0) {
|
|
||||||
show_socket_error_reason("accept_server", connect_socket);
|
|
||||||
close(server_socket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
/*connection established,now can send/recv*/
|
|
||||||
ESP_LOGI(TAG, "tcp connection established!");
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
//use this esp32 as a tcp client. return ESP_OK:success ESP_FAIL:error
|
|
||||||
esp_err_t create_tcp_client()
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "client socket....serverip:port=%s:%d\n",
|
|
||||||
EXAMPLE_DEFAULT_SERVER_IP, EXAMPLE_DEFAULT_PORT);
|
|
||||||
connect_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (connect_socket < 0) {
|
|
||||||
show_socket_error_reason("create client", connect_socket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_port = htons(EXAMPLE_DEFAULT_PORT);
|
|
||||||
server_addr.sin_addr.s_addr = inet_addr(EXAMPLE_DEFAULT_SERVER_IP);
|
|
||||||
ESP_LOGI(TAG, "connecting to server...");
|
|
||||||
if (connect(connect_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
||||||
show_socket_error_reason("client connect", connect_socket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
ESP_LOGI(TAG, "connect to server success!");
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
//wifi_init_sta
|
|
||||||
void wifi_init_sta()
|
|
||||||
{
|
|
||||||
tcp_event_group = xEventGroupCreate();
|
|
||||||
|
|
||||||
tcpip_adapter_init();
|
|
||||||
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 = EXAMPLE_DEFAULT_SSID,
|
|
||||||
.password = EXAMPLE_DEFAULT_PWD
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start() );
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
|
||||||
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",
|
|
||||||
EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD);
|
|
||||||
}
|
|
||||||
//wifi_init_softap
|
|
||||||
void wifi_init_softap()
|
|
||||||
{
|
|
||||||
tcp_event_group = xEventGroupCreate();
|
|
||||||
|
|
||||||
tcpip_adapter_init();
|
|
||||||
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 = EXAMPLE_DEFAULT_SSID,
|
|
||||||
.ssid_len = 0,
|
|
||||||
.max_connection = EXAMPLE_MAX_STA_CONN,
|
|
||||||
.password = EXAMPLE_DEFAULT_PWD,
|
|
||||||
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
|
||||||
},
|
|
||||||
};
|
|
||||||
if (strlen(EXAMPLE_DEFAULT_PWD) == 0) {
|
|
||||||
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",
|
|
||||||
EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int get_socket_error_code(int socket)
|
|
||||||
{
|
|
||||||
int result;
|
|
||||||
u32_t optlen = sizeof(int);
|
|
||||||
int err = getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen);
|
|
||||||
if (err == -1) {
|
|
||||||
ESP_LOGE(TAG, "getsockopt failed:%s", strerror(err));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int show_socket_error_reason(const char *str, int socket)
|
|
||||||
{
|
|
||||||
int err = get_socket_error_code(socket);
|
|
||||||
|
|
||||||
if (err != 0) {
|
|
||||||
ESP_LOGW(TAG, "%s socket error %d %s", str, err, strerror(err));
|
|
||||||
}
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
int check_working_socket()
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
#if EXAMPLE_ESP_TCP_MODE_SERVER
|
|
||||||
ESP_LOGD(TAG, "check server_socket");
|
|
||||||
ret = get_socket_error_code(server_socket);
|
|
||||||
if (ret != 0) {
|
|
||||||
ESP_LOGW(TAG, "server socket error %d %s", ret, strerror(ret));
|
|
||||||
}
|
|
||||||
if (ret == ECONNRESET) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
ESP_LOGD(TAG, "check connect_socket");
|
|
||||||
ret = get_socket_error_code(connect_socket);
|
|
||||||
if (ret != 0) {
|
|
||||||
ESP_LOGW(TAG, "connect socket error %d %s", ret, strerror(ret));
|
|
||||||
}
|
|
||||||
if (ret != 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void close_socket()
|
|
||||||
{
|
|
||||||
close(connect_socket);
|
|
||||||
close(server_socket);
|
|
||||||
}
|
|
||||||
|
|
@ -1,94 +0,0 @@
|
|||||||
/* tcp_perf Example
|
|
||||||
|
|
||||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, this
|
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef __TCP_PERF_H__
|
|
||||||
#define __TCP_PERF_H__
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*test options*/
|
|
||||||
#define EXAMPLE_ESP_WIFI_MODE_AP CONFIG_TCP_PERF_WIFI_MODE_AP //TRUE:AP FALSE:STA
|
|
||||||
#define EXAMPLE_ESP_TCP_MODE_SERVER CONFIG_TCP_PERF_SERVER //TRUE:server FALSE:client
|
|
||||||
#define EXAMPLE_ESP_TCP_PERF_TX CONFIG_TCP_PERF_TX //TRUE:send FALSE:receive
|
|
||||||
#define EXAMPLE_ESP_TCP_DELAY_INFO CONFIG_TCP_PERF_DELAY_DEBUG //TRUE:show delay time info
|
|
||||||
|
|
||||||
/*AP info and tcp_server info*/
|
|
||||||
#define EXAMPLE_DEFAULT_SSID CONFIG_TCP_PERF_WIFI_SSID
|
|
||||||
#define EXAMPLE_DEFAULT_PWD CONFIG_TCP_PERF_WIFI_PASSWORD
|
|
||||||
#define EXAMPLE_DEFAULT_PORT CONFIG_TCP_PERF_SERVER_PORT
|
|
||||||
#define EXAMPLE_DEFAULT_PKTSIZE CONFIG_TCP_PERF_PKT_SIZE
|
|
||||||
#define EXAMPLE_MAX_STA_CONN 1 //how many sta can be connected(AP mode)
|
|
||||||
|
|
||||||
#ifdef CONFIG_TCP_PERF_SERVER_IP
|
|
||||||
#define EXAMPLE_DEFAULT_SERVER_IP CONFIG_TCP_PERF_SERVER_IP
|
|
||||||
#else
|
|
||||||
#define EXAMPLE_DEFAULT_SERVER_IP "192.168.4.1"
|
|
||||||
#endif /*CONFIG_TCP_PERF_SERVER_IP*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define EXAMPLE_PACK_BYTE_IS 97 //'a'
|
|
||||||
#define TAG "tcp_perf:"
|
|
||||||
|
|
||||||
/* FreeRTOS event group to signal when we are connected to wifi*/
|
|
||||||
extern EventGroupHandle_t tcp_event_group;
|
|
||||||
#define WIFI_CONNECTED_BIT BIT0
|
|
||||||
|
|
||||||
extern int g_total_data;
|
|
||||||
extern bool g_rxtx_need_restart;
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
|
||||||
extern int g_total_pack;
|
|
||||||
extern int g_send_success;
|
|
||||||
extern int g_send_fail;
|
|
||||||
extern int g_delay_classify[5];
|
|
||||||
#endif/*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
||||||
|
|
||||||
|
|
||||||
//using esp as station
|
|
||||||
void wifi_init_sta();
|
|
||||||
//using esp as softap
|
|
||||||
void wifi_init_softap();
|
|
||||||
|
|
||||||
//create a tcp server socket. return ESP_OK:success ESP_FAIL:error
|
|
||||||
esp_err_t create_tcp_server();
|
|
||||||
//create a tcp client socket. return ESP_OK:success ESP_FAIL:error
|
|
||||||
esp_err_t create_tcp_client();
|
|
||||||
|
|
||||||
//send data task
|
|
||||||
void send_data(void *pvParameters);
|
|
||||||
//receive data task
|
|
||||||
void recv_data(void *pvParameters);
|
|
||||||
|
|
||||||
//close all socket
|
|
||||||
void close_socket();
|
|
||||||
|
|
||||||
//get socket error code. return: error code
|
|
||||||
int get_socket_error_code(int socket);
|
|
||||||
|
|
||||||
//show socket error code. return: error code
|
|
||||||
int show_socket_error_reason(const char* str, int socket);
|
|
||||||
|
|
||||||
//check working socket
|
|
||||||
int check_working_socket();
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#endif /*#ifndef __TCP_PERF_H__*/
|
|
||||||
|
|
@ -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_perf
|
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
|
||||||
|
|
@ -1,112 +0,0 @@
|
|||||||
menu "Example Configuration"
|
|
||||||
|
|
||||||
#choice
|
|
||||||
# prompt "UDP_PERF_MODE "
|
|
||||||
# default MODE_UDP_SHIELDBOX
|
|
||||||
# help
|
|
||||||
# This option set performance mode.
|
|
||||||
#
|
|
||||||
# - Testing in shieldbox for "Performance in shieldbox" setting.
|
|
||||||
#
|
|
||||||
# - Testing in air for "Performance in air" setting.
|
|
||||||
#
|
|
||||||
# - Testing in long distance for "Performance in long distance" setting.
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#config MODE_UDP_SHIELDBOX
|
|
||||||
# bool "Performance in shieldbox"
|
|
||||||
#config MODE_UDP_AIR
|
|
||||||
# bool "Performance in air"
|
|
||||||
#config MODE_UDP_LONG_DISTANCE
|
|
||||||
# bool "Performance in long distance"
|
|
||||||
#endchoice
|
|
||||||
#
|
|
||||||
|
|
||||||
choice UDP_PERF_WIFI_MODE
|
|
||||||
prompt "AP or STA"
|
|
||||||
default UDP_PERF_ESP_IS_STATION
|
|
||||||
help
|
|
||||||
Whether the esp32 is softAP or station.
|
|
||||||
|
|
||||||
config UDP_PERF_ESP_IS_SOFTAP
|
|
||||||
bool "SoftAP"
|
|
||||||
config UDP_PERF_ESP_IS_STATION
|
|
||||||
bool "Station"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
config UDP_PERF_WIFI_MODE_AP
|
|
||||||
bool
|
|
||||||
default y if UDP_PERF_ESP_IS_SOFTAP
|
|
||||||
default n if UDP_PERF_ESP_IS_STATION
|
|
||||||
|
|
||||||
choice UDP_PERF_SERVER_CLIENT
|
|
||||||
prompt "server or client"
|
|
||||||
default UDP_PERF_ESP_IS_CLIENT
|
|
||||||
help
|
|
||||||
Whether the esp32 is tcp server or client.
|
|
||||||
|
|
||||||
We suggest to choose "client" if you choose "station" in "wifi mode".
|
|
||||||
|
|
||||||
config UDP_PERF_ESP_IS_SERVER
|
|
||||||
bool "server"
|
|
||||||
config UDP_PERF_ESP_IS_CLIENT
|
|
||||||
bool "client"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
config UDP_PERF_SERVER
|
|
||||||
bool
|
|
||||||
default y if UDP_PERF_ESP_IS_SERVER
|
|
||||||
default n if UDP_PERF_ESP_IS_CLIENT
|
|
||||||
|
|
||||||
choice UDP_PERF_TX_RX
|
|
||||||
prompt "send or receive"
|
|
||||||
default UDP_PERF_ESP_RECV
|
|
||||||
help
|
|
||||||
Whether the esp32 will send or receive.
|
|
||||||
|
|
||||||
config UDP_PERF_ESP_SEND
|
|
||||||
bool "send"
|
|
||||||
config UDP_PERF_ESP_RECV
|
|
||||||
bool "receive"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
config UDP_PERF_TX
|
|
||||||
bool
|
|
||||||
default y if UDP_PERF_ESP_SEND
|
|
||||||
default n if UDP_PERF_ESP_RECV
|
|
||||||
|
|
||||||
|
|
||||||
config UDP_PERF_WIFI_SSID
|
|
||||||
string "WiFi SSID"
|
|
||||||
default "esp_wifi_test1"
|
|
||||||
help
|
|
||||||
SSID (network name) for the example to connect to.
|
|
||||||
|
|
||||||
config UDP_PERF_WIFI_PASSWORD
|
|
||||||
string "WiFi Password"
|
|
||||||
default "1234567890"
|
|
||||||
help
|
|
||||||
WiFi password (WPA or WPA2) for the example to use.
|
|
||||||
|
|
||||||
config UDP_PERF_SERVER_PORT
|
|
||||||
int "UDP server port"
|
|
||||||
default 4567
|
|
||||||
help
|
|
||||||
Which will the udp server use.
|
|
||||||
|
|
||||||
config UDP_PERF_SERVER_IP
|
|
||||||
string "UDP server ip"
|
|
||||||
depends on UDP_PERF_ESP_IS_CLIENT
|
|
||||||
default "192.168.4.1"
|
|
||||||
help
|
|
||||||
IP of UDP server.
|
|
||||||
|
|
||||||
Ignore in UDP server.
|
|
||||||
|
|
||||||
config UDP_PERF_PKT_SIZE
|
|
||||||
int "Size of UDP packet"
|
|
||||||
default 1460
|
|
||||||
help
|
|
||||||
the data send&recv packet size.
|
|
||||||
|
|
||||||
endmenu
|
|
@ -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.
|
|
||||||
#
|
|
@ -1,121 +0,0 @@
|
|||||||
/* udp_perf Example
|
|
||||||
|
|
||||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, this
|
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
udp_perf example
|
|
||||||
|
|
||||||
Using this example to test udp throughput performance.
|
|
||||||
esp<->esp or esp<->ap
|
|
||||||
|
|
||||||
step1:
|
|
||||||
init wifi as AP/STA using config SSID/PASSWORD.
|
|
||||||
|
|
||||||
step2:
|
|
||||||
create a udp server/client socket using config PORT/(IP).
|
|
||||||
if server: wating for the first message of client.
|
|
||||||
if client: sending a packet to server first.
|
|
||||||
|
|
||||||
step3:
|
|
||||||
send/receive data to/from each other.
|
|
||||||
you can see the info in serial output.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
#include "freertos/FreeRTOS.h"
|
|
||||||
#include "freertos/task.h"
|
|
||||||
#include "freertos/event_groups.h"
|
|
||||||
#include "esp_log.h"
|
|
||||||
#include "esp_err.h"
|
|
||||||
#include "nvs_flash.h"
|
|
||||||
|
|
||||||
#include "udp_perf.h"
|
|
||||||
|
|
||||||
|
|
||||||
//this task establish a UDP connection and receive data from UDP
|
|
||||||
static void udp_conn(void *pvParameters)
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "task udp_conn start.");
|
|
||||||
/*wating for connecting to AP*/
|
|
||||||
xEventGroupWaitBits(udp_event_group, WIFI_CONNECTED_BIT,false, true, portMAX_DELAY);
|
|
||||||
ESP_LOGI(TAG, "sta has connected to ap.");
|
|
||||||
|
|
||||||
/*create udp socket*/
|
|
||||||
int socket_ret;
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_UDP_MODE_SERVER
|
|
||||||
ESP_LOGI(TAG, "create udp server after 3s...");
|
|
||||||
vTaskDelay(3000 / portTICK_RATE_MS);
|
|
||||||
ESP_LOGI(TAG, "create_udp_server.");
|
|
||||||
socket_ret=create_udp_server();
|
|
||||||
#else /*EXAMPLE_ESP_UDP_MODE_SERVER*/
|
|
||||||
ESP_LOGI(TAG, "create udp client after 20s...");
|
|
||||||
vTaskDelay(20000 / portTICK_RATE_MS);
|
|
||||||
ESP_LOGI(TAG, "create_udp_client.");
|
|
||||||
socket_ret = create_udp_client();
|
|
||||||
#endif
|
|
||||||
if(socket_ret == ESP_FAIL) {
|
|
||||||
ESP_LOGI(TAG, "create udp socket error,stop.");
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*create a task to tx/rx data*/
|
|
||||||
TaskHandle_t tx_rx_task;
|
|
||||||
xTaskCreate(&send_recv_data, "send_recv_data", 4096, NULL, 4, &tx_rx_task);
|
|
||||||
|
|
||||||
/*waiting udp connected success*/
|
|
||||||
xEventGroupWaitBits(udp_event_group, UDP_CONNCETED_SUCCESS,false, true, portMAX_DELAY);
|
|
||||||
int bps;
|
|
||||||
while (1) {
|
|
||||||
total_data = 0;
|
|
||||||
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
|
|
||||||
bps = total_data / 3;
|
|
||||||
|
|
||||||
if (total_data <= 0) {
|
|
||||||
int err_ret = check_connected_socket();
|
|
||||||
if (err_ret == -1) { //-1 reason: low level netif error
|
|
||||||
ESP_LOGW(TAG, "udp send & recv stop.\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_UDP_PERF_TX
|
|
||||||
ESP_LOGI(TAG, "udp send %d byte per sec! total pack: %d \n", bps, success_pack);
|
|
||||||
#else
|
|
||||||
ESP_LOGI(TAG, "udp recv %d byte per sec! total pack: %d \n", bps, success_pack);
|
|
||||||
#endif /*EXAMPLE_ESP_UDP_PERF_TX*/
|
|
||||||
}
|
|
||||||
close_socket();
|
|
||||||
vTaskDelete(tx_rx_task);
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void app_main(void)
|
|
||||||
{
|
|
||||||
esp_err_t ret = nvs_flash_init();
|
|
||||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
|
|
||||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
||||||
ret = nvs_flash_init();
|
|
||||||
}
|
|
||||||
ESP_ERROR_CHECK( ret );
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_WIFI_MODE_AP
|
|
||||||
ESP_LOGI(TAG, "EXAMPLE_ESP_WIFI_MODE_AP");
|
|
||||||
wifi_init_softap();
|
|
||||||
#else /*EXAMPLE_ESP_WIFI_MODE_AP*/
|
|
||||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
|
||||||
wifi_init_sta();
|
|
||||||
#endif
|
|
||||||
xTaskCreate(&udp_conn, "udp_conn", 4096, NULL, 5, NULL);
|
|
||||||
}
|
|
@ -1,253 +0,0 @@
|
|||||||
/* udp_perf Example
|
|
||||||
|
|
||||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, this
|
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/socket.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_log.h"
|
|
||||||
|
|
||||||
|
|
||||||
#include "udp_perf.h"
|
|
||||||
|
|
||||||
|
|
||||||
/* FreeRTOS event group to signal when we are connected to WiFi and ready to start UDP test*/
|
|
||||||
EventGroupHandle_t udp_event_group;
|
|
||||||
|
|
||||||
|
|
||||||
static int mysocket;
|
|
||||||
|
|
||||||
static struct sockaddr_in remote_addr;
|
|
||||||
static unsigned int socklen;
|
|
||||||
|
|
||||||
int total_data = 0;
|
|
||||||
int success_pack = 0;
|
|
||||||
|
|
||||||
|
|
||||||
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();
|
|
||||||
xEventGroupClearBits(udp_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_CONNECTED:
|
|
||||||
break;
|
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
|
||||||
ESP_LOGI(TAG, "event_handler:SYSTEM_EVENT_STA_GOT_IP!");
|
|
||||||
ESP_LOGI(TAG, "got ip:%s\n",
|
|
||||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
|
||||||
xEventGroupSetBits(udp_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
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);
|
|
||||||
xEventGroupSetBits(udp_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
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);
|
|
||||||
xEventGroupClearBits(udp_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//wifi_init_sta
|
|
||||||
void wifi_init_sta()
|
|
||||||
{
|
|
||||||
udp_event_group = xEventGroupCreate();
|
|
||||||
|
|
||||||
tcpip_adapter_init();
|
|
||||||
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 = EXAMPLE_DEFAULT_SSID,
|
|
||||||
.password = EXAMPLE_DEFAULT_PWD
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start() );
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
|
||||||
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",
|
|
||||||
EXAMPLE_DEFAULT_SSID,EXAMPLE_DEFAULT_PWD);
|
|
||||||
}
|
|
||||||
//wifi_init_softap
|
|
||||||
void wifi_init_softap()
|
|
||||||
{
|
|
||||||
udp_event_group = xEventGroupCreate();
|
|
||||||
|
|
||||||
tcpip_adapter_init();
|
|
||||||
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 = EXAMPLE_DEFAULT_SSID,
|
|
||||||
.ssid_len=0,
|
|
||||||
.max_connection=EXAMPLE_MAX_STA_CONN,
|
|
||||||
.password = EXAMPLE_DEFAULT_PWD,
|
|
||||||
.authmode=WIFI_AUTH_WPA_WPA2_PSK
|
|
||||||
},
|
|
||||||
};
|
|
||||||
if (strlen(EXAMPLE_DEFAULT_PWD) ==0) {
|
|
||||||
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",
|
|
||||||
EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD);
|
|
||||||
}
|
|
||||||
|
|
||||||
//create a udp server socket. return ESP_OK:success ESP_FAIL:error
|
|
||||||
esp_err_t create_udp_server()
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "create_udp_server() port:%d", EXAMPLE_DEFAULT_PORT);
|
|
||||||
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
if (mysocket < 0) {
|
|
||||||
show_socket_error_reason(mysocket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
struct sockaddr_in server_addr;
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_port = htons(EXAMPLE_DEFAULT_PORT);
|
|
||||||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
if (bind(mysocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
||||||
show_socket_error_reason(mysocket);
|
|
||||||
close(mysocket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
//create a udp client socket. return ESP_OK:success ESP_FAIL:error
|
|
||||||
esp_err_t create_udp_client()
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "create_udp_client()");
|
|
||||||
ESP_LOGI(TAG, "connecting to %s:%d",
|
|
||||||
EXAMPLE_DEFAULT_SERVER_IP, EXAMPLE_DEFAULT_PORT);
|
|
||||||
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
if (mysocket < 0) {
|
|
||||||
show_socket_error_reason(mysocket);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
/*for client remote_addr is also server_addr*/
|
|
||||||
remote_addr.sin_family = AF_INET;
|
|
||||||
remote_addr.sin_port = htons(EXAMPLE_DEFAULT_PORT);
|
|
||||||
remote_addr.sin_addr.s_addr = inet_addr(EXAMPLE_DEFAULT_SERVER_IP);
|
|
||||||
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//send or recv data task
|
|
||||||
void send_recv_data(void *pvParameters)
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "task send_recv_data start!\n");
|
|
||||||
|
|
||||||
int len;
|
|
||||||
char databuff[EXAMPLE_DEFAULT_PKTSIZE];
|
|
||||||
|
|
||||||
/*send&receive first packet*/
|
|
||||||
socklen = sizeof(remote_addr);
|
|
||||||
memset(databuff, EXAMPLE_PACK_BYTE_IS, EXAMPLE_DEFAULT_PKTSIZE);
|
|
||||||
#if EXAMPLE_ESP_UDP_MODE_SERVER
|
|
||||||
ESP_LOGI(TAG, "first recvfrom:");
|
|
||||||
len = recvfrom(mysocket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen);
|
|
||||||
#else
|
|
||||||
ESP_LOGI(TAG, "first sendto:");
|
|
||||||
len = sendto(mysocket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (len > 0) {
|
|
||||||
ESP_LOGI(TAG, "transfer data with %s:%u\n",
|
|
||||||
inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));
|
|
||||||
xEventGroupSetBits(udp_event_group, UDP_CONNCETED_SUCCESS);
|
|
||||||
} else {
|
|
||||||
show_socket_error_reason(mysocket);
|
|
||||||
close(mysocket);
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
} /*if (len > 0)*/
|
|
||||||
|
|
||||||
#if EXAMPLE_ESP_UDP_PERF_TX
|
|
||||||
vTaskDelay(500 / portTICK_RATE_MS);
|
|
||||||
#endif
|
|
||||||
ESP_LOGI(TAG, "start count!\n");
|
|
||||||
while(1) {
|
|
||||||
#if EXAMPLE_ESP_UDP_PERF_TX
|
|
||||||
len = sendto(mysocket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
|
|
||||||
#else
|
|
||||||
len = recvfrom(mysocket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen);
|
|
||||||
#endif
|
|
||||||
if (len > 0) {
|
|
||||||
total_data += len;
|
|
||||||
success_pack++;
|
|
||||||
} else {
|
|
||||||
if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) {
|
|
||||||
show_socket_error_reason(mysocket);
|
|
||||||
}
|
|
||||||
} /*if (len > 0)*/
|
|
||||||
} /*while(1)*/
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int get_socket_error_code(int socket)
|
|
||||||
{
|
|
||||||
int result;
|
|
||||||
u32_t optlen = sizeof(int);
|
|
||||||
if(getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen) == -1) {
|
|
||||||
ESP_LOGE(TAG, "getsockopt failed");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int show_socket_error_reason(int socket)
|
|
||||||
{
|
|
||||||
int err = get_socket_error_code(socket);
|
|
||||||
ESP_LOGW(TAG, "socket error %d %s", err, strerror(err));
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
int check_connected_socket()
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
ESP_LOGD(TAG, "check connect_socket");
|
|
||||||
ret = get_socket_error_code(mysocket);
|
|
||||||
if(ret != 0) {
|
|
||||||
ESP_LOGW(TAG, "socket error %d %s", ret, strerror(ret));
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void close_socket()
|
|
||||||
{
|
|
||||||
close(mysocket);
|
|
||||||
}
|
|
@ -1,86 +0,0 @@
|
|||||||
/* udp_perf Example
|
|
||||||
|
|
||||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, this
|
|
||||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef __UDP_PERF_H__
|
|
||||||
#define __UDP_PERF_H__
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*test options*/
|
|
||||||
#define EXAMPLE_ESP_WIFI_MODE_AP CONFIG_UDP_PERF_WIFI_MODE_AP //TRUE:AP FALSE:STA
|
|
||||||
#define EXAMPLE_ESP_UDP_MODE_SERVER CONFIG_UDP_PERF_SERVER //TRUE:server FALSE:client
|
|
||||||
#define EXAMPLE_ESP_UDP_PERF_TX CONFIG_UDP_PERF_TX //TRUE:send FALSE:receive
|
|
||||||
#define EXAMPLE_PACK_BYTE_IS 97 //'a'
|
|
||||||
/*AP info and tcp_server info*/
|
|
||||||
#define EXAMPLE_DEFAULT_SSID CONFIG_UDP_PERF_WIFI_SSID
|
|
||||||
#define EXAMPLE_DEFAULT_PWD CONFIG_UDP_PERF_WIFI_PASSWORD
|
|
||||||
#define EXAMPLE_DEFAULT_PORT CONFIG_UDP_PERF_SERVER_PORT
|
|
||||||
#define EXAMPLE_DEFAULT_PKTSIZE CONFIG_UDP_PERF_PKT_SIZE
|
|
||||||
#define EXAMPLE_MAX_STA_CONN 1 //how many sta can be connected(AP mode)
|
|
||||||
|
|
||||||
#ifdef CONFIG_UDP_PERF_SERVER_IP
|
|
||||||
#define EXAMPLE_DEFAULT_SERVER_IP CONFIG_UDP_PERF_SERVER_IP
|
|
||||||
#else
|
|
||||||
#define EXAMPLE_DEFAULT_SERVER_IP "192.168.4.1"
|
|
||||||
#endif /*CONFIG_UDP_PERF_SERVER_IP*/
|
|
||||||
|
|
||||||
|
|
||||||
#define TAG "udp_perf:"
|
|
||||||
|
|
||||||
/* FreeRTOS event group to signal when we are connected to WiFi and ready to start UDP test*/
|
|
||||||
extern EventGroupHandle_t udp_event_group;
|
|
||||||
#define WIFI_CONNECTED_BIT BIT0
|
|
||||||
#define UDP_CONNCETED_SUCCESS BIT1
|
|
||||||
|
|
||||||
extern int total_data;
|
|
||||||
extern int success_pack;
|
|
||||||
|
|
||||||
|
|
||||||
//using esp as station
|
|
||||||
void wifi_init_sta();
|
|
||||||
//using esp as softap
|
|
||||||
void wifi_init_softap();
|
|
||||||
|
|
||||||
//create a udp server socket. return ESP_OK:success ESP_FAIL:error
|
|
||||||
esp_err_t create_udp_server();
|
|
||||||
//create a udp client socket. return ESP_OK:success ESP_FAIL:error
|
|
||||||
esp_err_t create_udp_client();
|
|
||||||
|
|
||||||
//send or recv data task
|
|
||||||
void send_recv_data(void *pvParameters);
|
|
||||||
|
|
||||||
//get socket error code. return: error code
|
|
||||||
int get_socket_error_code(int socket);
|
|
||||||
|
|
||||||
//show socket error code. return: error code
|
|
||||||
int show_socket_error_reason(int socket);
|
|
||||||
|
|
||||||
//check connected socket. return: error code
|
|
||||||
int check_connected_socket();
|
|
||||||
|
|
||||||
//close all socket
|
|
||||||
void close_socket();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#endif /*#ifndef __UDP_PERF_H__*/
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user