-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathlwipTcp.cpp
232 lines (198 loc) · 6.98 KB
/
lwipTcp.cpp
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
#include "lwipTcp.h"
#include "lwipClient.h"
#if LWIP_TCP
static err_t tcp_recv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err);
static err_t tcp_sent_callback(void* arg, struct tcp_pcb* tpcb, u16_t len);
static void tcp_err_callback(void* arg, err_t err);
/**
* @brief Function called when TCP connection established
* @param arg: user supplied argument
* @param tpcb: pointer on the connection control block
* @param err: when connection correctly established err should be ERR_OK
* @retval err_t: returned error
*/
err_t tcp_connected_callback(void* arg, struct tcp_pcb* tpcb, err_t err)
{
struct tcp_struct* tcp_arg = (struct tcp_struct*)arg;
if (err == ERR_OK) {
if ((tcp_arg != NULL) && (tcp_arg->pcb == tpcb)) {
tcp_arg->state = TCP_CONNECTED;
/* initialize LwIP tcp_recv callback function */
tcp_recv(tpcb, tcp_recv_callback);
/* initialize LwIP tcp_sent callback function */
tcp_sent(tpcb, tcp_sent_callback);
/* initialize LwIP tcp_err callback function */
tcp_err(tpcb, tcp_err_callback);
return ERR_OK;
} else {
/* close connection */
tcp_connection_close(tpcb, tcp_arg);
return ERR_ARG;
}
} else {
/* close connection */
tcp_connection_close(tpcb, tcp_arg);
}
return err;
}
/**
* @brief This function is the implementation of tcp_accept LwIP callback
* @param arg user supplied argument
* @param newpcb: pointer on tcp_pcb struct for the newly created tcp connection
* @param err: when connection correctly established err should be ERR_OK
* @retval err_t: error status
*/
err_t tcp_accept_callback(void* arg, struct tcp_pcb* newpcb, err_t err)
{
err_t ret_err;
uint8_t accepted;
lwipClient* tcpClient = (lwipClient*)arg;
/* set priority for the newly accepted tcp connection newpcb */
tcp_setprio(newpcb, TCP_PRIO_MIN);
if ((tcpClient != NULL) && (ERR_OK == err)) {
struct tcp_struct* client = (struct tcp_struct*)mem_malloc(sizeof(struct tcp_struct));
if (client != NULL) {
client->state = TCP_ACCEPTED;
client->pcb = newpcb;
client->data.p = NULL;
client->data.available = 0;
/* Looking for an empty socket */
for (uint16_t i = 0; i < MAX_CLIENT; i++) {
if (!tcpClient[i]) {
tcpClient[i] = lwipClient(client);
accepted = 1;
break;
}
}
if (accepted) {
/* pass newly allocated client structure as argument to newpcb */
tcp_arg(newpcb, client);
/* initialize lwip tcp_recv callback function for newpcb */
tcp_recv(newpcb, tcp_recv_callback);
/* initialize lwip tcp_err callback function for newpcb */
tcp_err(newpcb, tcp_err_callback);
/* initialize LwIP tcp_sent callback function */
tcp_sent(newpcb, tcp_sent_callback);
ret_err = ERR_OK;
} else {
/* close tcp connection */
tcp_connection_close(newpcb, client);
mem_free(client);
/* return memory error */
ret_err = ERR_MEM;
}
} else {
/* close tcp connection */
tcp_connection_close(newpcb, client);
mem_free(client);
/* return memory error */
ret_err = ERR_MEM;
}
} else {
tcp_close(newpcb);
ret_err = ERR_ARG;
}
return ret_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_recv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err)
{
struct tcp_struct* tcp_arg = (struct tcp_struct*)arg;
err_t ret_err;
/* if we receive an empty tcp frame from server => close connection */
if (p == NULL) {
/* we're done sending, close connection */
tcp_connection_close(tpcb, tcp_arg);
ret_err = ERR_OK;
}
/* 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);
}
ret_err = err;
} else if ((tcp_arg->state == TCP_CONNECTED) || (tcp_arg->state == TCP_ACCEPTED)) {
/* Acknowledge data reception */
tcp_recved(tpcb, p->tot_len);
if (tcp_arg->data.p == NULL) {
tcp_arg->data.p = p;
} else {
pbuf_chain(tcp_arg->data.p, p);
}
tcp_arg->data.available += p->len;
ret_err = ERR_OK;
}
/* 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;
}
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_sent_callback(void* arg, struct tcp_pcb* tpcb, u16_t len)
{
struct tcp_struct* tcp_arg = (struct tcp_struct*)arg;
LWIP_UNUSED_ARG(len);
if ((tcp_arg != NULL) && (tcp_arg->pcb == tpcb)) {
return ERR_OK;
}
return ERR_ARG;
}
/** Function prototype for tcp error callback functions. Called when the pcb
* receives a RST or is unexpectedly closed for any other reason.
*
* @note The corresponding pcb is already freed when this callback is called!
*
* @param arg Additional argument to pass to the callback function (@see tcp_arg())
* @param err Error code to indicate why the pcb has been closed
* ERR_ABRT: aborted through tcp_abort or by a TCP timer
* ERR_RST: the connection was reset by the remote host
*/
static void tcp_err_callback(void* arg, err_t err)
{
struct tcp_struct* tcp_arg = (struct tcp_struct*)arg;
if (tcp_arg != NULL) {
if (ERR_OK != err) {
tcp_arg->pcb = NULL;
tcp_arg->state = TCP_CLOSING;
}
}
}
/**
* @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
*/
void tcp_connection_close(struct tcp_pcb* tpcb, struct tcp_struct* tcp)
{
/* remove callbacks */
tcp_recv(tpcb, NULL);
tcp_sent(tpcb, NULL);
tcp_poll(tpcb, NULL, 0);
tcp_err(tpcb, NULL);
tcp_accept(tpcb, NULL);
/* close tcp connection */
tcp_close(tpcb);
tcp->pcb = NULL;
tcp->state = TCP_CLOSING;
}
#endif /* LWIP_TCP */