2017-04-13 11:14:01 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2017-03-21 12:06:32 +00:00
|
|
|
|
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-21 12:06:32 +00:00
|
|
|
#include "udp_perf.h"
|
|
|
|
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
/* FreeRTOS event group to signal when we are connected to WiFi and ready to start UDP test*/
|
|
|
|
EventGroupHandle_t udp_event_group;
|
|
|
|
|
2017-03-31 08:34:12 +00:00
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
static int mysocket;
|
|
|
|
|
|
|
|
static struct sockaddr_in remote_addr;
|
|
|
|
static unsigned int socklen;
|
|
|
|
|
2017-04-10 12:39:04 +00:00
|
|
|
int total_data = 0;
|
|
|
|
int success_pack = 0;
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
|
|
|
|
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();
|
2017-04-13 11:14:01 +00:00
|
|
|
xEventGroupClearBits(udp_event_group, WIFI_CONNECTED_BIT);
|
2017-03-21 12:06:32 +00:00
|
|
|
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));
|
2017-04-13 11:14:01 +00:00
|
|
|
xEventGroupSetBits(udp_event_group, WIFI_CONNECTED_BIT);
|
2017-03-21 12:06:32 +00:00
|
|
|
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);
|
2017-04-13 11:14:01 +00:00
|
|
|
xEventGroupSetBits(udp_event_group, WIFI_CONNECTED_BIT);
|
2017-03-21 12:06:32 +00:00
|
|
|
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);
|
2017-04-13 11:14:01 +00:00
|
|
|
xEventGroupClearBits(udp_event_group, WIFI_CONNECTED_BIT);
|
2017-03-21 12:06:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//wifi_init_sta
|
|
|
|
void wifi_init_sta()
|
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
udp_event_group = xEventGroupCreate();
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
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 = {
|
2017-04-13 11:14:01 +00:00
|
|
|
.ssid = EXAMPLE_DEFAULT_SSID,
|
|
|
|
.password = EXAMPLE_DEFAULT_PWD
|
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() );
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
|
|
|
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",
|
2017-04-13 11:14:01 +00:00
|
|
|
EXAMPLE_DEFAULT_SSID,EXAMPLE_DEFAULT_PWD);
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
|
|
|
//wifi_init_softap
|
|
|
|
void wifi_init_softap()
|
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
udp_event_group = xEventGroupCreate();
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
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 = {
|
2017-04-13 11:14:01 +00:00
|
|
|
.ssid = EXAMPLE_DEFAULT_SSID,
|
2017-03-21 12:06:32 +00:00
|
|
|
.ssid_len=0,
|
2017-04-13 11:14:01 +00:00
|
|
|
.max_connection=EXAMPLE_MAX_STA_CONN,
|
|
|
|
.password = EXAMPLE_DEFAULT_PWD,
|
2017-03-21 12:06:32 +00:00
|
|
|
.authmode=WIFI_AUTH_WPA_WPA2_PSK
|
|
|
|
},
|
|
|
|
};
|
2017-04-13 11:14:01 +00:00
|
|
|
if (strlen(EXAMPLE_DEFAULT_PWD) ==0) {
|
2017-03-23 08:36:32 +00:00
|
|
|
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
|
|
|
}
|
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());
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",
|
2017-04-13 11:14:01 +00:00
|
|
|
EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD);
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 12:39:04 +00:00
|
|
|
//create a udp server socket. return ESP_OK:success ESP_FAIL:error
|
|
|
|
esp_err_t create_udp_server()
|
2017-03-21 12:06:32 +00:00
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
ESP_LOGI(TAG, "create_udp_server() port:%d", EXAMPLE_DEFAULT_PORT);
|
2017-03-21 12:06:32 +00:00
|
|
|
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (mysocket < 0) {
|
2017-04-13 11:14:01 +00:00
|
|
|
show_socket_error_reason(mysocket);
|
2017-03-31 08:34:12 +00:00
|
|
|
return ESP_FAIL;
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
2017-04-10 12:39:04 +00:00
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
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);
|
|
|
|
if (bind(mysocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
2017-04-13 11:14:01 +00:00
|
|
|
show_socket_error_reason(mysocket);
|
2017-03-21 12:06:32 +00:00
|
|
|
close(mysocket);
|
2017-03-31 08:34:12 +00:00
|
|
|
return ESP_FAIL;
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
2017-03-31 08:34:12 +00:00
|
|
|
return ESP_OK;
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 12:39:04 +00:00
|
|
|
//create a udp client socket. return ESP_OK:success ESP_FAIL:error
|
|
|
|
esp_err_t create_udp_client()
|
2017-03-21 12:06:32 +00:00
|
|
|
{
|
2017-04-10 12:39:04 +00:00
|
|
|
ESP_LOGI(TAG, "create_udp_client()");
|
2017-04-13 11:14:01 +00:00
|
|
|
ESP_LOGI(TAG, "connecting to %s:%d",
|
|
|
|
EXAMPLE_DEFAULT_SERVER_IP, EXAMPLE_DEFAULT_PORT);
|
2017-03-21 12:06:32 +00:00
|
|
|
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (mysocket < 0) {
|
2017-04-13 11:14:01 +00:00
|
|
|
show_socket_error_reason(mysocket);
|
2017-03-31 08:34:12 +00:00
|
|
|
return ESP_FAIL;
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
2017-04-10 12:39:04 +00:00
|
|
|
/*for client remote_addr is also server_addr*/
|
2017-03-21 12:06:32 +00:00
|
|
|
remote_addr.sin_family = AF_INET;
|
2017-04-13 11:14:01 +00:00
|
|
|
remote_addr.sin_port = htons(EXAMPLE_DEFAULT_PORT);
|
|
|
|
remote_addr.sin_addr.s_addr = inet_addr(EXAMPLE_DEFAULT_SERVER_IP);
|
2017-03-21 12:06:32 +00:00
|
|
|
|
2017-03-31 08:34:12 +00:00
|
|
|
return ESP_OK;
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//send or recv data task
|
|
|
|
void send_recv_data(void *pvParameters)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "task send_recv_data start!\n");
|
|
|
|
|
|
|
|
int len;
|
2017-04-13 11:14:01 +00:00
|
|
|
char databuff[EXAMPLE_DEFAULT_PKTSIZE];
|
2017-03-21 12:06:32 +00:00
|
|
|
|
|
|
|
/*send&receive first packet*/
|
|
|
|
socklen = sizeof(remote_addr);
|
2017-04-13 11:14:01 +00:00
|
|
|
memset(databuff, EXAMPLE_PACK_BYTE_IS, EXAMPLE_DEFAULT_PKTSIZE);
|
|
|
|
#if EXAMPLE_ESP_UDP_MODE_SERVER
|
2017-03-21 12:06:32 +00:00
|
|
|
ESP_LOGI(TAG, "first recvfrom:");
|
2017-04-13 11:14:01 +00:00
|
|
|
len = recvfrom(mysocket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen);
|
2017-03-21 12:06:32 +00:00
|
|
|
#else
|
|
|
|
ESP_LOGI(TAG, "first sendto:");
|
2017-04-13 11:14:01 +00:00
|
|
|
len = sendto(mysocket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
|
2017-03-21 12:06:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
ESP_LOGI(TAG, "transfer data with %s:%u\n",
|
|
|
|
inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));
|
2017-04-13 11:14:01 +00:00
|
|
|
xEventGroupSetBits(udp_event_group, UDP_CONNCETED_SUCCESS);
|
2017-03-21 12:06:32 +00:00
|
|
|
} else {
|
2017-04-13 11:14:01 +00:00
|
|
|
show_socket_error_reason(mysocket);
|
2017-03-21 12:06:32 +00:00
|
|
|
close(mysocket);
|
|
|
|
vTaskDelete(NULL);
|
2017-04-13 11:14:01 +00:00
|
|
|
} /*if (len > 0)*/
|
2017-03-21 12:06:32 +00:00
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
#if EXAMPLE_ESP_UDP_PERF_TX
|
2017-03-21 12:06:32 +00:00
|
|
|
vTaskDelay(500 / portTICK_RATE_MS);
|
|
|
|
#endif
|
|
|
|
ESP_LOGI(TAG, "start count!\n");
|
2017-04-13 11:14:01 +00:00
|
|
|
while(1) {
|
|
|
|
#if EXAMPLE_ESP_UDP_PERF_TX
|
|
|
|
len = sendto(mysocket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
|
2017-03-21 12:06:32 +00:00
|
|
|
#else
|
2017-04-13 11:14:01 +00:00
|
|
|
len = recvfrom(mysocket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen);
|
2017-03-21 12:06:32 +00:00
|
|
|
#endif
|
|
|
|
if (len > 0) {
|
2017-04-10 12:39:04 +00:00
|
|
|
total_data += len;
|
2017-03-21 12:06:32 +00:00
|
|
|
success_pack++;
|
|
|
|
} else {
|
2017-04-13 11:14:01 +00:00
|
|
|
if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) {
|
|
|
|
show_socket_error_reason(mysocket);
|
|
|
|
}
|
|
|
|
} /*if (len > 0)*/
|
|
|
|
} /*while(1)*/
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
int get_socket_error_code(int socket)
|
2017-04-10 12:39:04 +00:00
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
int result;
|
|
|
|
u32_t optlen = sizeof(int);
|
|
|
|
if(getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen) == -1) {
|
|
|
|
ESP_LOGE(TAG, "getsockopt failed");
|
|
|
|
return -1;
|
2017-04-10 12:39:04 +00:00
|
|
|
}
|
2017-04-13 11:14:01 +00:00
|
|
|
return result;
|
2017-04-10 12:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
int show_socket_error_reason(int socket)
|
2017-04-10 12:39:04 +00:00
|
|
|
{
|
2017-04-13 11:14:01 +00:00
|
|
|
int err = get_socket_error_code(socket);
|
|
|
|
ESP_LOGW(TAG, "socket error %d %s", err, strerror(err));
|
|
|
|
return err;
|
2017-04-10 12:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-04-10 12:39:04 +00:00
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
void close_socket()
|
|
|
|
{
|
|
|
|
close(mysocket);
|
|
|
|
}
|