Skip to content
Open
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
14 changes: 14 additions & 0 deletions Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ config CMF_WIFI
bool "Include WiFi extension"
default "n"

menu "WiFi features"
depends on CMF_WIFI

config CMF_WIFI_STA_SUPPORT
bool "WiFi station support"
default "n"

config CMF_WIFI_AP_SUPPORT
depends on ESP_WIFI_SOFTAP_SUPPORT
bool "WiFi access point support"
default "n"

endmenu

config CMF_TOUCH
bool "Include touch input extension"
default "n"
Expand Down
85 changes: 54 additions & 31 deletions src/Periphery/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ WiFi::WiFi() noexcept : Super() {
}, this, &nativeEventHandler);
}

#ifdef CONFIG_CMF_WIFI_AP_SUPPORT

void WiFi::startAccessPoint(const std::string& name, const std::string& password, uint8_t channel /*= 1*/, uint8_t maxConnections /*= 1*/) noexcept {
if(type != WiFiType::None) {
CMF_LOG(WiFi, LogLevel::Warning, "WiFi access point attempted to start when WiFi is already running.");
Expand Down Expand Up @@ -54,25 +56,6 @@ void WiFi::startAccessPoint(const std::string& name, const std::string& password
initSemaphore.acquire();
}

void WiFi::startStation() noexcept {
if(type != WiFiType::None) {
CMF_LOG(WiFi, LogLevel::Warning, "WiFi station attempted to start when WiFi is already running.");
return;
}

type = WiFiType::Station;

createNetif();

const wifi_init_config_t cfg_wifi = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg_wifi);

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());

initSemaphore.acquire();
}

bool WiFi::isHidden() const noexcept {
if(type == WiFiType::AccessPoint) {
return false;
Expand Down Expand Up @@ -109,6 +92,29 @@ void WiFi::setNetworkParameters(const std::string& name, const std::string& pass
esp_wifi_set_config(WIFI_IF_AP, &config);
}

#endif

#ifdef CONFIG_CMF_WIFI_STA_SUPPORT

void WiFi::startStation() noexcept {
if(type != WiFiType::None) {
CMF_LOG(WiFi, LogLevel::Warning, "WiFi station attempted to start when WiFi is already running.");
return;
}

type = WiFiType::Station;

createNetif();

const wifi_init_config_t cfg_wifi = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg_wifi);

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());

initSemaphore.acquire();
}

