-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientProxy.cpp
268 lines (215 loc) · 7.16 KB
/
ClientProxy.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
267
////////////////////////////////////////////////////////////////////////////////
//
// ClientProxy.cpp - Definition of HTTP Client Proxy
//
// ----------------------
//
// This file is free software; you can redistribute it and/or modify
// it under the terms of either the GNU General Public License version 2
// or the GNU Lesser General Public License version 2.1, both as
// published by the Free Software Foundation.
//
////////////////////////////////////////////////////////////////////////////////
#include <Arduino.h>
#include "ClientProxy.h"
#include "utility/W5100.h"
///////////////////////////////////////////////////////////////////////////////
ClientProxy::ClientProxy()
: my_sn(W5100::socket_undefined)
{}
ClientProxy::~ClientProxy()
{}
///////////////////////////////////////////////////////////////////////////////
void ClientProxy::setConnection(W5100::socket_e the_sn)
{ my_sn = the_sn; }
bool ClientProxy::closeConnection()
{
if (!prv_isValidSn()) return true;
while (!W5100::isClosed(my_sn))
if (W5100::close(my_sn) != W5100::rc_ok)
return false;
my_sn = W5100::socket_undefined;
return true;
}
bool ClientProxy::isConnected() const
{
if (!prv_isValidSn()) return false;
return W5100::isConnected(my_sn);
}
void ClientProxy::triggerConnTimeout()
{ my_connIdleStart = millis(); }
bool ClientProxy::connTimeoutExpired() const
{ return millis() - my_connIdleStart > 5000; }
///////////////////////////////////////////////////////////////////////////////
W5100::socket_e ClientProxy::socket() const
{ return my_sn; }
uint16_t ClientProxy::localPort() const
{ return (prv_isValidSn() ? W5100::read_Sn_R16(my_sn, W5100_Sn_PORT) : 0); }
uint16_t ClientProxy::remotePort() const
{ return (prv_isValidSn() ? W5100::read_Sn_R16(my_sn, W5100_Sn_DPORT) : 0); }
IPAddress ClientProxy::remoteIpAddr() const
{
W5100::ipv4_address_t ipAddr = prv_isValidSn() ? W5100::ipv4_address_t(my_sn) : W5100::ipv4_address_t(0,0,0,0);
return IPAddress(ipAddr.ip0(), ipAddr.ip1(), ipAddr.ip2(), ipAddr.ip3());
}
W5100::mac_address_t ClientProxy::remoteMacAddr() const
{ return (prv_isValidSn() ? W5100::mac_address_t(my_sn) : W5100::mac_address_t(0,0,0,0,0,0)); }
///////////////////////////////////////////////////////////////////////////////
bool ClientProxy::readByte(uint8_t& the_byte)
{
if (!prv_isValidSn()) return false;
if (my_unreadByteAvail)
{
my_unreadByteAvail = false;
the_byte = my_unreadByte;
++my_totRead;
return true;;
}
if (W5100::waitReceivePending(my_sn) != W5100::rc_ok)
{
closeConnection();
return false;
}
uint16_t uRead = W5100::receive(my_sn, &the_byte, 1);
my_totRead += uRead;
return (uRead == 1);
}
uint16_t ClientProxy::readBuffer(uint8_t* the_buffer, uint16_t the_size)
{
if (!prv_isValidSn()) return 0;
if (!the_buffer || !the_size) return 0;
uint16_t uRead = 0;
if (my_unreadByteAvail)
{
my_unreadByteAvail = false;
*the_buffer = my_unreadByte;
the_buffer++;
uRead++;
the_size--;
}
if (W5100::waitReceivePending(my_sn) != W5100::rc_ok)
{ my_totRead += uRead; closeConnection(); return uRead; }
uRead += W5100::receive(my_sn, the_buffer, the_size);
my_totRead += uRead;
return uRead;
}
bool ClientProxy::unreadByte(uint8_t the_byte)
{
if (!prv_isValidSn()) return false;
if (my_unreadByteAvail) return false;
my_unreadByte = the_byte;
my_unreadByteAvail = true;
--my_totRead;
return true;
}
bool ClientProxy::peekByte(uint8_t& the_byte)
{ return (readByte(the_byte) && unreadByte(the_byte)); }
bool ClientProxy::anyDataReceived() const
{
if (!prv_isValidSn()) return false;
return (W5100::checkReceivePending(my_sn) == W5100::rc_ok);
}
///////////////////////////////////////////////////////////////////////////////
bool ClientProxy::skipAllCRLF()
{
uint8_t ch;
while (readByte(ch))
if ((ch != '\r') && (ch != '\n'))
return unreadByte(ch);
return false;
}
///////////////////////////////////////////////////////////////////////////////
bool ClientProxy::skipAllLWS()
{
uint8_t ch;
while (readByte(ch))
if ((ch != ' ') && (ch != '\t'))
return unreadByte(ch);
return false;
}
///////////////////////////////////////////////////////////////////////////////
bool ClientProxy::skipToNextLine()
{
// Ignore everything up to CRLF, but do not consume CRLF.
// CRLF is considered as the beginning of next line. Although weird, this approach
// helps a lot in recognizing field boundaries in form/multipart content types
uint8_t ch;
while (readByte(ch))
if ((ch == '\r') || (ch == '\n'))
return unreadByte(ch);
return false;
}
///////////////////////////////////////////////////////////////////////////////
bool ClientProxy::readCRLF()
{
uint8_t ch;
if (!readByte(ch)) return false;
if (ch != '\r') { unreadByte(ch); return false; }
if (!readByte(ch)) return false;
if (ch != '\n') { unreadByte(ch); return false; }
return true;
}
///////////////////////////////////////////////////////////////////////////////
uint16_t ClientProxy::readToken(char * the_buffer, uint16_t the_bufferLen)
{
if (!the_buffer) return 0;
if (!the_bufferLen) return 0;
uint16_t u = 0;
for (; u < the_bufferLen-1; ++u)
{
uint8_t ch;
if (!readByte(ch)) break;
if ((ch == ' ') || (ch == '\r') || (ch == '\n')) { unreadByte(ch); break; }
the_buffer[u] = ch;
}
the_buffer[u] = 0;
return u;
}
///////////////////////////////////////////////////////////////////////////////
uint16_t ClientProxy::readToEOL(char * the_buffer, uint16_t the_bufferLen)
{
// This function reads until one of the following conditions is met:
// - there are no more chars available
// - the buffer length is exceeded
// - a CR char is met (but not consumed) not at the beginning of line
if (!the_buffer) return 0;
if (!the_bufferLen) return 0;
uint16_t u = 0;
for (; u < the_bufferLen-1; ++u)
{
uint8_t ch;
if (!readByte(ch)) break;
if ((ch == '\r') && (u > 0)) { unreadByte(ch); break; }
the_buffer[u] = ch;
}
the_buffer[u] = 0;
return u;
}
///////////////////////////////////////////////////////////////////////////////
bool ClientProxy::writeByte(uint8_t the_byte)
{
if (!prv_isValidSn()) return false;
if (!W5100::canTransmitData(my_sn)) { closeConnection(); return false; }
if (W5100::send(my_sn, &the_byte, 1) != 1) return false;
if (W5100::waitSendCompleted(my_sn) != W5100::rc_ok) { closeConnection(); return false; }
++my_totWrite;
return true;
}
uint16_t ClientProxy::writeBuffer(uint8_t * the_buffer, uint16_t the_size)
{
if (!prv_isValidSn()) return 0;
if (!W5100::canTransmitData(my_sn)) { closeConnection(); return 0; }
the_size = W5100::send(my_sn, the_buffer, the_size);
if (W5100::waitSendCompleted(my_sn) != W5100::rc_ok) { closeConnection(); return 0; }
my_totWrite += the_size;
return the_size;
}
void ClientProxy::flush()
{
if (prv_isValidSn()) return;
if (W5100::waitSendCompleted(my_sn) != W5100::rc_ok) closeConnection();
}
///////////////////////////////////////////////////////////////////////////////
bool ClientProxy::prv_isValidSn() const
{ return my_sn != W5100::socket_undefined; }
///////////////////////////////////////////////////////////////////////////////