Skip to content

Commit 4ef2700

Browse files
committed
Merge branch 'Links2004-esp8266' into esp8266
* Links2004-esp8266: ESP8266WiFi extended functions
2 parents cec2fb4 + 3c7f046 commit 4ef2700

File tree

3 files changed

+186
-49
lines changed

3 files changed

+186
-49
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

+102-30
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ extern "C" void esp_yield();
3939

4040
ESP8266WiFiClass::ESP8266WiFiClass()
4141
{
42-
42+
useApMode = false;
43+
useClientMode = false;
4344
}
4445

4546
void ESP8266WiFiClass::mode(WiFiMode m)
@@ -49,34 +50,56 @@ void ESP8266WiFiClass::mode(WiFiMode m)
4950
ETS_UART_INTR_ENABLE();
5051
}
5152

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);
5655
}
5756

57+
int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, uint8_t bssid[6]){
58+
useClientMode = true;
5859

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) {
6361
// turn on AP+STA mode
6462
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;
6576
}
6677

6778
struct station_config conf;
6879
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
69-
if (passphrase)
80+
81+
if (passphrase) {
7082
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
71-
else
83+
} else {
7284
*conf.password = 0;
85+
}
7386

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+
}
7593

7694
ETS_UART_INTR_DISABLE();
7795
wifi_station_set_config(&conf);
7896
wifi_station_connect();
7997
ETS_UART_INTR_ENABLE();
98+
99+
if(channel > 0 && channel <= 13) {
100+
wifi_set_channel(channel);
101+
}
102+
80103
wifi_station_dhcpc_start();
81104
return status();
82105
}
@@ -120,10 +143,22 @@ void ESP8266WiFiClass::softAP(const char* ssid)
120143

121144
void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel)
122145
{
123-
if (wifi_get_opmode() < WIFI_AP)//will be OFF or STA
124-
{
146+
if(useClientMode) {
125147
// turn on AP+STA mode
126148
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;
127162
}
128163

129164
struct softap_config conf;
@@ -209,22 +244,16 @@ char* ESP8266WiFiClass::SSID()
209244
return reinterpret_cast<char*>(conf.ssid);
210245
}
211246

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+
}
223253

224-
// uint8_t ESP8266WiFiClass::encryptionType()
225-
// {
226-
// return WiFiDrv::getCurrentEncryptionType();
227-
// }
254+
int32_t ESP8266WiFiClass::Channel(void) {
255+
return wifi_get_channel();
256+
}
228257

