-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgs.cpp
More file actions
450 lines (385 loc) · 7.92 KB
/
gs.cpp
File metadata and controls
450 lines (385 loc) · 7.92 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
gs.cpp - HAL driver to talk with Gainspan GS1011 WiFi module
Copyright (C) 2011 DIYSandbox LLC
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <avr/interrupt.h>
#include <HardwareSerial.h>
#include <WProgram.h>
#include "gs.h"
GSClass GS;
uint8_t GSClass::init(void)
{
Serial.begin(9600);
delay(1000);
Serial.flush();
Serial.println();
delay(1000);
dev_mode = 0;
// disable echo
if (!send_cmd_w_resp(CMD_DISABLE_ECHO)) {
return 0;
}
return 1;
}
typedef struct _cmd_tbl {
String cmd_str;
} CMD_TBL;
CMD_TBL cmd_tbl[] = {
{"ATE0"},
{"AT+WWPA="},
{"AT+WA="},
{"AT+NDHCP=0"},
{"AT+NDHCP=1"},
{"AT+WD"},
{"AT+NSTCP=80"},
};
uint8_t hex_to_int(char c)
{
uint8_t val = 0;
if (c >= '0' && c <= '9') {
val = c - '0';
}
else if (c >= 'A' && c <= 'F') {
val = c - 'A' + 10;
}
else if (c >= 'a' && c <= 'f') {
val = c - 'a' + 10;
}
return val;
}
char int_to_hex(uint8_t c)
{
char val = '0';
if (c >= 0 && c <= 9) {
val = c + '0';
}
else if (c >= 10 && c <= 15) {
val = c + 'A' - 10;
}
return val;
}
uint8_t GSClass::send_cmd(uint8_t cmd)
{
Serial.flush();
switch(cmd) {
case CMD_DISABLE_ECHO:
case CMD_DISABLE_DHCP:
case CMD_DISCONNECT:
case CMD_ENABLE_DHCP:
case CMD_LISTEN:
{
Serial.println(cmd_tbl[cmd].cmd_str);
break;
}
case CMD_SET_WPA_PSK:
{
String cmd_buf = cmd_tbl[cmd].cmd_str + this->security_key;
Serial.println(cmd_buf);
break;
}
case CMD_SET_SSID:
{
String cmd_buf = cmd_tbl[cmd].cmd_str + this->ssid;
Serial.println(cmd_buf);
break;
}
default:
break;
}
return 1;
}
uint8_t GSClass::parse_resp(uint8_t cmd)
{
uint8_t resp_done = 0;
uint8_t ret = 0;
String buf;
while (!resp_done) {
buf = readline();
switch(cmd) {
case CMD_DISABLE_ECHO:
case CMD_DISABLE_DHCP:
case CMD_DISCONNECT:
case CMD_SET_WPA_PSK:
case CMD_SET_SSID:
case CMD_ENABLE_DHCP:
{
if (buf == "OK") {
/* got OK */
ret = 1;
resp_done = 1;
} else if (buf.startsWith("ERROR")) {
/* got ERROR */
ret = 0;
resp_done = 1;
}
break;
}
case CMD_LISTEN:
{
if (buf.startsWith("CONNECT")) {
/* got CONNECT */
serv_cid = hex_to_int(buf[8]);
} else if (buf == "OK") {
/* got OK */
ret = 1;
resp_done = 1;
} else if (buf.startsWith("ERROR")) {
/* got ERROR */
ret = 0;
resp_done = 1;
}
break;
}
default:
break;
}
}
return ret;
}
uint8_t GSClass::send_cmd_w_resp(uint8_t cmd)
{
if (send_cmd(cmd)) {
return parse_resp(cmd);
} else {
return 0;
}
}
void GSClass::configure(GS_PROFILE *prof)
{
// configure params
// configure DHCP state
// configure mode
// configure security
// configure SSID
dhcp_state = prof->dhcp_state;
mode = prof->mode;
this->ssid = prof->ssid;
this->security_key = prof->security_key;
}
uint8_t GSClass::connect()
{
if (!send_cmd_w_resp(CMD_DISCONNECT)) {
return 0;
}
if (!send_cmd_w_resp(CMD_DISABLE_DHCP)) {
return 0;
}
if (!send_cmd_w_resp(CMD_SET_WPA_PSK)) {
return 0;
}
if (!send_cmd_w_resp(CMD_SET_SSID)) {
return 0;
}
if (!send_cmd_w_resp(CMD_ENABLE_DHCP)) {
return 0;
}
return 1;
}
String GSClass::readline(void)
{
String strBuf;
char inByte;
bool endDetected = false;
while (!endDetected)
{
if (Serial.available())
{
// valid data in HW UART buffer, so check if it's \r or \n
// if so, throw away
// if strBuf length greater than 0, then this is a true end of line,
// so break out
inByte = Serial.read();
if ((inByte == '\r') || (inByte == '\n'))
{
// throw away
if ((strBuf.length() > 0) && (inByte == '\n'))
{
endDetected = true;
}
}
else
{
strBuf += inByte;
}
}
}
return strBuf;
}
uint8_t GSClass::listen(uint16_t port)
{
// FIXME : use the input argument
if (!send_cmd_w_resp(CMD_LISTEN)) {
return 0;
}
}
uint8_t GSClass::socket_status(uint8_t port)
{
// FIXME : dummy, to be implemented
return 0;
}
void GSClass::process()
{
String strBuf;
char inByte;
uint8_t processDone = 0;
if (!Serial.available())
return;
while (!processDone) {
if (dev_mode == 0) {
while (1) {
if (Serial.available()) {
inByte = Serial.read();
if (inByte == 0x1b) {
// escape seq
// switch mode
dev_mode = 1;
break;
} else {
// command string
if ((inByte == '\r') || (inByte == '\n')) {
// throw away
if ((strBuf.length() > 0) && (inByte == '\n'))
{
// parse command
parse_cmd(strBuf);
processDone = 1;
break;
}
}
else
{
strBuf += inByte;
}
}
}
}
} else if (dev_mode == 1) {
/* data mode */
while(1) {
if (Serial.available()) {
inByte = Serial.read();
if (inByte == 0x53) {
/* data start, switch to data RX mode */
dev_mode = 2;
break;
} else if (inByte == 0x45) {
/* data end, switch to command mode */
dev_mode = 0;
/* send response if required */
send_http_resp();
processDone = 1;
break;
} else if (inByte == 0x4f) {
/* data mode ok */
dev_mode = 0;
break;
} else {
/* unknown */
dev_mode = 0;
break;
}
}
}
} else if (dev_mode == 2) {
/* data RX mode */
http_req_type = HTTP_REQ_NONE;
while (dev_mode == 2) {
String dataBuf;
while (1) {
if (Serial.available()) {
inByte = Serial.read();
if (inByte == 0x1b) {
/* switch mode */
dev_mode = 1;
break;
} else {
if ((inByte == '\r') || (inByte == '\n')) {
// throw away
if ((dataBuf.length() > 0) && (inByte == '\n'))
{
// parse data
parse_data(dataBuf);
break;
}
}
else
{
dataBuf += inByte;
}
}
}
}
}
}
}
}
void GSClass::parse_cmd(String buf)
{
if (buf.startsWith("CONNECT")) {
/* got CONNECT */
if (serv_cid == hex_to_int(buf[8])) {
/* client connected */
client_cid = hex_to_int(buf[10]);
}
} else if (buf.startsWith("DISCONNECT")) {
/* got disconnect */
}
}
void GSClass::parse_data(String buf)
{
if (buf.startsWith("GET", 1)) {
if (buf.startsWith("/ ", 5)) {
digitalWrite(3, LOW);
http_req_type = HTTP_REQ_GET_ROOT;
} else {
http_req_type = HTTP_REQ_UNKNOWN;
}
}
}
char data_st[3] = { 0x1b, 0x53, 0x00 };
char data_end[2] = { 0x1b, 0x43 };
char http_resp[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
"<html><meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\""
"<center><h1>Hello World</h1><br /><h1>from Hydrogen</h1><br />"
"<a href=\"1\">Page 1</a></center></html>";
char http_img_resp[] = "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\n";
void GSClass::send_http_resp()
{
uint8_t i;
switch(http_req_type) {
case HTTP_REQ_GET_ROOT:
data_st[2] = int_to_hex(client_cid);
for (i = 0; i < 3; i++) {
Serial.print(data_st[i]);
}
Serial.println(http_resp);
for (i = 0; i < 2; i++) {
Serial.print(data_end[i]);
}
break;
case HTTP_REQ_UNKNOWN:
data_st[2] = int_to_hex(client_cid);
for (i = 0; i < 3; i++) {
Serial.print(data_st[i]);
}
Serial.println(http_img_resp);
for (i = 0; i < 2; i++) {
Serial.print(data_end[i]);
}
break;
default:
break;
}
}