forked from Seeed-Shield/WiFi_Shield
/
WiFly_communicator.cpp
382 lines (308 loc) · 7.19 KB
/
WiFly_communicator.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
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
#include <string.h>
#include "WiFly_communicator.h"
#include "Debug.h"
WiFly* WiFly::instance;
WiFly::WiFly(Stream *serial)
{
instance = this;
this->serial = serial;
setTimeout(DEFAULT_WAIT_RESPONSE_TIME);
command_mode = false;
associated = false;
}
int WiFly::available()
{
return serial->available();
}
int WiFly::read()
{
return serial->read();
}
int WiFly::peek()
{
return serial->peek();
}
void WiFly::flush()
{
return serial->flush();
}
size_t WiFly::write(uint8_t c)
{
return serial->write(c);
}
size_t WiFly::write(const uint8_t *buffer, size_t size)
{
return serial->write(buffer, size);
}
boolean WiFly::reset()
{
return sendCommand("factory R\r", "Defaults");
}
boolean WiFly::reboot()
{
sendCommand("reboot\r"); // first this
command_mode = false; // then this
return true;
}
boolean WiFly::runWebApp()
{
sendCommand("run web_app\r", "READY*"); // first this
command_mode = false; // then this
return true;
}
/*
boolean WiFly::scan()
{ // v4.00 in apmode you need to issue the scan command twice to
// get WifiNetworks back
// first a short scan then a longer one
// first a short scan of 1ms per channel
sendCommand("scan 1\r");
// 2nd longer scan
// timeout: 14 ch * 400 = 5600 + 1000ms
sendCommand("scan 400\r", "SCAN", 6600);
char c;
while (receive((uint8_t *)&c, 1, 300) > 0) {
Serial.print((char)c);
}
}
*/
boolean WiFly::init()
{
boolean result = true;
#if 0
// set time
result = result & sendCommand("set c t 20\r", "AOK");
// set size
result = result & sendCommand("set c s 128\r", "AOK");
// red led on when tcp connection active
result = result & sendCommand("set s i 0x40\r", "AOK");
// no string sent to the tcp client
result = result & sendCommand("set c r 0\r", "AOK");
// tcp protocol
result = result & sendCommand("set i p 2\r", "AOK");
// tcp retry
result = result & sendCommand("set i f 0x7\r", "AOK");
//no echo
result = result & sendCommand("set u m 1\r", "AOK");
// no auto join
result = result & sendCommand("set w j 0\r", "AOK");
// DHCP on
result = result & sendCommand("set i d 1\r", "AOK");
#endif
return result;
}
boolean WiFly::staticIP(const char *ip, const char *mask, const char *gateway)
{
boolean result = true;
char cmd[MAX_CMD_LEN];
result = sendCommand("set i d 0\r", "AOK");
snprintf(cmd, MAX_CMD_LEN, "set i a %s\r", ip);
result = result & sendCommand(cmd, "AOK");
snprintf(cmd, MAX_CMD_LEN, "set i n %s\r", mask);
result = result & sendCommand(cmd, "AOK");
snprintf(cmd, MAX_CMD_LEN, "set i g %s\r", gateway);
result = result & sendCommand(cmd, "AOK");
return result;
}
boolean WiFly::join(const char *ssid, const char *phrase, int auth)
{
char cmd[MAX_CMD_LEN];
// ssid
snprintf(cmd, MAX_CMD_LEN, "set w s %s\r", ssid);
sendCommand(cmd, "AOK");
//auth
snprintf(cmd, MAX_CMD_LEN, "set w a %d\r", auth);
sendCommand(cmd, "AOK");
//key
if (auth != WIFLY_AUTH_OPEN) {
if ((auth == WIFLY_AUTH_WEP) || (auth == WIFLY_AUTH_WEP64))
snprintf(cmd, MAX_CMD_LEN, "set w k %s\r", phrase);
else
snprintf(cmd, MAX_CMD_LEN, "set w p %s\r", phrase);
sendCommand(cmd, "AOK");
}
//join the network
if (!sendCommand("join\r", "ssociated")) {
return false;
}
clear();
associated = true;
return true;
}
boolean WiFly::leave()
{
if (sendCommand("leave\r", "DeAuth")) {
associated = false;
return true;
}
return false;
}
boolean WiFly::connect(const char *host, uint16_t port, int timeout)
{
char cmd[MAX_CMD_LEN];
snprintf(cmd, sizeof(cmd), "set d n %s\r", host);
sendCommand(cmd, "AOK");
snprintf(cmd, sizeof(cmd), "set i r %d\r", port);
sendCommand(cmd, "AOK");
sendCommand("set i p 2\r", "AOK");
sendCommand("set i h 0\r", "AOK");
sendCommand("set c r 0\r", "AOK");
if (!sendCommand("open\r", "*OPEN*", timeout)) {
sendCommand("close\r");
clear();
return false;
}
command_mode = false;
return true;
}
int WiFly::send(const uint8_t *data, int len, int timeout)
{
int write_bytes = 0;
boolean write_error = false;
unsigned long start_millis;
if (data == NULL) {
return 0;
}
while (write_bytes < len) {
if (write(data[write_bytes]) == 1) {
write_bytes++;
write_error = false;
} else { // failed to write, set timeout
if (write_error) {
if ((millis() - start_millis) > timeout) {
DBG("Send data. Timeout!\r\n");
break;
}
} else {
write_error = true;
start_millis = millis();
}
}
}
return write_bytes;
}
int WiFly::send(const char *data, int timeout)
{
send((uint8_t*)data, strlen(data), timeout);
}
boolean WiFly::ask(const char *q, const char *a, int timeout)
{
unsigned long start;
unsigned long end;
int q_len = strlen(q);
send((uint8_t *)q, q_len, timeout);
if (a != NULL) {
setTimeout(timeout);
start = millis();
boolean found = find((char *)a);
if (!found) {
end = millis();
if ((end - start) < timeout) {
DBG("\r\n");
DBG(q);
DBG("\r\nTry to find: ");
DBG(a);
DBG("\r\nTimeout: ");
DBG(timeout);
DBG("\r\nStart time: ");
DBG(start);
DBG("\r\nEnd time: ");
DBG(end);
DBG("\r\n***** Probably ot enough memory *****\r\n");
}
return false;
}
}
return true;
}
boolean WiFly::sendCommand(const char *cmd, const char *ack, int timeout)
{
clear();
if (!command_mode) {
commandMode();
}
if (!ask(cmd, ack, timeout)) {
DBG("Failed to run: ");
DBG(cmd);
DBG("\r\n");
return false;
}
DBG("run OK: ");
DBG(cmd);
DBG("\r\n");
return true;
}
boolean WiFly::commandMode()
{
if (command_mode) return true;
if (!ask("$$$", "CMD")) {
if (!ask("\r", "ERR")) {
DBG("Failed to enter command mode\r\n");
command_mode = false; // added
return false;
}
}
DBG("Entering command mode\r\n");
command_mode = true;
return true;
}
boolean WiFly::dataMode()
{
if (command_mode) {
if (!ask("exit\r", "EXIT")) {
if (ask("\r", "ERR")) {
DBG("Failed to enter data mode\r\n");
return false;
}
}
DBG("Data mode\r\n");
command_mode = false;
}
return true;
}
void WiFly::clear()
{
char r;
while (receive((uint8_t *)&r, 1, 10) == 1) {
}
}
float WiFly::version()
{
if (!sendCommand("ver\r", "Ver ")) {
return -1;
}
char buf[48];
int read_bytes;
read_bytes = receive((uint8_t *)buf, 48 -1);
buf[read_bytes] = '\0';
float version;
version = atof(buf);
if (version == 0) {
char *ptr = strchr(buf, '<');
if (ptr != NULL) {
version = atof(ptr + 1);
}
}
return version;
}
int WiFly::receive(uint8_t *buf, int len, int timeout)
{
int read_bytes = 0;
int ret;
unsigned long end_millis;
while (read_bytes < len) {
end_millis = millis() + timeout;
do {
ret = read();
if (ret >= 0) {
break;
}
} while (millis() < end_millis);
if (ret < 0) {
return read_bytes;
}
buf[read_bytes] = (char)ret;
read_bytes++;
}
return read_bytes;
}