-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathlwipClient.cpp
266 lines (228 loc) · 8.27 KB
/
lwipClient.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
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
extern "C" {
#include "string.h"
}
#include "Arduino.h"
#include "lwipClient.h"
/* -------------------------------------------------------------------------- */
lwipClient::lwipClient()
{
}
/* -------------------------------------------------------------------------- */
/* Deprecated constructor. Keeps compatibility with W5100 architecture
sketches but sock is ignored. */
/* -------------------------------------------------------------------------- */
lwipClient::lwipClient(uint8_t sock)
{
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
lwipClient::lwipClient(struct tcp_struct* tcpClient)
{
if (tcpClient == NULL)
return;
_tcp_client.reset(tcpClient,
[](struct tcp_struct *tcp_client) { (void) tcp_client; } ); // empty deleter
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int lwipClient::connect(const char* host, uint16_t port)
{
/* -------------------------------------------------------------------------- */
IPAddress remote_addr;
int ret = CLwipIf::getInstance().getHostByName(host, remote_addr);
if (ret == 1) {
return connect(remote_addr, port);
} else {
return 0;
}
}
/* -------------------------------------------------------------------------- */
int lwipClient::connect(IPAddress ip, uint16_t port)
{
/* -------------------------------------------------------------------------- */
if (_tcp_client == NULL) {
/* Allocates memory for client */
_tcp_client.reset((struct tcp_struct*)mem_malloc(sizeof(struct tcp_struct)),
[](struct tcp_struct *tcp_client) { mem_free(tcp_client); } );
if (_tcp_client == NULL) {
return 0;
}
}
/* Creates a new TCP protocol control block */
_tcp_client->pcb = tcp_new();
if (_tcp_client->pcb == NULL) {
return 0;
}
_tcp_client->data.p = NULL;
_tcp_client->data.available = 0;
_tcp_client->state = TCP_NONE;
uint32_t startTime = millis();
ip_addr_t ipaddr;
tcp_arg(_tcp_client->pcb, _tcp_client.get());
if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) {
stop();
return 0;
}
startTime = millis();
while (_tcp_client->state == TCP_NONE) {
CLwipIf::getInstance().lwip_task();
if ((_tcp_client->state == TCP_CLOSING) || ((millis() - startTime) >= _timeout)) {
stop();
return 0;
}
}
return 1;
}
/* -------------------------------------------------------------------------- */
size_t lwipClient::write(uint8_t b)
{
/* -------------------------------------------------------------------------- */
return write(&b, 1);
}
/* -------------------------------------------------------------------------- */
size_t lwipClient::write(const uint8_t* buf, size_t size)
{
/* -------------------------------------------------------------------------- */
if ((_tcp_client == NULL) || (_tcp_client->pcb == NULL) || (buf == NULL) || (size == 0)) {
return 0;
}
/* If client not connected or accepted, it can't write because connection is
not ready */
if ((_tcp_client->state != TCP_ACCEPTED) && (_tcp_client->state != TCP_CONNECTED)) {
return 0;
}
size_t max_send_size, bytes_to_send;
size_t bytes_sent = 0;
size_t bytes_left = size;
err_t res;
do {
max_send_size = tcp_sndbuf(_tcp_client->pcb);
bytes_to_send = bytes_left > max_send_size ? max_send_size : bytes_left;
if (bytes_to_send > 0) {
res = tcp_write(_tcp_client->pcb, &buf[bytes_sent], bytes_to_send, TCP_WRITE_FLAG_COPY);
if (res == ERR_OK) {
bytes_sent += bytes_to_send;
bytes_left = size - bytes_sent;
} else if (res != ERR_MEM) {
// other error, cannot continue
return 0;
}
}
// Force to send data right now!
if (ERR_OK != tcp_output(_tcp_client->pcb)) {
return 0;
}
CLwipIf::getInstance().lwip_task();
} while (bytes_sent != size);
return size;
}
/* -------------------------------------------------------------------------- */
int lwipClient::available()
{
/* -------------------------------------------------------------------------- */
CLwipIf::getInstance().lwip_task();
if (_tcp_client != NULL) {
return _tcp_client->data.available;
}
return 0;
}
/* -------------------------------------------------------------------------- */
int lwipClient::read()
{
/* -------------------------------------------------------------------------- */
uint8_t b;
if ((_tcp_client != NULL) && (_tcp_client->data.p != NULL)) {
__disable_irq();
pbuffer_get_data(&(_tcp_client->data), &b, 1);
__enable_irq();
return b;
}
// No data available
return -1;
}
/* -------------------------------------------------------------------------- */
int lwipClient::read(uint8_t* buf, size_t size)
{
/* -------------------------------------------------------------------------- */
if ((_tcp_client != NULL) && (_tcp_client->data.p != NULL)) {
__disable_irq();
int rv = pbuffer_get_data(&(_tcp_client->data), buf, size);
__enable_irq();
return rv;
}
return -1;
}
/* -------------------------------------------------------------------------- */
int lwipClient::peek()
{
/* -------------------------------------------------------------------------- */
uint8_t b;
// Unlike recv, peek doesn't check to see if there's any data available, so we must
if (!available()) {
return -1;
}
__disable_irq();
b = pbuf_get_at(_tcp_client->data.p, 0);
__enable_irq();
return b;
}
/* -------------------------------------------------------------------------- */
void lwipClient::flush()
{
/* -------------------------------------------------------------------------- */
if ((_tcp_client == NULL) || (_tcp_client->pcb == NULL)) {
return;
}
tcp_output(_tcp_client->pcb);
CLwipIf::getInstance().lwip_task();
}
/* -------------------------------------------------------------------------- */
void lwipClient::stop()
{
/* -------------------------------------------------------------------------- */
if (_tcp_client == NULL) {
return;
}
// close tcp connection if not closed yet
if (status() != TCP_CLOSING) {
tcp_connection_close(_tcp_client->pcb, _tcp_client.get());
}
}
/* -------------------------------------------------------------------------- */
uint8_t lwipClient::connected()
{
/* -------------------------------------------------------------------------- */
uint8_t s = status();
return ((available() && (s == TCP_CLOSING)) || (s == TCP_CONNECTED) || (s == TCP_ACCEPTED));
}
/* -------------------------------------------------------------------------- */
uint8_t lwipClient::status()
{
if (_tcp_client == NULL) {
return TCP_NONE;
}
return _tcp_client->state;
}
// the next function allows us to use the client returned by
// EthernetServer::available() as the condition in an if-statement.
/* -------------------------------------------------------------------------- */
lwipClient::operator bool()
{
/* -------------------------------------------------------------------------- */
return (_tcp_client && (_tcp_client->state != TCP_CLOSING));
}
/* -------------------------------------------------------------------------- */
bool lwipClient::operator==(const lwipClient& rhs)
{
/* -------------------------------------------------------------------------- */
return _tcp_client == rhs._tcp_client && _tcp_client->pcb == rhs._tcp_client->pcb;
}
/* This function is not a function defined by Arduino. This is a function
specific to the W5100 architecture. To keep the compatibility we leave it and
returns always 0. */
/* -------------------------------------------------------------------------- */
uint8_t lwipClient::getSocketNumber()
{
/* -------------------------------------------------------------------------- */
return 0;
}