Skip to content

fixed support for psk #4076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -183,15 +177,16 @@ 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!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] no ssid or ssid to long\n");
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;
Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 8 additions & 8 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#define WIFICLIENTMULTI_H_

#include "ESP8266WiFi.h"
#undef min
#undef max
#include <vector>

#ifdef DEBUG_ESP_WIFI
Expand All @@ -42,22 +40,24 @@
#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:
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);

Expand Down
13 changes: 9 additions & 4 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
#include "ESP8266WiFiGeneric.h"
#include "ESP8266WiFiSTA.h"

extern "C" {
#include "c_types.h"
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#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_
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

/**
Expand Down