21
21
*/
22
22
23
23
#include < inttypes.h>
24
- #include < pico/cyw43_arch.h>
25
- #include < cyw43.h>
26
24
#include " lwip/raw.h"
27
25
#include " lwip/icmp.h"
28
26
#include " lwip/inet_chksum.h"
31
29
32
30
// This is the real WiFi network object, we just tickle it to do our magic
33
31
#include < LwipEthernet.h>
32
+ #ifdef ARDUINO_RASPBERRY_PI_PICO_W
33
+ #include < pico/cyw43_arch.h>
34
34
static CYW43lwIP _wifi (1 );
35
+ #else
36
+ static NoDriverLwIP _wifi;
37
+ #endif
35
38
36
39
WiFiClass::WiFiClass () {
37
40
}
@@ -75,7 +78,6 @@ int WiFiClass::beginBSSID(const char* ssid, const uint8_t *bssid) {
75
78
return begin (ssid, nullptr , bssid);
76
79
}
77
80
78
- #ifdef ARDUINO_RASPBERRY_PI_PICO_W
79
81
/* Start WiFi connection with passphrase
80
82
the most secure supported mode will be automatically selected
81
83
@@ -166,8 +168,8 @@ uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase) {
166
168
167
169
return WL_CONNECTED;
168
170
}
169
- #endif
170
171
172
+ #ifdef ARDUINO_RASPBERRY_PI_PICO_W
171
173
uint8_t WiFiClass::softAPgetStationNum () {
172
174
if (!_apMode || !_wifiHWInitted) {
173
175
return 0 ;
@@ -182,10 +184,10 @@ uint8_t WiFiClass::softAPgetStationNum() {
182
184
free (macs);
183
185
return m;
184
186
}
185
-
187
+ # endif
186
188
187
189
bool WiFiClass::connected () {
188
- return (_apMode && _wifiHWInitted) || (_wifi.connected () && localIP ().isSet () && ( cyw43_wifi_link_status (&cyw43_state, _apMode ? 1 : 0 ) == CYW43_LINK_JOIN) );
190
+ return (_apMode && _wifiHWInitted) || (_wifi.connected () && localIP ().isSet ());
189
191
}
190
192
191
193
/* Change Ip configuration settings disabling the dhcp client
@@ -270,7 +272,6 @@ int WiFiClass::disconnect(bool wifi_off __unused) {
270
272
}
271
273
if (_wifiHWInitted) {
272
274
_wifiHWInitted = false ;
273
- cyw43_wifi_leave (&cyw43_state, _apMode ? 1 : 0 );
274
275
_wifi.end ();
275
276
}
276
277
return WL_DISCONNECTED;
@@ -288,12 +289,13 @@ void WiFiClass::end(void) {
288
289
return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
289
290
*/
290
291
uint8_t * WiFiClass::macAddress (uint8_t * mac) {
292
+ #ifdef ARDUINO_RASPBERRY_PI_PICO_W
291
293
if (!_wifiHWInitted) {
292
294
_apMode = false ;
293
295
cyw43_wifi_set_up (&cyw43_state, _apMode ? 1 : 0 , true , CYW43_COUNTRY_WORLDWIDE);
294
296
}
295
- cyw43_wifi_get_mac (&cyw43_state, _apMode ? 1 : 0 , mac);
296
- return mac;
297
+ # endif
298
+ return _wifi. macAddress (_apMode, mac) ;
297
299
}
298
300
299
301
/*
@@ -348,30 +350,11 @@ const String &WiFiClass::SSID() {
348
350
return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
349
351
*/
350
352
uint8_t * WiFiClass::BSSID (uint8_t * bssid) {
351
- #ifndef CYW43_IOCTL_GET_BSSID
352
- #define CYW43_IOCTL_GET_BSSID ( (uint32_t )23 * 2 )
353
- #endif
354
-
355
- if (_wifi.connected ()) {
356
- cyw43_ioctl (&cyw43_state, CYW43_IOCTL_GET_BSSID, WL_MAC_ADDR_LENGTH, bssid, CYW43_ITF_STA);
357
- } else {
358
- memset (bssid, 0 , WL_MAC_ADDR_LENGTH);
359
- }
360
- return bssid;
353
+ return _wifi.BSSID (bssid);
361
354
}
362
355
363
356
int WiFiClass::channel () {
364
- #ifndef CYW43_IOCTL_GET_CHANNEL
365
- #define CYW43_IOCTL_GET_CHANNEL 0x3a
366
- #endif
367
-
368
- int32_t channel;
369
- if (_wifi.connected ()) {
370
- cyw43_ioctl (&cyw43_state, CYW43_IOCTL_GET_CHANNEL, sizeof channel, (uint8_t *)&channel, CYW43_ITF_STA);
371
- } else {
372
- channel = -1 ;
373
- }
374
- return channel;
357
+ return _wifi.channel ();
375
358
}
376
359
377
360
@@ -382,17 +365,7 @@ int WiFiClass::channel() {
382
365
return: signed value
383
366
*/
384
367
int32_t WiFiClass::RSSI () {
385
- #ifndef CYW43_IOCTL_GET_RSSI
386
- #define CYW43_IOCTL_GET_RSSI 0xFE
387
- #endif
388
-
389
- int32_t rssi;
390
- if (_wifi.connected ()) {
391
- cyw43_ioctl (&cyw43_state, CYW43_IOCTL_GET_RSSI, sizeof rssi, (uint8_t *)&rssi, CYW43_ITF_STA);
392
- } else {
393
- rssi = -255 ;
394
- }
395
- return rssi;
368
+ return _wifi.RSSI ();
396
369
}
397
370
398
371
/*
@@ -401,31 +374,10 @@ int32_t WiFiClass::RSSI() {
401
374
return: one value of wl_enc_type enum
402
375
*/
403
376
uint8_t WiFiClass::encryptionType () {
404
- // TODO - Driver does not return this?!
405
377
if (_password == nullptr ) {
406
378
return ENC_TYPE_NONE;
407
379
}
408
- return ENC_TYPE_AUTO;
409
- }
410
-
411
- // TODO - this can be in the class
412
- static uint64_t _to64 (uint8_t b[8 ]) {
413
- uint64_t x = 0 ;
414
- for (int i = 0 ; i < 6 ; i++) {
415
- x <<= 8LL ;
416
- x |= b[i] & 255 ;
417
- }
418
- return x;
419
- }
420
-
421
- int WiFiClass::_scanCB (void *env, const cyw43_ev_scan_result_t *result) {
422
- WiFiClass *w = (WiFiClass *)env;
423
- if (result) {
424
- cyw43_ev_scan_result_t s;
425
- memcpy (&s, result, sizeof (s));
426
- w->_scan .insert_or_assign (_to64 (s.bssid ), s);
427
- }
428
- return 0 ;
380
+ return _wifi.encryptionType ();
429
381
}
430
382
431
383
/*
@@ -434,39 +386,22 @@ int WiFiClass::_scanCB(void *env, const cyw43_ev_scan_result_t *result) {
434
386
return: Number of discovered networks
435
387
*/
436
388
int8_t WiFiClass::scanNetworks (bool async) {
437
- cyw43_wifi_scan_options_t scan_options;
438
- memset (&scan_options, 0 , sizeof (scan_options));
439
- _scan.clear ();
440
389
if (!_wifiHWInitted) {
441
390
_apMode = false ;
391
+ #ifdef ARDUINO_RASPBERRY_PI_PICO_W
442
392
cyw43_arch_enable_sta_mode ();
393
+ #endif
443
394
_wifiHWInitted = true ;
444
395
}
445
- int err = cyw43_wifi_scan (&cyw43_state, &scan_options, this , _scanCB);
446
- if (err) {
447
- return 0 ;
448
- }
449
- if (!async) {
450
- uint32_t now = millis ();
451
- while (cyw43_wifi_scan_active (&cyw43_state) && (millis () - now < 10000 )) {
452
- delay (10 );
453
- }
454
- return _scan.size ();
455
- } else {
456
- return -1 ;
457
- }
396
+ return _wifi.scanNetworks (async);
458
397
}
459
398
460
399
int8_t WiFiClass::scanComplete () {
461
- if (cyw43_wifi_scan_active (&cyw43_state)) {
462
- return -1 ;
463
- } else {
464
- return _scan.size ();
465
- }
400
+ return _wifi.scanComplete ();
466
401
}
467
402
468
403
void WiFiClass::scanDelete () {
469
- _scan. clear ();
404
+ _wifi. scanDelete ();
470
405
}
471
406
472
407
/*
@@ -477,14 +412,7 @@ void WiFiClass::scanDelete() {
477
412
return: ssid string of the specified item on the networks scanned list
478
413
*/
479
414
const char *WiFiClass::SSID (uint8_t networkItem) {
480
- if (networkItem >= _scan.size ()) {
481
- return nullptr ;
482
- }
483
- auto it = _scan.begin ();
484
- for (int i = 0 ; i < networkItem; i++) {
485
- ++it;
486
- }
487
- return (const char *)it->second .ssid ;
415
+ return _wifi.SSID (networkItem);
488
416
}
489
417
490
418
/*
@@ -495,44 +423,15 @@ const char*WiFiClass::SSID(uint8_t networkItem) {
495
423
return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list
496
424
*/
497
425
uint8_t WiFiClass::encryptionType (uint8_t networkItem) {
498
- if (networkItem >= _scan.size ()) {
499
- return ENC_TYPE_UNKNOWN;
500
- }
501
- auto it = _scan.begin ();
502
- for (int i = 0 ; i < networkItem; i++) {
503
- ++it;
504
- }
505
- // TODO - the driver returns a small integer but does not actually provide a way of mapping that to the proper enc type. My best guesses here...
506
- switch (it->second .auth_mode ) {
507
- case 0 : return ENC_TYPE_NONE;
508
- case 3 : return ENC_TYPE_TKIP;
509
- case 5 : return ENC_TYPE_CCMP;
510
- case 7 : return ENC_TYPE_AUTO;
511
- }
512
- return ENC_TYPE_UNKNOWN;
426
+ return _wifi.encryptionType (networkItem);
513
427
}
514
428
515
429
uint8_t * WiFiClass::BSSID (uint8_t networkItem, uint8_t * bssid) {
516
- if (networkItem >= _scan.size ()) {
517
- return nullptr ;
518
- }
519
- auto it = _scan.begin ();
520
- for (int i = 0 ; i < networkItem; i++) {
521
- ++it;
522
- }
523
- memcpy (bssid, it->second .bssid , 6 );
524
- return bssid;
430
+ return _wifi.BSSID (networkItem, bssid);
525
431
}
526
432
527
433
uint8_t WiFiClass::channel (uint8_t networkItem) {
528
- if (networkItem >= _scan.size ()) {
529
- return 255 ;
530
- }
531
- auto it = _scan.begin ();
532
- for (int i = 0 ; i < networkItem; i++) {
533
- ++it;
534
- }
535
- return it->second .channel ;
434
+ return _wifi.channel (networkItem);
536
435
}
537
436
538
437
/*
@@ -543,14 +442,7 @@ uint8_t WiFiClass::channel(uint8_t networkItem) {
543
442
return: signed value of RSSI of the specified item on the networks scanned list
544
443
*/
545
444
int32_t WiFiClass::RSSI (uint8_t networkItem) {
546
- if (networkItem >= _scan.size ()) {
547
- return -9999 ;
548
- }
549
- auto it = _scan.begin ();
550
- for (int i = 0 ; i < networkItem; i++) {
551
- ++it;
552
- }
553
- return it->second .rssi ;
445
+ return _wifi.RSSI (networkItem);
554
446
}
555
447
556
448
/*
@@ -562,14 +454,7 @@ uint8_t WiFiClass::status() {
562
454
if (_apMode && _wifiHWInitted) {
563
455
return WL_CONNECTED;
564
456
}
565
- switch (cyw43_wifi_link_status (&cyw43_state, _apMode ? 1 : 0 )) {
566
- case CYW43_LINK_DOWN: return WL_IDLE_STATUS;
567
- case CYW43_LINK_JOIN: return localIP ().isSet () ? WL_CONNECTED : WL_DISCONNECTED;
568
- case CYW43_LINK_FAIL: return WL_CONNECT_FAILED;
569
- case CYW43_LINK_NONET: return WL_CONNECT_FAILED;
570
- case CYW43_LINK_BADAUTH: return WL_CONNECT_FAILED;
571
- }
572
- return WL_NO_MODULE;
457
+ return _wifi.status ();
573
458
}
574
459
575
460
/*
@@ -599,17 +484,19 @@ unsigned long WiFiClass::getTime() {
599
484
return millis ();
600
485
}
601
486
487
+ #ifdef ARDUINO_RASPBERRY_PI_PICO_W
602
488
void WiFiClass::aggressiveLowPowerMode () {
603
489
cyw43_wifi_pm (&cyw43_state, CYW43_AGGRESSIVE_PM);
604
490
}
605
491
606
492
void WiFiClass::defaultLowPowerMode () {
607
493
cyw43_wifi_pm (&cyw43_state, CYW43_DEFAULT_PM);
608
494
}
495
+ #endif
609
496
610
497
// The difference between the default CYW43_DEFAULT_PM (0xA11142) and not low power (0xA11140) is that it changed from "Powersave mode on specified interface with High throughput" to "No Powersave mode". All other parameters stayed the same.
611
498
void WiFiClass::noLowPowerMode () {
612
- cyw43_wifi_pm (&cyw43_state, 0xA11140 );
499
+ _wifi. noLowPowerMode ( );
613
500
}
614
501
615
502
int WiFiClass::ping (const char * hostname, uint8_t ttl) {
0 commit comments