Skip to content
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

Revise station configuration to support BSSID #2522

Merged
merged 3 commits into from
Jun 27, 2022
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
37 changes: 27 additions & 10 deletions Sming/Components/Network/Arch/Esp32/Platform/StationImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,34 @@ bool StationImpl::isEnabled() const
return (mode == WIFI_MODE_STA) || (mode == WIFI_MODE_APSTA);
}

bool StationImpl::config(const String& ssid, const String& password, bool autoConnectOnStartup, bool save)
bool StationImpl::config(const Config& cfg)
{
wifi_config_t config{};

if(ssid.length() >= sizeof(config.sta.ssid)) {
if(cfg.ssid.length() >= sizeof(config.sta.ssid)) {
return false;
}
if(password.length() >= sizeof(config.sta.password)) {
if(cfg.password.length() >= sizeof(config.sta.password)) {
return false;
}

memcpy(config.sta.ssid, ssid.c_str(), ssid.length());
memcpy(config.sta.password, password.c_str(), password.length());
memcpy(config.sta.ssid, cfg.ssid.c_str(), cfg.ssid.length());
memcpy(config.sta.password, cfg.password.c_str(), cfg.password.length());

enable(true, save);
if(cfg.bssid) {
config.sta.bssid_set = true;
cfg.bssid.getOctets(config.sta.bssid);
} else {
config.sta.bssid_set = false;
}

enable(true, cfg.save);

if(save) {
setAutoConnect(autoConnectOnStartup);
if(cfg.save) {
setAutoConnect(cfg.autoConnectOnStartup);
}

ESP_ERROR_CHECK(esp_wifi_set_storage(save ? WIFI_STORAGE_FLASH : WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_storage(cfg.save ? WIFI_STORAGE_FLASH : WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &config));

return connect();
Expand Down Expand Up @@ -328,6 +335,16 @@ String StationImpl::getSSID() const
return ssid;
}

MacAddress StationImpl::getBSSID() const
{
wifi_config_t config{};
if(esp_wifi_get_config(WIFI_IF_STA, &config) != ESP_OK) {
debug_e("Can't read station configuration!");
return MacAddress{};
}
return config.sta.bssid;
}

int8_t StationImpl::getRssi() const
{
wifi_ap_record_t info;
Expand Down Expand Up @@ -472,7 +489,7 @@ void StationImpl::internalSmartConfig(smartconfig_event_t event_id, void* pdata)

switch(event_id) {
case SC_EVENT_GOT_SSID_PSWD:
config(evt.ssid, evt.password, true, true);
StationClass::config(evt.ssid, evt.password, true, true);
connect();
break;
case SC_EVENT_SEND_ACK_DONE:
Expand Down
3 changes: 2 additions & 1 deletion Sming/Components/Network/Arch/Esp32/Platform/StationImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StationImpl : public StationClass, protected ISystemReadyHandler

void enable(bool enabled, bool save) override;
bool isEnabled() const override;
bool config(const String& ssid, const String& password, bool autoConnectOnStartup, bool save) override;
bool config(const Config& cfg) override;
bool connect() override;
bool disconnect() override;
StationConnectionStatus getConnectionStatus() const override;
Expand All @@ -51,6 +51,7 @@ class StationImpl : public StationClass, protected ISystemReadyHandler
IpAddress getNetworkBroadcast() const override;
bool setIP(IpAddress address, IpAddress netmask, IpAddress gateway) override;
String getSSID() const override;
MacAddress getBSSID() const override;
String getPassword() const override;
int8_t getRssi() const override;
uint8_t getChannel() const override;
Expand Down
46 changes: 31 additions & 15 deletions Sming/Components/Network/Arch/Esp8266/Platform/StationImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ bool StationImpl::isEnabled() const
return wifi_get_opmode() & STATION_MODE;
}

bool StationImpl::config(const String& ssid, const String& password, bool autoConnectOnStartup, bool save)
bool StationImpl::config(const Config& cfg)
{
station_config config = {0};
if(ssid.length() >= sizeof(config.ssid)) {
if(cfg.ssid.length() >= sizeof(config.ssid)) {
return false;
}
if(password.length() >= sizeof(config.password)) {
if(cfg.password.length() >= sizeof(config.password)) {
return false;
}

Expand All @@ -70,10 +70,10 @@ bool StationImpl::config(const String& ssid, const String& password, bool autoCo
bool setConfig;
if(wifi_station_get_config(&config)) {
// Determine if config has changed
setConfig =
strncmp(ssid.c_str(), reinterpret_cast<const char*>(config.ssid), sizeof(config.ssid)) != 0 ||
strncmp(password.c_str(), reinterpret_cast<const char*>(config.password), sizeof(config.password)) != 0 ||
config.bssid_set;
setConfig = strncmp(cfg.ssid.c_str(), reinterpret_cast<const char*>(config.ssid), sizeof(config.ssid)) != 0 ||
strncmp(cfg.password.c_str(), reinterpret_cast<const char*>(config.password),
sizeof(config.password)) != 0 ||
(config.bssid_set && cfg.bssid != config.bssid);
} else {
debugf("Can't read station configuration!");
setConfig = true;
Expand All @@ -83,13 +83,19 @@ bool StationImpl::config(const String& ssid, const String& password, bool autoCo
if(setConfig) {
memset(config.ssid, 0, sizeof(config.ssid));
memset(config.password, 0, sizeof(config.password));
config.bssid_set = false;
ssid.getBytes(config.ssid, sizeof(config.ssid));
password.getBytes(config.password, sizeof(config.password));
cfg.ssid.getBytes(config.ssid, sizeof(config.ssid));
cfg.password.getBytes(config.password, sizeof(config.password));

if(cfg.bssid) {
config.bssid_set = true;
cfg.bssid.getOctets(config.bssid);
} else {
config.bssid_set = false;
}

noInterrupts();

if(save) {
if(cfg.save) {
success = wifi_station_set_config(&config);
} else {
success = wifi_station_set_config_current(&config);
Expand All @@ -98,12 +104,12 @@ bool StationImpl::config(const String& ssid, const String& password, bool autoCo
interrupts();

if(success) {
debugf("Station configuration was updated to: %s", ssid.c_str());
debugf("Station configuration was updated to: %s", cfg.ssid.c_str());
} else {
debugf("Can't set station configuration!");
}
} else {
debugf("Station configuration is: %s", ssid.c_str());
debugf("Station configuration is: %s", cfg.ssid.c_str());
success = true;
}

Expand All @@ -115,7 +121,7 @@ bool StationImpl::config(const String& ssid, const String& password, bool autoCo
}

if(success) {
wifi_station_set_auto_connect(autoConnectOnStartup);
wifi_station_set_auto_connect(cfg.autoConnectOnStartup);
}

return success;
Expand Down Expand Up @@ -234,6 +240,16 @@ String StationImpl::getSSID() const
return ssid;
}

MacAddress StationImpl::getBSSID() const
{
station_config config = {0};
if(!wifi_station_get_config(&config)) {
debugf("Can't read station configuration!");
return MacAddress{};
}
return config.bssid;
}

int8_t StationImpl::getRssi() const
{
debugf("Rssi: %d dBm", wifi_station_get_rssi());
Expand Down Expand Up @@ -364,7 +380,7 @@ void StationImpl::internalSmartConfig(sc_status status, void* pdata)
case SC_STATUS_GETTING_SSID_PSWD:
break;
case SC_STATUS_LINK:
config(evt.ssid, evt.password, true, true);
StationClass::config(evt.ssid, evt.password, true, true);
connect();
break;
case SC_STATUS_LINK_OVER:
Expand Down
3 changes: 2 additions & 1 deletion Sming/Components/Network/Arch/Esp8266/Platform/StationImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class StationImpl : public StationClass, protected ISystemReadyHandler

void enable(bool enabled, bool save) override;
bool isEnabled() const override;
bool config(const String& ssid, const String& password, bool autoConnectOnStartup, bool save) override;
bool config(const Config& cfg) override;
bool connect() override;
bool disconnect() override;
StationConnectionStatus getConnectionStatus() const override;
Expand All @@ -44,6 +44,7 @@ class StationImpl : public StationClass, protected ISystemReadyHandler
IpAddress getNetworkBroadcast() const override;
bool setIP(IpAddress address, IpAddress netmask, IpAddress gateway) override;
String getSSID() const override;
MacAddress getBSSID() const override;
String getPassword() const override;
int8_t getRssi() const override;
uint8_t getChannel() const override;
Expand Down
21 changes: 13 additions & 8 deletions Sming/Components/Network/Arch/Host/Platform/StationImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,30 @@ bool StationImpl::isEnabled() const
return currentConfig.enabled;
}

bool StationImpl::config(const String& ssid, const String& password, bool autoConnectOnStartup, bool save)
bool StationImpl::config(const Config& cfg)
{
for(auto& ap : apInfoList) {
if(ssid == ap.ssid) {
if(cfg.ssid == ap.ssid) {
if(ap.authMode != AUTH_OPEN) {
if(password != ap.pwd) {
debug_w("Bad password for '%s'", ssid.c_str());
if(cfg.password != ap.pwd) {
debug_w("Bad password for '%s'", cfg.ssid.c_str());
return false;
}
}

currentAp = &ap;
if(save) {
if(cfg.save) {
savedAp = &ap;
}

debug_i("Connected to SSID '%s'", ssid.c_str());
debug_i("Connected to SSID '%s'", cfg.ssid.c_str());

autoConnect = autoConnectOnStartup;
autoConnect = cfg.autoConnectOnStartup;
return true;
}
}

debug_w("SSID '%s' not found", ssid.c_str());
debug_w("SSID '%s' not found", cfg.ssid.c_str());
return false;
}

Expand Down Expand Up @@ -293,6 +293,11 @@ String StationImpl::getSSID() const
return currentAp ? currentAp->ssid : nullptr;
}

MacAddress StationImpl::getBSSID() const
{
return MacAddress({0xff, 0xff, 0xff, 0x00, 0x01});
}

int8_t StationImpl::getRssi() const
{
return getRandomRssi();
Expand Down
3 changes: 2 additions & 1 deletion Sming/Components/Network/Arch/Host/Platform/StationImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class StationImpl : public StationClass
// StationClass
void enable(bool enabled, bool save) override;
bool isEnabled() const override;
bool config(const String& ssid, const String& password, bool autoConnectOnStartup, bool save) override;
bool config(const Config& cfg) override;
bool connect() override;
bool disconnect() override;
StationConnectionStatus getConnectionStatus() const override;
Expand All @@ -52,6 +52,7 @@ class StationImpl : public StationClass
IpAddress getNetworkBroadcast() const override;
bool setIP(IpAddress address, IpAddress netmask, IpAddress gateway) override;
String getSSID() const override;
MacAddress getBSSID() const override;
String getPassword() const override;
int8_t getRssi() const override;
uint8_t getChannel() const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool StationImpl::isEnabled() const
return false;
}

bool StationImpl::config(const String& ssid, const String& password, bool autoConnectOnStartup, bool save)
bool StationImpl::config(const Config& cfg)
{
return false;
}
Expand Down Expand Up @@ -96,6 +96,11 @@ String StationImpl::getSSID() const
return nullptr;
}

MacAddress StationImpl::getBSSID() const
{
return {};
}

int8_t StationImpl::getRssi() const
{
return 0;
Expand Down
3 changes: 2 additions & 1 deletion Sming/Components/Network/Arch/Rp2040/Platform/StationImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class StationImpl : public StationClass, protected ISystemReadyHandler

void enable(bool enabled, bool save) override;
bool isEnabled() const override;
bool config(const String& ssid, const String& password, bool autoConnectOnStartup, bool save) override;
bool config(const Config& cfg) override;
bool connect() override;
bool disconnect() override;
StationConnectionStatus getConnectionStatus() const override;
Expand All @@ -39,6 +39,7 @@ class StationImpl : public StationClass, protected ISystemReadyHandler
IpAddress getNetworkBroadcast() const override;
bool setIP(IpAddress address, IpAddress netmask, IpAddress gateway) override;
String getSSID() const override;
MacAddress getBSSID() const override;
String getPassword() const override;
int8_t getRssi() const override;
uint8_t getChannel() const override;
Expand Down
38 changes: 35 additions & 3 deletions Sming/Components/Network/src/Platform/Station.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ using WPSConfigDelegate = Delegate<bool(WpsStatus status)>;
class StationClass
{
public:
/**
* @brief Station configuration passed to config method
*/
struct Config {
String ssid; ///< Service Set to connect to (may be advertised by multiple access points)
String password; ///< Password (if required)
MacAddress bssid; ///< Set this to connect to a specific access point
bool autoConnectOnStartup = true; ///< Auto connect to this AP on system restart
bool save = true; ///< Store new settings in NV memory
};

virtual ~StationClass()
{
}
Expand All @@ -130,8 +141,24 @@ class StationClass
* @param autoConnectOnStartup True to auto connect. False for manual. (Default: True)
* @param save True to save the SSID and password in Flash. False otherwise. (Default: True)
*/
virtual bool config(const String& ssid, const String& password, bool autoConnectOnStartup = true,
bool save = true) = 0;
virtual bool config(const Config& config) = 0;

/** @brief Configure WiFi station
* @param ssid WiFi SSID
* @param password WiFi password
* @param autoConnectOnStartup True to auto connect. False for manual. (Default: True)
* @param save True to save the SSID and password in Flash. False otherwise. (Default: True)
*/
bool config(const String& ssid, const String& password, bool autoConnectOnStartup = true, bool save = true)
{
Config cfg{
.ssid = ssid,
.password = password,
.autoConnectOnStartup = autoConnectOnStartup,
.save = save,
};
return config(cfg);
}

/** @brief Connect WiFi station to network
*/
Expand Down Expand Up @@ -250,11 +277,16 @@ class StationClass
*/
virtual bool setIP(IpAddress address, IpAddress netmask, IpAddress gateway) = 0;

/** @brief Get WiFi station SSID
/** @brief Get WiFi station SSID (Service Set Identifier)
* @retval String WiFi station SSID
*/
virtual String getSSID() const = 0;

/** @brief Get BSSID (Basic Service Set Identifier) for connected AP
* @retval MacAddress Identifier of connected Access Point
*/
virtual MacAddress getBSSID() const = 0;

/** @brief Get WiFi station password
* @retval String WiFi station password
*/
Expand Down