-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathWiFi.cpp
1186 lines (980 loc) · 26.4 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
WiFi.cpp - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef ARDUINO_ARCH_AVR
#include <avr/version.h>
#if (__AVR_LIBC_MAJOR__ < 2)
#define WIFI_101_NO_TIME_H
#endif
#endif
#ifndef WIFI_101_NO_TIME_H
#include <time.h>
#endif
#if !defined(_TIME_H_) && !defined(TIME_H)
// another library overrode the time.h header
#define WIFI_101_NO_TIME_H
#endif
#include "utility/WiFiSocket.h"
#include "WiFi101.h"
extern "C" {
#include "bsp/include/nm_bsp.h"
#include "bsp/include/nm_bsp_arduino.h"
#include "driver/include/m2m_periph.h"
#include "driver/include/m2m_ssl.h"
#include "driver/include/m2m_wifi.h"
}
static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
{
WiFi.handleEvent(u8MsgType, pvMsg);
}
void WiFiClass::handleEvent(uint8_t u8MsgType, void *pvMsg)
{
switch (u8MsgType) {
case M2M_WIFI_RESP_DEFAULT_CONNECT:
{
tstrM2MDefaultConnResp *pstrDefaultConnResp = (tstrM2MDefaultConnResp *)pvMsg;
if (pstrDefaultConnResp->s8ErrorCode) {
_status = WL_DISCONNECTED;
}
}
break;
case M2M_WIFI_RESP_CON_STATE_CHANGED:
{
tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg;
if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED) {
//SERIAL_PORT_MONITOR.println("wifi_cb: M2M_WIFI_RESP_CON_STATE_CHANGED: CONNECTED");
if (_mode == WL_STA_MODE && !_dhcp) {
_status = WL_CONNECTED;
#ifdef CONF_PERIPH
// WiFi led ON.
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 0);
#endif
} else if (_mode == WL_AP_MODE) {
_status = WL_AP_CONNECTED;
}
} else if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED) {
//SERIAL_PORT_MONITOR.println("wifi_cb: M2M_WIFI_RESP_CON_STATE_CHANGED: DISCONNECTED");
if (_mode == WL_STA_MODE) {
_status = WL_DISCONNECTED;
if (_dhcp) {
_localip = 0;
_submask = 0;
_gateway = 0;
}
// Close sockets to clean state
// Clients will need to reconnect once the physical link will be re-established
for (int i = 0; i < MAX_SOCKET; i++) {
WiFiSocket.close(i);
}
} else if (_mode == WL_AP_MODE) {
_status = WL_AP_LISTENING;
}
#ifdef CONF_PERIPH
// WiFi led OFF (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 1);
#endif
}
}
break;
case M2M_WIFI_REQ_DHCP_CONF:
{
if (_mode == WL_STA_MODE) {
tstrM2MIPConfig *pstrIPCfg = (tstrM2MIPConfig *)pvMsg;
_localip = pstrIPCfg->u32StaticIP;
_submask = pstrIPCfg->u32SubnetMask;
_gateway = pstrIPCfg->u32Gateway;
_status = WL_CONNECTED;
#ifdef CONF_PERIPH
// WiFi led ON (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 0);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 0);
#endif
}
/*uint8_t *pu8IPAddress = (uint8_t *)pvMsg;
SERIAL_PORT_MONITOR.print("wifi_cb: M2M_WIFI_REQ_DHCP_CONF: IP is ");
SERIAL_PORT_MONITOR.print(pu8IPAddress[0], 10);
SERIAL_PORT_MONITOR.print(".");
SERIAL_PORT_MONITOR.print(pu8IPAddress[1], 10);
SERIAL_PORT_MONITOR.print(".");
SERIAL_PORT_MONITOR.print(pu8IPAddress[2], 10);
SERIAL_PORT_MONITOR.print(".");
SERIAL_PORT_MONITOR.print(pu8IPAddress[3], 10);
SERIAL_PORT_MONITOR.println("");*/
}
break;
case M2M_WIFI_RESP_CURRENT_RSSI:
{
_resolve = *((int8_t *)pvMsg);
}
break;
case M2M_WIFI_RESP_PROVISION_INFO:
{
tstrM2MProvisionInfo *pstrProvInfo = (tstrM2MProvisionInfo *)pvMsg;
//SERIAL_PORT_MONITOR.println("wifi_cb: M2M_WIFI_RESP_PROVISION_INFO");
if (pstrProvInfo->u8Status == M2M_SUCCESS) {
memset(_ssid, 0, M2M_MAX_SSID_LEN);
memcpy(_ssid, (char *)pstrProvInfo->au8SSID, strlen((char *)pstrProvInfo->au8SSID));
_mode = WL_STA_MODE;
_localip = 0;
_submask = 0;
_gateway = 0;
m2m_wifi_connect((char *)pstrProvInfo->au8SSID, strlen((char *)pstrProvInfo->au8SSID),
pstrProvInfo->u8SecType, pstrProvInfo->au8Password, M2M_WIFI_CH_ALL);
} else {
_status = WL_PROVISIONING_FAILED;
//SERIAL_PORT_MONITOR.println("wifi_cb: Provision failed.\r\n");
beginProvision();
}
}
break;
case M2M_WIFI_RESP_SCAN_DONE:
{
tstrM2mScanDone *pstrInfo = (tstrM2mScanDone *)pvMsg;
if (pstrInfo->u8NumofCh >= 1) {
_status = WL_SCAN_COMPLETED;
}
}
break;
case M2M_WIFI_RESP_SCAN_RESULT:
{
tstrM2mWifiscanResult *pstrScanResult = (tstrM2mWifiscanResult *)pvMsg;
uint16_t scan_ssid_len = strlen((const char *)pstrScanResult->au8SSID);
memset(_scan_ssid, 0, M2M_MAX_SSID_LEN);
if (scan_ssid_len) {
memcpy(_scan_ssid, (const char *)pstrScanResult->au8SSID, scan_ssid_len);
}
if (_remoteMacAddress) {
// reverse copy the remote MAC
for(int i = 0; i < 6; i++) {
_remoteMacAddress[i] = pstrScanResult->au8BSSID[5-i];
}
}
_resolve = pstrScanResult->s8rssi;
_scan_auth = pstrScanResult->u8AuthType;
_scan_channel = pstrScanResult->u8ch;
_status = WL_SCAN_COMPLETED;
}
break;
case M2M_WIFI_RESP_CONN_INFO:
{
tstrM2MConnInfo *pstrConnInfo = (tstrM2MConnInfo*)pvMsg;
if (_remoteMacAddress) {
// reverse copy the remote MAC
for(int i = 0; i < 6; i++) {
_remoteMacAddress[i] = pstrConnInfo->au8MACAddress[5-i];
}
_remoteMacAddress = 0;
}
strcpy((char *)_ssid, pstrConnInfo->acSSID);
}
break;
case M2M_WIFI_RESP_GET_SYS_TIME:
{
if (_resolve != 0) {
memcpy((tstrSystemTime *)_resolve, pvMsg, sizeof(tstrSystemTime));
_resolve = 0;
}
}
break;
default:
break;
}
}
static void resolve_cb(uint8_t * hostName, uint32_t hostIp)
{
WiFi.handleResolve(hostName, hostIp);
}
void WiFiClass::handleResolve(uint8_t * /*hostName*/, uint32_t hostIp)
{
_resolve = hostIp;
}
static void socket_cb(SOCKET sock, uint8 u8Msg, void *pvMsg)
{
WiFiSocket.eventCallback(sock, u8Msg, pvMsg);
}
void ping_cb(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode)
{
WiFi.handlePingResponse(u32IPAddr, u32RTT, u8ErrorCode);
}
void WiFiClass::handlePingResponse(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode)
{
if (PING_ERR_SUCCESS == u8ErrorCode) {
// Ensure this ICMP reply comes from requested IP address
if (_resolve == u32IPAddr) {
_resolve = (uint32_t)u32RTT;
} else {
// Another network device replied to the our ICMP request
_resolve = (uint32_t)WL_PING_DEST_UNREACHABLE;
}
} else if (PING_ERR_DEST_UNREACH == u8ErrorCode) {
_resolve = (uint32_t)WL_PING_DEST_UNREACHABLE;
} else if (PING_ERR_TIMEOUT == u8ErrorCode) {
_resolve = (uint32_t)WL_PING_TIMEOUT;
} else {
_resolve = (uint32_t)WL_PING_ERROR;
}
}
WiFiClass::WiFiClass() :
_init(0),
_mode(WL_RESET_MODE),
_status(WL_NO_SHIELD),
_timeout(60000)
{
}
void WiFiClass::setPins(int8_t cs, int8_t irq, int8_t rst, int8_t en)
{
gi8Winc1501CsPin = cs;
gi8Winc1501IntnPin = irq;
gi8Winc1501ResetPin = rst;
gi8Winc1501ChipEnPin = en;
}
int WiFiClass::init()
{
tstrWifiInitParam param;
int8_t ret;
// Initialize the WiFi BSP:
nm_bsp_init();
// Initialize WiFi module and register status callback:
param.pfAppWifiCb = wifi_cb;
ret = m2m_wifi_init(¶m);
if (M2M_SUCCESS != ret && M2M_ERR_FW_VER_MISMATCH != ret) {
#ifdef CONF_PERIPH
if (ret != M2M_ERR_INVALID) {
// Error led ON (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO18, 0);
m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO6, 1);
}
#endif
return ret;
}
// Initialize socket API and register socket callback:
socketInit();
registerSocketCallback(socket_cb, resolve_cb);
_init = 1;
_status = WL_IDLE_STATUS;
_localip = 0;
_submask = 0;
_gateway = 0;
_dhcp = 1;
_resolve = 0;
_remoteMacAddress = 0;
extern uint32 nmdrv_firm_ver;
if (nmdrv_firm_ver >= M2M_MAKE_VERSION(19, 5, 0)) {
// enable AES-128 and AES-256 Ciphers, if firmware is 19.5.0 or higher
m2m_ssl_set_active_ciphersuites(SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA | SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 | SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 | SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA | SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256);
}
#ifdef CONF_PERIPH
// Initialize IO expander LED control (rev A then rev B)..
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO18, 1);
m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO15, 1);
m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO16, 1);
m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO18, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO6, 1);
m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO4, 1);
m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO5, 1);
m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO6, 1);
#endif
return ret;
}
extern "C" {
sint8 nm_get_firmware_info(tstrM2mRev* M2mRev);
}
char* WiFiClass::firmwareVersion()
{
tstrM2mRev rev;
if (!_init) {
init();
}
nm_get_firmware_info(&rev);
memset(_version, 0, 9);
if (rev.u8FirmwareMajor != rev.u8DriverMajor && rev.u8FirmwareMinor != rev.u8DriverMinor) {
sprintf(_version, "-Err-");
}
else {
sprintf(_version, "%d.%d.%d", rev.u8FirmwareMajor, rev.u8FirmwareMinor, rev.u8FirmwarePatch);
}
return _version;
}
uint8_t WiFiClass::begin()
{
if (!_init) {
init();
}
// Connect to router:
if (_dhcp) {
_localip = 0;
_submask = 0;
_gateway = 0;
}
if (m2m_wifi_default_connect() < 0) {
_status = WL_CONNECT_FAILED;
return _status;
}
_status = WL_IDLE_STATUS;
_mode = WL_STA_MODE;
// Wait for connection or timeout:
for (unsigned long start = millis(); millis() - start < _timeout;)
{
m2m_wifi_handle_events(NULL);
if ((_status & WL_CONNECTED) || (_status & WL_DISCONNECTED)) {
break;
}
}
memset(_ssid, 0, M2M_MAX_SSID_LEN);
if (!(_status & WL_CONNECTED)) {
_mode = WL_RESET_MODE;
} else {
m2m_wifi_get_connection_info();
m2m_wifi_handle_events(NULL);
}
return _status;
}
uint8_t WiFiClass::begin(const char *ssid)
{
return startConnect(ssid, M2M_WIFI_SEC_OPEN, (void *)0);
}
uint8_t WiFiClass::begin(const char *ssid, uint8_t key_idx, const char* key)
{
tstrM2mWifiWepParams wep_params;
memset(&wep_params, 0, sizeof(tstrM2mWifiWepParams));
wep_params.u8KeyIndx = key_idx;
wep_params.u8KeySz = strlen(key);
strcpy((char *)&wep_params.au8WepKey[0], key);
return startConnect(ssid, M2M_WIFI_SEC_WEP, &wep_params);
}
uint8_t WiFiClass::begin(const char *ssid, const char *key)
{
return startConnect(ssid, M2M_WIFI_SEC_WPA_PSK, key);
}
uint8_t WiFiClass::startConnect(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo)
{
if (!_init) {
init();
}
// Connect to router:
if (_dhcp) {
_localip = 0;
_submask = 0;
_gateway = 0;
}
if (m2m_wifi_connect((char*)ssid, strlen(ssid), u8SecType, (void*)pvAuthInfo, M2M_WIFI_CH_ALL) < 0) {
_status = WL_CONNECT_FAILED;
return _status;
}
_status = WL_IDLE_STATUS;
_mode = WL_STA_MODE;
// Wait for connection or timeout:
for (unsigned long start = millis(); millis() - start < _timeout;)
{
m2m_wifi_handle_events(NULL);
if ((_status & WL_CONNECTED) || (_status & WL_DISCONNECTED)) {
break;
}
}
if (!(_status & WL_CONNECTED)) {
_mode = WL_RESET_MODE;
}
memset(_ssid, 0, M2M_MAX_SSID_LEN);
memcpy(_ssid, ssid, strlen(ssid));
return _status;
}
uint8_t WiFiClass::beginAP(const char *ssid)
{
return beginAP(ssid, 1);
}
uint8_t WiFiClass::beginAP(const char *ssid, uint8_t channel)
{
return startAP(ssid, M2M_WIFI_SEC_OPEN, NULL, channel);
}
uint8_t WiFiClass::beginAP(const char *ssid, uint8_t key_idx, const char* key)
{
return beginAP(ssid, key_idx, key, 1);
}
uint8_t WiFiClass::beginAP(const char *ssid, uint8_t key_idx, const char* key, uint8_t channel)
{
tstrM2mWifiWepParams wep_params;
if (key_idx == 0) {
key_idx = 1; // 1 is the minimum key index
}
memset(&wep_params, 0, sizeof(tstrM2mWifiWepParams));
wep_params.u8KeyIndx = key_idx;
wep_params.u8KeySz = strlen(key);
strcpy((char *)&wep_params.au8WepKey[0], key);
return startAP(ssid, M2M_WIFI_SEC_WEP, &wep_params, channel);
}
uint8_t WiFiClass::beginAP(const char *ssid, const char* key)
{
return beginAP(ssid, key, 1);
}
uint8_t WiFiClass::beginAP(const char *ssid, const char* key, uint8_t channel)
{
return startAP(ssid, M2M_WIFI_SEC_WPA_PSK, key, channel);
}
uint8_t WiFiClass::startAP(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo, uint8_t channel)
{
tstrM2MAPConfig strM2MAPConfig;
if (!_init) {
init();
}
if (channel == 0) {
channel = 1; // channel 1 is the minimum channel
}
// Enter Access Point mode:
memset(&strM2MAPConfig, 0x00, sizeof(tstrM2MAPConfig));
strcpy((char *)&strM2MAPConfig.au8SSID, ssid);
strM2MAPConfig.u8ListenChannel = channel;
strM2MAPConfig.u8SecType = u8SecType;
if (_localip == 0) {
strM2MAPConfig.au8DHCPServerIP[0] = 192;
strM2MAPConfig.au8DHCPServerIP[1] = 168;
strM2MAPConfig.au8DHCPServerIP[2] = 1;
strM2MAPConfig.au8DHCPServerIP[3] = 1;
} else {
memcpy(strM2MAPConfig.au8DHCPServerIP, &_localip, sizeof(_localip));
if (strM2MAPConfig.au8DHCPServerIP[3] == 100) {
// limitation of WINC1500 firmware, IP address of client is always x.x.x.100
_status = WL_AP_FAILED;
return _status;
}
}
if (u8SecType == M2M_WIFI_SEC_WEP) {
tstrM2mWifiWepParams* wep_params = (tstrM2mWifiWepParams*)pvAuthInfo;
strM2MAPConfig.u8KeyIndx = wep_params->u8KeyIndx;
strM2MAPConfig.u8KeySz = wep_params->u8KeySz;
strcpy((char*)strM2MAPConfig.au8WepKey, (char *)wep_params->au8WepKey);
}
if (u8SecType == M2M_WIFI_SEC_WPA_PSK) {
strM2MAPConfig.u8KeySz = strlen((char*)pvAuthInfo);
strcpy((char*)strM2MAPConfig.au8Key, (char *)pvAuthInfo);
}
if (m2m_wifi_enable_ap(&strM2MAPConfig) < 0) {
_status = WL_AP_FAILED;
return _status;
}
_status = WL_AP_LISTENING;
_mode = WL_AP_MODE;
memset(_ssid, 0, M2M_MAX_SSID_LEN);
memcpy(_ssid, ssid, strlen(ssid));
m2m_memcpy((uint8 *)&_localip, (uint8 *)&strM2MAPConfig.au8DHCPServerIP[0], 4);
_submask = 0x00FFFFFF;
_gateway = _localip;
#ifdef CONF_PERIPH
// WiFi led ON (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 0);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 0);
#endif
return _status;
}
uint8_t WiFiClass::beginProvision()
{
return beginProvision(1);
}
uint8_t WiFiClass::beginProvision(uint8_t channel)
{
// try to connect using begin
if (begin() != WL_CONNECTED) {
// failed, enter provisioning mode
uint8_t mac[6];
char provSsid[13];
// get MAC address for provisioning SSID
macAddress(mac);
sprintf(provSsid, "wifi101-%.2X%.2X", mac[1], mac[0]);
// start provisioning mode
startProvision(provSsid, "wifi101", channel);
}
return status();
}
uint8_t WiFiClass::startProvision(const char *ssid, const char *url, uint8_t channel)
{
tstrM2MAPConfig strM2MAPConfig;
if (!_init) {
init();
}
// Enter Provision mode:
memset(&strM2MAPConfig, 0x00, sizeof(tstrM2MAPConfig));
strcpy((char *)&strM2MAPConfig.au8SSID, ssid);
strM2MAPConfig.u8ListenChannel = channel;
strM2MAPConfig.u8SecType = M2M_WIFI_SEC_OPEN;
strM2MAPConfig.u8SsidHide = SSID_MODE_VISIBLE;
strM2MAPConfig.au8DHCPServerIP[0] = 192;
strM2MAPConfig.au8DHCPServerIP[1] = 168;
strM2MAPConfig.au8DHCPServerIP[2] = 1;
strM2MAPConfig.au8DHCPServerIP[3] = 1;
if (m2m_wifi_start_provision_mode((tstrM2MAPConfig *)&strM2MAPConfig, (char*)url, 1) < 0) {
_status = WL_PROVISIONING_FAILED;
return _status;
}
_status = WL_PROVISIONING;
_mode = WL_PROV_MODE;
memset(_ssid, 0, M2M_MAX_SSID_LEN);
memcpy(_ssid, ssid, strlen(ssid));
m2m_memcpy((uint8 *)&_localip, (uint8 *)&strM2MAPConfig.au8DHCPServerIP[0], 4);
_submask = 0x00FFFFFF;
_gateway = _localip;
#ifdef CONF_PERIPH
// WiFi led ON (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 0);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 0);
#endif
return _status;
}
uint32_t WiFiClass::provisioned()
{
m2m_wifi_handle_events(NULL);
if (_mode == WL_STA_MODE) {
return 1;
}
else {
return 0;
}
}
void WiFiClass::config(IPAddress local_ip)
{
// Assume the DNS server will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress dns = local_ip;
dns[3] = 1;
config(local_ip, dns);
}
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server)
{
// Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress gateway = local_ip;
gateway[3] = 1;
config(local_ip, dns_server, gateway);
}
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
{
IPAddress subnet(255, 255, 255, 0);
config(local_ip, dns_server, gateway, subnet);
}
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{
tstrM2MIPConfig conf;
if (!_init) {
init();
}
conf.u32DNS = (uint32_t)dns_server;
conf.u32Gateway = (uint32_t)gateway;
conf.u32StaticIP = (uint32_t)local_ip;
conf.u32SubnetMask = (uint32_t)subnet;
_dhcp = 0;
m2m_wifi_enable_dhcp(0); // disable DHCP
m2m_wifi_set_static_ip(&conf);
_localip = conf.u32StaticIP;
_submask = conf.u32SubnetMask;
_gateway = conf.u32Gateway;
}
void WiFiClass::hostname(const char* name)
{
if (!_init) {
init();
}
m2m_wifi_set_device_name((uint8 *)name, strlen(name));
}
void WiFiClass::disconnect()
{
// Close sockets to clean state
for (int i = 0; i < MAX_SOCKET; i++) {
WiFiSocket.close(i);
}
m2m_wifi_disconnect();
#ifdef CONF_PERIPH
// WiFi led OFF (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 1);
#endif
}
void WiFiClass::end()
{
// Close sockets to clean state
for (int i = 0; i < MAX_SOCKET; i++) {
WiFiSocket.close(i);
}
if (_mode == WL_AP_MODE) {
m2m_wifi_disable_ap();
} else {
if (_mode == WL_PROV_MODE) {
m2m_wifi_stop_provision_mode();
}
m2m_wifi_disconnect();
}
#ifdef CONF_PERIPH
// WiFi led OFF (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO15, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO4, 1);
#endif
socketDeinit();
m2m_wifi_deinit(NULL);
nm_bsp_deinit();
_mode = WL_RESET_MODE;
_status = WL_NO_SHIELD;
_init = 0;
}
uint8_t *WiFiClass::macAddress(uint8_t *mac)
{
m2m_wifi_get_mac_address(mac);
byte tmpMac[6], i;
m2m_wifi_get_mac_address(tmpMac);
for(i = 0; i < 6; i++)
mac[i] = tmpMac[5-i];
return mac;
}
uint32_t WiFiClass::localIP()
{
return _localip;
}
uint32_t WiFiClass::subnetMask()
{
return _submask;
}
uint32_t WiFiClass::gatewayIP()
{
return _gateway;
}
char* WiFiClass::SSID()
{
if (_status == WL_CONNECTED || _status == WL_AP_LISTENING || _status == WL_AP_CONNECTED) {
return _ssid;
}
else {
return 0;
}
}
uint8_t* WiFiClass::BSSID(uint8_t* bssid)
{
if (_mode == WL_AP_MODE) {
return macAddress(bssid);
} else {
return remoteMacAddress(bssid);
}
}
uint8_t* WiFiClass::APClientMacAddress(uint8_t* mac)
{
if (_mode == WL_AP_MODE) {
return remoteMacAddress(mac);
} else {
memset(mac, 0, 6);
return mac;
}
}
uint8_t* WiFiClass::remoteMacAddress(uint8_t* remoteMacAddress)
{
_remoteMacAddress = remoteMacAddress;
memset(remoteMacAddress, 0, 6);
m2m_wifi_get_connection_info();
// Wait for connection or timeout:
unsigned long start = millis();
while (_remoteMacAddress != 0 && millis() - start < 1000) {
m2m_wifi_handle_events(NULL);
}
_remoteMacAddress = 0;
return remoteMacAddress;
}
int32_t WiFiClass::RSSI()
{
if (_mode == WL_RESET_MODE) {
return -100;
}
// Clear pending events:
m2m_wifi_handle_events(NULL);
// Send RSSI request:
_resolve = 0;
if (m2m_wifi_req_curr_rssi() < 0) {
return 0;
}
// Wait for connection or timeout:
unsigned long start = millis();
while (_resolve == 0 && millis() - start < 1000) {
m2m_wifi_handle_events(NULL);
}
int32_t rssi = _resolve;
_resolve = 0;
return rssi;
}
int8_t WiFiClass::scanNetworks()
{
wl_status_t tmp = _status;
if (!_init) {
init();
}
// Start scan:
if (m2m_wifi_request_scan(M2M_WIFI_CH_ALL) < 0) {
return 0;
}
// Wait for scan result or timeout:
_status = WL_IDLE_STATUS;
unsigned long start = millis();
while (!(_status & WL_SCAN_COMPLETED) && millis() - start < 5000) {
m2m_wifi_handle_events(NULL);
}
_status = tmp;
return m2m_wifi_get_num_ap_found();
}
char* WiFiClass::SSID(uint8_t pos)
{
wl_status_t tmp = _status;
// Get scan SSID result:
memset(_scan_ssid, 0, M2M_MAX_SSID_LEN);
if (m2m_wifi_req_scan_result(pos) < 0) {
return 0;
}
// Wait for connection or timeout:
_status = WL_IDLE_STATUS;
unsigned long start = millis();
while (!(_status & WL_SCAN_COMPLETED) && millis() - start < 2000) {
m2m_wifi_handle_events(NULL);
}
_status = tmp;
_resolve = 0;
return _scan_ssid;
}
int32_t WiFiClass::RSSI(uint8_t pos)
{
wl_status_t tmp = _status;
// Get scan RSSI result:
if (m2m_wifi_req_scan_result(pos) < 0) {
return 0;
}
// Wait for connection or timeout:
_status = WL_IDLE_STATUS;
unsigned long start = millis();
while (!(_status & WL_SCAN_COMPLETED) && millis() - start < 2000) {
m2m_wifi_handle_events(NULL);
}
_status = tmp;
int32_t rssi = _resolve;
_resolve = 0;
return rssi;
}
uint8_t WiFiClass::encryptionType()
{
int8_t net = scanNetworks();
for (uint8_t i = 0; i < net; ++i) {
SSID(i);
if (strcmp(_scan_ssid, _ssid) == 0) {
break;
}
}
return _scan_auth;
}
uint8_t WiFiClass::encryptionType(uint8_t pos)
{
wl_status_t tmp = _status;
// Get scan auth result:
if (m2m_wifi_req_scan_result(pos) < 0) {
return 0;
}
// Wait for connection or timeout:
_status = WL_IDLE_STATUS;
unsigned long start = millis();
while (!(_status & WL_SCAN_COMPLETED) && millis() - start < 2000) {
m2m_wifi_handle_events(NULL);
}
_status = tmp;
_resolve = 0;
return _scan_auth;
}
uint8_t* WiFiClass::BSSID(uint8_t pos, uint8_t* bssid)
{
wl_status_t tmp = _status;
_remoteMacAddress = bssid;
// Get scan auth result:
if (m2m_wifi_req_scan_result(pos) < 0) {
return 0;
}
// Wait for connection or timeout:
_status = WL_IDLE_STATUS;
unsigned long start = millis();
while (!(_status & WL_SCAN_COMPLETED) && millis() - start < 2000) {
m2m_wifi_handle_events(NULL);
}
_status = tmp;
_resolve = 0;
_remoteMacAddress = 0;
return bssid;
}
uint8_t WiFiClass::channel(uint8_t pos)
{
wl_status_t tmp = _status;
// Get scan auth result:
if (m2m_wifi_req_scan_result(pos) < 0) {
return 0;
}
// Wait for connection or timeout:
_status = WL_IDLE_STATUS;
unsigned long start = millis();
while (!(_status & WL_SCAN_COMPLETED) && millis() - start < 2000) {
m2m_wifi_handle_events(NULL);
}
_status = tmp;
_resolve = 0;
return _scan_channel;