forked from Digilent/openscope-mz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiFi.cpp
457 lines (399 loc) · 15 KB
/
WiFi.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
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
450
451
452
453
454
455
456
457
/************************************************************************/
/* */
/* WiFi.cpp */
/* */
/* To support WiFi connection and processing */
/* */
/************************************************************************/
/* Author: Keith Vogel */
/* Copyright 2016, Digilent Inc. */
/************************************************************************/
/* Revision History: */
/* 5/27/2016(KeithV): Created */
/************************************************************************/
#include <OpenScope.h>
STATE WiFiDisconnect(void)
{
static STATE state = Idle;
uint8_t connectionState;
switch(state)
{
case Idle:
// everything must shutdown to do a scan
HTTPEnable(false);
deIPcK.end();
// only works after the end because end does not remove
// the adaptor pointer, but does take it out of the link layer
deIPcK.wfDisconnect();
state = WiFiWaitingDisconnect;
break;
case WiFiWaitingDisconnect:
// wait until we disconnect
// unfortunately we have no clear indication when
// this is done, isLinked will immediately return
// disconnected, but we don't want to start a control
// function until it really IS disconnected.
WF_ConnectionStateGet(&connectionState);
// FF is what is returned if the SPI port is not connected
// clearly not connected if the SPI is working
if(connectionState == WF_CSTATE_NOT_CONNECTED || connectionState == 0xFF)
{
state = Idle;
}
break;
default:
state = Idle;
return(InvalidState);
break;
}
return(state);
}
STATE WiFiScan(WiFiScanInfo& wifiScan)
{
STATE retState = Idle;
switch(wifiScan.state)
{
case Idle:
wifiScan.state = WiFiWaitingDisconnect;
// fall thru
case WiFiWaitingDisconnect:
if((retState = WiFiDisconnect()) == Idle)
{
wifiScan.state = WiFiScanning;
}
else if(IsIPStatusAnError(retState))
{
wifiScan.state = Idle;
return(retState);
}
break;
case WiFiScanning:
if(deIPcK.wfScan((int *) &wifiScan.cNetworks, &wifiScan.status))
{
wifiScan.state = Idle;
}
else if(IsIPStatusAnError(wifiScan.status))
{
wifiScan.state = Idle;
return(WiFiScanFailure | wifiScan.status);
}
break;
default:
wifiScan.state = Idle;
return(InvalidState);
break;
}
return(wifiScan.state);
}
STATE WiFiConnect(WiFiConnectInfo& wifiConn, bool fForce)
{
switch(wifiConn.comhdr.state)
{
case Idle:
// we are going to use the start time as an error code
wifiConn.comhdr.tStart = Idle;
case WiFiWaitingConnect:
if(wifiConn.wifiKey == DEWF_SECURITY_WPA2_WITH_KEY || wifiConn.wifiKey == DEWF_SECURITY_WPA_WITH_KEY)
{
wifiConn.comhdr.state = WiFiWaitingConnectWPAWithKey;
}
else if(wifiConn.wifiKey == DEWF_SECURITY_OPEN)
{
wifiConn.comhdr.state = WiFiWaitingConnectNone;
}
else
{
wifiConn.comhdr.state = Idle;
return(WiFiUnsuporttedSecurity);
}
// if we are to force a disconnect
if(IsHTTPRunning())
{
if(fForce)
{
wifiConn.comhdr.state = WiFiWaitingDisconnect;
}
else
{
wifiConn.comhdr.state = Idle;
return(WiFiIsRunning);
}
}
break;
case WiFiWaitingDisconnect:
if(WiFiDisconnect() == Idle)
{
if(wifiConn.comhdr.tStart == 0)
{
// go back to the connect
wifiConn.comhdr.state = WiFiWaitingConnect;
}
else
{
STATE retError = wifiConn.comhdr.tStart;
wifiConn.comhdr.tStart = 0;
wifiConn.comhdr.state = Idle;
return(retError);
}
}
break;
case WiFiWaitingConnectNone:
if(deIPcK.wfConnect(wifiConn.ssid, &wifiConn.status))
{
wifiConn.comhdr.state = WiFiEnable;
}
else if(IsIPStatusAnError(wifiConn.status))
{
wifiConn.comhdr.tStart = WiFiConnectionError | wifiConn.status;
wifiConn.comhdr.state = WiFiWaitingDisconnect;
}
break;
case WiFiWaitingConnectWPAWithKey:
if(deIPcK.wfConnect(wifiConn.ssid, wifiConn.key.wpa2Key, &wifiConn.status))
{
wifiConn.comhdr.state = WiFiEnable;
}
else if(IsIPStatusAnError(wifiConn.status))
{
wifiConn.comhdr.tStart = WiFiConnectionError | wifiConn.status;
wifiConn.comhdr.state = WiFiWaitingDisconnect;
}
break;
case WiFiEnable:
if(HTTPEnable(true))
{
wifiConn.comhdr.state = Idle;
// make this active
memcpy(&pjcmd.iWiFi.wifiAConn, &wifiConn, sizeof(WiFiConnectInfo));
}
else
{
wifiConn.comhdr.tStart = WiFiUnableToStartHTTPServer;
wifiConn.comhdr.state = WiFiWaitingDisconnect;
}
break;
default:
wifiConn.comhdr.state = Idle;
return(InvalidState);
break;
}
return(wifiConn.comhdr.state);
}
STATE WiFiLookupConnInfo(DFILE& dFile, WiFiConnectInfo& wifiConn)
{
static WiFiScanInfo wifiScan = WiFiScanInfo();
static char sz[128] = {0};
static char szSSID[DEWF_MAX_SSID_LENGTH+1];
static char szSSIDMax[DEWF_MAX_SSID_LENGTH+1];
static uint8_t rssiMax = 0;
static VOLTYPE volMax = VOLNONE;
static IDHDR idHdr = wifiConn.comhdr.idhdr;
static WiFiConnectInfo wifiConnT = WiFiConnectInfo();
STATE retState = Idle;
switch(wifiConn.comhdr.state)
{
case Idle:
wifiConn.comhdr.state = WiFiScanning;
szSSID[0] = '\0';
szSSIDMax[0] = '\0';
rssiMax = 0;
volMax = VOLNONE;
memcpy(&idHdr , &wifiConn.comhdr.idhdr, sizeof(IDHDR));
memcpy((void *) &idHdr.mac , &macOpenScope, sizeof(idHdr.mac));
// fall thru
case WiFiScanning:
if((retState = WiFiScan(wifiScan)) == Idle)
{
wifiScan.iNetwork = 0;
wifiConn.comhdr.state = WiFiCheckFileName;
}
else if(IsStateAnError(retState))
{
wifiConn.comhdr.state = Idle;
return(retState);
}
break;
case WiFiCheckFileName:
// see if we are done looking at all available SSIDs
if(wifiScan.iNetwork >= wifiScan.cNetworks)
{
if(volMax != VOLNONE)
{
// no go read the file
wifiConn.comhdr.state = WiFiCreateFileName;
}
else
{
wifiConn.comhdr.state = Idle;
return(WiFiNoNetworksFound);
}
}
// get the next SSID
else if(!deIPcK.getScanInfo(wifiScan.iNetwork, &wifiScan.scanInfo))
{
wifiConn.comhdr.state = Idle;
return(WiFiNoScanData);
}
memcpy(szSSID, wifiScan.scanInfo.ssid, wifiScan.scanInfo.ssidLen);
szSSID[wifiScan.scanInfo.ssidLen] = '\0';
CFGCreateFileName(idHdr, szSSID, sz, sizeof(sz));
if(wifiScan.scanInfo.rssi > rssiMax)
{
COMHDR cmdHdr = {{sizeof(COMHDR), WFVER, WIFIPARAM_ID, CFGUNCAL, {0,0,0,0,0,0}}, SMFnNone, Idle, 0, 0};
uint32_t cbRead;
// look in flash
if( DFATFS::fschdrive(DFATFS::szFatFsVols[VOLFLASH]) == FR_OK &&
DFATFS::fschdir(DFATFS::szRoot) == FR_OK &&
DFATFS::fsexists(sz) &&
dFile.fsopen(sz, FA_READ) == FR_OK )
{
if( dFile.fssize() == sizeof(WiFiConnectInfo) &&
dFile.fsread(&cmdHdr, sizeof(COMHDR), &cbRead) == FR_OK &&
cbRead == sizeof(COMHDR) &&
cmdHdr.activeFunc == WIFIFnAutoConnect )
{
rssiMax = wifiScan.scanInfo.rssi;
strcpy(szSSIDMax, szSSID);
volMax = VOLFLASH;
}
dFile.fsclose();
}
// look on the SD card
else if( DFATFS::fschdrive(DFATFS::szFatFsVols[VOLSD]) == FR_OK &&
DFATFS::fschdir(DFATFS::szRoot) == FR_OK &&
DFATFS::fsexists(sz) &&
dFile.fsopen(sz, FA_READ) == FR_OK )
{
if( dFile.fssize() == sizeof(WiFiConnectInfo) &&
dFile.fsread(&cmdHdr, sizeof(COMHDR), &cbRead) == FR_OK &&
cbRead == sizeof(COMHDR) &&
cmdHdr.activeFunc == WIFIFnAutoConnect )
{
rssiMax = wifiScan.scanInfo.rssi;
strcpy(szSSIDMax, szSSID);
volMax = VOLSD;
}
dFile.fsclose();
}
}
// look at the next SSID
wifiScan.iNetwork++;
break;
case WiFiCreateFileName:
CFGCreateFileName(idHdr, szSSIDMax, sz, sizeof(sz));
wifiConn.comhdr.state = WiFiReadFile;
// we are going to read into a temp
memcpy((void*) &wifiConnT, &wifiConn, sizeof(WiFiConnectInfo));
// fall thru
case WiFiReadFile:
if((retState = IOReadFile(dFile, volMax, sz, wifiConnT.comhdr.idhdr)) == Idle)
{
// make sure we have the correct calibration version.
if((wifiConnT.comhdr.idhdr.ver != WFVER))
{
wifiConn.comhdr.state = Idle;
wifiConn.ssid[0] = '\0';
return(CFGUnableToReadConfigFile);
}
// we got it.
memcpy((void*) &wifiConn, &wifiConnT, sizeof(WiFiConnectInfo));
// we are done, and we read the file
wifiConn.comhdr.state = Idle;
}
// something bad happened
else if(IsStateAnError(retState))
{
wifiConn.comhdr.state = Idle;
wifiConn.ssid[0] = '\0';
return(retState);
}
else
{
// have to do this we are reading in wifiConn and that could
// blow our state away, so we must put it back.
wifiConn.comhdr.state = WiFiReadFile;
}
break;
default:
ASSERT(NEVER_SHOULD_GET_HERE);
break;
}
return(wifiConn.comhdr.state);
}
STATE WiFiLoadConnInfo(DFILE& dFile, VOLTYPE const vol, char const szSSID[], WiFiConnectInfo& wifiConn)
{
static char sz[128] = {0};
STATE retState = Idle;
switch(wifiConn.comhdr.state)
{
case Idle:
memcpy((void *) &wifiConn.comhdr.idhdr.mac , &macOpenScope, sizeof(wifiConn.comhdr.idhdr.mac));
wifiConn.ssid[0] = '\0';
wifiConn.comhdr.state = WiFiCreateFileName;
// fall thru
case WiFiCreateFileName:
CFGCreateFileName(wifiConn.comhdr.idhdr, szSSID, sz, sizeof(sz));
wifiConn.comhdr.state = WiFiCheckFileName;
break;
case WiFiCheckFileName:
if((retState = (STATE) DFATFS::fschdrive(DFATFS::szFatFsVols[vol])) != (STATE) FR_OK || (retState = (STATE) DFATFS::fschdir(DFATFS::szRoot)) != (STATE) FR_OK)
{
wifiConn.comhdr.state = Idle;
return(retState | CFGMountError);
}
else if(!DFATFS::fsexists(sz))
{
wifiConn.comhdr.state = Idle;
return(WiFiNoMatchingSSID);
}
else
{
wifiConn.comhdr.state = WiFiReadFile;
}
break;
case WiFiReadFile:
if((retState = IOReadFile(dFile, vol, sz, wifiConn.comhdr.idhdr)) == Idle)
{
wifiConn.comhdr.state = Idle;
}
else if(IsStateAnError(retState))
{
wifiConn.comhdr.state = Idle;
return(retState);
}
else
{
// have to do this we are reading in wifiConn and that could
// blow our state away, so we must put it back.
wifiConn.comhdr.state = WiFiReadFile;
}
break;
default:
ASSERT(NEVER_SHOULD_GET_HERE);
break;
}
return(wifiConn.comhdr.state);
}
STATE WiFiSaveConnInfo(DFILE& dFile, VOLTYPE const vol, WiFiConnectInfo& wifiConn)
{
static char sz[128] = {0};
STATE retState = Idle;
if(sz[0] == '\0')
{
CFGCreateFileName(wifiConn.comhdr.idhdr, wifiConn.ssid, sz, sizeof(sz));
}
if((retState = IOWriteFile(dFile, vol, sz, wifiConn.comhdr.idhdr)) == Idle)
{
sz[0] = '\0';
return(Idle);
}
else if(IsStateAnError(retState))
{
sz[0] = '\0';
return(retState);
}
else
{
return(WiFiWriteFile);
}
}