|
@@ -1,395 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * http_socket.c
|
3
|
|
- *
|
4
|
|
- * Based on BittyHTTP example socket interface.
|
5
|
|
- *
|
6
|
|
- * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
|
7
|
|
- *
|
8
|
|
- * This program is free software: you can redistribute it and/or modify
|
9
|
|
- * it under the terms of the GNU General Public License as published by
|
10
|
|
- * the Free Software Foundation, either version 3 of the License, or
|
11
|
|
- * (at your option) any later version.
|
12
|
|
- *
|
13
|
|
- * This program is distributed in the hope that it will be useful,
|
14
|
|
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
|
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
|
- * GNU General Public License for more details.
|
17
|
|
- *
|
18
|
|
- * See <http://www.gnu.org/licenses/>.
|
19
|
|
- */
|
20
|
|
-
|
21
|
|
-#include <string.h>
|
22
|
|
-
|
23
|
|
-#include "lwip/tcp.h"
|
24
|
|
-#include "lwip/dns.h"
|
25
|
|
-#include "SocketsCon.h"
|
26
|
|
-
|
27
|
|
-#include "config.h"
|
28
|
|
-#include "log.h"
|
29
|
|
-#include "main.h"
|
30
|
|
-#include "ring.h"
|
31
|
|
-#include "http.h"
|
32
|
|
-
|
33
|
|
-#define MAX_SOCK 4
|
34
|
|
-#define SOCK_RECV_BUFF 512
|
35
|
|
-
|
36
|
|
-#define HTTP_DNS_TIMEOUT_MS 5000
|
37
|
|
-#define HTTP_CONNECT_TIMEOUT_MS 5000
|
38
|
|
-
|
39
|
|
-struct tcp_sock {
|
40
|
|
- bool set;
|
41
|
|
- struct tcp_pcb *pcb;
|
42
|
|
-
|
43
|
|
- // TODO listening server socket has unused buffer
|
44
|
|
- uint8_t rx_buf[SOCK_RECV_BUFF];
|
45
|
|
- struct ring_buffer rx_rb;
|
46
|
|
-
|
47
|
|
- struct tcp_pcb *child_buf[MAX_SOCK];
|
48
|
|
- struct ring_buffer child_rb;
|
49
|
|
-};
|
50
|
|
-
|
51
|
|
-static struct tcp_sock sock[MAX_SOCK] = {0};
|
52
|
|
-
|
53
|
|
-bool SocketsCon_InitSocketConSystem(void) {
|
54
|
|
- return true;
|
55
|
|
-}
|
56
|
|
-
|
57
|
|
-void SocketsCon_ShutdownSocketConSystem(void) { }
|
58
|
|
-
|
59
|
|
-static void tcp_server_err(void *arg, err_t err) {
|
60
|
|
- debug("tcp error %d", err);
|
61
|
|
-
|
62
|
|
- struct SocketCon *Con = arg;
|
63
|
|
- Con->ErrorCode = e_ConnectErrorMAX;
|
64
|
|
- Con->State = e_ConnectState_Error;
|
65
|
|
-}
|
66
|
|
-
|
67
|
|
-static err_t tcp_server_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
|
68
|
|
- (void)tpcb;
|
69
|
|
- debug("tcp recv %d %d", p->len, err);
|
70
|
|
-
|
71
|
|
- struct SocketCon *Con = arg;
|
72
|
|
- size_t idx = Con->SocketFD;
|
73
|
|
-
|
74
|
|
- if (rb_space(&sock[idx].rx_rb) < p->len) {
|
75
|
|
- debug("not enough space (%d < %d)", rb_space(&sock[idx].rx_rb), p->len);
|
76
|
|
- tcp_abort(sock[idx].pcb);
|
77
|
|
- Con->ErrorCode = e_ConnectErrorMAX;
|
78
|
|
- Con->State = e_ConnectState_Error;
|
79
|
|
- return ERR_ABRT;
|
80
|
|
- }
|
81
|
|
-
|
82
|
|
- rb_add(&sock[idx].rx_rb, p->payload, p->len);
|
83
|
|
- return ERR_OK;
|
84
|
|
-}
|
85
|
|
-
|
86
|
|
-bool SocketsCon_InitSockCon(struct SocketCon *Con) {
|
87
|
|
- if (!Con) {
|
88
|
|
- debug("invalid param");
|
89
|
|
- return false;
|
90
|
|
- }
|
91
|
|
-
|
92
|
|
- ssize_t next = -1;
|
93
|
|
- for (size_t i = 0; i < MAX_SOCK; i++) {
|
94
|
|
- if (!sock[i].set) {
|
95
|
|
- next = i;
|
96
|
|
- break;
|
97
|
|
- }
|
98
|
|
- }
|
99
|
|
-
|
100
|
|
- if (next < 0) {
|
101
|
|
- debug("error: too many sockets");
|
102
|
|
- return false;
|
103
|
|
- }
|
104
|
|
-
|
105
|
|
- debug("new socket at %d", next);
|
106
|
|
-
|
107
|
|
- sock[next].pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
|
108
|
|
- if (sock[next].pcb == NULL) {
|
109
|
|
- debug("error allocating new socket");
|
110
|
|
- return false;
|
111
|
|
- }
|
112
|
|
-
|
113
|
|
- tcp_arg(sock[next].pcb, Con);
|
114
|
|
- tcp_err(sock[next].pcb, tcp_server_err);
|
115
|
|
- tcp_recv(sock[next].pcb, tcp_server_recv);
|
116
|
|
-
|
117
|
|
- Con->SocketFD = next;
|
118
|
|
- sock[next].set = true;
|
119
|
|
-
|
120
|
|
- struct ring_buffer tmp = RB_INIT(sock[next].rx_buf, SOCK_RECV_BUFF, sizeof(uint8_t));
|
121
|
|
- sock[next].rx_rb = tmp;
|
122
|
|
-
|
123
|
|
- struct ring_buffer tmp2 = RB_INIT(sock[next].child_buf, MAX_SOCK, sizeof(struct tcp_pcb *));
|
124
|
|
- sock[next].child_rb = tmp2;
|
125
|
|
-
|
126
|
|
- Con->ErrorCode = e_ConnectError_AllOk;
|
127
|
|
- Con->State = e_ConnectState_Idle;
|
128
|
|
- Con->ReadInProgress = false;
|
129
|
|
-
|
130
|
|
- return true;
|
131
|
|
-}
|
132
|
|
-
|
133
|
|
-static err_t tcp_server_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
|
134
|
|
- (void)tpcb;
|
135
|
|
- debug("tcp connected");
|
136
|
|
-
|
137
|
|
- struct SocketCon *Con = arg;
|
138
|
|
- Con->State = (err == ERR_OK) ? e_ConnectState_Connected : e_ConnectState_Idle;
|
139
|
|
- return ERR_OK;
|
140
|
|
-}
|
141
|
|
-
|
142
|
|
-bool SocketsCon_Connect(struct SocketCon *Con,
|
143
|
|
- const char *ServerName, int portNo) {
|
144
|
|
- if ((!Con) || (!ServerName)) {
|
145
|
|
- debug("invalid param");
|
146
|
|
- return false;
|
147
|
|
- }
|
148
|
|
-
|
149
|
|
- ip_addr_t ipaddr;
|
150
|
|
- err_t err;
|
151
|
|
- uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
152
|
|
- do {
|
153
|
|
- err = dns_gethostbyname(ServerName, &ipaddr, NULL, NULL);
|
154
|
|
- main_loop_hw();
|
155
|
|
-
|
156
|
|
- uint32_t now = to_ms_since_boot(get_absolute_time());
|
157
|
|
- if ((now - start_time) >= HTTP_DNS_TIMEOUT_MS) {
|
158
|
|
- break;
|
159
|
|
- }
|
160
|
|
- } while (err == ERR_INPROGRESS);
|
161
|
|
- if (err != ERR_OK) {
|
162
|
|
- debug("error getting IP for '%s'", ServerName);
|
163
|
|
- return false;
|
164
|
|
- } else {
|
165
|
|
- debug("IP %s for '%s'", ip4addr_ntoa(&ipaddr), ServerName);
|
166
|
|
- }
|
167
|
|
-
|
168
|
|
- Con->State = e_ConnectState_Connecting;
|
169
|
|
-
|
170
|
|
- size_t idx = Con->SocketFD;
|
171
|
|
- err = tcp_connect(sock[idx].pcb,
|
172
|
|
- &ipaddr, portNo,
|
173
|
|
- tcp_server_connected);
|
174
|
|
- if (err != ERR_OK) {
|
175
|
|
- debug("error connecting (%d)", err);
|
176
|
|
- Con->State = e_ConnectState_Idle;
|
177
|
|
- return false;
|
178
|
|
- }
|
179
|
|
-
|
180
|
|
- start_time = to_ms_since_boot(get_absolute_time());
|
181
|
|
- while (Con->State == e_ConnectState_Connecting) {
|
182
|
|
- main_loop_hw();
|
183
|
|
-
|
184
|
|
- uint32_t now = to_ms_since_boot(get_absolute_time());
|
185
|
|
- if ((now - start_time) >= HTTP_CONNECT_TIMEOUT_MS) {
|
186
|
|
- break;
|
187
|
|
- }
|
188
|
|
- }
|
189
|
|
-
|
190
|
|
- // TODO ?
|
191
|
|
- //if (Con->State == e_ConnectState_Error) {
|
192
|
|
- // Con->State = e_ConnectState_Idle;
|
193
|
|
- //}
|
194
|
|
-
|
195
|
|
- return (Con->State == e_ConnectState_Connected);
|
196
|
|
-}
|
197
|
|
-
|
198
|
|
-bool SocketsCon_EnableAddressReuse(struct SocketCon *Con, bool Enable) {
|
199
|
|
- (void)Con;
|
200
|
|
- (void)Enable;
|
201
|
|
- return true;
|
202
|
|
-}
|
203
|
|
-
|
204
|
|
-void SocketsCon_Tick(struct SocketCon *Con) { (void)Con; }
|
205
|
|
-
|
206
|
|
-bool SocketsCon_Write(struct SocketCon *Con, const void *buf, int num) {
|
207
|
|
- if ((!Con) || (!buf) || (num <= 0)) {
|
208
|
|
- debug("invalid param");
|
209
|
|
- return false;
|
210
|
|
- }
|
211
|
|
-
|
212
|
|
- size_t idx = Con->SocketFD;
|
213
|
|
- err_t err = tcp_write(sock[idx].pcb,
|
214
|
|
- buf, num, TCP_WRITE_FLAG_COPY);
|
215
|
|
- if (err != ERR_OK) {
|
216
|
|
- debug("error writing to socket");
|
217
|
|
- return false;
|
218
|
|
- }
|
219
|
|
-
|
220
|
|
- return true;
|
221
|
|
-}
|
222
|
|
-
|
223
|
|
-int SocketsCon_Read(struct SocketCon *Con, void *buf, int num) {
|
224
|
|
- if ((!Con) || (!buf) || (num <= 0)) {
|
225
|
|
- debug("invalid param");
|
226
|
|
- return false;
|
227
|
|
- }
|
228
|
|
-
|
229
|
|
- // TODO irq disable?
|
230
|
|
-
|
231
|
|
- size_t idx = Con->SocketFD;
|
232
|
|
- size_t len = rb_get(&sock[idx].rx_rb, buf, num);
|
233
|
|
-
|
234
|
|
- // TODO irq enable?
|
235
|
|
-
|
236
|
|
- return len;
|
237
|
|
-}
|
238
|
|
-
|
239
|
|
-void SocketsCon_Close(struct SocketCon *Con) {
|
240
|
|
- if (!Con) {
|
241
|
|
- debug("invalid param");
|
242
|
|
- return;
|
243
|
|
- }
|
244
|
|
-
|
245
|
|
- size_t idx = Con->SocketFD;
|
246
|
|
- sock[idx].set = false;
|
247
|
|
-
|
248
|
|
- err_t err = tcp_close(sock[idx].pcb);
|
249
|
|
- if (err != ERR_OK) {
|
250
|
|
- debug("error closing socket (%d)", err);
|
251
|
|
- // TODO retry?
|
252
|
|
- }
|
253
|
|
-}
|
254
|
|
-
|
255
|
|
-static err_t tcp_server_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
|
256
|
|
- if (err != ERR_OK) {
|
257
|
|
- debug("ignoring failed accept");
|
258
|
|
- return ERR_OK;
|
259
|
|
- }
|
260
|
|
-
|
261
|
|
- struct SocketCon *Con = arg;
|
262
|
|
- size_t idx = Con->SocketFD;
|
263
|
|
- if (rb_space(&sock[idx].child_rb) <= 0) {
|
264
|
|
- debug("no space for new connection");
|
265
|
|
- tcp_abort(newpcb);
|
266
|
|
- return ERR_OK; // ERR_ABRT ?
|
267
|
|
- }
|
268
|
|
-
|
269
|
|
- debug("new connection (%d)", err);
|
270
|
|
-
|
271
|
|
- rb_push(&sock[idx].child_rb, &newpcb);
|
272
|
|
- return ERR_OK;
|
273
|
|
-}
|
274
|
|
-
|
275
|
|
-bool SocketsCon_Listen(struct SocketCon *Con, const char *bindadd, int PortNo) {
|
276
|
|
- if (!Con) {
|
277
|
|
- debug("invalid param");
|
278
|
|
- return false;
|
279
|
|
- }
|
280
|
|
-
|
281
|
|
- ip_addr_t ipaddr;
|
282
|
|
- err_t err;
|
283
|
|
- if (bindadd) {
|
284
|
|
- uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
285
|
|
- do {
|
286
|
|
- err = dns_gethostbyname(bindadd, &ipaddr, NULL, NULL);
|
287
|
|
- main_loop_hw();
|
288
|
|
-
|
289
|
|
- uint32_t now = to_ms_since_boot(get_absolute_time());
|
290
|
|
- if ((now - start_time) >= HTTP_DNS_TIMEOUT_MS) {
|
291
|
|
- break;
|
292
|
|
- }
|
293
|
|
- } while (err == ERR_INPROGRESS);
|
294
|
|
- if (err != ERR_OK) {
|
295
|
|
- debug("error getting IP for '%s'", bindadd);
|
296
|
|
- return false;
|
297
|
|
- } else {
|
298
|
|
- debug("IP %s for '%s'", ip4addr_ntoa(&ipaddr), bindadd);
|
299
|
|
- }
|
300
|
|
- }
|
301
|
|
-
|
302
|
|
- size_t idx = Con->SocketFD;
|
303
|
|
- err = tcp_bind(sock[idx].pcb,
|
304
|
|
- bindadd ? &ipaddr : IP_ANY_TYPE,
|
305
|
|
- PortNo);
|
306
|
|
- if (err != ERR_OK) {
|
307
|
|
- debug("error binding to '%s'", bindadd);
|
308
|
|
- return false;
|
309
|
|
- }
|
310
|
|
-
|
311
|
|
- struct tcp_pcb *tmp = tcp_listen(sock[idx].pcb);
|
312
|
|
- if (tmp == NULL) {
|
313
|
|
- debug("error listening on socket");
|
314
|
|
- return false;
|
315
|
|
- }
|
316
|
|
- sock[idx].pcb = tmp;
|
317
|
|
-
|
318
|
|
- tcp_accept(sock[idx].pcb, tcp_server_accept);
|
319
|
|
-
|
320
|
|
- return true;
|
321
|
|
-}
|
322
|
|
-
|
323
|
|
-bool SocketsCon_Accept(struct SocketCon *Con, struct SocketCon *NewCon) {
|
324
|
|
- if ((!Con) || (!NewCon)) {
|
325
|
|
- debug("invalid param");
|
326
|
|
- return false;
|
327
|
|
- }
|
328
|
|
-
|
329
|
|
- size_t idx = Con->SocketFD;
|
330
|
|
- size_t new_idx = NewCon->SocketFD;
|
331
|
|
-
|
332
|
|
- if (rb_len(&sock[idx].child_rb) <= 0) {
|
333
|
|
- return false;
|
334
|
|
- }
|
335
|
|
-
|
336
|
|
- debug("accepting new connection");
|
337
|
|
-
|
338
|
|
- struct tcp_pcb *new_pcb;
|
339
|
|
- rb_pop(&sock[idx].child_rb, &new_pcb);
|
340
|
|
-
|
341
|
|
- err_t err = tcp_close(sock[new_idx].pcb);
|
342
|
|
- if (err != ERR_OK) {
|
343
|
|
- debug("error closing prev new socket");
|
344
|
|
- }
|
345
|
|
-
|
346
|
|
- sock[new_idx].pcb = new_pcb;
|
347
|
|
- return true;
|
348
|
|
-}
|
349
|
|
-
|
350
|
|
-bool SocketsCon_HasError(struct SocketCon *Con) {
|
351
|
|
- if (!Con) {
|
352
|
|
- debug("invalid param");
|
353
|
|
- return true;
|
354
|
|
- }
|
355
|
|
- return Con->State == e_ConnectState_Error;
|
356
|
|
-}
|
357
|
|
-
|
358
|
|
-bool SocketsCon_IsConnected(struct SocketCon *Con) {
|
359
|
|
- if (!Con) {
|
360
|
|
- debug("invalid param");
|
361
|
|
- return false;
|
362
|
|
- }
|
363
|
|
- return Con->State == e_ConnectState_Connected;
|
364
|
|
-}
|
365
|
|
-
|
366
|
|
-int SocketsCon_GetLastErrNo(struct SocketCon *Con) {
|
367
|
|
- if (!Con) {
|
368
|
|
- debug("invalid param");
|
369
|
|
- return -1;
|
370
|
|
- }
|
371
|
|
- return Con->Last_errno;
|
372
|
|
-}
|
373
|
|
-
|
374
|
|
-e_ConnectErrorType SocketsCon_GetErrorCode(struct SocketCon *Con) {
|
375
|
|
- if (!Con) {
|
376
|
|
- debug("invalid param");
|
377
|
|
- return e_ConnectErrorMAX;
|
378
|
|
- }
|
379
|
|
- return Con->ErrorCode;
|
380
|
|
-}
|
381
|
|
-
|
382
|
|
-bool SocketsCon_GetSocketHandle(struct SocketCon *Con,
|
383
|
|
- t_ConSocketHandle *RetHandle) {
|
384
|
|
- if (!Con) {
|
385
|
|
- debug("invalid param");
|
386
|
|
- return false;
|
387
|
|
- }
|
388
|
|
-
|
389
|
|
- if (Con->SocketFD < 0) {
|
390
|
|
- return false;
|
391
|
|
- }
|
392
|
|
-
|
393
|
|
- *RetHandle = Con->SocketFD;
|
394
|
|
- return true;
|
395
|
|
-}
|