kopia lustrzana https://github.com/espressif/esp-idf
tcp_udp: remove some unnecessary functions.
rodzic
e261e16981
commit
68bf54607a
|
@ -5,12 +5,12 @@ menu "Example Configuration"
|
|||
# default MODE_TCP_SHIELDBOX
|
||||
# help
|
||||
# This option set performance mode.
|
||||
#
|
||||
# - Testing in shieldbox for "Performance in shieldbox" setting.
|
||||
#
|
||||
# - for "Performance in shieldbox" setting,it will receive data by tcp.
|
||||
#
|
||||
# - for "Performance in air" setting, it will send data by tcp.
|
||||
#
|
||||
# - for "Performance in long distance" setting, it will send data by tcp.
|
||||
# - Testing in air for "Performance in air" setting.
|
||||
#
|
||||
# - Testing in long distance for "Performance in long distance" setting.
|
||||
#
|
||||
#
|
||||
#config MODE_TCP_SHIELDBOX
|
||||
|
@ -51,7 +51,7 @@ config TCP_PERF_DELAY_DEBUG
|
|||
|
||||
config TCP_PERF_WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "tp_wifi_test1"
|
||||
default "esp_wifi_test1"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
|
||||
|
|
|
@ -19,6 +19,12 @@ step3:
|
|||
you can see the info in com port output.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "tcp_perf.h"
|
||||
|
||||
int connectedflag = 0;
|
||||
|
@ -57,7 +63,7 @@ static void tcp_conn(void *pvParameters)
|
|||
ESP_LOGI(TAG, "creat_tcp_client.");
|
||||
socret = creat_tcp_client();
|
||||
#endif
|
||||
if(-1 == socret) {
|
||||
if(ESP_FAIL == socret) {
|
||||
ESP_LOGI(TAG, "creat tcp socket error,stop.");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
@ -103,7 +109,6 @@ static void tcp_conn(void *pvParameters)
|
|||
|
||||
void app_main(void)
|
||||
{
|
||||
nvs_flash_init();
|
||||
#if ESP_WIFI_MODE_AP
|
||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n");
|
||||
wifi_init_softap();
|
||||
|
|
|
@ -1,8 +1,16 @@
|
|||
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.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 & ready to make a request */
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
/*socket*/
|
||||
static int sever_socket = 0;
|
||||
static struct sockaddr_in sever_addr;
|
||||
|
@ -126,7 +134,7 @@ void recv_data(void *pvParameters)
|
|||
}
|
||||
|
||||
|
||||
//use this esp32 as a tcp sever. return 0:success -1:error
|
||||
//use this esp32 as a tcp sever. return ESP_OK:success ESP_FAIL:error
|
||||
int creat_tcp_sever()
|
||||
{
|
||||
ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT);
|
||||
|
@ -134,7 +142,7 @@ int creat_tcp_sever()
|
|||
if (sever_socket < 0) {
|
||||
show_socket_error_code(sever_socket);
|
||||
//perror("socket() error:");
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
sever_addr.sin_family = AF_INET;
|
||||
sever_addr.sin_port = htons(DEFAULT_PORT);
|
||||
|
@ -143,26 +151,26 @@ int creat_tcp_sever()
|
|||
show_socket_error_code(sever_socket);
|
||||
//perror("bind() error");
|
||||
close(sever_socket);
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (listen(sever_socket, 5) < 0) {
|
||||
show_socket_error_code(sever_socket);
|
||||
//perror("listen() error");
|
||||
close(sever_socket);
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen);
|
||||
if (connect_soc<0) {
|
||||
show_socket_error_code(connect_soc);
|
||||
//perror("accept() error");
|
||||
close(sever_socket);
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
/*connection established,now can send/recv*/
|
||||
ESP_LOGI(TAG, "tcp connection established!");
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
//use this esp32 as a tcp client. return 0:success -1:error
|
||||
//use this esp32 as a tcp client. return ESP_OK:success ESP_FAIL:error
|
||||
int creat_tcp_client()
|
||||
{
|
||||
ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n",
|
||||
|
@ -171,7 +179,7 @@ int creat_tcp_client()
|
|||
if (connect_soc < 0) {
|
||||
show_socket_error_code(connect_soc);
|
||||
//perror("socket failed!");
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
sever_addr.sin_family = AF_INET;
|
||||
sever_addr.sin_port = htons(DEFAULT_PORT);
|
||||
|
@ -180,17 +188,16 @@ int creat_tcp_client()
|
|||
if (connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) {
|
||||
show_socket_error_code(connect_soc);
|
||||
//perror("connect to sever error!");
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGI(TAG, "connect to sever success!");
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//wifi_init_sta
|
||||
void wifi_init_sta()
|
||||
{
|
||||
tcpip_adapter_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
|
@ -214,7 +221,6 @@ void wifi_init_sta()
|
|||
void wifi_init_softap()
|
||||
{
|
||||
tcpip_adapter_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
|
|
|
@ -1,22 +1,6 @@
|
|||
#ifndef __TCP_PERF_H__
|
||||
#define __TCP_PERF_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.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_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include "driver/uart.h"
|
||||
#include "soc/uart_struct.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
menu "Example Configuration"
|
||||
|
||||
#choice
|
||||
# prompt "UCP_PERF_MODE "
|
||||
# default MODE_UDP_SHIELDBOX
|
||||
# prompt "TCP_PERF_MODE "
|
||||
# default MODE_TCP_SHIELDBOX
|
||||
# help
|
||||
# This option set performance mode.
|
||||
#
|
||||
# - for "Performance in shieldbox" setting,it will receive data by udp.
|
||||
# - for "Performance in air" setting, it will send data by udp.
|
||||
#
|
||||
# - for "Performance in long distance" setting, it will send data by udp.
|
||||
# - 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
|
||||
|
@ -28,7 +29,7 @@ config UDP_PERF_WIFI_MODE_AP
|
|||
|
||||
config UDP_PERF_SEVER
|
||||
bool "TCP performance sever enable"
|
||||
default n
|
||||
default y
|
||||
help
|
||||
yes:ESP32 is UDP sever. no:ESP32 is UDP client.
|
||||
|
||||
|
@ -36,13 +37,13 @@ config UDP_PERF_SEVER
|
|||
|
||||
config UDP_PERF_TX
|
||||
bool "UDP performance TX test enable"
|
||||
default y
|
||||
default n
|
||||
help
|
||||
yes:UDP TX test. no:UDP RX test.
|
||||
|
||||
config UDP_PERF_WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "tp_wifi_test1"
|
||||
default "esp_wifi_test1"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
|
||||
|
|
|
@ -19,6 +19,11 @@ step3:
|
|||
you can see the info in com port output.
|
||||
*/
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "udp_perf.h"
|
||||
|
||||
int connectedflag = 0;
|
||||
|
@ -50,7 +55,7 @@ static void udp_conn(void *pvParameters)
|
|||
ESP_LOGI(TAG, "creat_udp_client.");
|
||||
socret = creat_udp_client();
|
||||
#endif
|
||||
if(-1 == socret) {
|
||||
if(ESP_FAIL == socret) {
|
||||
ESP_LOGI(TAG, "creat udp socket error,stop.");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
@ -80,7 +85,6 @@ static void udp_conn(void *pvParameters)
|
|||
|
||||
void app_main(void)
|
||||
{
|
||||
nvs_flash_init();
|
||||
#if ESP_WIFI_MODE_AP
|
||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n");
|
||||
wifi_init_softap();
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.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 & ready to make a request */
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
static int mysocket;
|
||||
|
||||
static struct sockaddr_in remote_addr;
|
||||
|
@ -49,7 +57,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|||
void wifi_init_sta()
|
||||
{
|
||||
tcpip_adapter_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
|
@ -73,7 +80,6 @@ void wifi_init_sta()
|
|||
void wifi_init_softap()
|
||||
{
|
||||
tcpip_adapter_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
|
@ -99,14 +105,14 @@ void wifi_init_softap()
|
|||
DEFAULT_SSID, DEFAULT_PWD);
|
||||
}
|
||||
|
||||
//creat a udp sever socket. return 0:success -1:error
|
||||
//creat a udp sever socket. return ESP_OK:success ESP_FAIL:error
|
||||
int creat_udp_sever()
|
||||
{
|
||||
ESP_LOGI(TAG, "creat_udp_sever()");
|
||||
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (mysocket < 0) {
|
||||
perror("socket failed:");
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
struct sockaddr_in sever_addr;
|
||||
sever_addr.sin_family = AF_INET;
|
||||
|
@ -115,26 +121,26 @@ int creat_udp_sever()
|
|||
if (bind(mysocket, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) {
|
||||
perror("bind local port error:");
|
||||
close(mysocket);
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//creat a udp client socket. return 0:success -1:error
|
||||
//creat a udp client socket. return ESP_OK:success ESP_FAIL:error
|
||||
int creat_udp_client()
|
||||
{
|
||||
ESP_LOGI(TAG, "creat_udp_client()");
|
||||
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (mysocket < 0) {
|
||||
perror("socket failed:");
|
||||
return -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
/*for client remote_addr is also sever_addr*/
|
||||
remote_addr.sin_family = AF_INET;
|
||||
remote_addr.sin_port = htons(DEFAULT_PORT);
|
||||
remote_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP);
|
||||
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,22 +1,6 @@
|
|||
#ifndef __UDP_PERF_H__
|
||||
#define __UDP_PERF_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.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_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include "driver/uart.h"
|
||||
#include "soc/uart_struct.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
Ładowanie…
Reference in New Issue