229258
extern "C"
230259
{
@@ -298,7 +327,7 @@ int8_t ESP8266WiFiClass::scanNetworks()
298327

299328
void * ESP8266WiFiClass::_getScanInfoByIndex(int i)
300329
{
301-
if (!ESP8266WiFiClass::_scanResult || i > ESP8266WiFiClass::_scanCount)
330+
if (!ESP8266WiFiClass::_scanResult || (size_t)i > ESP8266WiFiClass::_scanCount)
302331
{
303332
return 0;
304333
}
@@ -315,6 +344,49 @@ const char* ESP8266WiFiClass::SSID(uint8_t i)
315344
return reinterpret_cast<const char*>(it->ssid);
316345
}
317346

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+
318390
int32_t ESP8266WiFiClass::RSSI(uint8_t i)
319391
{
320392
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h

+63-13
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,18 @@ class ESP8266WiFiClass
4242

4343
void mode(WiFiMode);
4444

45-
46-
/* Start Wifi connection for OPEN networks
47-
*
48-
* param ssid: Pointer to the SSID string.
45+
/**
46+
* Start Wifi connection
47+
* if passphrase is set the most secure supported mode will be automatically selected
48+
* @param ssid const char* Pointer to the SSID string.
49+
* @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal).
50+
* @param bssid uint8_t[6] Optional. BSSID / MAC of AP
51+
* @param channel Optional. Channel of AP
52+
* @return
4953
*/
50-
int begin(const char* ssid);
54+
int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
55+
int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
5156

52-
/* Start Wifi connection with passphrase
53-
* the most secure supported mode will be automatically selected
54-
*
55-
* param ssid: Pointer to the SSID string.
56-
* param passphrase: Passphrase. Valid characters in a passphrase
57-
* must be between ASCII 32-126 (decimal).
58-
*/
59-
int begin(const char* ssid, const char *passphrase);
6057

6158
/* Wait for Wifi connection to reach a result
6259
* returns the status reached or disconnect if STA is off
@@ -151,6 +148,20 @@ class ESP8266WiFiClass
151148
*/
152149
char* SSID();
153150

151+
/*
152+
* Return the current bssid / mac associated with the network if configured
153+
*
154+
* return: bssid string
155+
*/
156+
uint8_t * BSSID(void);
157+
158+
/*
159+
* Return the current channel associated with the network
160+
*
161+
* return: channel
162+
*/
163+
int32_t Channel(void);
164+
154165
/*
155166
* Return the current network RSSI. Note: this is just a stub, there is no way to
156167
* get the RSSI in the Espressif SDK yet.
@@ -194,6 +205,42 @@ class ESP8266WiFiClass
194205
*/
195206
int32_t RSSI(uint8_t networkItem);
196207

208+
209+
/**
210+
* return MAC / BSSID of scanned wifi
211+
* @param networkItem specify from which network item want to get the information
212+
* @return uint8_t * to MAC / BSSID of scanned wifi
213+
*/
214+
uint8_t * BSSID(uint8_t networkItem);
215+
216+
/**
217+
* return Channel of scanned wifi
218+
* @param networkItem specify from which network item want to get the information
219+
* @return uint32_t Channel of scanned wifi
220+
*/
221+
int32_t Channel(uint8_t networkItem);
222+
223+
/**
224+
* return if the scanned wifi is Hidden (no SSID)
225+
* @param networkItem specify from which network item want to get the information
226+
* @return bool (true == hidden)
227+
*/
228+
bool isHidden(uint8_t networkItem);
229+
230+
/**
231+
* loads all infos from a scanned wifi in to the ptr parameters
232+
* @param networkItem uint8_t
233+
* @param ssid const char**
234+
* @param encryptionType uint8_t *
235+
* @param RSSI int32_t *
236+
* @param BSSID uint8_t **
237+
* @param channel int32_t *
238+
* @param isHidden bool *
239+
* @return (true if ok)
240+
*/
241+
bool getNetworkInfo(uint8_t networkItem, const char** ssid, uint8_t * encryptionType, int32_t * RSSI, uint8_t ** BSSID, int32_t * channel, bool * isHidden);
242+
243+
197244
/*
198245
* Return Connection status.
199246
*
@@ -243,6 +290,9 @@ class ESP8266WiFiClass
243290
static void _smartConfigDone(void* result);
244291
bool _smartConfigStarted = false;
245292

293+
bool useApMode;
294+
bool useClientMode;
295+
246296
static size_t _scanCount;
247297
static void* _scanResult;
248298

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp

+21-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ wl_status_t ESP8266WiFiMulti::run(void) {
4444

4545
WifiAPlist_t bestNetwork { NULL, NULL };
4646
int bestNetworkDb = INT_MIN;
47+
uint8 bestBSSID[6];
48+
int32_t bestChannel;
4749

4850
// WiFi.scanNetworks will return the number of networks found
4951
int8_t n = WiFi.scanNetworks();
@@ -56,9 +58,16 @@ wl_status_t ESP8266WiFiMulti::run(void) {
5658
} else {
5759
DEBUG_WIFI_MULTI("[WIFI] %d networks found\n", n);
5860
for(int8_t i = 0; i < n; ++i) {
59-
const char * ssid_scan = WiFi.SSID(i);
60-
int32_t rssi_scan = WiFi.RSSI(i);
61-
uint8_t sec_scan = WiFi.encryptionType(i);
61+
62+
const char * ssid_scan;
63+
int32_t rssi_scan;
64+
uint8_t sec_scan;
65+
uint8_t * BSSID_scan;
66+
int32_t chan_scan;
67+
bool hidden_scan;
68+
69+
WiFi.getNetworkInfo(i, &ssid_scan, &sec_scan, &rssi_scan, &BSSID_scan, &chan_scan, &hidden_scan);
70+
6271

6372
bool known = false;
6473
for(uint32_t x = 0; x < APlist.size(); x++) {
@@ -69,7 +78,9 @@ wl_status_t ESP8266WiFiMulti::run(void) {
6978
if(rssi_scan > bestNetworkDb) { // best network
7079
if(sec_scan == ENC_TYPE_NONE || entry.passphrase) { // check for passphrase if not open wlan
7180
bestNetworkDb = rssi_scan;
81+
bestChannel = chan_scan;
7282
memcpy((void*) &bestNetwork, (void*) &entry, sizeof(bestNetwork));
83+
memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID));
7384
}
7485
}
7586
break;
@@ -82,7 +93,7 @@ wl_status_t ESP8266WiFiMulti::run(void) {
8293
DEBUG_WIFI_MULTI(" ");
8394
}
8495

85-
DEBUG_WIFI_MULTI(" %d: %s (%d) %c\n", i, ssid_scan, rssi_scan, (sec_scan == ENC_TYPE_NONE) ? ' ' : '*');
96+
DEBUG_WIFI_MULTI(" %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c\n", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan, rssi_scan, (sec_scan == ENC_TYPE_NONE) ? ' ' : '*');
8697
delay(0);
8798
}
8899
}
@@ -91,9 +102,9 @@ wl_status_t ESP8266WiFiMulti::run(void) {
91102
delay(0);
92103

93104
if(bestNetwork.ssid) {
94-
DEBUG_WIFI_MULTI("[WIFI] Connecting SSID: %s (%d)\n", bestNetwork.ssid, bestNetworkDb);
105+
DEBUG_WIFI_MULTI("[WIFI] Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s Channal: %d (%d)\n", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], bestNetwork.ssid, bestChannel, bestNetworkDb);
95106

96-
WiFi.begin(bestNetwork.ssid, bestNetwork.passphrase);
107+
WiFi.begin(bestNetwork.ssid, bestNetwork.passphrase, bestChannel, bestBSSID);
97108
status = WiFi.status();
98109

99110
// wait for connection or fail
@@ -103,12 +114,16 @@ wl_status_t ESP8266WiFiMulti::run(void) {
103114
}
104115

105116
IPAddress ip;
117+
uint8_t * mac;
106118
switch(status) {
107119
case WL_CONNECTED:
108120
ip = WiFi.localIP();
121+
mac = WiFi.BSSID();
109122
DEBUG_WIFI_MULTI("[WIFI] Connecting done.\n");
110123
DEBUG_WIFI_MULTI("[WIFI] SSID: %s\n", WiFi.SSID());
111124
DEBUG_WIFI_MULTI("[WIFI] IP: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
125+
DEBUG_WIFI_MULTI("[WIFI] MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
126+
DEBUG_WIFI_MULTI("[WIFI] Channel: %d\n", WiFi.Channel());
112127
break;
113128
case WL_NO_SSID_AVAIL:
114129
DEBUG_WIFI_MULTI("[WIFI] Connecting Faild AP not found.\n");

0 commit comments

Comments
 (0)