2017-04-13 11:14:01 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
tcp_perf example
|
|
|
|
|
|
|
|
Using this example to test tcp throughput performance.
|
|
|
|
esp<->esp or esp<->ap
|
|
|
|
|
|
|
|
step1:
|
2017-03-21 12:06:32 +00:00
|
|
|
init wifi as AP/STA using config SSID/PASSWORD.
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
step2:
|
2017-04-10 12:39:04 +00:00
|
|
|
create a tcp server/client socket using config PORT/(IP).
|
|
|
|
if server: wating for connect.
|
|
|
|
if client connect to server.
|
2017-03-20 05:43:04 +00:00
|
|
|
step3:
|
2017-03-21 12:06:32 +00:00
|
|
|
send/receive data to/from each other.
|
|
|
|
if the tcp connect established. esp will send or receive data.
|
2017-04-10 12:39:04 +00:00
|
|
|
you can see the info in serial output.
|
2017-03-20 05:43:04 +00:00
|
|
|
*/
|
|
|
|
|
2017-03-31 08:34:12 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2017-04-13 11:14:01 +00:00
|
|
|
#include "freertos/event_groups.h"
|
2017-03-31 08:34:12 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
#include "tcp_perf.h"
|
|
|
|
|
|
|
|
//this task establish a TCP connection and receive data from TCP
|
|
|
|
static void tcp_conn(void *pvParameters)
|
|
|
|
{
|
2017-07-21 03:40:30 +00:00
|
|
|
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;
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
#if EXAMPLE_ESP_TCP_MODE_SERVER
|
2017-07-21 03:40:30 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-04-13 11:14:01 +00:00
|
|
|
#else /*EXAMPLE_ESP_TCP_MODE_SERVER*/
|
2017-07-21 03:40:30 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-03-20 05:43:04 +00:00
|
|
|
#endif
|
2017-07-21 03:40:30 +00:00
|
|
|
if (socket_ret == ESP_FAIL) {
|
|
|
|
ESP_LOGI(TAG, "create tcp socket error,stop.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*create a task to tx/rx data*/
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX
|
2017-07-21 03:40:30 +00:00
|
|
|
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!");
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 11:14:01 +00:00
|
|
|
#else /*EXAMPLE_ESP_TCP_PERF_TX*/
|
2017-07-21 03:40:30 +00:00
|
|
|
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!");
|
|
|
|
}
|
|
|
|
}
|
2017-03-20 05:43:04 +00:00
|
|
|
#endif
|
2017-07-21 03:40:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-03-21 12:06:32 +00:00
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX
|
2017-07-21 03:40:30 +00:00
|
|
|
ESP_LOGI(TAG, "tcp send %.2f Mbits per sec!\n", bps);
|
2017-04-13 11:14:01 +00:00
|
|
|
#if EXAMPLE_ESP_TCP_DELAY_INFO
|
2017-07-21 03:40:30 +00:00
|
|
|
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]);
|
2017-04-13 11:14:01 +00:00
|
|
|
#endif /*EXAMPLE_ESP_TCP_DELAY_INFO*/
|
2017-03-20 05:43:04 +00:00
|
|
|
#else
|
2017-07-21 03:40:30 +00:00
|
|
|
ESP_LOGI(TAG, "tcp recv %.2f Mbits per sec!\n", bps);
|
2017-04-13 11:14:01 +00:00
|
|
|
#endif /*EXAMPLE_ESP_TCP_PERF_TX*/
|
2017-07-21 03:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close_socket();
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
2017-07-21 03:40:30 +00:00
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
vTaskDelete(NULL);
|
2017-03-20 05:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
#if EXAMPLE_ESP_WIFI_MODE_AP
|
|
|
|
ESP_LOGI(TAG, "EXAMPLE_ESP_WIFI_MODE_AP");
|
2017-03-20 05:43:04 +00:00
|
|
|
wifi_init_softap();
|
2017-04-13 11:14:01 +00:00
|
|
|
#else
|
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
2017-03-20 05:43:04 +00:00
|
|
|
wifi_init_sta();
|
2017-04-13 11:14:01 +00:00
|
|
|
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/
|
2017-03-21 12:06:32 +00:00
|
|
|
xTaskCreate(&tcp_conn, "tcp_conn", 4096, NULL, 5, NULL);
|
2017-03-20 05:43:04 +00:00
|
|
|
}
|