forked from xuhongv/StudyInEsp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xuhong_Tcp.c
379 lines (330 loc) · 10.3 KB
/
xuhong_Tcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* 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.
*/
#include <string.h>
#include <sys/socket.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_log.h"
#include "xuhong_Tcp.h"
/* FreeRTOS event group to signal when we are connected to wifi */
EventGroupHandle_t tcp_event_group;
/*socket*/
static int server_socket = 0;
static struct sockaddr_in server_addr;
static struct sockaddr_in client_addr;
static unsigned int socklen = sizeof(client_addr);
static int connect_socket = 0;
bool g_rxtx_need_restart = false;
int g_total_data = 0;
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
int g_total_pack = 0;
int g_send_success = 0;
int g_send_fail = 0;
int g_delay_classify[5] = {0};
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
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();
xEventGroupClearBits(tcp_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_CONNECTED:
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "got ip:%s\n",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
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);
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
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);
g_rxtx_need_restart = true;
xEventGroupClearBits(tcp_event_group, WIFI_CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
//send data
void send_data(void *pvParameters)
{
int len = 0;
char *databuff = (char *)malloc(EXAMPLE_DEFAULT_PKTSIZE * sizeof(char));
memset(databuff, EXAMPLE_PACK_BYTE_IS, EXAMPLE_DEFAULT_PKTSIZE);
vTaskDelay(100 / portTICK_RATE_MS);
ESP_LOGI(TAG, "start sending...");
while (1)
{
int to_write = EXAMPLE_DEFAULT_PKTSIZE;
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);
if (err != ENOMEM)
{
show_socket_error_reason("send_data", connect_socket);
break;
}
}
}
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
gettimeofday(&tv_finish, NULL);
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
if (g_total_data > 0)
{
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
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]++;
}
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
}
else
{
break;
}
}
g_rxtx_need_restart = true;
free(databuff);
vTaskDelete(NULL);
}
//receive data
void recv_data(void *pvParameters)
{
int len = 0;
char databuff[1024];
while (1)
{
//每次接收都要清空接收数组
memset(databuff, 0x00, sizeof(databuff));
len = recv(connect_socket, databuff, sizeof(databuff), 0);
g_rxtx_need_restart = false;
if (len > 0)
{
g_total_data += len;
//打印接收到的数组
ESP_LOGI(TAG, "recvData: %s\n", databuff);
//原路返回,不指定某个客户端
send(connect_socket, databuff, sizeof(databuff), 0);
//sendto(connect_socket, databuff , sizeof(databuff), 0, (struct sockaddr *) &remote_addr,sizeof(remote_addr));
}
else
{
show_socket_error_reason("recv_data", connect_socket);
g_rxtx_need_restart = true;
#if !TCP_SERVER_CLIENT_OPTION
break;
#endif
}
}
close_socket();
g_rxtx_need_restart = true;
vTaskDelete(NULL);
}
esp_err_t create_tcp_server(bool isCreatServer)
{
if (isCreatServer)
{
ESP_LOGI(TAG, "server socket....,port=%d", TCP_PORT);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0)
{
show_socket_error_reason("create_server", server_socket);
return ESP_FAIL;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(TCP_PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
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;
}
}
if (listen(server_socket, 5) < 0)
{
show_socket_error_reason("listen_server", server_socket);
close(server_socket);
return ESP_FAIL;
}
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;
}
/*connection established,now can send/recv*/
ESP_LOGI(TAG, "tcp connection established!");
return ESP_OK;
}
//创建TCP客户端连接到指定的服务器
esp_err_t create_tcp_client()
{
ESP_LOGI(TAG, "will connect gateway ssid : %s port:%d\n",
TCP_SERVER_ADRESS, TCP_PORT);
connect_socket = socket(AF_INET, SOCK_STREAM, 0);
if (connect_socket < 0)
{
show_socket_error_reason("create client", connect_socket);
return ESP_FAIL;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(TCP_PORT);
server_addr.sin_addr.s_addr = inet_addr(TCP_SERVER_ADRESS);
ESP_LOGI(TAG, "connectting server...");
if (connect(connect_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
show_socket_error_reason("client connect", connect_socket);
ESP_LOGE(TAG, "connect failed!");
return ESP_FAIL;
}
ESP_LOGI(TAG, "connect success!");
return ESP_OK;
}
//wifi_init_sta
void wifi_init_sta()
{
tcp_event_group = xEventGroupCreate();
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 = {
.ssid = GATEWAY_SSID,
.password = GATEWAY_PAS},
};
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",
GATEWAY_SSID, GATEWAY_PAS);
}
//wifi_init_softap
void wifi_init_softap()
{
tcp_event_group = xEventGroupCreate();
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 = {
.ssid = SOFT_AP_SSID,
.ssid_len = 0,
.max_connection = SOFT_AP_MAX_CONNECT,
.password = SOFT_AP_PAS,
.authmode = WIFI_AUTH_WPA_WPA2_PSK},
};
if (strlen(EXAMPLE_DEFAULT_PWD) == 0)
{
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}
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, "SoftAP set finish:%s pas:%s \n",
EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD);
}
int get_socket_error_code(int socket)
{
int result;
u32_t optlen = sizeof(int);
int err = getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen);
if (err == -1)
{
ESP_LOGE(TAG, "getsockopt failed:%s", strerror(err));
return -1;
}
return result;
}
int show_socket_error_reason(const char *str, int socket)
{
int err = get_socket_error_code(socket);
if (err != 0)
{
ESP_LOGW(TAG, "%s socket error %d %s", str, err, strerror(err));
}
return err;
}
int check_working_socket()
{
int ret;
#if EXAMPLE_ESP_TCP_MODE_SERVER
ESP_LOGD(TAG, "check server_socket");
ret = get_socket_error_code(server_socket);
if (ret != 0)
{
ESP_LOGW(TAG, "server socket error %d %s", ret, strerror(ret));
}
if (ret == ECONNRESET)
{
return ret;
}
#endif
ESP_LOGD(TAG, "check connect_socket");
ret = get_socket_error_code(connect_socket);
if (ret != 0)
{
ESP_LOGW(TAG, "connect socket error %d %s", ret, strerror(ret));
}
if (ret != 0)
{
return ret;
}
return 0;
}
void close_socket()
{
close(connect_socket);
close(server_socket);
}