tcp_udp: remove some unnecessary functions.

This commit is contained in:
chenyudong 2017-03-31 16:34:12 +08:00
parent e261e16981
commit 68bf54607a
8 changed files with 66 additions and 76 deletions

View File

@ -6,11 +6,11 @@ menu "Example Configuration"
# help # help
# This option set performance mode. # This option set performance mode.
# #
# - for "Performance in shieldbox" setting,it will receive data by tcp. # - Testing in shieldbox for "Performance in shieldbox" setting.
# #
# - for "Performance in air" setting, it will send data by tcp. # - Testing in air for "Performance in air" setting.
# #
# - for "Performance in long distance" setting, it will send data by tcp. # - Testing in long distance for "Performance in long distance" setting.
# #
# #
#config MODE_TCP_SHIELDBOX #config MODE_TCP_SHIELDBOX
@ -51,7 +51,7 @@ config TCP_PERF_DELAY_DEBUG
config TCP_PERF_WIFI_SSID config TCP_PERF_WIFI_SSID
string "WiFi SSID" string "WiFi SSID"
default "tp_wifi_test1" default "esp_wifi_test1"
help help
SSID (network name) for the example to connect to. SSID (network name) for the example to connect to.

View File

@ -19,6 +19,12 @@ step3:
you can see the info in com port output. 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" #include "tcp_perf.h"
int connectedflag = 0; int connectedflag = 0;
@ -57,7 +63,7 @@ static void tcp_conn(void *pvParameters)
ESP_LOGI(TAG, "creat_tcp_client."); ESP_LOGI(TAG, "creat_tcp_client.");
socret = creat_tcp_client(); socret = creat_tcp_client();
#endif #endif
if(-1 == socret) { if(ESP_FAIL == socret) {
ESP_LOGI(TAG, "creat tcp socket error,stop."); ESP_LOGI(TAG, "creat tcp socket error,stop.");
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@ -103,7 +109,6 @@ static void tcp_conn(void *pvParameters)
void app_main(void) void app_main(void)
{ {
nvs_flash_init();
#if ESP_WIFI_MODE_AP #if ESP_WIFI_MODE_AP
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n"); ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n");
wifi_init_softap(); wifi_init_softap();

View File

@ -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" #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*/ /*socket*/
static int sever_socket = 0; static int sever_socket = 0;
static struct sockaddr_in sever_addr; 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() int creat_tcp_sever()
{ {
ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT); ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT);
@ -134,7 +142,7 @@ int creat_tcp_sever()
if (sever_socket < 0) { if (sever_socket < 0) {
show_socket_error_code(sever_socket); show_socket_error_code(sever_socket);
//perror("socket() error:"); //perror("socket() error:");
return -1; return ESP_FAIL;
} }
sever_addr.sin_family = AF_INET; sever_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULT_PORT); sever_addr.sin_port = htons(DEFAULT_PORT);
@ -143,26 +151,26 @@ int creat_tcp_sever()
show_socket_error_code(sever_socket); show_socket_error_code(sever_socket);
//perror("bind() error"); //perror("bind() error");
close(sever_socket); close(sever_socket);
return -1; return ESP_FAIL;
} }
if (listen(sever_socket, 5) < 0) { if (listen(sever_socket, 5) < 0) {
show_socket_error_code(sever_socket); show_socket_error_code(sever_socket);
//perror("listen() error"); //perror("listen() error");
close(sever_socket); close(sever_socket);
return -1; return ESP_FAIL;
} }
connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen); connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen);
if (connect_soc<0) { if (connect_soc<0) {
show_socket_error_code(connect_soc); show_socket_error_code(connect_soc);
//perror("accept() error"); //perror("accept() error");
close(sever_socket); close(sever_socket);
return -1; return ESP_FAIL;
} }
/*connection establishednow can send/recv*/ /*connection establishednow can send/recv*/
ESP_LOGI(TAG, "tcp connection established!"); 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() int creat_tcp_client()
{ {
ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n", ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n",
@ -171,7 +179,7 @@ int creat_tcp_client()
if (connect_soc < 0) { if (connect_soc < 0) {
show_socket_error_code(connect_soc); show_socket_error_code(connect_soc);
//perror("socket failed!"); //perror("socket failed!");
return -1; return ESP_FAIL;
} }
sever_addr.sin_family = AF_INET; sever_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULT_PORT); 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) { if (connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) {
show_socket_error_code(connect_soc); show_socket_error_code(connect_soc);
//perror("connect to sever error!"); //perror("connect to sever error!");
return -1; return ESP_FAIL;
} }
ESP_LOGI(TAG, "connect to sever success!"); ESP_LOGI(TAG, "connect to sever success!");
return 0; return ESP_OK;
} }
//wifi_init_sta //wifi_init_sta
void wifi_init_sta() void wifi_init_sta()
{ {
tcpip_adapter_init(); tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
@ -214,7 +221,6 @@ void wifi_init_sta()
void wifi_init_softap() void wifi_init_softap()
{ {
tcpip_adapter_init(); tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

View File

@ -1,22 +1,6 @@
#ifndef __TCP_PERF_H__ #ifndef __TCP_PERF_H__
#define __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 #ifdef __cplusplus

View File

@ -1,15 +1,16 @@
menu "Example Configuration" menu "Example Configuration"
#choice #choice
# prompt "UCP_PERF_MODE " # prompt "TCP_PERF_MODE "
# default MODE_UDP_SHIELDBOX # default MODE_TCP_SHIELDBOX
# help # help
# This option set performance mode. # This option set performance mode.
# #
# - for "Performance in shieldbox" setting,it will receive data by udp. # - Testing in shieldbox for "Performance in shieldbox" setting.
# - 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 air for "Performance in air" setting.
#
# - Testing in long distance for "Performance in long distance" setting.
# #
# #
#config MODE_UDP_SHIELDBOX #config MODE_UDP_SHIELDBOX
@ -28,7 +29,7 @@ config UDP_PERF_WIFI_MODE_AP
config UDP_PERF_SEVER config UDP_PERF_SEVER
bool "TCP performance sever enable" bool "TCP performance sever enable"
default n default y
help help
yes:ESP32 is UDP sever. no:ESP32 is UDP client. yes:ESP32 is UDP sever. no:ESP32 is UDP client.
@ -36,13 +37,13 @@ config UDP_PERF_SEVER
config UDP_PERF_TX config UDP_PERF_TX
bool "UDP performance TX test enable" bool "UDP performance TX test enable"
default y default n
help help
yes:UDP TX test. no:UDP RX test. yes:UDP TX test. no:UDP RX test.
config UDP_PERF_WIFI_SSID config UDP_PERF_WIFI_SSID
string "WiFi SSID" string "WiFi SSID"
default "tp_wifi_test1" default "esp_wifi_test1"
help help
SSID (network name) for the example to connect to. SSID (network name) for the example to connect to.

View File

@ -19,6 +19,11 @@ step3:
you can see the info in com port output. 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" #include "udp_perf.h"
int connectedflag = 0; int connectedflag = 0;
@ -50,7 +55,7 @@ static void udp_conn(void *pvParameters)
ESP_LOGI(TAG, "creat_udp_client."); ESP_LOGI(TAG, "creat_udp_client.");
socret = creat_udp_client(); socret = creat_udp_client();
#endif #endif
if(-1 == socret) { if(ESP_FAIL == socret) {
ESP_LOGI(TAG, "creat udp socket error,stop."); ESP_LOGI(TAG, "creat udp socket error,stop.");
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@ -80,7 +85,6 @@ static void udp_conn(void *pvParameters)
void app_main(void) void app_main(void)
{ {
nvs_flash_init();
#if ESP_WIFI_MODE_AP #if ESP_WIFI_MODE_AP
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n"); ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n");
wifi_init_softap(); wifi_init_softap();

View File

@ -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" #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 int mysocket;
static struct sockaddr_in remote_addr; 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() void wifi_init_sta()
{ {
tcpip_adapter_init(); tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
@ -73,7 +80,6 @@ void wifi_init_sta()
void wifi_init_softap() void wifi_init_softap()
{ {
tcpip_adapter_init(); tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
@ -99,14 +105,14 @@ void wifi_init_softap()
DEFAULT_SSID, DEFAULT_PWD); 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() int creat_udp_sever()
{ {
ESP_LOGI(TAG, "creat_udp_sever()"); ESP_LOGI(TAG, "creat_udp_sever()");
mysocket = socket(AF_INET, SOCK_DGRAM, 0); mysocket = socket(AF_INET, SOCK_DGRAM, 0);
if (mysocket < 0) { if (mysocket < 0) {
perror("socket failed:"); perror("socket failed:");
return -1; return ESP_FAIL;
} }
struct sockaddr_in sever_addr; struct sockaddr_in sever_addr;
sever_addr.sin_family = AF_INET; 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) { if (bind(mysocket, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) {
perror("bind local port error:"); perror("bind local port error:");
close(mysocket); 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() int creat_udp_client()
{ {
ESP_LOGI(TAG, "creat_udp_client()"); ESP_LOGI(TAG, "creat_udp_client()");
mysocket = socket(AF_INET, SOCK_DGRAM, 0); mysocket = socket(AF_INET, SOCK_DGRAM, 0);
if (mysocket < 0) { if (mysocket < 0) {
perror("socket failed:"); perror("socket failed:");
return -1; return ESP_FAIL;
} }
/*for client remote_addr is also sever_addr*/ /*for client remote_addr is also sever_addr*/
remote_addr.sin_family = AF_INET; remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(DEFAULT_PORT); remote_addr.sin_port = htons(DEFAULT_PORT);
remote_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP); remote_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP);
return 0; return ESP_OK;
} }

View File

@ -1,22 +1,6 @@
#ifndef __UDP_PERF_H__ #ifndef __UDP_PERF_H__
#define __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 #ifdef __cplusplus