Skip to content

Commit

Permalink
AccessPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Aug 4, 2019
1 parent 8de7361 commit add1714
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 75 deletions.
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,41 @@ bool AccessPointClass::setIP(IPAddress address)
return true;
}

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

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 +189,7 @@ void AccessPointClass::onSystemReady()
runConfig = nullptr;

wifi_softap_dhcps_start();
enable(enabled);
enable(enabled, false);
interrupts();
}
}
32 changes: 32 additions & 0 deletions Sming/Arch/Esp8266/Platform/AccessPointImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#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;
bool getMacAddr(uint8_t hwaddr[6]) 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;
};
12 changes: 12 additions & 0 deletions Sming/Platform/AccessPoint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "AccessPoint.h"
#include <Data/HexString.h>

String AccessPointClass::getMAC(char sep) const
{
uint8 hwaddr[6];
if(getMacAddr(hwaddr)) {
return makeHexString(hwaddr, sizeof(hwaddr), sep);
} else {
return nullptr;
}
}
61 changes: 28 additions & 33 deletions Sming/Platform/AccessPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,27 @@

#pragma once

#include "System.h"
#include "WString.h"
#include "IPAddress.h"
#include <WString.h>
#include <IPAddress.h>
#include "BssInfo.h"

class AccessPointClass : protected ISystemReadyHandler
/** @brief Access point class
* @addtogroup wifi_ap
* @{
*/
class AccessPointClass
{
public:
/** @brief Access point class
* @addtogroup wifi_ap
* @{
*/
AccessPointClass()
{
System.onReady(this);
}

/** @brief Enable or disable WiFi AP
* @param enabled True to enable AP. False to disable.
* @param save True to save operational mode to flash, False to set current operational mode only
*/
void enable(bool enabled, bool save = false);
virtual void enable(bool enabled, bool save = false) = 0;

/** @brief Get WiFi AP enable status
* @retval bool True if WiFi AP enabled.
*/
bool isEnabled();
virtual bool isEnabled() const = 0;

/** @brief Configure WiFi AP
* @param ssid WiFi AP SSID
Expand All @@ -55,58 +50,58 @@ class AccessPointClass : protected ISystemReadyHandler
* @param beaconInterval WiFi AP beacon interval in milliseconds (Default: 200ms)
* @retval bool True on success
*/
bool config(const String& ssid, String password, AUTH_MODE mode, bool hidden = false, int channel = 7,
int beaconInterval = 200);
virtual bool config(const String& ssid, String password, EBssAuthMode mode, bool hidden = false, int channel = 7,
int beaconInterval = 200) = 0;

/** @brief Get WiFi AP IP address
* @retval IPAddress WiFi AP IP address
*/
IPAddress getIP();
virtual IPAddress getIP() const = 0;

/** @brief Set WiFi AP IP addres
* @param address New IP address for WiFi AP
* @retval bool True on success
*/
bool setIP(IPAddress address);
virtual bool setIP(IPAddress address) = 0;

/** @brief Get WiFi AP MAC address
* @param hwaddr
* @retval bool true on success
*/
virtual bool getMacAddr(uint8_t hwaddr[6]) const = 0;

/** @brief Get WiFi AP MAC address
* @param sep separator between bytes (e.g. ':')
* @retval String WiFi AP MAC address
*/
String getMAC(char sep = '\0');
String getMAC(char sep = '\0') const;

/** @brief Get WiFi AP network mask
* @retval IPAddress WiFi AP network mask
*/
IPAddress getNetworkMask();
virtual IPAddress getNetworkMask() const = 0;

/** @brief Get WiFi AP default gateway
* @retval IPAddress WiFi AP default gateway
*/
IPAddress getNetworkGateway();
virtual IPAddress getNetworkGateway() const = 0;

/** @brief Get WiFi AP broadcast address
* @retval IPAddress WiFi AP broadcast address
*/
IPAddress getNetworkBroadcast();
virtual IPAddress getNetworkBroadcast() const = 0;

/** @brief Get WiFi access point SSID
* @retval String WiFi access point SSID
*/
String getSSID();
virtual String getSSID() const = 0;

/** @brief Get WiFi access point password
* @retval String WiFi access point password
*/
String getPassword();
/** @} */

protected:
void onSystemReady() override;

private:
softap_config* runConfig = nullptr;
virtual String getPassword() const = 0;
};
/** @} */

/** @brief Global instance of WiFi access point object
* @note Use WiFiAccessPoint.<i>function</i> to access WiFi access point functions
Expand All @@ -116,4 +111,4 @@ class AccessPointClass : protected ISystemReadyHandler
* @endcode
* @ingroup wifi_ap
*/
extern AccessPointClass WifiAccessPoint;
extern AccessPointClass& WifiAccessPoint;

0 comments on commit add1714

Please sign in to comment.