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-31 08:34:12 +00:00
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/socket.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_wifi.h"
|
|
|
|
|
#include "esp_event_loop.h"
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
|
#include "tcp_perf.h"
|
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
|
|
|
|
|
|
/* FreeRTOS event group to signal when we are connected to wifi */
|
|
|
|
|
EventGroupHandle_t tcp_event_group;
|
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
|
/*socket*/
|
2017-04-10 12:39:04 +00:00
|
|
|
|
static int server_socket = 0;
|
|
|
|
|
static struct sockaddr_in server_addr;
|
2017-03-20 05:43:04 +00:00
|
|
|
|
static struct sockaddr_in client_addr;
|
|
|
|
|
static unsigned int socklen = sizeof(client_addr);
|
2017-04-10 12:39:04 +00:00
|
|
|
|
static int connect_socket = 0;
|
2017-07-21 03:40:30 +00:00
|
|
|
|
bool g_rxtx_need_restart = false;
|
2017-04-10 12:39:04 +00:00
|
|
|
|
|
2017-07-21 03:40:30 +00:00
|
|
|
|
int g_total_data = 0;
|
2017-04-10 12:39:04 +00:00
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
2017-04-10 12:39:04 +00:00
|
|
|
|
|
2017-07-21 03:40:30 +00:00
|
|
|
|
int g_total_pack = 0;
|
|
|
|
|
int g_send_success = 0;
|
|
|
|
|
int g_send_fail = 0;
|
|
|
|
|
int g_delay_classify[5] = { 0 };
|
2017-04-10 12:39:04 +00:00
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
|
|
|
|
|
|
|
|
|
|
2017-04-10 12:39:04 +00:00
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
|
|
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|
|
|
|
{
|
2017-07-21 03:40:30 +00:00
|
|
|
|
switch (event->event_id) {
|
2017-03-20 05:43:04 +00:00
|
|
|
|
case SYSTEM_EVENT_STA_START:
|
|
|
|
|
esp_wifi_connect();
|
|
|
|
|
break;
|
|
|
|
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
|
|
|
|
esp_wifi_connect();
|
2017-04-13 11:14:01 +00:00
|
|
|
|
xEventGroupClearBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
2017-03-20 05:43:04 +00:00
|
|
|
|
break;
|
|
|
|
|
case SYSTEM_EVENT_STA_CONNECTED:
|
|
|
|
|
break;
|
|
|
|
|
case SYSTEM_EVENT_STA_GOT_IP:
|
2017-07-21 03:40:30 +00:00
|
|
|
|
ESP_LOGI(TAG, "got ip:%s\n",
|
|
|
|
|
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
|
|
|
|
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
2017-03-20 05:43:04 +00:00
|
|
|
|
break;
|
|
|
|
|
case SYSTEM_EVENT_AP_STACONNECTED:
|
2017-07-21 03:40:30 +00:00
|
|
|
|
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;
|
2017-03-20 05:43:04 +00:00
|
|
|
|
case SYSTEM_EVENT_AP_STADISCONNECTED:
|
2017-07-21 03:40:30 +00:00
|
|
|
|
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;
|
2017-03-20 05:43:04 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//send data
|
|
|
|
|
void send_data(void *pvParameters)
|
|
|
|
|
{
|
2017-03-21 12:06:32 +00:00
|
|
|
|
int len = 0;
|
2017-07-21 03:40:30 +00:00
|
|
|
|
char *databuff = (char *)malloc(EXAMPLE_DEFAULT_PKTSIZE * sizeof(char));
|
2017-04-13 11:14:01 +00:00
|
|
|
|
memset(databuff, EXAMPLE_PACK_BYTE_IS, EXAMPLE_DEFAULT_PKTSIZE);
|
2017-07-21 03:40:30 +00:00
|
|
|
|
vTaskDelay(100 / portTICK_RATE_MS);
|
2017-03-21 12:06:32 +00:00
|
|
|
|
ESP_LOGI(TAG, "start sending...");
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
2017-03-21 12:06:32 +00:00
|
|
|
|
//delaytime
|
|
|
|
|
struct timeval tv_start;
|
|
|
|
|
struct timeval tv_finish;
|
|
|
|
|
unsigned long send_delay_ms;
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
2017-07-21 03:40:30 +00:00
|
|
|
|
while (1) {
|
|
|
|
|
int to_write = EXAMPLE_DEFAULT_PKTSIZE;
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
2017-07-21 03:40:30 +00:00
|
|
|
|
g_total_pack++;
|
|
|
|
|
gettimeofday(&tv_start, NULL);
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
2017-07-21 03:40:30 +00:00
|
|
|
|
//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);
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
2017-07-21 03:40:30 +00:00
|
|
|
|
g_send_fail++;
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
2017-07-21 03:40:30 +00:00
|
|
|
|
|
|
|
|
|
if (err != ENOMEM) {
|
|
|
|
|
show_socket_error_reason("send_data", connect_socket);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
2017-07-21 03:40:30 +00:00
|
|
|
|
gettimeofday(&tv_finish, NULL);
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
2017-07-21 03:40:30 +00:00
|
|
|
|
if (g_total_data > 0) {
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
2017-07-21 03:40:30 +00:00
|
|
|
|
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]++;
|
|
|
|
|
}
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
2017-07-21 03:40:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_rxtx_need_restart = true;
|
|
|
|
|
free(databuff);
|
|
|
|
|
vTaskDelete(NULL);
|
2017-03-20 05:43:04 +00:00
|
|
|
|
}
|
2017-07-21 03:40:30 +00:00
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
|
//receive data
|
2017-03-20 05:43:04 +00:00
|
|
|
|
void recv_data(void *pvParameters)
|
|
|
|
|
{
|
2017-03-21 12:06:32 +00:00
|
|
|
|
int len = 0;
|
2017-07-21 03:40:30 +00:00
|
|
|
|
char *databuff = (char *)malloc(EXAMPLE_DEFAULT_PKTSIZE * sizeof(char));
|
2017-03-21 12:06:32 +00:00
|
|
|
|
while (1) {
|
2017-07-21 03:40:30 +00:00
|
|
|
|
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;
|
2017-04-13 11:14:01 +00:00
|
|
|
|
}
|
2017-07-21 03:40:30 +00:00
|
|
|
|
}
|
|
|
|
|
if (g_total_data > 0) {
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-03-21 12:06:32 +00:00
|
|
|
|
}
|
2017-07-21 03:40:30 +00:00
|
|
|
|
|
|
|
|
|
g_rxtx_need_restart = true;
|
|
|
|
|
free(databuff);
|
|
|
|
|
vTaskDelete(NULL);
|
2017-03-20 05:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
|
|
2017-04-10 12:39:04 +00:00
|
|
|
|
//use this esp32 as a tcp server. return ESP_OK:success ESP_FAIL:error
|
|
|
|
|
esp_err_t create_tcp_server()
|
2017-03-20 05:43:04 +00:00
|
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
|
ESP_LOGI(TAG, "server socket....port=%d\n", EXAMPLE_DEFAULT_PORT);
|
2017-04-10 12:39:04 +00:00
|
|
|
|
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
if (server_socket < 0) {
|
2017-07-21 03:40:30 +00:00
|
|
|
|
show_socket_error_reason("create_server", server_socket);
|
|
|
|
|
return ESP_FAIL;
|
2017-03-21 12:06:32 +00:00
|
|
|
|
}
|
2017-07-21 03:40:30 +00:00
|
|
|
|
|
2017-04-10 12:39:04 +00:00
|
|
|
|
server_addr.sin_family = AF_INET;
|
2017-04-13 11:14:01 +00:00
|
|
|
|
server_addr.sin_port = htons(EXAMPLE_DEFAULT_PORT);
|
2017-04-10 12:39:04 +00:00
|
|
|
|
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
2017-07-21 03:40:30 +00:00
|
|
|
|
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;
|
2017-03-21 12:06:32 +00:00
|
|
|
|
}
|
2017-04-10 12:39:04 +00:00
|
|
|
|
if (listen(server_socket, 5) < 0) {
|
2017-07-21 03:40:30 +00:00
|
|
|
|
show_socket_error_reason("listen_server", server_socket);
|
|
|
|
|
close(server_socket);
|
|
|
|
|
return ESP_FAIL;
|
2017-03-21 12:06:32 +00:00
|
|
|
|
}
|
2017-07-21 03:40:30 +00:00
|
|
|
|
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;
|
2017-03-21 12:06:32 +00:00
|
|
|
|
}
|
|
|
|
|
/*connection established,now can send/recv*/
|
|
|
|
|
ESP_LOGI(TAG, "tcp connection established!");
|
2017-03-31 08:34:12 +00:00
|
|
|
|
return ESP_OK;
|
2017-03-20 05:43:04 +00:00
|
|
|
|
}
|
2017-03-31 08:34:12 +00:00
|
|
|
|
//use this esp32 as a tcp client. return ESP_OK:success ESP_FAIL:error
|
2017-04-10 12:39:04 +00:00
|
|
|
|
esp_err_t create_tcp_client()
|
2017-03-20 05:43:04 +00:00
|
|
|
|
{
|
2017-07-21 03:40:30 +00:00
|
|
|
|
ESP_LOGI(TAG, "client socket....serverip:port=%s:%d\n",
|
|
|
|
|
EXAMPLE_DEFAULT_SERVER_IP, EXAMPLE_DEFAULT_PORT);
|
2017-04-10 12:39:04 +00:00
|
|
|
|
connect_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
if (connect_socket < 0) {
|
2017-07-21 03:40:30 +00:00
|
|
|
|
show_socket_error_reason("create client", connect_socket);
|
|
|
|
|
return ESP_FAIL;
|
2017-03-21 12:06:32 +00:00
|
|
|
|
}
|
2017-04-10 12:39:04 +00:00
|
|
|
|
server_addr.sin_family = AF_INET;
|
2017-04-13 11:14:01 +00:00
|
|
|
|
server_addr.sin_port = htons(EXAMPLE_DEFAULT_PORT);
|
|
|
|
|
server_addr.sin_addr.s_addr = inet_addr(EXAMPLE_DEFAULT_SERVER_IP);
|
2017-04-10 12:39:04 +00:00
|
|
|
|
ESP_LOGI(TAG, "connecting to server...");
|
|
|
|
|
if (connect(connect_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
2017-07-21 03:40:30 +00:00
|
|
|
|
show_socket_error_reason("client connect", connect_socket);
|
|
|
|
|
return ESP_FAIL;
|
2017-03-21 12:06:32 +00:00
|
|
|
|
}
|
2017-04-10 12:39:04 +00:00
|
|
|
|
ESP_LOGI(TAG, "connect to server success!");
|
2017-03-31 08:34:12 +00:00
|
|
|
|
return ESP_OK;
|
2017-03-20 05:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//wifi_init_sta
|
|
|
|
|
void wifi_init_sta()
|
|
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
|
tcp_event_group = xEventGroupCreate();
|
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
|
tcpip_adapter_init();
|
2017-03-21 12:06:32 +00:00
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
|
.sta = {
|
2017-04-13 11:14:01 +00:00
|
|
|
|
.ssid = EXAMPLE_DEFAULT_SSID,
|
|
|
|
|
.password = EXAMPLE_DEFAULT_PWD
|
2017-03-20 05:43:04 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
|
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() );
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
2017-03-21 12:06:32 +00:00
|
|
|
|
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",
|
2017-07-21 03:40:30 +00:00
|
|
|
|
EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD);
|
2017-03-20 05:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
//wifi_init_softap
|
|
|
|
|
void wifi_init_softap()
|
|
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
|
tcp_event_group = xEventGroupCreate();
|
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
|
tcpip_adapter_init();
|
2017-03-21 12:06:32 +00:00
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
|
.ap = {
|
2017-04-13 11:14:01 +00:00
|
|
|
|
.ssid = EXAMPLE_DEFAULT_SSID,
|
2017-03-23 08:36:32 +00:00
|
|
|
|
.ssid_len = 0,
|
2017-07-21 03:40:30 +00:00
|
|
|
|
.max_connection = EXAMPLE_MAX_STA_CONN,
|
2017-04-13 11:14:01 +00:00
|
|
|
|
.password = EXAMPLE_DEFAULT_PWD,
|
2017-03-23 08:36:32 +00:00
|
|
|
|
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
2017-03-20 05:43:04 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
2017-07-21 03:40:30 +00:00
|
|
|
|
if (strlen(EXAMPLE_DEFAULT_PWD) == 0) {
|
|
|
|
|
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
2017-03-23 08:36:32 +00:00
|
|
|
|
}
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
|
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());
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
|
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",
|
2017-07-21 03:40:30 +00:00
|
|
|
|
EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD);
|
2017-03-20 05:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 08:36:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
|
int get_socket_error_code(int socket)
|
2017-03-23 08:36:32 +00:00
|
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
|
int result;
|
|
|
|
|
u32_t optlen = sizeof(int);
|
2017-07-21 03:40:30 +00:00
|
|
|
|
int err = getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen);
|
|
|
|
|
if (err == -1) {
|
|
|
|
|
ESP_LOGE(TAG, "getsockopt failed:%s", strerror(err));
|
|
|
|
|
return -1;
|
2017-04-10 12:39:04 +00:00
|
|
|
|
}
|
2017-04-13 11:14:01 +00:00
|
|
|
|
return result;
|
2017-03-23 08:36:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 03:40:30 +00:00
|
|
|
|
int show_socket_error_reason(const char *str, int socket)
|
2017-03-23 08:36:32 +00:00
|
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
|
int err = get_socket_error_code(socket);
|
2017-07-21 03:40:30 +00:00
|
|
|
|
|
|
|
|
|
if (err != 0) {
|
|
|
|
|
ESP_LOGW(TAG, "%s socket error %d %s", str, err, strerror(err));
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
|
return err;
|
2017-03-23 08:36:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
|
int check_working_socket()
|
2017-03-23 08:36:32 +00:00
|
|
|
|
{
|
2017-04-10 12:39:04 +00:00
|
|
|
|
int ret;
|
2017-04-13 11:14:01 +00:00
|
|
|
|
#if EXAMPLE_ESP_TCP_MODE_SERVER
|
|
|
|
|
ESP_LOGD(TAG, "check server_socket");
|
|
|
|
|
ret = get_socket_error_code(server_socket);
|
2017-07-21 03:40:30 +00:00
|
|
|
|
if (ret != 0) {
|
|
|
|
|
ESP_LOGW(TAG, "server socket error %d %s", ret, strerror(ret));
|
|
|
|
|
}
|
|
|
|
|
if (ret == ECONNRESET) {
|
|
|
|
|
return ret;
|
2017-04-13 11:14:01 +00:00
|
|
|
|
}
|
2017-03-23 08:36:32 +00:00
|
|
|
|
#endif
|
2017-04-13 11:14:01 +00:00
|
|
|
|
ESP_LOGD(TAG, "check connect_socket");
|
|
|
|
|
ret = get_socket_error_code(connect_socket);
|
2017-07-21 03:40:30 +00:00
|
|
|
|
if (ret != 0) {
|
|
|
|
|
ESP_LOGW(TAG, "connect socket error %d %s", ret, strerror(ret));
|
|
|
|
|
}
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
return ret;
|
2017-04-13 11:14:01 +00:00
|
|
|
|
}
|
2017-04-10 12:39:04 +00:00
|
|
|
|
return 0;
|
2017-03-23 08:36:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
|
void close_socket()
|
|
|
|
|
{
|
2017-04-10 12:39:04 +00:00
|
|
|
|
close(connect_socket);
|
|
|
|
|
close(server_socket);
|
2017-03-20 05:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
|