Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "lwip/memp.h"
#include <stdio.h>
#include <string.h>
Private variables
recev_buf
message_count
data
echoclient_pcb
echoclient_states
echoclient
Private function prototypes
Private functions
tcp_echoclient_connect()
tcp_echoclient_connected(void *, struct tcp_pcb *, err_t)
tcp_echoclient_recv(void *, struct tcp_pcb *, struct pbuf *, err_t)
tcp_echoclient_send(struct tcp_pcb *, struct echoclient *)
tcp_echoclient_poll(void *, struct tcp_pcb *)
tcp_echoclient_sent(void *, struct tcp_pcb *, u16_t)
tcp_echoclient_connection_close(struct tcp_pcb *, struct echoclient *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesLwIP_TCP_Echo_ClientSrc/tcp_echoclient.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file LwIP/LwIP_TCP_Echo_Client/Src/tcp_echoclient.c * @author MCD Application Team * @brief tcp echoclient application using LwIP RAW API ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "lwip/debug.h" #include "lwip/stats.h" #include "lwip/tcp.h" #include "lwip/memp.h" #include <stdio.h> #include <string.h> 7 includes #if LWIP_TCPIncludes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ u8_t recev_buf[50]; __IO uint32_t message_count=0; u8_t data[100]; struct tcp_pcb *echoclient_pcb; /* ECHO protocol states */ enum echoclient_states { ES_NOT_CONNECTED = 0, ES_CONNECTED, ES_RECEIVED, ES_CLOSING, ...}; /* structure to be passed as argument to the tcp callbacks */ struct echoclient { enum echoclient_states state; /* connection status */ struct tcp_pcb *pcb; /* pointer on the current tcp_pcb */ struct pbuf *p_tx; /* pointer on pbuf to be transmitted */ ...}; Private variables /* Private function prototypes -----------------------------------------------*/ static err_t tcp_echoclient_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err); static void tcp_echoclient_connection_close(struct tcp_pcb *tpcb, struct echoclient * es); static err_t tcp_echoclient_poll(void *arg, struct tcp_pcb *tpcb); static err_t tcp_echoclient_sent(void *arg, struct tcp_pcb *tpcb, u16_t len); static void tcp_echoclient_send(struct tcp_pcb *tpcb, struct echoclient * es); static err_t tcp_echoclient_connected(void *arg, struct tcp_pcb *tpcb, err_t err); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Connects to the TCP echo server * @param None * @retval None *//* ... */ void tcp_echoclient_connect(void) { ip_addr_t DestIPaddr; /* create new tcp pcb */ echoclient_pcb = tcp_new(); if (echoclient_pcb != NULL) { IP4_ADDR( &DestIPaddr, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 ); /* connect to destination address/port */ tcp_connect(echoclient_pcb,&DestIPaddr,DEST_PORT,tcp_echoclient_connected); }if (echoclient_pcb != NULL) { ... } }{ ... } /** * @brief Function called when TCP connection established * @param tpcb: pointer on the connection control block * @param err: when connection correctly established err should be ERR_OK * @retval err_t: returned error *//* ... */ static err_t tcp_echoclient_connected(void *arg, struct tcp_pcb *tpcb, err_t err) { struct echoclient *es = NULL; if (err == ERR_OK) { /* allocate structure es to maintain tcp connection information */ es = (struct echoclient *)mem_malloc(sizeof(struct echoclient)); if (es != NULL) { es->state = ES_CONNECTED; es->pcb = tpcb; sprintf((char*)data, "sending tcp client message %d", (int)message_count); /* allocate pbuf */ es->p_tx = pbuf_alloc(PBUF_TRANSPORT, strlen((char*)data) , PBUF_POOL); if (es->p_tx) { /* copy data to pbuf */ pbuf_take(es->p_tx, (char*)data, strlen((char*)data)); /* pass newly allocated es structure as argument to tpcb */ tcp_arg(tpcb, es); /* initialize LwIP tcp_recv callback function */ tcp_recv(tpcb, tcp_echoclient_recv); /* initialize LwIP tcp_sent callback function */ tcp_sent(tpcb, tcp_echoclient_sent); /* initialize LwIP tcp_poll callback function */ tcp_poll(tpcb, tcp_echoclient_poll, 1); /* send data */ tcp_echoclient_send(tpcb,es); return ERR_OK; }if (es->p_tx) { ... } }if (es != NULL) { ... } else { /* close connection */ tcp_echoclient_connection_close(tpcb, es); /* return memory allocation error */ return ERR_MEM; }else { ... } }if (err == ERR_OK) { ... } else { /* close connection */ tcp_echoclient_connection_close(tpcb, es); }else { ... } return err; }{ ... } /** * @brief tcp_receiv callback * @param arg: argument to be passed to receive callback * @param tpcb: tcp connection control block * @param err: receive error code * @retval err_t: returned error *//* ... */ static err_t tcp_echoclient_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { struct echoclient *es; err_t ret_err; LWIP_ASSERT("arg != NULL",arg != NULL); es = (struct echoclient *)arg; /* if we receive an empty tcp frame from server => close connection */ if (p == NULL) { /* remote host closed connection */ es->state = ES_CLOSING; if(es->p_tx == NULL) { /* we're done sending, close connection */ tcp_echoclient_connection_close(tpcb, es); }if (es->p_tx == NULL) { ... } else { /* send remaining data*/ tcp_echoclient_send(tpcb, es); }else { ... } ret_err = ERR_OK; }if (p == NULL) { ... } /* else : a non empty frame was received from echo server but for some reason err != ERR_OK */ else if(err != ERR_OK) { /* free received pbuf*/ if (p != NULL) { pbuf_free(p); }if (p != NULL) { ... } ret_err = err; }else if (err != ERR_OK) { ... } else if(es->state == ES_CONNECTED) { /* increment message count */ message_count++; /* Acknowledge data reception */ tcp_recved(tpcb, p->tot_len); pbuf_free(p); tcp_echoclient_connection_close(tpcb, es); ret_err = ERR_OK; }else if (es->state == ES_CONNECTED) { ... } /* data received when connection already closed */ else { /* Acknowledge data reception */ tcp_recved(tpcb, p->tot_len); /* free pbuf and do nothing */ pbuf_free(p); ret_err = ERR_OK; }else { ... } return ret_err; }{ ... } /** * @brief function used to send data * @param tpcb: tcp control block * @param es: pointer on structure of type echoclient containing info on data * to be sent * @retval None *//* ... */ static void tcp_echoclient_send(struct tcp_pcb *tpcb, struct echoclient * es) { struct pbuf *ptr; err_t wr_err = ERR_OK; while ((wr_err == ERR_OK) && (es->p_tx != NULL) && (es->p_tx->len <= tcp_sndbuf(tpcb))) { /* get pointer on pbuf from es structure */ ptr = es->p_tx; /* enqueue data for transmission */ wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1); if (wr_err == ERR_OK) { /* continue with next pbuf in chain (if any) */ es->p_tx = ptr->next; if(es->p_tx != NULL) { /* increment reference count for es->p */ pbuf_ref(es->p_tx); }if (es->p_tx != NULL) { ... } /* free pbuf: will free pbufs up to es->p (because es->p has a reference count > 0) */ pbuf_free(ptr); }if (wr_err == ERR_OK) { ... } else if(wr_err == ERR_MEM) { /* we are low on memory, try later, defer to poll */ es->p_tx = ptr; }else if (wr_err == ERR_MEM) { ... } else { /* other problem ?? */ }else { ... } }while ((wr_err == ERR_OK) && (es->p_tx != NULL) && (es->p_tx->len <= tcp_sndbuf(tpcb))) { ... } }{ ... } /** * @brief This function implements the tcp_poll callback function * @param arg: pointer on argument passed to callback * @param tpcb: tcp connection control block * @retval err_t: error code *//* ... */ static err_t tcp_echoclient_poll(void *arg, struct tcp_pcb *tpcb) { err_t ret_err; struct echoclient *es; es = (struct echoclient*)arg; if (es != NULL) { if (es->p_tx != NULL) { /* there is a remaining pbuf (chain) , try to send data */ tcp_echoclient_send(tpcb, es); }if (es->p_tx != NULL) { ... } else { /* no remaining pbuf (chain) */ if(es->state == ES_CLOSING) { /* close tcp connection */ tcp_echoclient_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 arg: pointer on argument passed to callback * @param tcp_pcb: tcp connection control block * @param len: length of data sent * @retval err_t: returned error code *//* ... */ static err_t tcp_echoclient_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) { struct echoclient *es; LWIP_UNUSED_ARG(len); es = (struct echoclient *)arg; if(es->p_tx != NULL) { /* still got pbufs to send */ tcp_echoclient_send(tpcb, es); }if (es->p_tx != NULL) { ... } return ERR_OK; }{ ... } /** * @brief This function is used to close the tcp connection with server * @param tpcb: tcp connection control block * @param es: pointer on echoclient structure * @retval None *//* ... */ static void tcp_echoclient_connection_close(struct tcp_pcb *tpcb, struct echoclient * es ) { /* remove callbacks */ tcp_recv(tpcb, NULL); tcp_sent(tpcb, NULL); tcp_poll(tpcb, NULL,0); if (es != NULL) { mem_free(es); }if (es != NULL) { ... } /* close tcp connection */ tcp_close(tpcb); }{ ... } Private functions#endif/* ... */ /* LWIP_TCP */