void WiFi::resetIPInfo() const noexcept {
if(type != WiFiType::Station) {
return;
Expand Down Expand Up @@ -199,6 +205,7 @@ std::vector<wifi_ap_record_t> WiFi::getAPRecords(size_t maxScanSize) const noexc

return result;
}
#endif

void WiFi::onNativeEvent(uint32_t id, void *data) noexcept {
CMF_LOG(WiFi, LogLevel::Info, "WiFi native event received: %ld", id);
Expand All @@ -213,26 +220,30 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept {
break;
}
case WIFI_EVENT_SCAN_DONE: {
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
const wifi_event_sta_scan_done_t* scan_done = static_cast<wifi_event_sta_scan_done_t*>(data);
if(scan_done == nullptr) {
break;
}

OnScanDone.broadcast(scan_done->status, scan_done->number, scan_done->scan_id);

#endif
break;
}
case WIFI_EVENT_STA_START: {
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
OnStationStart.broadcast();

#endif
break;
}
case WIFI_EVENT_STA_STOP: {
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
OnStationStop.broadcast();

#endif
break;
}
case WIFI_EVENT_STA_CONNECTED: {
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
const wifi_event_sta_connected_t* connected = static_cast<wifi_event_sta_connected_t*>(data);
if(connected == nullptr) {
break;
Expand All @@ -242,10 +253,11 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept {
//const std::string bssid(reinterpret_cast<const char*>(connected->bssid), 6);

OnStationConnected.broadcast(/*ssid, bssid,*/ connected->channel, connected->authmode, connected->aid);

#endif
break;
}
case WIFI_EVENT_STA_DISCONNECTED: {
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
const wifi_event_sta_disconnected_t* disconnected = static_cast<wifi_event_sta_disconnected_t*>(data);
if(disconnected == nullptr) {
break;
Expand All @@ -255,17 +267,18 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept {
//const std::string bssid(reinterpret_cast<const char*>(disconnected->bssid), 6);

OnStationDisconnected.broadcast(/*ssid, bssid,*/ disconnected->reason, disconnected->rssi);

#endif
break;
}
case WIFI_EVENT_STA_AUTHMODE_CHANGE: {
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
const wifi_event_sta_authmode_change_t* auth = static_cast<wifi_event_sta_authmode_change_t*>(data);
if(auth == nullptr) {
break;
}

OnStationAuthModeChanged.broadcast(auth->old_mode, auth->new_mode);

#endif
break;
}
case WIFI_EVENT_STA_WPS_ER_SUCCESS: {
Expand All @@ -285,28 +298,32 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept {
break;
}
case WIFI_EVENT_AP_START: {
#ifdef CONFIG_CMF_WIFI_AP_SUPPORT
OnAccessPointStart.broadcast();

#endif
break;
}
case WIFI_EVENT_AP_STOP: {
OnAccessPointStop.broadcast();

#ifdef CONFIG_CMF_WIFI_AP_SUPPORT
OnAccessPointStop.broadcast();
#endif
break;
}
case WIFI_EVENT_AP_STACONNECTED: {
const wifi_event_ap_staconnected_t* connected = static_cast<wifi_event_ap_staconnected_t*>(data);
#ifdef CONFIG_CMF_WIFI_AP_SUPPORT
const wifi_event_ap_staconnected_t* connected = static_cast<wifi_event_ap_staconnected_t*>(data);
if(connected == nullptr) {
break;
}

//const std::string mac(reinterpret_cast<const char *>(connected->mac), 6);

OnAccessPointConnection.broadcast(/*mac,*/ connected->aid, connected->is_mesh_child);

#endif
break;
}
case WIFI_EVENT_AP_STADISCONNECTED: {
#ifdef CONFIG_CMF_WIFI_AP_SUPPORT
const wifi_event_ap_stadisconnected_t* disconnected = static_cast<wifi_event_ap_stadisconnected_t*>(data);
if(disconnected == nullptr) {
break;
Expand All @@ -315,7 +332,7 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept {
//const std::string mac(reinterpret_cast<const char *>(disconnected->mac), 6);

OnAccessPointDisconnection.broadcast(/*mac,*/ disconnected->aid, disconnected->is_mesh_child, disconnected->reason);

#endif
break;
}
case WIFI_EVENT_AP_PROBEREQRECVED: {
Expand Down Expand Up @@ -360,7 +377,9 @@ void WiFi::createNetif() const noexcept {

ESP_ERROR_CHECK(esp_netif_init());

// ReSharper disable once CppDFAConstantConditions
if(type == WiFiType::AccessPoint) {
#ifdef CONFIG_CMF_WIFI_AP_SUPPORT
esp_netif_inherent_config_t base{};
memcpy(&base, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(esp_netif_inherent_config_t));
base.flags = (esp_netif_flags_t) ((base.flags & ~(ESP_NETIF_DHCP_SERVER | ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_EVENT_IP_MODIFIED)) | ESP_NETIF_FLAG_GARP);
Expand All @@ -384,7 +403,10 @@ void WiFi::createNetif() const noexcept {

esp_netif_attach_wifi_ap(netif);
esp_wifi_set_default_wifi_ap_handlers();
#endif
// ReSharper disable once CppDFAConstantConditions
}else if(type == WiFiType::Station) {
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
esp_netif_inherent_config_t base{};
memcpy(&base, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(esp_netif_inherent_config_t));
base.flags = (esp_netif_flags_t) ((base.flags & ~(ESP_NETIF_DHCP_SERVER | ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_EVENT_IP_MODIFIED)) | ESP_NETIF_FLAG_GARP);
Expand All @@ -408,5 +430,6 @@ void WiFi::createNetif() const noexcept {

esp_netif_attach_wifi_station(netif);
esp_wifi_set_default_wifi_sta_handlers();
#endif
}
}
17 changes: 12 additions & 5 deletions src/Periphery/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#define WIFI_H

#include <semaphore>
#include <esp_event.h>
#include <esp_netif_types.h>
#include <esp_wifi_types_generic.h>
#include "Object/Object.h"
#include "Event/EventBroadcaster.h"
Expand All @@ -22,6 +20,7 @@ class WiFi : public Object{

public:
// STATION EVENTS
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
DECLARE_EVENT(ScanDoneEvent, WiFi, uint32_t, uint8_t, uint8_t);
ScanDoneEvent OnScanDone{this};

Expand All @@ -39,8 +38,10 @@ class WiFi : public Object{

DECLARE_EVENT(StationAuthModeChangedEvent, WiFi, wifi_auth_mode_t, wifi_auth_mode_t);
StationAuthModeChangedEvent OnStationAuthModeChanged{this};
#endif

// ACCESS POINT EVENTS
#ifdef CONFIG_CMF_WIFI_AP_SUPPORT
DECLARE_EVENT(AccessPointStartEvent, WiFi);
AccessPointStartEvent OnAccessPointStart{this};

Expand All @@ -52,19 +53,24 @@ class WiFi : public Object{

DECLARE_EVENT(AccessPointDisconnectionEvent, WiFi, /*std::string,*/ uint8_t, bool, uint8_t);
AccessPointDisconnectionEvent OnAccessPointDisconnection{this};
#endif

public:
WiFi() noexcept;

void startAccessPoint(const std::string& name, const std::string& password, uint8_t channel = 1, uint8_t maxConnections = 1) noexcept;
void startStation() noexcept;

// ACCESS POINT FUNCTIONS
#ifdef CONFIG_CMF_WIFI_AP_SUPPORT
void startAccessPoint(const std::string& name, const std::string& password, uint8_t channel = 1, uint8_t maxConnections = 1) noexcept;

bool isHidden() const noexcept;
void setHidden(bool hidden) const noexcept;
void setNetworkParameters(const std::string& name, const std::string& password) const noexcept;
#endif

// STATION FUNCTIONS
#ifdef CONFIG_CMF_WIFI_STA_SUPPORT
void startStation() noexcept;

void resetIPInfo() const noexcept;
void connect() const noexcept;
void startScanning(wifi_scan_type_t scanType = WIFI_SCAN_TYPE_PASSIVE, wifi_scan_time_t scanTime = {.active = {.min = 0, .max = 1500}, .passive = 1500}) const noexcept;
Expand All @@ -73,6 +79,7 @@ class WiFi : public Object{
int getConnectionRSSI() const noexcept;
void setTargetParameters(const std::string& ssid, const std::string& password) const noexcept;
std::vector<wifi_ap_record_t> getAPRecords(size_t maxScanSize) const noexcept;
#endif

private:
WiFiType type;
Expand Down