From 7c5004c19e21b2d2623c4ee3332690ef1c9ddbc4 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Thu, 7 Sep 2023 09:19:25 +0200 Subject: [PATCH] Add wifi bandwidth and remove default: switch cases to allow for compiler errors just in case new enum values are introduced --- src/espwifistack.cpp | 24 +++++++++++++----- src/espwifistackconfig.h | 6 ++++- src/espwifiutils.cpp | 55 +++++++++++++++++++++++++--------------- src/espwifiutils.h | 1 + src/udpsender.cpp | 6 ++--- 5 files changed, 60 insertions(+), 32 deletions(-) diff --git a/src/espwifistack.cpp b/src/espwifistack.cpp index 8c573ba..c2ef19a 100644 --- a/src/espwifistack.cpp +++ b/src/espwifistack.cpp @@ -2214,13 +2214,25 @@ esp_err_t wifi_sync_mode(const config &config) if (newMode) { - if (config.sta && config.sta->long_range) - if (const auto result = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR); result != ESP_OK) - ESP_LOGE(TAG, "esp_wifi_set_protocol() for STA long range failed with %s", esp_err_to_name(result)); + if (config.sta) + { + if (config.sta->long_range) + if (const auto result = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR); result != ESP_OK) + ESP_LOGE(TAG, "esp_wifi_set_protocol() for STA long range failed with %s", esp_err_to_name(result)); - if (config.ap && config.ap->long_range) - if (const auto result = esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR); result != ESP_OK) - ESP_LOGE(TAG, "esp_wifi_set_protocol() for AP long range failed with %s", esp_err_to_name(result)); + if (const auto result = esp_wifi_set_bandwidth(WIFI_IF_STA, config.sta->bandwidth); result != ESP_OK) + ESP_LOGE(TAG, "esp_wifi_set_bandwidth() for STA failed with %s", esp_err_to_name(result)); + } + + if (config.ap) + { + if (config.ap->long_range) + if (const auto result = esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR); result != ESP_OK) + ESP_LOGE(TAG, "esp_wifi_set_protocol() for AP long range failed with %s", esp_err_to_name(result)); + + if (const auto result = esp_wifi_set_bandwidth(WIFI_IF_AP, config.ap->bandwidth); result != ESP_OK) + ESP_LOGE(TAG, "esp_wifi_set_bandwidth() for AP failed with %s", esp_err_to_name(result)); + } if (config.country) if (const auto result = esp_wifi_set_country(&*config.country); result != ESP_OK) diff --git a/src/espwifistackconfig.h b/src/espwifistackconfig.h index b4733b5..50d3cc1 100644 --- a/src/espwifistackconfig.h +++ b/src/espwifistackconfig.h @@ -166,6 +166,7 @@ struct sta_config std::array wifis; int8_t min_rssi = -90; bool long_range = false; + wifi_bandwidth_t bandwidth = WIFI_BW_HT20; sta_scan_config scan; friend bool operator==(const sta_config &left, const sta_config &right) @@ -174,6 +175,7 @@ struct sta_config left.wifis == right.wifis && left.min_rssi == right.min_rssi && left.long_range == right.long_range && + left.bandwidth == right.bandwidth && left.scan == right.scan; } @@ -195,6 +197,7 @@ struct ap_config int max_connection = 4; uint16_t beacon_interval = 100; bool long_range = false; + wifi_bandwidth_t bandwidth = WIFI_BW_HT20; friend bool operator==(const ap_config &left, const ap_config &right) { @@ -207,7 +210,8 @@ struct ap_config left.ssid_hidden == right.ssid_hidden && left.max_connection == right.max_connection && left.beacon_interval == right.beacon_interval && - left.long_range == right.long_range; + left.long_range == right.long_range && + left.bandwidth == right.bandwidth; } friend bool operator!=(const ap_config &left, const ap_config &right) diff --git a/src/espwifiutils.cpp b/src/espwifiutils.cpp index 0302d25..26fe4fc 100644 --- a/src/espwifiutils.cpp +++ b/src/espwifiutils.cpp @@ -66,11 +66,11 @@ std::string toString(wifi_auth_mode_t authMode) case WIFI_AUTH_WPA3_PSK: return "WPA3_PSK"; case WIFI_AUTH_WPA2_WPA3_PSK: return "WPA2_WPA3_PSK"; case WIFI_AUTH_WAPI_PSK: return "WAPI_PSK"; + case WIFI_AUTH_OWE: return "OWE"; case WIFI_AUTH_MAX: return "MAX"; - default: - ESP_LOGW(TAG, "Unknown wifi_auth_mode_t(%i)", std::to_underlying(authMode)); - return fmt::format("Unknown wifi_auth_mode_t({})", std::to_underlying(authMode)); } + ESP_LOGW(TAG, "Unknown wifi_auth_mode_t(%i)", std::to_underlying(authMode)); + return fmt::format("Unknown wifi_auth_mode_t({})", std::to_underlying(authMode)); } std::string toString(wifi_cipher_type_t cipherType) @@ -85,11 +85,25 @@ std::string toString(wifi_cipher_type_t cipherType) case WIFI_CIPHER_TYPE_TKIP_CCMP: return "TKIP_CCMP"; case WIFI_CIPHER_TYPE_AES_CMAC128: return "AES_CMAC128"; case WIFI_CIPHER_TYPE_SMS4: return "SMS4"; + case WIFI_CIPHER_TYPE_GCMP: return "GCMP"; + case WIFI_CIPHER_TYPE_GCMP256: return "GCMP256"; + case WIFI_CIPHER_TYPE_AES_GMAC128: return "AES_GMAC128"; + case WIFI_CIPHER_TYPE_AES_GMAC256: return "AES_GMAC256"; case WIFI_CIPHER_TYPE_UNKNOWN: return "UNKNOWN"; - default: - ESP_LOGW(TAG, "Unknown wifi_cipher_type_t(%i)", std::to_underlying(cipherType)); - return fmt::format("Unknown wifi_cipher_type_t({})", std::to_underlying(cipherType)); } + ESP_LOGW(TAG, "Unknown wifi_cipher_type_t(%i)", std::to_underlying(cipherType)); + return fmt::format("Unknown wifi_cipher_type_t({})", std::to_underlying(cipherType)); +} + +std::string toString(wifi_bandwidth_t bandwidth) +{ + switch (bandwidth) + { + case WIFI_BW_HT20: return "HT20"; + case WIFI_BW_HT40: return "HT40"; + } + ESP_LOGW(TAG, "Unknown wifi_bandwidth_t(%i)", std::to_underlying(bandwidth)); + return fmt::format("Unknown wifi_bandwidth_t({})", std::to_underlying(bandwidth)); } std::string toString(esp_interface_t interface) @@ -98,24 +112,25 @@ std::string toString(esp_interface_t interface) { case ESP_IF_WIFI_STA: return "STA"; case ESP_IF_WIFI_AP: return "AP"; + case ESP_IF_WIFI_NAN: return "NAN"; case ESP_IF_ETH: return "ETH"; - default: - ESP_LOGW(TAG, "Unknown esp_interface_t(%i)", std::to_underlying(interface)); - return fmt::format("Unknown esp_interface_t({})", std::to_underlying(interface)); + case ESP_IF_MAX: return "MAX"; } + ESP_LOGW(TAG, "Unknown esp_interface_t(%i)", std::to_underlying(interface)); + return fmt::format("Unknown esp_interface_t({})", std::to_underlying(interface)); } std::string toString(esp_netif_dhcp_status_t status) { switch (status) { - case ESP_NETIF_DHCP_INIT: return "INIT"; - case ESP_NETIF_DHCP_STARTED: return "STARTED"; - case ESP_NETIF_DHCP_STOPPED: return "STOPPED"; - default: - ESP_LOGW(TAG, "Unknown esp_netif_dhcp_status_t(%i)", std::to_underlying(status)); - return fmt::format("Unknown esp_netif_dhcp_status_t({})", std::to_underlying(status)); + case ESP_NETIF_DHCP_INIT: return "INIT"; + case ESP_NETIF_DHCP_STARTED: return "STARTED"; + case ESP_NETIF_DHCP_STOPPED: return "STOPPED"; + case ESP_NETIF_DHCP_STATUS_MAX: return "STATUS_MAX"; } + ESP_LOGW(TAG, "Unknown esp_netif_dhcp_status_t(%i)", std::to_underlying(status)); + return fmt::format("Unknown esp_netif_dhcp_status_t({})", std::to_underlying(status)); } const char * toString(wifi_err_reason_t reason) @@ -297,10 +312,9 @@ std::string toString(ip_addr_t val) { case IPADDR_TYPE_V4: return toString(val.u_addr.ip4); case IPADDR_TYPE_V6: return toString(val.u_addr.ip6); - default: - //ESP_LOGW(TAG, "Unknown ipv%hhu", val.type); - return fmt::format("Unknown ipv{}", val.type); } + //ESP_LOGW(TAG, "Unknown ipv%hhu", val.type); + return fmt::format("Unknown ipv{}", val.type); } std::string toString(const esp_ip_addr_t &val) @@ -309,10 +323,9 @@ std::string toString(const esp_ip_addr_t &val) { case IPADDR_TYPE_V4: return toString(val.u_addr.ip4); case IPADDR_TYPE_V6: return toString(val.u_addr.ip6); - default: - ESP_LOGW(TAG, "Unknown ipv%hhu", val.type); - return fmt::format("Unknown ipv{}", val.type); } + ESP_LOGW(TAG, "Unknown ipv%hhu", val.type); + return fmt::format("Unknown ipv{}", val.type); } } // namespace wifi_stack diff --git a/src/espwifiutils.h b/src/espwifiutils.h index c118fae..7d42a65 100644 --- a/src/espwifiutils.h +++ b/src/espwifiutils.h @@ -20,6 +20,7 @@ bool wifi_sta_config_equal(const wifi_sta_config_t& lhs, const wifi_sta_config_t std::string toString(wifi_auth_mode_t authMode); std::string toString(wifi_cipher_type_t cipherType); +std::string toString(wifi_bandwidth_t bandwidth); std::string toString(esp_interface_t interface); std::string toString(esp_netif_dhcp_status_t status); const char * toString(wifi_err_reason_t reason); diff --git a/src/udpsender.cpp b/src/udpsender.cpp index e301f83..0a6d991 100644 --- a/src/udpsender.cpp +++ b/src/udpsender.cpp @@ -121,9 +121,8 @@ std::expected UdpSender::send(ip_addr_t ip, uint16_t port, st recipient.sin6_port = htons(port); return send(recipient, buf); } - default: - return std::unexpected(fmt::format("unsupported ip type {}", ip.type)); } + return std::unexpected(fmt::format("unsupported ip type {}", ip.type)); } std::expected UdpSender::send(esp_ip_addr_t ip, uint16_t port, std::string_view buf) @@ -150,9 +149,8 @@ std::expected UdpSender::send(esp_ip_addr_t ip, uint16_t port recipient.sin6_port = htons(port); return send(recipient, buf); } - default: - return std::unexpected(fmt::format("unsupported ip type {}", ip.type)); } + return std::unexpected(fmt::format("unsupported ip type {}", ip.type)); } } // namespace wifi_stack