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
54
55
57
58
64
65
66
67
68
69
70
71
72
73
74
75
80
81
82
83
84
85
86
87
88
89
90
91
92
100
105
106
107
108
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
271
272
273
274
275
276
277
278
283
284
285
291
292
293
294
295
296
297
316
322
323
324
325
331
332
333
334
335
336
337
338
339
340
346
352
353
354
355
356
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
393
394
395
396
400
401
402
403
404
409
413
414
415
416
422
423
424
425
426
427
428
429
430
431
432
433
437
438
439
440
441
442
/* ... */
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#if LWIP_TCP
static struct tcp_pcb *tcp_echoserver_pcb;
enum tcp_echoserver_states
{
ES_NONE = 0,
ES_ACCEPTED,
ES_RECEIVED,
ES_CLOSING
...};
/* ... */
struct tcp_echoserver_struct
{
u8_t state;
u8_t retries;
struct tcp_pcb *pcb;
struct pbuf *p;
...};
static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
static void tcp_echoserver_error(void *arg, err_t err);
static err_t tcp_echoserver_poll(void *arg, struct tcp_pcb *tpcb);
static err_t tcp_echoserver_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
static void tcp_echoserver_send(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es);
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es);
/* ... */
void tcp_echoserver_init(void)
{
tcp_echoserver_pcb = tcp_new();
if (tcp_echoserver_pcb != NULL)
{
err_t err;
err = tcp_bind(tcp_echoserver_pcb, IP_ADDR_ANY, 7);
if (err == ERR_OK)
{
tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb);
tcp_accept(tcp_echoserver_pcb, tcp_echoserver_accept);
}if (err == ERR_OK) { ... }
else
{
memp_free(MEMP_TCP_PCB, tcp_echoserver_pcb);
}else { ... }
}if (tcp_echoserver_pcb != NULL) { ... }
}{ ... }
/* ... */
static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
err_t ret_err;
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(err);
tcp_setprio(newpcb, TCP_PRIO_MIN);
es = (struct tcp_echoserver_struct *)mem_malloc(sizeof(struct tcp_echoserver_struct));
if (es != NULL)
{
es->state = ES_ACCEPTED;
es->pcb = newpcb;
es->retries = 0;
es->p = NULL;
tcp_arg(newpcb, es);
tcp_recv(newpcb, tcp_echoserver_recv);
tcp_err(newpcb, tcp_echoserver_error);
tcp_poll(newpcb, tcp_echoserver_poll, 0);
ret_err = ERR_OK;
}if (es != NULL) { ... }
else
{
tcp_echoserver_connection_close(newpcb, es);
ret_err = ERR_MEM;
}else { ... }
return ret_err;
}{ ... }
/* ... */
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
struct tcp_echoserver_struct *es;
err_t ret_err;
LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct tcp_echoserver_struct *)arg;
if (p == NULL)
{
es->state = ES_CLOSING;
if(es->p == NULL)
{
tcp_echoserver_connection_close(tpcb, es);
}if (es->p == NULL) { ... }
else
{
tcp_sent(tpcb, tcp_echoserver_sent);
tcp_echoserver_send(tpcb, es);
}else { ... }
ret_err = ERR_OK;
}if (p == NULL) { ... }
else if(err != ERR_OK)
{
if (p != NULL)
{
es->p = NULL;
pbuf_free(p);
}if (p != NULL) { ... }
ret_err = err;
}else if (err != ERR_OK) { ... }
else if(es->state == ES_ACCEPTED)
{
es->state = ES_RECEIVED;
es->p = p;
tcp_sent(tpcb, tcp_echoserver_sent);
tcp_echoserver_send(tpcb, es);
ret_err = ERR_OK;
}else if (es->state == ES_ACCEPTED) { ... }
else if (es->state == ES_RECEIVED)
{
if(es->p == NULL)
{
es->p = p;
tcp_echoserver_send(tpcb, es);
}if (es->p == NULL) { ... }
else
{
struct pbuf *ptr;
ptr = es->p;
pbuf_chain(ptr,p);
}else { ... }
ret_err = ERR_OK;
}else if (es->state == ES_RECEIVED) { ... }
else if(es->state == ES_CLOSING)
{
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}else if (es->state == ES_CLOSING) { ... }
else
{
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}else { ... }
return ret_err;
}{ ... }
/* ... */
static void tcp_echoserver_error(void *arg, err_t err)
{
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(err);
es = (struct tcp_echoserver_struct *)arg;
if (es != NULL)
{
mem_free(es);
}if (es != NULL) { ... }
}{ ... }
/* ... */
static err_t tcp_echoserver_poll(void *arg, struct tcp_pcb *tpcb)
{
err_t ret_err;
struct tcp_echoserver_struct *es;
es = (struct tcp_echoserver_struct *)arg;
if (es != NULL)
{
if (es->p != NULL)
{
tcp_sent(tpcb, tcp_echoserver_sent);
tcp_echoserver_send(tpcb, es);
}if (es->p != NULL) { ... }
else
{
if(es->state == ES_CLOSING)
{
tcp_echoserver_connection_close(tpcb, es);
}if (es->state == ES_CLOSING) { ... }
}else { ... }
ret_err = ERR_OK;
}if (es != NULL) { ... }
else
{
tcp_abort(tpcb);
ret_err = ERR_ABRT;
}else { ... }
return ret_err;
}{ ... }
/* ... */
static err_t tcp_echoserver_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(len);
es = (struct tcp_echoserver_struct *)arg;
es->retries = 0;
if(es->p != NULL)
{
tcp_sent(tpcb, tcp_echoserver_sent);
tcp_echoserver_send(tpcb, es);
}if (es->p != NULL) { ... }
else
{
if(es->state == ES_CLOSING)
tcp_echoserver_connection_close(tpcb, es);
}else { ... }
return ERR_OK;
}{ ... }
/* ... */
static void tcp_echoserver_send(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es)
{
struct pbuf *ptr;
err_t wr_err = ERR_OK;
while ((wr_err == ERR_OK) &&
(es->p != NULL) &&
(es->p->len <= tcp_sndbuf(tpcb)))
{
ptr = es->p;
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);
if (wr_err == ERR_OK)
{
u16_t plen;
u8_t freed;
plen = ptr->len;
es->p = ptr->next;
if(es->p != NULL)
{
pbuf_ref(es->p);
}if (es->p != NULL) { ... }
do
{
freed = pbuf_free(ptr);
...}
while(freed == 0);
tcp_recved(tpcb, plen);
}if (wr_err == ERR_OK) { ... }
else if(wr_err == ERR_MEM)
{
es->p = ptr;
}else if (wr_err == ERR_MEM) { ... }
else
{
}else { ... }
}while ((wr_err == ERR_OK) && (es->p != NULL) && (es->p->len <= tcp_sndbuf(tpcb))) { ... }
}{ ... }
/* ... */
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es)
{
tcp_arg(tpcb, NULL);
tcp_sent(tpcb, NULL);
tcp_recv(tpcb, NULL);
tcp_err(tpcb, NULL);
tcp_poll(tpcb, NULL, 0);
if (es != NULL)
{
mem_free(es);
}if (es != NULL) { ... }
tcp_close(tpcb);
}{ ... }
/* ... */#endif