1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

fixed support for psk in WiFiSTA, added support for psk to WiFiMulti, minor code cleanups (#4076)

This commit is contained in:
Develo
2018-01-10 23:15:24 -03:00
committed by GitHub
parent 332e059724
commit 02259a412c
3 changed files with 24 additions and 25 deletions

View File

@ -38,10 +38,6 @@ bool ESP8266WiFiMulti::addAP(const char* ssid, const char *passphrase) {
return APlistAdd(ssid, passphrase);
}
int ESP8266WiFiMulti::count(void) {
return APlist.size();
}
wl_status_t ESP8266WiFiMulti::run(void) {
wl_status_t status = WiFi.status();
@ -71,7 +67,7 @@ wl_status_t ESP8266WiFiMulti::run(void) {
if(scanResult > 0) {
// scan done, analyze
WifiAPlist_t bestNetwork { NULL, NULL };
WifiAPEntry bestNetwork { NULL, NULL };
int bestNetworkDb = INT_MIN;
uint8 bestBSSID[6];
int32_t bestChannel;
@ -92,16 +88,14 @@ wl_status_t ESP8266WiFiMulti::run(void) {
WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan, hidden_scan);
bool known = false;
for(uint32_t x = 0; x < APlist.size(); x++) {
WifiAPlist_t entry = APlist[x];
for(auto entry : APlist) {
if(ssid_scan == entry.ssid) { // SSID match
known = true;
if(rssi_scan > bestNetworkDb) { // best network
if(sec_scan == ENC_TYPE_NONE || entry.passphrase) { // check for passphrase if not open wlan
bestNetworkDb = rssi_scan;
bestChannel = chan_scan;
memcpy((void*) &bestNetwork, (void*) &entry, sizeof(bestNetwork));
bestNetwork = entry;
memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID));
}
}
@ -183,7 +177,7 @@ wl_status_t ESP8266WiFiMulti::run(void) {
bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
WifiAPlist_t newAP;
WifiAPEntry newAP;
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
// fail SSID to long or missing!
@ -191,7 +185,8 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
return false;
}
if(passphrase && strlen(passphrase) > 63) {
//for passphrase, max is 63 ascii + null. For psk, 64hex + null.
if(passphrase && strlen(passphrase) > 64) {
// fail passphrase to long!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] passphrase to long\n");
return false;
@ -222,8 +217,7 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
}
void ESP8266WiFiMulti::APlistClean(void) {
for(uint32_t i = 0; i < APlist.size(); i++) {
WifiAPlist_t entry = APlist[i];
for(auto entry : APlist) {
if(entry.ssid) {
free(entry.ssid);
}

View File

@ -28,8 +28,6 @@
#define WIFICLIENTMULTI_H_
#include "ESP8266WiFi.h"
#undef min
#undef max
#include <vector>
#ifdef DEBUG_ESP_WIFI
@ -42,10 +40,12 @@
#define DEBUG_WIFI_MULTI(...)
#endif
typedef struct {
char * ssid;
char * passphrase;
} WifiAPlist_t;
struct WifiAPEntry {
char * ssid;
char * passphrase;
};
typedef std::vector<WifiAPEntry> WifiAPlist;
class ESP8266WiFiMulti {
public:
@ -53,11 +53,11 @@ class ESP8266WiFiMulti {
~ESP8266WiFiMulti();
bool addAP(const char* ssid, const char *passphrase = NULL);
int count(void);
wl_status_t run(void);
private:
std::vector<WifiAPlist_t> APlist;
WifiAPlist APlist;
bool APlistAdd(const char* ssid, const char *passphrase = NULL);
void APlistClean(void);

View File

@ -26,7 +26,6 @@
#include "ESP8266WiFiGeneric.h"
#include "ESP8266WiFiSTA.h"
extern "C" {
#include "c_types.h"
#include "ets_sys.h"
#include "os_type.h"
@ -34,6 +33,8 @@ extern "C" {
#include "mem.h"
#include "user_interface.h"
#include "smartconfig.h"
extern "C" {
#include "lwip/err.h"
#include "lwip/dns.h"
#include "lwip/init.h" // LWIP_VERSION_
@ -62,7 +63,8 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh
return false;
}
if(strcmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password)) != 0) {
//in case of password, use strncmp with size 64 to cover 64byte psk case (no null term)
if(strncmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password), sizeof(lhs.password)) != 0) {
return false;
}
@ -116,7 +118,7 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
if(passphrase) {
if (strlen(passphrase) == 64) // it's not a passphrase, is the PSK
if (strlen(passphrase) == 64) // it's not a passphrase, is the PSK, which is copied into conf.password without null term
memcpy(reinterpret_cast<char*>(conf.password), passphrase, 64);
else
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
@ -514,7 +516,10 @@ String ESP8266WiFiSTAClass::SSID() const {
String ESP8266WiFiSTAClass::psk() const {
struct station_config conf;
wifi_station_get_config(&conf);
return String(reinterpret_cast<char*>(conf.password));
char tmp[65]; //psk is 64 bytes hex => plus null term
memcpy(tmp, conf.password, sizeof(conf.password));
tmp[64] = 0; //null term in case of 64 byte psk
return String(reinterpret_cast<char*>(tmp));
}
/**