Skip to content

Commit

Permalink
Refactor/abstract Wifi classes (#1802)
Browse files Browse the repository at this point in the history
The `StationClass`, `WifiEventsClass` and `AccessPointClass` classes are now virtual base classes.
The actual arch-specific implementations are `StationImpl`, `WifiEventsImpl` and `AccessPointImpl`.

Looking at Basic_SmartConfig sample it needs more work, the callback should be architecture-independent. (That'll be another breaking change.)

Breaking changes:

* `StationClass::getConnectionStatusName()` returns String instead of `const char*`.

Esp8266 'StationImpl':

* Use `addElement(BssInfo*)` instead of `add(BssInfo&)` to avoid additional copy operation
* Simplify config() logic, should be a little easier to follow
* Add `getMacAddr` method to retrieve raw MAC address

Added dependencies:

* ENABLE_WPS=1 requires ENABLE_ESPCONN=1
* ENABLE_SMART_CONFIG has been added as it too has dependencies

Host updates:

* Provides fixed list of AP's
* Neither WPS nor SmartConfig are supported and will generate a warning. (Was an error, but this causes dist-clean to fail.)
  • Loading branch information
mikee47 authored and slaff committed Aug 6, 2019
1 parent 29a6789 commit 5dda877
Show file tree
Hide file tree
Showing 59 changed files with 2,007 additions and 1,642 deletions.
8 changes: 0 additions & 8 deletions Sming/Arch/Esp8266/Components/esp_wifi/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,3 @@ Esp8266 WiFi

All related libraries for WiFi support. Definitions are provided in the
Espressif Non-OS SDK.

Options
-------

.. envvar:: ENABLE_WPS

Set to 1 to enable WiFi Protected Setup (WPS)
WPS is not enabled by default to preserve resources, and because there may be security implications which you should consider carefully.
20 changes: 11 additions & 9 deletions Sming/Arch/Esp8266/Components/esp_wifi/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ COMPONENT_LIBNAME :=

COMPONENT_DEPENDS := esp8266

# => WPS
COMPONENT_VARS := ENABLE_WPS
ifeq ($(ENABLE_WPS), 1)
GLOBAL_CFLAGS += -DENABLE_WPS=1
EXTRA_LIBS := wps
endif

EXTRA_LIBS := \
phy \
pp \
net80211 \
wpa \
crypto \
smartconfig
crypto

ifeq ($(ENABLE_WPS), 1)
ENABLE_ESPCONN := 1
EXTRA_LIBS += wps
endif

ifeq ($(ENABLE_SMART_CONFIG),1)
ENABLE_ESPCONN := 1
EXTRA_LIBS += smartconfig
endif
5 changes: 5 additions & 0 deletions Sming/Arch/Esp8266/Components/sming-arch/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ COMPONENT_DEPENDS := \
gdbstub \
spi_flash

# => Platform WiFi
COMPONENT_VARS := \
ENABLE_WPS \
ENABLE_SMART_CONFIG

# => SSL
ifeq ($(ENABLE_SSL),1)
COMPONENT_DEPENDS += axtls-8266
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,54 @@
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* AccessPoint.cpp
* AccessPointImpl.cpp
*
****/

#include "Platform/AccessPoint.h"
#include "Interrupts.h"
#include "Data/HexString.h"
#include "AccessPointImpl.h"
#include <Interrupts.h>
#include <Data/HexString.h>

AccessPointClass WifiAccessPoint;
static AccessPointImpl accessPoint;
AccessPointClass& WifiAccessPoint = accessPoint;

void AccessPointClass::enable(bool enabled, bool save)
void AccessPointImpl::enable(bool enabled, bool save)
{
uint8 mode;
if(save)
if(save) {
mode = wifi_get_opmode_default() & ~SOFTAP_MODE;
else
} else {
mode = wifi_get_opmode() & ~SOFTAP_MODE;
if(enabled)
}
if(enabled) {
mode |= SOFTAP_MODE;
if(save)
}
if(save) {
wifi_set_opmode(mode);
else
} else {
wifi_set_opmode_current(mode);
}
}

bool AccessPointClass::isEnabled()
bool AccessPointImpl::isEnabled() const
{
return wifi_get_opmode() & SOFTAP_MODE;
}

bool AccessPointClass::config(const String& ssid, String password, AUTH_MODE mode, bool hidden, int channel,
int beaconInterval)
bool AccessPointImpl::config(const String& ssid, String password, AUTH_MODE mode, bool hidden, int channel,
int beaconInterval)
{
softap_config config = {0};
if(mode == AUTH_WEP)
if(mode == AUTH_WEP) {
return false; // Not supported!
}

if(mode == AUTH_OPEN)
password = "";
if(mode == AUTH_OPEN) {
password = nullptr;
}

bool enabled = isEnabled();
enable(true);
enable(true, false);
wifi_softap_dhcps_stop();
wifi_softap_get_config(&config);
if(channel != config.channel || hidden != config.ssid_hidden || mode != config.authmode ||
Expand All @@ -67,7 +73,7 @@ bool AccessPointClass::config(const String& ssid, String password, AUTH_MODE mod
if(!wifi_softap_set_config(&config)) {
interrupts();
wifi_softap_dhcps_start();
enable(enabled);
enable(enabled, false);
debugf("Can't set AP configuration!");
return false;
}
Expand All @@ -86,40 +92,40 @@ bool AccessPointClass::config(const String& ssid, String password, AUTH_MODE mod
}

wifi_softap_dhcps_start();
enable(enabled);
enable(enabled, false);

return true;
}

IPAddress AccessPointClass::getIP()
IPAddress AccessPointImpl::getIP() const
{
struct ip_info info = {0};
wifi_get_ip_info(SOFTAP_IF, &info);
return info.ip;
}

IPAddress AccessPointClass::getNetworkBroadcast()
IPAddress AccessPointImpl::getNetworkBroadcast() const
{
struct ip_info info = {0};
wifi_get_ip_info(SOFTAP_IF, &info);
return (info.ip.addr | ~info.netmask.addr);
}

IPAddress AccessPointClass::getNetworkMask()
IPAddress AccessPointImpl::getNetworkMask() const
{
struct ip_info info = {0};
wifi_get_ip_info(SOFTAP_IF, &info);
return info.netmask;
}

IPAddress AccessPointClass::getNetworkGateway()
IPAddress AccessPointImpl::getNetworkGateway() const
{
struct ip_info info = {0};
wifi_get_ip_info(SOFTAP_IF, &info);
return info.gw;
}

bool AccessPointClass::setIP(IPAddress address)
bool AccessPointImpl::setIP(IPAddress address)
{
if(System.isReady()) {
debugf("IP can be changed only in init() method");
Expand All @@ -137,43 +143,46 @@ bool AccessPointClass::setIP(IPAddress address)
return true;
}

String AccessPointClass::getMAC(char sep)
MACAddress AccessPointImpl::getMacAddr() const
{
uint8 hwaddr[6];
if(wifi_get_macaddr(SOFTAP_IF, hwaddr))
return makeHexString(hwaddr, sizeof(hwaddr), sep);
else
return nullptr;
MACAddress addr;
if(wifi_get_macaddr(SOFTAP_IF, &addr[0])) {
return addr;
} else {
return MACADDR_NONE;
}
}

String AccessPointClass::getSSID()
String AccessPointImpl::getSSID() const
{
softap_config config = {0};
if(!wifi_softap_get_config(&config)) {
debugf("Can't read AP configuration!");
return "";
return nullptr;
}
debugf("SSID: %s", (char*)config.ssid);
return String((char*)config.ssid);
auto ssid = reinterpret_cast<const char*>(config.ssid);
debugf("SSID: %s", ssid);
return ssid;
}

String AccessPointClass::getPassword()
String AccessPointImpl::getPassword() const
{
softap_config config = {0};
if(!wifi_softap_get_config(&config)) {
debugf("Can't read AP configuration!");
return "";
return nullptr;
}
debugf("Pass: %s", (char*)config.password);
return String((char*)config.password);
auto pwd = reinterpret_cast<const char*>(config.password);
debugf("Pass: %s", pwd);
return pwd;
}

void AccessPointClass::onSystemReady()
void AccessPointImpl::onSystemReady()
{
if(runConfig != nullptr) {
noInterrupts();
bool enabled = isEnabled();
enable(true);
enable(true, false);
wifi_softap_dhcps_stop();

if(!wifi_softap_set_config(runConfig)) {
Expand All @@ -185,7 +194,7 @@ void AccessPointClass::onSystemReady()
runConfig = nullptr;

wifi_softap_dhcps_start();
enable(enabled);
enable(enabled, false);
interrupts();
}
}
42 changes: 42 additions & 0 deletions Sming/Arch/Esp8266/Platform/AccessPointImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* AccessPointImpl.h - Esp8266 WiFi Access Point
*
****/

#pragma once

#include <Platform/AccessPoint.h>
#include <Platform/System.h>

class AccessPointImpl : public AccessPointClass, protected ISystemReadyHandler
{
public:
AccessPointImpl()
{
System.onReady(this);
}

void enable(bool enabled, bool save) override;
bool isEnabled() const override;
bool config(const String& ssid, String password, AUTH_MODE mode, bool hidden, int channel,
int beaconInterval) override;
IPAddress getIP() const override;
bool setIP(IPAddress address) override;
MACAddress getMacAddr() const override;
IPAddress getNetworkMask() const override;
IPAddress getNetworkGateway() const override;
IPAddress getNetworkBroadcast() const override;
String getSSID() const override;
String getPassword() const override;

protected:
void onSystemReady() override;

private:
softap_config* runConfig = nullptr;
};
Loading

0 comments on commit 5dda877

Please sign in to comment.