From 86cbb87bd547ec013242d73a6cbd7f6519093767 Mon Sep 17 00:00:00 2001 From: Fr0zen3k Date: Thu, 11 Jun 2026 14:31:01 +0200 Subject: [PATCH] WiFi station and access point separation in compile time --- Kconfig.projbuild | 14 +++++++ src/Periphery/WiFi.cpp | 85 +++++++++++++++++++++++++++--------------- src/Periphery/WiFi.h | 17 ++++++--- 3 files changed, 80 insertions(+), 36 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index 4a0afea..5e73682 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -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" diff --git a/src/Periphery/WiFi.cpp b/src/Periphery/WiFi.cpp index f9ca24e..b563ce2 100644 --- a/src/Periphery/WiFi.cpp +++ b/src/Periphery/WiFi.cpp @@ -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."); @@ -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; @@ -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; @@ -199,6 +205,7 @@ std::vector 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); @@ -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(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(data); if(connected == nullptr) { break; @@ -242,10 +253,11 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept { //const std::string bssid(reinterpret_cast(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(data); if(disconnected == nullptr) { break; @@ -255,17 +267,18 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept { //const std::string bssid(reinterpret_cast(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(data); if(auth == nullptr) { break; } OnStationAuthModeChanged.broadcast(auth->old_mode, auth->new_mode); - +#endif break; } case WIFI_EVENT_STA_WPS_ER_SUCCESS: { @@ -285,17 +298,20 @@ 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(data); +#ifdef CONFIG_CMF_WIFI_AP_SUPPORT + const wifi_event_ap_staconnected_t* connected = static_cast(data); if(connected == nullptr) { break; } @@ -303,10 +319,11 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept { //const std::string mac(reinterpret_cast(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(data); if(disconnected == nullptr) { break; @@ -315,7 +332,7 @@ void WiFi::onNativeEvent(uint32_t id, void *data) noexcept { //const std::string mac(reinterpret_cast(disconnected->mac), 6); OnAccessPointDisconnection.broadcast(/*mac,*/ disconnected->aid, disconnected->is_mesh_child, disconnected->reason); - +#endif break; } case WIFI_EVENT_AP_PROBEREQRECVED: { @@ -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); @@ -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); @@ -408,5 +430,6 @@ void WiFi::createNetif() const noexcept { esp_netif_attach_wifi_station(netif); esp_wifi_set_default_wifi_sta_handlers(); +#endif } } diff --git a/src/Periphery/WiFi.h b/src/Periphery/WiFi.h index 721b8ba..e5d4aaa 100644 --- a/src/Periphery/WiFi.h +++ b/src/Periphery/WiFi.h @@ -2,8 +2,6 @@ #define WIFI_H #include -#include -#include #include #include "Object/Object.h" #include "Event/EventBroadcaster.h" @@ -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}; @@ -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}; @@ -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; @@ -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 getAPRecords(size_t maxScanSize) const noexcept; +#endif private: WiFiType type;