Select one of the symbols to view example projects that use it.
 
Outline
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
tcp_echoserver_pcb
tcp_echoserver_states
tcp_echoserver_struct
tcp_echoserver_init()
tcp_echoserver_accept(void *, struct tcp_pcb *, err_t)
tcp_echoserver_recv(void *, struct tcp_pcb *, struct pbuf *, err_t)
tcp_echoserver_error(void *, err_t)
tcp_echoserver_poll(void *, struct tcp_pcb *)
tcp_echoserver_sent(void *, struct tcp_pcb *, u16_t)
tcp_echoserver_send(struct tcp_pcb *, struct tcp_echoserver_struct *)
tcp_echoserver_connection_close(struct tcp_pcb *, struct tcp_echoserver_struct *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesLwIP_TCP_Echo_ServerSrc/tcp_echoserver.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of and a contribution to the lwIP TCP/IP stack. * * Credits go to Adam Dunkels (and the current maintainers) of this software. * * Christiaan Simons rewrote this file to get a more stable echo application. * **//* ... */ /* This file was modified by ST */ #include "lwip/debug.h" #include "lwip/stats.h" #include "lwip/tcp.h" #if LWIP_TCP static struct tcp_pcb *tcp_echoserver_pcb; /* ECHO protocol states */ enum tcp_echoserver_states { ES_NONE = 0, ES_ACCEPTED, ES_RECEIVED, ES_CLOSING ...}; /* structure for maintaining connection infos to be passed as argument to LwIP callbacks*//* ... */ struct tcp_echoserver_struct { u8_t state; /* current connection state */ u8_t retries; struct tcp_pcb *pcb; /* pointer on the current tcp_pcb */ struct pbuf *p; /* pointer on the received/to be transmitted pbuf */ ...}; 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); /** * @brief Initializes the tcp echo server * @param None * @retval None *//* ... */ void tcp_echoserver_init(void) { /* create new tcp pcb */ tcp_echoserver_pcb = tcp_new(); if (tcp_echoserver_pcb != NULL) { err_t err; /* bind echo_pcb to port 7 (ECHO protocol) */ err = tcp_bind(tcp_echoserver_pcb, IP_ADDR_ANY, 7); if (err == ERR_OK) { /* start tcp listening for echo_pcb */ tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb); /* initialize LwIP tcp_accept callback function */ tcp_accept(tcp_echoserver_pcb, tcp_echoserver_accept); }if (err == ERR_OK) { ... } else { /* deallocate the pcb */ memp_free(MEMP_TCP_PCB, tcp_echoserver_pcb); }else { ... } }if (tcp_echoserver_pcb != NULL) { ... } }{ ... } /** * @brief This function is the implementation of tcp_accept LwIP callback * @param arg: not used * @param newpcb: pointer on tcp_pcb struct for the newly created tcp connection * @param err: not used * @retval err_t: error status *//* ... */ 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); /* set priority for the newly accepted tcp connection newpcb */ tcp_setprio(newpcb, TCP_PRIO_MIN); /* allocate structure es to maintain tcp connection information */ 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; /* pass newly allocated es structure as argument to newpcb */ tcp_arg(newpcb, es); /* initialize lwip tcp_recv callback function for newpcb */ tcp_recv(newpcb, tcp_echoserver_recv); /* initialize lwip tcp_err callback function for newpcb */ tcp_err(newpcb, tcp_echoserver_error); /* initialize lwip tcp_poll callback function for newpcb */ tcp_poll(newpcb, tcp_echoserver_poll, 0); ret_err = ERR_OK; }if (es != NULL) { ... } else { /* close tcp connection */ tcp_echoserver_connection_close(newpcb, es); /* return memory error */ ret_err = ERR_MEM; }else { ... } return ret_err; }{ ... } /** * @brief This function is the implementation for tcp_recv LwIP callback * @param arg: pointer on a argument for the tcp_pcb connection * @param tpcb: pointer on the tcp_pcb connection * @param pbuf: pointer on the received pbuf * @param err: error information regarding the reveived pbuf * @retval err_t: error code *//* ... */ 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 we receive an empty tcp frame from client => close connection */ if (p == NULL) { /* remote host closed connection */ es->state = ES_CLOSING; if(es->p == NULL) { /* we're done sending, close connection */ tcp_echoserver_connection_close(tpcb, es); }if (es->p == NULL) { ... } else { /* we're not done yet */ /* acknowledge received packet */ tcp_sent(tpcb, tcp_echoserver_sent); /* send remaining data*/ tcp_echoserver_send(tpcb, es); }else { ... } ret_err = ERR_OK; }if (p == NULL) { ... } /* else : a non empty frame was received from client but for some reason err != ERR_OK */ else if(err != ERR_OK) { /* free received pbuf*/ 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) { /* first data chunk in p->payload */ es->state = ES_RECEIVED; /* store reference to incoming pbuf (chain) */ es->p = p; /* initialize LwIP tcp_sent callback function */ tcp_sent(tpcb, tcp_echoserver_sent); /* send back the received data (echo) */ tcp_echoserver_send(tpcb, es); ret_err = ERR_OK; }else if (es->state == ES_ACCEPTED) { ... } else if (es->state == ES_RECEIVED) { /* more data received from client and previous data has been already sent*/ if(es->p == NULL) { es->p = p; /* send back received data */ tcp_echoserver_send(tpcb, es); }if (es->p == NULL) { ... } else { struct pbuf *ptr; /* chain pbufs to the end of what we recv'ed previously */ ptr = es->p; pbuf_chain(ptr,p); }else { ... } ret_err = ERR_OK; }else if (es->state == ES_RECEIVED) { ... } else if(es->state == ES_CLOSING) { /* odd case, remote side closing twice, trash data */ tcp_recved(tpcb, p->tot_len); es->p = NULL; pbuf_free(p); ret_err = ERR_OK; }else if (es->state == ES_CLOSING) { ... } else { /* unknown es->state, trash data */ tcp_recved(tpcb, p->tot_len); es->p = NULL; pbuf_free(p); ret_err = ERR_OK; }else { ... } return ret_err; }{ ... } /** * @brief This function implements the tcp_err callback function (called * when a fatal tcp_connection error occurs. * @param arg: pointer on argument parameter * @param err: not used * @retval None *//* ... */ 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) { /* free es structure */ mem_free(es); }if (es != NULL) { ... } }{ ... } /** * @brief This function implements the tcp_poll LwIP callback function * @param arg: pointer on argument passed to callback * @param tpcb: pointer on the tcp_pcb for the current tcp connection * @retval err_t: error code *//* ... */ 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); /* there is a remaining pbuf (chain) , try to send data */ tcp_echoserver_send(tpcb, es); }if (es->p != NULL) { ... } else { /* no remaining pbuf (chain) */ if(es->state == ES_CLOSING) { /* close tcp connection */ tcp_echoserver_connection_close(tpcb, es); }if (es->state == ES_CLOSING) { ... } }else { ... } ret_err = ERR_OK; }if (es != NULL) { ... } else { /* nothing to be done */ tcp_abort(tpcb); ret_err = ERR_ABRT; }else { ... } return ret_err; }{ ... } /** * @brief This function implements the tcp_sent LwIP callback (called when ACK * is received from remote host for sent data) * @param None * @retval None *//* ... */ 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) { /* still got pbufs to send */ tcp_sent(tpcb, tcp_echoserver_sent); tcp_echoserver_send(tpcb, es); }if (es->p != NULL) { ... } else { /* if no more data to send and client closed connection*/ if(es->state == ES_CLOSING) tcp_echoserver_connection_close(tpcb, es); }else { ... } return ERR_OK; }{ ... } /** * @brief This function is used to send data for tcp connection * @param tpcb: pointer on the tcp_pcb connection * @param es: pointer on echo_state structure * @retval None *//* ... */ 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))) { /* get pointer on pbuf from es structure */ ptr = es->p; /* enqueue data for transmission */ wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1); if (wr_err == ERR_OK) { u16_t plen; u8_t freed; plen = ptr->len; /* continue with next pbuf in chain (if any) */ es->p = ptr->next; if(es->p != NULL) { /* increment reference count for es->p */ pbuf_ref(es->p); }if (es->p != NULL) { ... } /* chop first pbuf from chain */ do { /* try hard to free pbuf */ freed = pbuf_free(ptr); ...} while(freed == 0); /* we can read more data now */ tcp_recved(tpcb, plen); }if (wr_err == ERR_OK) { ... } else if(wr_err == ERR_MEM) { /* we are low on memory, try later / harder, defer to poll */ es->p = ptr; }else if (wr_err == ERR_MEM) { ... } else { /* other problem ?? */ }else { ... } }while ((wr_err == ERR_OK) && (es->p != NULL) && (es->p->len <= tcp_sndbuf(tpcb))) { ... } }{ ... } /** * @brief This functions closes the tcp connection * @param tcp_pcb: pointer on the tcp connection * @param es: pointer on echo_state structure * @retval None *//* ... */ static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es) { /* remove all callbacks */ tcp_arg(tpcb, NULL); tcp_sent(tpcb, NULL); tcp_recv(tpcb, NULL); tcp_err(tpcb, NULL); tcp_poll(tpcb, NULL, 0); /* delete es structure */ if (es != NULL) { mem_free(es); }if (es != NULL) { ... } /* close tcp connection */ tcp_close(tpcb); }{ ... } /* ... */#endif /* LWIP_TCP */