forked from adafruit/Adafruit_CC3000_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_CC3000.cpp
1660 lines (1408 loc) · 47.2 KB
/
Adafruit_CC3000.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
/**************************************************************************/
/*!
@file Adafruit_CC3000.cpp
@author KTOWN (Kevin Townsend for Adafruit Industries)
@license BSD (see license.txt)
This is a library for the Adafruit CC3000 WiFi breakout board
This library works with the Adafruit CC3000 breakout
----> https://www.adafruit.com/products/1469
Check out the links above for our tutorials and wiring diagrams
These chips use SPI to communicate.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - Initial release
*/
/**************************************************************************/
#include "Adafruit_CC3000.h"
#include "ccspi.h"
#include "utility/cc3000_common.h"
#include "utility/evnt_handler.h"
#include "utility/hci.h"
#include "utility/netapp.h"
#include "utility/nvmem.h"
#include "utility/security.h"
#include "utility/socket.h"
#include "utility/wlan.h"
#include "utility/debug.h"
#include "utility/sntp.h"
uint8_t g_csPin, g_irqPin, g_vbatPin, g_IRQnum, g_SPIspeed;
static const uint8_t dreqinttable[] = {
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined (__AVR_ATmega328__) || defined(__AVR_ATmega8__)
2, 0,
3, 1,
#elif defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
2, 0,
3, 1,
21, 2,
20, 3,
19, 4,
18, 5,
#elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)
5, 0,
6, 1,
7, 2,
8, 3,
#elif defined(__AVR_AT90USB1286__) && defined(CORE_TEENSY)
0, 0,
1, 1,
2, 2,
3, 3,
36, 4,
37, 5,
18, 6,
19, 7,
#elif defined(__arm__) && defined(CORE_TEENSY)
0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
10, 10, 11, 11, 12, 12, 13, 13, 14, 14,
15, 15, 16, 16, 17, 17, 18, 18, 19, 19,
20, 20, 21, 21, 22, 22, 23, 23, 24, 24,
25, 25, 26, 26, 27, 27, 28, 28, 29, 29,
30, 30, 31, 31, 32, 32, 33, 33,
#elif defined(__AVR_ATmega32U4__)
7, 4,
3, 0,
2, 1,
0, 2,
1, 3,
#elif defined(__arm__) && defined(__SAM3X8E__) // Arduino Due
0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
10, 10, 11, 11, 12, 12, 13, 13, 14, 14,
15, 15, 16, 16, 17, 17, 18, 18, 19, 19,
20, 20, 21, 21, 22, 22, 23, 23, 24, 24,
25, 25, 26, 26, 27, 27, 28, 28, 29, 29,
30, 30, 31, 31, 32, 32, 33, 33, 34, 34,
35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
40, 40, 41, 41, 42, 42, 43, 43, 44, 44,
45, 45, 46, 46, 47, 47, 48, 48, 49, 49,
50, 50, 51, 51, 52, 52, 53, 53, 54, 54,
55, 55, 56, 56, 57, 57, 58, 58, 59, 59,
60, 60, 61, 61, 62, 62, 63, 63, 64, 64,
65, 65, 66, 66, 67, 67, 68, 68, 69, 69,
70, 70, 71, 71,
#endif
};
/***********************/
uint8_t pingReportnum;
netapp_pingreport_args_t pingReport;
#define CC3000_SUCCESS (0)
#define CHECK_SUCCESS(func,Notify,errorCode) {if ((func) != CC3000_SUCCESS) { CHECK_PRINTER CC3KPrinter->println(F(Notify)); return errorCode;}}
#define MAXSSID (32)
#define MAXLENGTHKEY (32) /* Cleared for 32 bytes by TI engineering 29/08/13 */
#define MAX_SOCKETS 32 // can change this
boolean closed_sockets[MAX_SOCKETS] = {false, false, false, false};
/* *********************************************************************** */
/* */
/* PRIVATE FIELDS (SmartConfig) */
/* */
/* *********************************************************************** */
class CC3000BitSet {
public:
static const byte IsSmartConfigFinished = 0x01;
static const byte IsConnected = 0x02;
static const byte HasDHCP = 0x04;
static const byte OkToShutDown = 0x08;
void clear() {
flags = 0;
}
bool test(const byte flag) {
return (flags & flag) != 0;
}
void set(const byte flag) {
flags |= flag;
}
void reset(const byte flag) {
flags &= ~flag;
}
private:
volatile byte flags;
}cc3000Bitset;
volatile long ulSocket;
char _cc3000_prefix[] = { 'T', 'T', 'T' };
Print* CC3KPrinter; // user specified output stream for general messages and debug
/* *********************************************************************** */
/* */
/* PRIVATE FUNCTIONS */
/* */
/* *********************************************************************** */
/**************************************************************************/
/*!
@brief Scans for SSID/APs in the CC3000's range
@note This command isn't available when the CC3000 is configured
in 'CC3000_TINY_DRIVER' mode
@returns False if an error occured!
*/
/**************************************************************************/
#ifndef CC3000_TINY_DRIVER
bool Adafruit_CC3000::scanSSIDs(uint32_t time)
{
const unsigned long intervalTime[16] = { 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000 };
if (!_initialised)
{
return false;
}
// We can abort a scan with a time of 0
if (time)
{
CHECK_PRINTER {
CC3KPrinter->println(F("Started AP/SSID scan\n\r"));
}
}
// Set SSID Scan params to includes channels above 11
CHECK_SUCCESS(
wlan_ioctl_set_scan_params(time, 20, 100, 5, 0x1FFF, -120, 0, 300,
(unsigned long * ) &intervalTime),
"Failed setting params for SSID scan", false);
return true;
}
#endif
/* *********************************************************************** */
/* */
/* CONSTRUCTORS */
/* */
/* *********************************************************************** */
/**************************************************************************/
/*!
@brief Instantiates a new CC3000 class.
Note that by default this class will assume the first hardware
serial should be used for debug output. This behavior can be
changed by explicitly specifying a cc3kPrinter parameter.
*/
/**************************************************************************/
Adafruit_CC3000::Adafruit_CC3000(uint8_t csPin, uint8_t irqPin, uint8_t vbatPin, uint8_t SPIspeed, Print* cc3kPrinter)
{
_initialised = false;
g_csPin = csPin;
g_irqPin = irqPin;
g_vbatPin = vbatPin;
g_IRQnum = 0xFF;
g_SPIspeed = SPIspeed;
cc3000Bitset.clear();
CC3KPrinter = cc3kPrinter;
}
/* *********************************************************************** */
/* */
/* PUBLIC FUNCTIONS */
/* */
/* *********************************************************************** */
/**************************************************************************/
/*!
@brief Setups the HW
@args[in] patchReq
Set this to true if we are starting a firmware patch,
otherwise false for normal operation
@args[in] useSmartConfig
Set this to true if you want to use the connection details
that were stored on the device from the SmartConfig process,
otherwise false to erase existing profiles and start a
clean connection
*/
/**************************************************************************/
bool Adafruit_CC3000::begin(uint8_t patchReq, bool useSmartConfigData, const char *_deviceName)
{
if (_initialised) return true;
#ifndef CORE_ADAX
// determine irq #
for (uint8_t i=0; i<sizeof(dreqinttable); i+=2) {
if (g_irqPin == dreqinttable[i]) {
g_IRQnum = dreqinttable[i+1];
}
}
if (g_IRQnum == 0xFF) {
CHECK_PRINTER {
CC3KPrinter->println(F("IRQ pin is not an INT pin!"));
}
return false;
}
#else
g_IRQnum = g_irqPin;
// (almost) every single pin on Xmega supports interrupt
#endif
init_spi();
DEBUGPRINT_F("init\n\r");
wlan_init(CC3000_UsynchCallback,
sendWLFWPatch, sendDriverPatch, sendBootLoaderPatch,
ReadWlanInterruptPin,
WlanInterruptEnable,
WlanInterruptDisable,
WriteWlanPin);
DEBUGPRINT_F("start\n\r");
wlan_start(patchReq);
DEBUGPRINT_F("ioctl\n\r");
// Check if we should erase previous stored connection details
// (most likely written with data from the SmartConfig app)
if (!useSmartConfigData)
{
// Manual connection only (no auto, profiles, etc.)
wlan_ioctl_set_connection_policy(0, 0, 0);
// Delete previous profiles from memory
wlan_ioctl_del_profile(255);
}
else
{
// Auto Connect - the C3000 device tries to connect to any AP it detects during scanning:
// wlan_ioctl_set_connection_policy(1, 0, 0)
// Fast Connect - the CC3000 device tries to reconnect to the last AP connected to:
// wlan_ioctl_set_connection_policy(0, 1, 0)
// Use Profiles - the CC3000 device tries to connect to an AP from profiles:
wlan_ioctl_set_connection_policy(0, 0, 1);
}
CHECK_SUCCESS(
wlan_set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT |
//HCI_EVNT_WLAN_ASYNC_PING_REPORT |// we want ping reports
//HCI_EVNT_BSD_TCP_CLOSE_WAIT |
//HCI_EVNT_WLAN_TX_COMPLETE |
HCI_EVNT_WLAN_KEEPALIVE),
"WLAN Set Event Mask FAIL", false);
_initialised = true;
// Wait for re-connection if we're using SmartConfig data
if (useSmartConfigData)
{
// Wait for a connection
uint32_t timeout = 0;
while(!cc3000Bitset.test(CC3000BitSet::IsConnected))
{
cc3k_int_poll();
if(timeout > WLAN_CONNECT_TIMEOUT)
{
CHECK_PRINTER {
CC3KPrinter->println(F("Timed out using SmartConfig data"));
}
return false;
}
timeout += 10;
delay(10);
}
delay(1000);
if (cc3000Bitset.test(CC3000BitSet::HasDHCP))
{
mdnsAdvertiser(1, (char *) _deviceName, strlen(_deviceName));
}
}
return true;
}
/**************************************************************************/
/*!
@brief Prints a hexadecimal value in plain characters
@param data Pointer to the byte data
@param numBytes Data length in bytes
*/
/**************************************************************************/
void Adafruit_CC3000::printHex(const byte * data, const uint32_t numBytes)
{
if (CC3KPrinter == 0) return;
uint32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
CC3KPrinter->print(F("0x"));
// Append leading 0 for small values
if (data[szPos] <= 0xF)
CC3KPrinter->print(F("0"));
CC3KPrinter->print(data[szPos], HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
CC3KPrinter->print(' ');
}
}
CC3KPrinter->println();
}
/**************************************************************************/
/*!
@brief Prints a hexadecimal value in plain characters, along with
the char equivalents in the following format
00 00 00 00 00 00 ......
@param data Pointer to the byte data
@param numBytes Data length in bytes
*/
/**************************************************************************/
void Adafruit_CC3000::printHexChar(const byte * data, const uint32_t numBytes)
{
if (CC3KPrinter == 0) return;
uint32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
// Append leading 0 for small values
if (data[szPos] <= 0xF)
CC3KPrinter->print('0');
CC3KPrinter->print(data[szPos], HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
CC3KPrinter->print(' ');
}
}
CC3KPrinter->print(" ");
for (szPos=0; szPos < numBytes; szPos++)
{
if (data[szPos] <= 0x1F)
CC3KPrinter->print('.');
else
CC3KPrinter->print(data[szPos]);
}
CC3KPrinter->println();
}
/**************************************************************************/
/*!
@brief Helper function to display an IP address with dots
*/
/**************************************************************************/
void Adafruit_CC3000::printIPdots(uint32_t ip) {
if (CC3KPrinter == 0) return;
CC3KPrinter->print((uint8_t)(ip));
CC3KPrinter->print('.');
CC3KPrinter->print((uint8_t)(ip >> 8));
CC3KPrinter->print('.');
CC3KPrinter->print((uint8_t)(ip >> 16));
CC3KPrinter->print('.');
CC3KPrinter->print((uint8_t)(ip >> 24));
}
/**************************************************************************/
/*!
@brief Helper function to display an IP address with dots, printing
the bytes in reverse order
*/
/**************************************************************************/
void Adafruit_CC3000::printIPdotsRev(uint32_t ip) {
if (CC3KPrinter == 0) return;
CC3KPrinter->print((uint8_t)(ip >> 24));
CC3KPrinter->print('.');
CC3KPrinter->print((uint8_t)(ip >> 16));
CC3KPrinter->print('.');
CC3KPrinter->print((uint8_t)(ip >> 8));
CC3KPrinter->print('.');
CC3KPrinter->print((uint8_t)(ip));
}
/**************************************************************************/
/*!
@brief Helper function to convert four bytes to a U32 IP value
*/
/**************************************************************************/
uint32_t Adafruit_CC3000::IP2U32(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
uint32_t ip = a;
ip <<= 8;
ip |= b;
ip <<= 8;
ip |= c;
ip <<= 8;
ip |= d;
return ip;
}
/**************************************************************************/
/*!
@brief Reboot CC3000 (stop then start)
*/
/**************************************************************************/
void Adafruit_CC3000::reboot(uint8_t patch)
{
if (!_initialised)
{
return;
}
wlan_stop();
delay(5000);
wlan_start(patch);
}
/**************************************************************************/
/*!
@brief Stop CC3000
*/
/**************************************************************************/
void Adafruit_CC3000::stop(void)
{
if (!_initialised)
{
return;
}
wlan_stop();
}
/**************************************************************************/
/*!
@brief Disconnects from the network
@returns False if an error occured!
*/
/**************************************************************************/
bool Adafruit_CC3000::disconnect(void)
{
if (!_initialised)
{
return false;
}
long retVal = wlan_disconnect();
return retVal != 0 ? false : true;
}
/**************************************************************************/
/*!
@brief Deletes all profiles stored in the CC3000
@returns False if an error occured!
*/
/**************************************************************************/
bool Adafruit_CC3000::deleteProfiles(void)
{
if (!_initialised)
{
return false;
}
CHECK_SUCCESS(wlan_ioctl_set_connection_policy(0, 0, 0),
"deleteProfiles connection failure", false);
CHECK_SUCCESS(wlan_ioctl_del_profile(255),
"Failed deleting profiles", false);
return true;
}
/**************************************************************************/
/*!
@brief Reads the MAC address
@param address Buffer to hold the 6 byte Mac Address
@returns False if an error occured!
*/
/**************************************************************************/
bool Adafruit_CC3000::getMacAddress(uint8_t address[6])
{
if (!_initialised)
{
return false;
}
CHECK_SUCCESS(nvmem_read(NVMEM_MAC_FILEID, 6, 0, address),
"Failed reading MAC address!", false);
return true;
}
/**************************************************************************/
/*!
@brief Sets a new MAC address
@param address Buffer pointing to the 6 byte Mac Address
@returns False if an error occured!
*/
/**************************************************************************/
bool Adafruit_CC3000::setMacAddress(uint8_t address[6])
{
if (!_initialised)
{
return false;
}
if (address[0] == 0)
{
return false;
}
CHECK_SUCCESS(netapp_config_mac_adrress(address),
"Failed setting MAC address!", false);
wlan_stop();
delay(200);
wlan_start(0);
return true;
}
/**************************************************************************/
/*!
@brief Set the CC3000 to use a static IP address when it's connected
to the network. Use the cc3000.IP2U32 function to specify the
IP, subnet mask (typically 255.255.255.0), default gateway
(typically 192.168.1.1), and DNS server (can use Google's DNS
of 8.8.8.8 or 8.8.4.4). Note that the static IP configuration
will be saved in the CC3000's non-volatile storage and reused
on next reconnect. This means you only need to call this once
and the CC3000 will remember the setting forever. To revert
back to use DHCP, call the cc3000.setDHCP function.
@param ip IP address
@param subnetmask Subnet mask
@param defaultGateway Default gateway
@param dnsServer DNS server
@returns False if an error occurred, true if successfully set.
*/
/**************************************************************************/
bool Adafruit_CC3000::setStaticIPAddress(uint32_t ip, uint32_t subnetMask, uint32_t defaultGateway, uint32_t dnsServer)
{
// Reverse order of bytes in parameters so IP2U32 packed values can be used with the netapp_dhcp function.
ip = (ip >> 24) | ((ip >> 8) & 0x0000FF00L) | ((ip << 8) & 0x00FF0000L) | (ip << 24);
subnetMask = (subnetMask >> 24) | ((subnetMask >> 8) & 0x0000FF00L) | ((subnetMask << 8) & 0x00FF0000L) | (subnetMask << 24);
defaultGateway = (defaultGateway >> 24) | ((defaultGateway >> 8) & 0x0000FF00L) | ((defaultGateway << 8) & 0x00FF0000L) | (defaultGateway << 24);
dnsServer = (dnsServer >> 24) | ((dnsServer >> 8) & 0x0000FF00L) | ((dnsServer << 8) & 0x00FF0000L) | (dnsServer << 24);
// Update DHCP state with specified values.
if (netapp_dhcp(&ip, &subnetMask, &defaultGateway, &dnsServer) != 0) {
return false;
}
// Reset CC3000 to use modified setting.
wlan_stop();
delay(200);
wlan_start(0);
return true;
}
/**************************************************************************/
/*!
@brief Set the CC3000 to use request an IP and network configuration
using DHCP. Note that this DHCP state will be saved in the
CC3000's non-volatile storage and reused on next reconnect.
This means you only need to call this once and the CC3000 will
remember the setting forever. To switch to use a static IP,
call the cc3000.setStaticIPAddress function.
@returns False if an error occurred, true if successfully set.
*/
/**************************************************************************/
bool Adafruit_CC3000::setDHCP()
{
return setStaticIPAddress(0,0,0,0);
}
/**************************************************************************/
/*!
@brief Reads the current IP address
@returns False if an error occured!
*/
/**************************************************************************/
bool Adafruit_CC3000::getIPAddress(uint32_t *retip, uint32_t *netmask, uint32_t *gateway, uint32_t *dhcpserv, uint32_t *dnsserv)
{
if (!_initialised) return false;
if (!cc3000Bitset.test(CC3000BitSet::IsConnected)) return false;
if (!cc3000Bitset.test(CC3000BitSet::HasDHCP)) return false;
tNetappIpconfigRetArgs ipconfig;
netapp_ipconfig(&ipconfig);
/* If byte 1 is 0 we don't have a valid address */
if (ipconfig.aucIP[3] == 0) return false;
memcpy(retip, ipconfig.aucIP, 4);
memcpy(netmask, ipconfig.aucSubnetMask, 4);
memcpy(gateway, ipconfig.aucDefaultGateway, 4);
memcpy(dhcpserv, ipconfig.aucDHCPServer, 4);
memcpy(dnsserv, ipconfig.aucDNSServer, 4);
return true;
}
/**************************************************************************/
/*!
@brief Gets the two byte ID for the firmware patch version
@note This command isn't available when the CC3000 is configured
in 'CC3000_TINY_DRIVER' mode
@returns False if an error occured!
*/
/**************************************************************************/
#ifndef CC3000_TINY_DRIVER
bool Adafruit_CC3000::getFirmwareVersion(uint8_t *major, uint8_t *minor)
{
uint8_t fwpReturn[2];
if (!_initialised)
{
return false;
}
CHECK_SUCCESS(nvmem_read_sp_version(fwpReturn),
"Unable to read the firmware version", false);
*major = fwpReturn[0];
*minor = fwpReturn[1];
return true;
}
#endif
/**************************************************************************/
/*!
@Brief Prints out the current status flag of the CC3000
@note This command isn't available when the CC3000 is configured
in 'CC3000_TINY_DRIVER' mode
*/
/**************************************************************************/
#ifndef CC3000_TINY_DRIVER
status_t Adafruit_CC3000::getStatus()
{
if (!_initialised)
{
return STATUS_DISCONNECTED;
}
long results = wlan_ioctl_statusget();
switch(results)
{
case 1:
return STATUS_SCANNING;
break;
case 2:
return STATUS_CONNECTING;
break;
case 3:
return STATUS_CONNECTED;
break;
case 0:
default:
return STATUS_DISCONNECTED;
break;
}
}
#endif
/**************************************************************************/
/*!
@brief Calls listSSIDs and then displays the results of the SSID scan
For the moment we only list these via CC3KPrinter->print since
this can consume a lot of memory passing all the data
back with a buffer
@note This command isn't available when the CC3000 is configured
in 'CC3000_TINY_DRIVER' mode
@returns False if an error occured!
*/
/**************************************************************************/
#ifndef CC3000_TINY_DRIVER
ResultStruct_t SSIDScanResultBuff;
bool Adafruit_CC3000::startSSIDscan(uint32_t *index) {
if (!_initialised)
{
return false;
}
// Setup a 4 second SSID scan
if (!scanSSIDs(4000))
{
// Oops ... SSID scan failed
return false;
}
// Wait for results
delay(4500);
CHECK_SUCCESS(wlan_ioctl_get_scan_results(0, (uint8_t* ) &SSIDScanResultBuff),
"SSID scan failed!", false);
*index = SSIDScanResultBuff.num_networks;
return true;
}
void Adafruit_CC3000::stopSSIDscan(void) {
// Stop scanning
scanSSIDs(0);
}
uint8_t Adafruit_CC3000::getNextSSID(uint8_t *rssi, uint8_t *secMode, char *ssidname) {
uint8_t valid = (SSIDScanResultBuff.rssiByte & (~0xFE));
*rssi = (SSIDScanResultBuff.rssiByte >> 1);
*secMode = (SSIDScanResultBuff.Sec_ssidLen & (~0xFC));
uint8_t ssidLen = (SSIDScanResultBuff.Sec_ssidLen >> 2);
strncpy(ssidname, (char *)SSIDScanResultBuff.ssid_name, ssidLen);
ssidname[ssidLen] = 0;
CHECK_SUCCESS(wlan_ioctl_get_scan_results(0, (uint8_t* ) &SSIDScanResultBuff),
"Problem with the SSID scan results", false);
return valid;
}
#endif
/**************************************************************************/
/*!
@brief Starts the smart config connection process
@note This command isn't available when the CC3000 is configured
in 'CC3000_TINY_DRIVER' mode
@returns False if an error occured!
*/
/**************************************************************************/
#ifndef CC3000_TINY_DRIVER
bool Adafruit_CC3000::startSmartConfig(const char *_deviceName, const char *smartConfigKey)
{
bool enableAES = smartConfigKey != NULL;
cc3000Bitset.clear();
uint32_t timeout = 0;
if (!_initialised) {
return false;
}
// Reset all the previous configurations
CHECK_SUCCESS(wlan_ioctl_set_connection_policy(WIFI_DISABLE, WIFI_DISABLE, WIFI_DISABLE),
"Failed setting the connection policy", false);
// Delete existing profile data
CHECK_SUCCESS(wlan_ioctl_del_profile(255),
"Failed deleting existing profiles", false);
// CC3KPrinter->println("Disconnecting");
// Wait until CC3000 is disconnected
while (cc3000Bitset.test(CC3000BitSet::IsConnected)) {
cc3k_int_poll();
CHECK_SUCCESS(wlan_disconnect(),
"Failed to disconnect from AP", false);
delay(10);
hci_unsolicited_event_handler();
}
// Reset the CC3000
wlan_stop();
delay(1000);
wlan_start(0);
// create new entry for AES encryption key
CHECK_SUCCESS(nvmem_create_entry(NVMEM_AES128_KEY_FILEID,16),
"Failed create NVMEM entry", false);
if (enableAES)
{
// write AES key to NVMEM
CHECK_SUCCESS(aes_write_key((unsigned char *)(smartConfigKey)),
"Failed writing AES key", false);
}
//CC3KPrinter->println("Set prefix");
// Wait until CC3000 is disconnected
CHECK_SUCCESS(wlan_smart_config_set_prefix((char *)&_cc3000_prefix),
"Failed setting the SmartConfig prefix", false);
//CC3KPrinter->println("Start config");
// Start the SmartConfig start process
CHECK_SUCCESS(wlan_smart_config_start(enableAES),
"Failed starting smart config", false);
// Wait for smart config process complete (event in CC3000_UsynchCallback)
while (!cc3000Bitset.test(CC3000BitSet::IsSmartConfigFinished))
{
cc3k_int_poll();
// waiting here for event SIMPLE_CONFIG_DONE
timeout+=10;
if (timeout > 60000) // ~60s
{
return false;
}
delay(10); // ms
// CC3KPrinter->print('.');
}
CHECK_PRINTER {
CC3KPrinter->println(F("Got smart config data"));
}
if (enableAES) {
CHECK_SUCCESS(wlan_smart_config_process(),
"wlan_smart_config_process failed",
false);
}
// ******************************************************
// Decrypt configuration information and add profile
// ToDo: This is causing stack overflow ... investigate
// CHECK_SUCCESS(wlan_smart_config_process(),
// "Smart config failed", false);
// ******************************************************
// Connect automatically to the AP specified in smart config settings
CHECK_SUCCESS(wlan_ioctl_set_connection_policy(WIFI_DISABLE, WIFI_DISABLE, WIFI_ENABLE),
"Failed setting connection policy", false);
// Reset the CC3000
wlan_stop();
delay(1000);
wlan_start(0);
// Mask out all non-required events
CHECK_SUCCESS(wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE |
HCI_EVNT_WLAN_UNSOL_INIT
//HCI_EVNT_WLAN_ASYNC_PING_REPORT |
//HCI_EVNT_WLAN_TX_COMPLETE
),
"Failed setting event mask", false);
// Wait for a connection
timeout = 0;
while(!cc3000Bitset.test(CC3000BitSet::IsConnected))
{
cc3k_int_poll();
if(timeout > WLAN_CONNECT_TIMEOUT) // ~20s
{
CHECK_PRINTER {
CC3KPrinter->println(F("Timed out waiting to connect"));
}
return false;
}
timeout += 10;
delay(10);
}
delay(1000);
if (cc3000Bitset.test(CC3000BitSet::HasDHCP))
{
mdnsAdvertiser(1, (char *) _deviceName, strlen(_deviceName));
}
return true;
}
#endif
/**************************************************************************/
/*!
Connect to an unsecured SSID/AP(security)
@param ssid The named of the AP to connect to (max 32 chars)
@returns False if an error occured!
*/
/**************************************************************************/
bool Adafruit_CC3000::connectOpen(const char *ssid)
{
if (!_initialised) {
return false;
}
#ifndef CC3000_TINY_DRIVER
CHECK_SUCCESS(wlan_ioctl_set_connection_policy(0, 0, 0),
"Failed to set connection policy", false);
delay(500);
CHECK_SUCCESS(wlan_connect(WLAN_SEC_UNSEC,
(const char*)ssid, strlen(ssid),
0 ,NULL,0),
"SSID connection failed", false);
#else
wlan_connect(ssid, strlen(ssid));
#endif
return true;
}
//*****************************************************************************
//
//! CC3000_UsynchCallback
//!
//! @param lEventType Event type
//! @param data
//! @param length
//!
//! @return none
//!
//! @brief The function handles asynchronous events that come from CC3000
//! device and operates a led for indicate
//
//*****************************************************************************
void CC3000_UsynchCallback(long lEventType, char * data, unsigned char length)
{
if (lEventType == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE)
{
cc3000Bitset.set(CC3000BitSet::IsSmartConfigFinished);
}
if (lEventType == HCI_EVNT_WLAN_UNSOL_CONNECT)
{
cc3000Bitset.set(CC3000BitSet::IsConnected);
}
if (lEventType == HCI_EVNT_WLAN_UNSOL_DISCONNECT)
{
cc3000Bitset.reset(CC3000BitSet::IsConnected | CC3000BitSet::HasDHCP);
}
if (lEventType == HCI_EVNT_WLAN_UNSOL_DHCP)
{
cc3000Bitset.set(CC3000BitSet::HasDHCP);
}
if (lEventType == HCI_EVENT_CC3000_CAN_SHUT_DOWN)
{
cc3000Bitset.set(CC3000BitSet::OkToShutDown);
}