@@ -39,7 +39,8 @@ extern "C" void esp_yield();
39
39
40
40
ESP8266WiFiClass::ESP8266WiFiClass ()
41
41
{
42
-
42
+ useApMode = false ;
43
+ useClientMode = false ;
43
44
}
44
45
45
46
void ESP8266WiFiClass::mode (WiFiMode m)
@@ -49,34 +50,56 @@ void ESP8266WiFiClass::mode(WiFiMode m)
49
50
ETS_UART_INTR_ENABLE ();
50
51
}
51
52
52
-
53
- int ESP8266WiFiClass::begin (const char * ssid)
54
- {
55
- return begin (ssid, 0 );
53
+ int ESP8266WiFiClass::begin (char * ssid, char *passphrase, int32_t channel, uint8_t bssid[6 ]){
54
+ return begin ((const char *) ssid, (const char *) passphrase, channel, bssid);
56
55
}
57
56
57
+ int ESP8266WiFiClass::begin (const char * ssid, const char *passphrase, int32_t channel, uint8_t bssid[6 ]){
58
+ useClientMode = true ;
58
59
59
- int ESP8266WiFiClass::begin (const char * ssid, const char *passphrase)
60
- {
61
- if ((wifi_get_opmode () & 1 ) == 0 )// 1 and 3 have STA enabled
62
- {
60
+ if (useApMode) {
63
61
// turn on AP+STA mode
64
62
mode (WIFI_AP_STA);
63
+ } else {
64
+ // turn on STA mode
65
+ mode (WIFI_STA);
66
+ }
67
+
68
+ if (!ssid || strlen (ssid) > 31 ) {
69
+ // fail SSID to long or missing!
70
+ return WL_CONNECT_FAILED;
71
+ }
72
+
73
+ if (passphrase && strlen (passphrase) > 63 ) {
74
+ // fail passphrase to long!
75
+ return WL_CONNECT_FAILED;
65
76
}
66
77
67
78
struct station_config conf;
68
79
strcpy (reinterpret_cast <char *>(conf.ssid ), ssid);
69
- if (passphrase)
80
+
81
+ if (passphrase) {
70
82
strcpy (reinterpret_cast <char *>(conf.password ), passphrase);
71
- else
83
+ } else {
72
84
*conf.password = 0 ;
85
+ }
73
86
74
- conf.bssid_set = 0 ;
87
+ if (bssid) {
88
+ conf.bssid_set = 1 ;
89
+ memcpy ((void *) &conf.bssid [0 ], (void *) bssid, 6 );
90
+ } else {
91
+ conf.bssid_set = 0 ;
92
+ }
75
93
76
94
ETS_UART_INTR_DISABLE ();
77
95
wifi_station_set_config (&conf);
78
96
wifi_station_connect ();
79
97
ETS_UART_INTR_ENABLE ();
98
+
99
+ if (channel > 0 && channel <= 13 ) {
100
+ wifi_set_channel (channel);
101
+ }
102
+
80
103
wifi_station_dhcpc_start ();
81
104
return status ();
82
105
}
@@ -120,10 +143,22 @@ void ESP8266WiFiClass::softAP(const char* ssid)
120
143
121
144
void ESP8266WiFiClass::softAP (const char * ssid, const char * passphrase, int channel)
122
145
{
123
- if (wifi_get_opmode () < WIFI_AP)// will be OFF or STA
124
- {
146
+ if (useClientMode) {
125
147
// turn on AP+STA mode
126
148
mode (WIFI_AP_STA);
149
+ } else {
150
+ // turn on STA mode
151
+ mode (WIFI_AP);
152
+ }
153
+
154
+ if (!ssid || strlen (ssid) > 31 ) {
155
+ // fail SSID to long or missing!
156
+ return ;
157
+ }
158
+
159
+ if (passphrase && strlen (passphrase) > 63 ) {
160
+ // fail passphrase to long!
161
+ return ;
127
162
}
128
163
129
164
struct softap_config conf;
@@ -209,22 +244,16 @@ char* ESP8266WiFiClass::SSID()
209
244
return reinterpret_cast <char *>(conf.ssid );
210
245
}
211
246
212
- // uint8_t* ESP8266WiFiClass::BSSID(uint8_t* bssid)
213
- // {
214
- // uint8_t* _bssid = WiFiDrv::getCurrentBSSID();
215
- // memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
216
- // return bssid;
217
- // }
218
-
219
- // int32_t ESP8266WiFiClass::RSSI()
220
- // {
221
- // return WiFiDrv::getCurrentRSSI();
222
- // }
247
+ uint8_t * ESP8266WiFiClass::BSSID (void )
248
+ {
249
+ static struct station_config conf;
250
+ wifi_station_get_config (&conf);
251
+ return reinterpret_cast <uint8_t *>(conf.bssid );
252
+ }
223
253
224
- // uint8_t ESP8266WiFiClass::encryptionType()
225
- // {
226
- // return WiFiDrv::getCurrentEncryptionType();
227
- // }
254
+ int32_t ESP8266WiFiClass::Channel (void ) {
255
+ return wifi_get_channel ();
256
+ }
228
257
229
258
extern " C"
230
259
{
@@ -298,7 +327,7 @@ int8_t ESP8266WiFiClass::scanNetworks()
298
327
299
328
void * ESP8266WiFiClass::_getScanInfoByIndex (int i)
300
329
{
301
- if (!ESP8266WiFiClass::_scanResult || i > ESP8266WiFiClass::_scanCount)
330
+ if (!ESP8266WiFiClass::_scanResult || ( size_t ) i > ESP8266WiFiClass::_scanCount)
302
331
{
303
332
return 0 ;
304
333
}
@@ -315,6 +344,49 @@ const char* ESP8266WiFiClass::SSID(uint8_t i)
315
344
return reinterpret_cast <const char *>(it->ssid );
316
345
}
317
346
347
+ uint8_t * ESP8266WiFiClass::BSSID (uint8_t i)
348
+ {
349
+ struct bss_info * it = reinterpret_cast <struct bss_info *>(_getScanInfoByIndex (i));
350
+ if (!it)
351
+ return 0 ;
352
+
353
+ return it->bssid ;
354
+ }
355
+
356
+ int32_t ESP8266WiFiClass::Channel (uint8_t i)
357
+ {
358
+ struct bss_info * it = reinterpret_cast <struct bss_info *>(_getScanInfoByIndex (i));
359
+ if (!it)
360
+ return 0 ;
361
+
362
+ return it->channel ;
363
+ }
364
+
365
+ bool ESP8266WiFiClass::isHidden (uint8_t i)
366
+ {
367
+ struct bss_info * it = reinterpret_cast <struct bss_info *>(_getScanInfoByIndex (i));
368
+ if (!it)
369
+ return false ;
370
+
371
+ return (it->is_hidden != 0 );
372
+ }
373
+
374
+ bool ESP8266WiFiClass::getNetworkInfo (uint8_t i, const char ** ssid, uint8_t * encType, int32_t * RSSI, uint8_t ** BSSID, int32_t * channel, bool * isHidden)
375
+ {
376
+ struct bss_info * it = reinterpret_cast <struct bss_info *>(_getScanInfoByIndex (i));
377
+ if (!it)
378
+ return false ;
379
+
380
+ *ssid = (const char *) &it->ssid [0 ]; // move ptr
381
+ *encType = encryptionType (i);
382
+ *RSSI = it->rssi ;
383
+ *BSSID = &it->bssid [0 ]; // move ptr
384
+ *channel = it->channel ;
385
+ *isHidden = (it->is_hidden != 0 );
386
+
387
+ return true ;
388
+ }
389
+
318
390
int32_t ESP8266WiFiClass::RSSI (uint8_t i)
319
391
{
320
392
struct bss_info * it = reinterpret_cast <struct bss_info *>(_getScanInfoByIndex (i));
0 commit comments