Updated to new esp-idf

This commit is contained in:
2023-02-27 17:27:15 +01:00
parent f6ce27ddff
commit b2f2ad72dd
8 changed files with 87 additions and 92 deletions

View File

@@ -25,7 +25,6 @@ set(dependencies
cpputils cpputils
espchrono espchrono
espcpputils espcpputils
expected
fmt fmt
) )

View File

@@ -144,7 +144,7 @@ mac_t _currentConnectPlanEntry;
std::vector<mac_t> _connectPlan; std::vector<mac_t> _connectPlan;
#ifdef CONFIG_ETH_ENABLED #ifdef CONFIG_ETH_ENABLED
std::optional<tl::expected<void, std::string>> _eth_init_status; std::optional<std::expected<void, std::string>> _eth_init_status;
#endif #endif
} // namespace } // namespace
@@ -163,7 +163,7 @@ const mac_t &currentConnectPlanEntry{_currentConnectPlanEntry};
const std::vector<mac_t> &connectPlan{_connectPlan}; const std::vector<mac_t> &connectPlan{_connectPlan};
#ifdef CONFIG_ETH_ENABLED #ifdef CONFIG_ETH_ENABLED
const std::optional<tl::expected<void, std::string>> &eth_init_status{_eth_init_status}; const std::optional<std::expected<void, std::string>> &eth_init_status{_eth_init_status};
#endif #endif
namespace { namespace {
@@ -280,8 +280,8 @@ esp_err_t wifi_low_level_init(const config &config);
esp_err_t wifi_start(); esp_err_t wifi_start();
esp_err_t wifi_low_level_deinit(); esp_err_t wifi_low_level_deinit();
esp_err_t wifi_stop(); esp_err_t wifi_stop();
tl::expected<void, std::string> applyBaseMac(const mac_t &mac); std::expected<void, std::string> applyBaseMac(const mac_t &mac);
tl::expected<mac_t, std::string> expectedBaseMac(const config &config); std::expected<mac_t, std::string> expectedBaseMac(const config &config);
esp_err_t wifi_set_ap_ip(const config &config, const static_ip_config &ip); esp_err_t wifi_set_ap_ip(const config &config, const static_ip_config &ip);
wifi_config_t make_sta_config(std::string_view ssid, std::string_view password, int8_t min_rssi, wifi_config_t make_sta_config(std::string_view ssid, std::string_view password, int8_t min_rssi,
std::optional<mac_t> bssid, uint8_t channel); std::optional<mac_t> bssid, uint8_t channel);
@@ -293,7 +293,7 @@ bool nextConnectPlanItem(const config &config, const sta_config &sta_config);
bool nextConnectPlanItem(const config &config, const sta_config &sta_config, const scan_result &scanResult); bool nextConnectPlanItem(const config &config, const sta_config &sta_config, const scan_result &scanResult);
void handleWifiEvents(const config &config, TickType_t xTicksToWait); void handleWifiEvents(const config &config, TickType_t xTicksToWait);
#ifdef CONFIG_ETH_ENABLED #ifdef CONFIG_ETH_ENABLED
tl::expected<void, std::string> eth_begin(const config &config, const eth_config &eth); std::expected<void, std::string> eth_begin(const config &config, const eth_config &eth);
#endif #endif
#ifdef CONFIG_PPP_SUPPORT #ifdef CONFIG_PPP_SUPPORT
esp_err_t modem_init(); esp_err_t modem_init();
@@ -816,13 +816,13 @@ WiFiStaStatus get_sta_status()
return _sta_status.load(); return _sta_status.load();
} }
tl::expected<void, std::string> begin_scan(const sta_config &sta_config) std::expected<void, std::string> begin_scan(const sta_config &sta_config)
{ {
if (!(get_wifi_mode() & WIFI_MODE_STA)) if (!(get_wifi_mode() & WIFI_MODE_STA))
return tl::make_unexpected("STA mode missing"); return std::unexpected("STA mode missing");
if (wifi_get_status_bits() & WIFI_SCANNING_BIT) if (wifi_get_status_bits() & WIFI_SCANNING_BIT)
return tl::make_unexpected("already scanning"); return std::unexpected("already scanning");
delete_scan_result(); delete_scan_result();
@@ -850,10 +850,10 @@ tl::expected<void, std::string> begin_scan(const sta_config &sta_config)
scanTimeout = time.max_per_chan * 20; scanTimeout = time.max_per_chan * 20;
} }
else else
return tl::make_unexpected("invalid scan settings (not active nor passive)!"); return std::unexpected("invalid scan settings (not active nor passive)!");
if (const auto result = esp_wifi_scan_start(&scan_config, false) != ESP_OK) if (const auto result = esp_wifi_scan_start(&scan_config, false) != ESP_OK)
return tl::make_unexpected(fmt::format("esp_wifi_scan_start() failed with: {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_wifi_scan_start() failed with: {}", esp_err_to_name(result)));
scanStarted = espchrono::millis_clock::now(); scanStarted = espchrono::millis_clock::now();
@@ -901,7 +901,7 @@ void delete_scan_result()
} }
} }
tl::expected<wifi_ap_record_t, std::string> get_sta_ap_info() std::expected<wifi_ap_record_t, std::string> get_sta_ap_info()
{ {
wifi_ap_record_t info; wifi_ap_record_t info;
if (const auto result = esp_wifi_sta_get_ap_info(&info); result == ESP_OK) if (const auto result = esp_wifi_sta_get_ap_info(&info); result == ESP_OK)
@@ -909,7 +909,7 @@ tl::expected<wifi_ap_record_t, std::string> get_sta_ap_info()
else else
{ {
ESP_LOGW(TAG, "esp_wifi_sta_get_ap_info() failed with %s", esp_err_to_name(result)); ESP_LOGW(TAG, "esp_wifi_sta_get_ap_info() failed with %s", esp_err_to_name(result));
return tl::make_unexpected(fmt::format("esp_wifi_sta_get_ap_info() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_wifi_sta_get_ap_info() failed with {}", esp_err_to_name(result)));
} }
} }
@@ -921,7 +921,7 @@ mac_or_error get_mac_addr(wifi_interface_t ifx)
else else
{ {
ESP_LOGW(TAG, "esp_wifi_get_mac() failed with %s", esp_err_to_name(result)); ESP_LOGW(TAG, "esp_wifi_get_mac() failed with %s", esp_err_to_name(result));
return tl::make_unexpected(fmt::format("esp_wifi_get_mac() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_wifi_get_mac() failed with {}", esp_err_to_name(result)));
} }
} }
@@ -934,7 +934,7 @@ mac_or_error get_default_mac_addr()
else else
{ {
//ESP_LOGE(TAG, "esp_efuse_mac_get_default() failed with %s", esp_err_to_name(result)); //ESP_LOGE(TAG, "esp_efuse_mac_get_default() failed with %s", esp_err_to_name(result));
return tl::make_unexpected(fmt::format("esp_efuse_mac_get_default() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_efuse_mac_get_default() failed with {}", esp_err_to_name(result)));
} }
}(); }();
@@ -950,7 +950,7 @@ mac_or_error get_custom_mac_addr()
else else
{ {
//ESP_LOGE(TAG, "esp_efuse_mac_get_custom() failed with %s", esp_err_to_name(result)); //ESP_LOGE(TAG, "esp_efuse_mac_get_custom() failed with %s", esp_err_to_name(result));
return tl::make_unexpected(fmt::format("esp_efuse_mac_get_custom() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_efuse_mac_get_custom() failed with {}", esp_err_to_name(result)));
} }
}(); }();
@@ -965,22 +965,22 @@ mac_or_error get_base_mac_addr()
else else
{ {
ESP_LOGE(TAG, "esp_base_mac_addr_get() failed with %s", esp_err_to_name(result)); ESP_LOGE(TAG, "esp_base_mac_addr_get() failed with %s", esp_err_to_name(result));
return tl::make_unexpected(fmt::format("esp_base_mac_addr_get() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_base_mac_addr_get() failed with {}", esp_err_to_name(result)));
} }
} }
tl::expected<void, std::string> set_base_mac_addr(mac_t mac_addr) std::expected<void, std::string> set_base_mac_addr(mac_t mac_addr)
{ {
if (const auto result = esp_base_mac_addr_set(std::cbegin(mac_addr)); result == ESP_OK) if (const auto result = esp_base_mac_addr_set(std::cbegin(mac_addr)); result == ESP_OK)
return {}; return {};
else else
{ {
ESP_LOGE(TAG, "esp_base_mac_addr_set() failed with %s", esp_err_to_name(result)); ESP_LOGE(TAG, "esp_base_mac_addr_set() failed with %s", esp_err_to_name(result));
return tl::make_unexpected(fmt::format("esp_base_mac_addr_set() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_base_mac_addr_set() failed with {}", esp_err_to_name(result)));
} }
} }
tl::expected<esp_netif_ip_info_t, std::string> get_ip_info(esp_netif_t *esp_netif) std::expected<esp_netif_ip_info_t, std::string> get_ip_info(esp_netif_t *esp_netif)
{ {
esp_netif_ip_info_t ip; esp_netif_ip_info_t ip;
if (const auto result = esp_netif_get_ip_info(esp_netif, &ip); result == ESP_OK) if (const auto result = esp_netif_get_ip_info(esp_netif, &ip); result == ESP_OK)
@@ -988,26 +988,26 @@ tl::expected<esp_netif_ip_info_t, std::string> get_ip_info(esp_netif_t *esp_neti
else else
{ {
ESP_LOGE(TAG, "esp_netif_get_ip_info() failed with %s", esp_err_to_name(result)); ESP_LOGE(TAG, "esp_netif_get_ip_info() failed with %s", esp_err_to_name(result));
return tl::make_unexpected(fmt::format("tcpip_adapter_get_ip_info() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("tcpip_adapter_get_ip_info() failed with {}", esp_err_to_name(result)));
} }
} }
tl::expected<std::string_view, std::string> get_hostname_for_interface(esp_interface_t interf) std::expected<std::string_view, std::string> get_hostname_for_interface(esp_interface_t interf)
{ {
if (const auto netif = esp_netifs[interf]) if (const auto netif = esp_netifs[interf])
return get_hostname_for_interface(netif); return get_hostname_for_interface(netif);
else else
return tl::make_unexpected(fmt::format("netif for {} is invalid", std::to_underlying(interf))); return std::unexpected(fmt::format("netif for {} is invalid", std::to_underlying(interf)));
} }
tl::expected<std::string_view, std::string> get_hostname_for_interface(esp_netif_t *esp_netif) std::expected<std::string_view, std::string> get_hostname_for_interface(esp_netif_t *esp_netif)
{ {
const char *hostname{}; const char *hostname{};
if (const auto result = esp_netif_get_hostname(esp_netif, &hostname)) if (const auto result = esp_netif_get_hostname(esp_netif, &hostname))
return tl::make_unexpected(fmt::format("esp_netif_get_hostname() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_netif_get_hostname() failed with {}", esp_err_to_name(result)));
if (!hostname) if (!hostname)
return tl::make_unexpected("esp_netif_get_hostname() returned a nullptr string"); return std::unexpected("esp_netif_get_hostname() returned a nullptr string");
return std::string_view{hostname}; return std::string_view{hostname};
} }
@@ -2078,7 +2078,7 @@ esp_err_t wifi_stop()
return ESP_OK; return ESP_OK;
} }
tl::expected<void, std::string> applyBaseMac(const mac_t &mac) std::expected<void, std::string> applyBaseMac(const mac_t &mac)
{ {
if (const auto result = set_base_mac_addr(mac); result) if (const auto result = set_base_mac_addr(mac); result)
return {}; return {};
@@ -2086,11 +2086,11 @@ tl::expected<void, std::string> applyBaseMac(const mac_t &mac)
{ {
const auto msg = fmt::format("set_base_mac_addr() {} failed: {}", toString(mac), result.error()); const auto msg = fmt::format("set_base_mac_addr() {} failed: {}", toString(mac), result.error());
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
} }
tl::expected<mac_t, std::string> expectedBaseMac(const config &config) std::expected<mac_t, std::string> expectedBaseMac(const config &config)
{ {
if (config.base_mac_override) if (config.base_mac_override)
{ {
@@ -2111,7 +2111,7 @@ tl::expected<mac_t, std::string> expectedBaseMac(const config &config)
{ {
const auto msg = fmt::format("no base mac fuse or override set and get_default_mac_addr() failed: {}", mac.error()); const auto msg = fmt::format("no base mac fuse or override set and get_default_mac_addr() failed: {}", mac.error());
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
} }
@@ -2698,7 +2698,7 @@ void handleWifiEvents(const config &config, TickType_t xTicksToWait)
} }
#ifdef CONFIG_ETH_ENABLED #ifdef CONFIG_ETH_ENABLED
tl::expected<void, std::string> eth_begin(const config &config, const eth_config &eth) std::expected<void, std::string> eth_begin(const config &config, const eth_config &eth)
{ {
esp_netif_config_t cfg ESP_NETIF_DEFAULT_ETH(); esp_netif_config_t cfg ESP_NETIF_DEFAULT_ETH();
esp_netif_inherent_config_t newBase = *cfg.base; esp_netif_inherent_config_t newBase = *cfg.base;
@@ -2710,7 +2710,7 @@ tl::expected<void, std::string> eth_begin(const config &config, const eth_config
{ {
auto msg = std::string{"esp_netif_new() failed"}; auto msg = std::string{"esp_netif_new() failed"};
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
esp_eth_mac_t *eth_mac{}; esp_eth_mac_t *eth_mac{};
@@ -2728,7 +2728,7 @@ tl::expected<void, std::string> eth_begin(const config &config, const eth_config
{ {
auto msg = std::string{"esp_eth_mac_new_esp32() failed"}; auto msg = std::string{"esp_eth_mac_new_esp32() failed"};
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
eth_phy_config_t phy_config ETH_PHY_DEFAULT_CONFIG(); eth_phy_config_t phy_config ETH_PHY_DEFAULT_CONFIG();
@@ -2744,7 +2744,7 @@ tl::expected<void, std::string> eth_begin(const config &config, const eth_config
{ {
auto msg = std::string{"esp_eth_phy_new_lan8720() failed"}; auto msg = std::string{"esp_eth_phy_new_lan8720() failed"};
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
} }
else else
@@ -2754,7 +2754,7 @@ tl::expected<void, std::string> eth_begin(const config &config, const eth_config
{ {
auto msg = std::string{"esp_eth_phy_new_ksz8041() failed"}; auto msg = std::string{"esp_eth_phy_new_ksz8041() failed"};
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
} }
@@ -2766,14 +2766,14 @@ tl::expected<void, std::string> eth_begin(const config &config, const eth_config
{ {
auto msg = fmt::format("esp_eth_driver_install() failed with {}", esp_err_to_name(result)); auto msg = fmt::format("esp_eth_driver_install() failed with {}", esp_err_to_name(result));
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
if (!eth_handle) if (!eth_handle)
{ {
auto msg = std::string{"esp_eth_driver_install() invalid eth_handle"}; auto msg = std::string{"esp_eth_driver_install() invalid eth_handle"};
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
/* attach Ethernet driver to TCP/IP stack */ /* attach Ethernet driver to TCP/IP stack */
@@ -2782,14 +2782,14 @@ tl::expected<void, std::string> eth_begin(const config &config, const eth_config
{ {
auto msg = std::string{"esp_eth_new_netif_glue() failed"}; auto msg = std::string{"esp_eth_new_netif_glue() failed"};
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
if (const auto result = esp_netif_attach(esp_netifs[ESP_IF_ETH], ptr); result != ESP_OK) if (const auto result = esp_netif_attach(esp_netifs[ESP_IF_ETH], ptr); result != ESP_OK)
{ {
auto msg = fmt::format("esp_netif_attach() failed with {}", esp_err_to_name(result)); auto msg = fmt::format("esp_netif_attach() failed with {}", esp_err_to_name(result));
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
eth_initialized = true; eth_initialized = true;
@@ -2800,7 +2800,7 @@ tl::expected<void, std::string> eth_begin(const config &config, const eth_config
{ {
auto msg = fmt::format("esp_eth_start() failed with {}", esp_err_to_name(result)); auto msg = fmt::format("esp_eth_start() failed with {}", esp_err_to_name(result));
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
eth_started = true; eth_started = true;

View File

@@ -7,6 +7,7 @@
#include <string_view> #include <string_view>
#include <vector> #include <vector>
#include <optional> #include <optional>
#include <expected>
// esp-idf includes // esp-idf includes
#include <esp_wifi.h> #include <esp_wifi.h>
@@ -17,7 +18,6 @@
#endif #endif
// 3rdparty lib includes // 3rdparty lib includes
#include <tl/expected.hpp>
#include <espchrono.h> #include <espchrono.h>
#include <cppsignal.h> #include <cppsignal.h>
#include <ring-buffer.h> #include <ring-buffer.h>
@@ -71,7 +71,7 @@ extern const mac_t &currentConnectPlanEntry;
extern const std::vector<mac_t> &connectPlan; extern const std::vector<mac_t> &connectPlan;
#ifdef CONFIG_ETH_ENABLED #ifdef CONFIG_ETH_ENABLED
extern const std::optional<tl::expected<void, std::string>> &eth_init_status; extern const std::optional<std::expected<void, std::string>> &eth_init_status;
#endif #endif
wifi_mode_t get_wifi_mode(); wifi_mode_t get_wifi_mode();
@@ -80,7 +80,7 @@ wifi_mode_t get_wifi_mode();
WiFiStaStatus get_sta_status(); WiFiStaStatus get_sta_status();
//! Tries to begin a new scan, if succeeds clears the old scan result //! Tries to begin a new scan, if succeeds clears the old scan result
tl::expected<void, std::string> begin_scan(const sta_config &sta_config); std::expected<void, std::string> begin_scan(const sta_config &sta_config);
//! Tells the status of the currently running scan (finished, ...) //! Tells the status of the currently running scan (finished, ...)
WiFiScanStatus get_scan_status(); WiFiScanStatus get_scan_status();
@@ -93,16 +93,16 @@ const std::optional<scan_result> &get_scan_result();
void delete_scan_result(); void delete_scan_result();
//! Util wrappers //! Util wrappers
using mac_or_error = tl::expected<mac_t, std::string>; using mac_or_error = std::expected<mac_t, std::string>;
tl::expected<wifi_ap_record_t, std::string> get_sta_ap_info(); std::expected<wifi_ap_record_t, std::string> get_sta_ap_info();
mac_or_error get_mac_addr(wifi_interface_t ifx); mac_or_error get_mac_addr(wifi_interface_t ifx);
mac_or_error get_default_mac_addr(); mac_or_error get_default_mac_addr();
mac_or_error get_custom_mac_addr(); mac_or_error get_custom_mac_addr();
mac_or_error get_base_mac_addr(); mac_or_error get_base_mac_addr();
tl::expected<void, std::string> set_base_mac_addr(mac_t mac_addr); std::expected<void, std::string> set_base_mac_addr(mac_t mac_addr);
tl::expected<esp_netif_ip_info_t, std::string> get_ip_info(esp_netif_t *esp_netif); std::expected<esp_netif_ip_info_t, std::string> get_ip_info(esp_netif_t *esp_netif);
tl::expected<std::string_view, std::string> get_hostname_for_interface(esp_interface_t interf); std::expected<std::string_view, std::string> get_hostname_for_interface(esp_interface_t interf);
tl::expected<std::string_view, std::string> get_hostname_for_interface(esp_netif_t *esp_netif); std::expected<std::string_view, std::string> get_hostname_for_interface(esp_netif_t *esp_netif);
#ifdef CONFIG_ETH_ENABLED #ifdef CONFIG_ETH_ENABLED
esp_eth_handle_t getEthHandle(); esp_eth_handle_t getEthHandle();

View File

@@ -21,7 +21,7 @@
inline bool operator==(const wifi_country_t &left, const wifi_country_t &right) inline bool operator==(const wifi_country_t &left, const wifi_country_t &right)
{ {
return left.cc == right.cc && return std::equal(std::begin(left.cc), std::end(left.cc), std::begin(right.cc)) &&
left.schan == right.schan && left.schan == right.schan &&
left.nchan == right.nchan && left.nchan == right.nchan &&
left.max_tx_power == right.max_tx_power && left.max_tx_power == right.max_tx_power &&

View File

@@ -184,14 +184,14 @@ const char * toString(wifi_err_reason_t reason)
return "UNKNOWN"; return "UNKNOWN";
} }
template<> tl::expected<mac_t, std::string> fromString<mac_t>(std::string_view str) template<> std::expected<mac_t, std::string> fromString<mac_t>(std::string_view str)
{ {
mac_t result; mac_t result{};
if (std::sscanf(str.data(), "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", if (std::sscanf(str.data(), "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
&result[0], &result[1], &result[2], &result[3], &result[4], &result[5]) == 6) &result[0], &result[1], &result[2], &result[3], &result[4], &result[5]) == 6)
return result; return result;
return tl::make_unexpected(fmt::format("invalid format ({})", str)); return std::unexpected(fmt::format("invalid format ({})", str));
} }
std::string toString(const mac_t &val) std::string toString(const mac_t &val)
@@ -205,7 +205,7 @@ std::string toString(const std::optional<mac_t> &val)
return val ? toString(*val) : "nullopt"; return val ? toString(*val) : "nullopt";
} }
template<> tl::expected<ip_address_t, std::string> fromString<ip_address_t>(std::string_view str) template<> std::expected<ip_address_t, std::string> fromString<ip_address_t>(std::string_view str)
{ {
// TODO: add support for "a", "a.b", "a.b.c" formats // TODO: add support for "a", "a.b", "a.b.c" formats
// TODO: replace with scanf for better performance // TODO: replace with scanf for better performance
@@ -221,22 +221,22 @@ template<> tl::expected<ip_address_t, std::string> fromString<ip_address_t>(std:
{ {
acc = acc * 10 + (c - '0'); acc = acc * 10 + (c - '0');
if (acc > 255) if (acc > 255)
return tl::make_unexpected("Value out of [0..255] range"); return std::unexpected("Value out of [0..255] range");
} }
else if (c == '.') else if (c == '.')
{ {
if (dots == 3) if (dots == 3)
return tl::make_unexpected("Too many dots (there must be 3 dots)"); return std::unexpected("Too many dots (there must be 3 dots)");
result[dots++] = acc; result[dots++] = acc;
acc = 0; acc = 0;
} }
else else
return tl::make_unexpected("Invalid char"); return std::unexpected("Invalid char");
} }
if (dots != 3) if (dots != 3)
return tl::make_unexpected("Too few dots (there must be 3 dots)"); return std::unexpected("Too few dots (there must be 3 dots)");
result[3] = acc; result[3] = acc;

View File

@@ -6,6 +6,7 @@
#include <string_view> #include <string_view>
#include <array> #include <array>
#include <optional> #include <optional>
#include <expected>
// esp-idf includes // esp-idf includes
#include <esp_wifi_types.h> #include <esp_wifi_types.h>
@@ -13,9 +14,6 @@
#include <esp_netif_types.h> #include <esp_netif_types.h>
#include <lwip/ip_addr.h> #include <lwip/ip_addr.h>
// 3rdparty lib includes
#include <tl/expected.hpp>
namespace wifi_stack { namespace wifi_stack {
bool wifi_ap_config_equal(const wifi_ap_config_t& lhs, const wifi_ap_config_t& rhs); bool wifi_ap_config_equal(const wifi_ap_config_t& lhs, const wifi_ap_config_t& rhs);
bool wifi_sta_config_equal(const wifi_sta_config_t& lhs, const wifi_sta_config_t& rhs); bool wifi_sta_config_equal(const wifi_sta_config_t& lhs, const wifi_sta_config_t& rhs);
@@ -26,7 +24,7 @@ std::string toString(esp_interface_t interface);
std::string toString(esp_netif_dhcp_status_t status); std::string toString(esp_netif_dhcp_status_t status);
const char * toString(wifi_err_reason_t reason); const char * toString(wifi_err_reason_t reason);
template<typename T> tl::expected<T, std::string> fromString(std::string_view str) = delete; template<typename T> std::expected<T, std::string> fromString(std::string_view str) = delete;
// A class to make it easier to handle and pass around mac addresses / bssids // A class to make it easier to handle and pass around mac addresses / bssids
@@ -49,7 +47,7 @@ public:
} }
}; };
template<> tl::expected<mac_t, std::string> fromString<mac_t>(std::string_view str); template<> std::expected<mac_t, std::string> fromString<mac_t>(std::string_view str);
std::string toString(const mac_t &val); std::string toString(const mac_t &val);
std::string toString(const std::optional<mac_t> &val); std::string toString(const std::optional<mac_t> &val);
@@ -100,7 +98,7 @@ public:
constexpr ip_address_t& operator=(value_t value) noexcept { _value = value; return *this; } constexpr ip_address_t& operator=(value_t value) noexcept { _value = value; return *this; }
}; };
template<> tl::expected<ip_address_t, std::string> fromString<ip_address_t>(std::string_view str); template<> std::expected<ip_address_t, std::string> fromString<ip_address_t>(std::string_view str);
std::string toString(ip_address_t val); std::string toString(ip_address_t val);
std::string toString(const std::optional<ip_address_t> &val); std::string toString(const std::optional<ip_address_t> &val);

View File

@@ -38,28 +38,28 @@ UdpSender::~UdpSender()
close(m_udp_server); close(m_udp_server);
} }
tl::expected<void, std::string> UdpSender::send(esp_interface_t interf, uint16_t port, std::string_view buf) std::expected<void, std::string> UdpSender::send(esp_interface_t interf, uint16_t port, std::string_view buf)
{ {
const auto interfPtr = esp_netifs[interf]; const auto interfPtr = esp_netifs[interf];
if (!interfPtr) if (!interfPtr)
return tl::make_unexpected(fmt::format("esp_netifs[{}] is invalid", std::to_underlying(interf))); return std::unexpected(fmt::format("esp_netifs[{}] is invalid", std::to_underlying(interf)));
return send(interfPtr, port, buf); return send(interfPtr, port, buf);
} }
tl::expected<void, std::string> UdpSender::send(esp_netif_t *interf, uint16_t port, std::string_view buf) std::expected<void, std::string> UdpSender::send(esp_netif_t *interf, uint16_t port, std::string_view buf)
{ {
if (!interf) if (!interf)
return tl::make_unexpected("invalid interf"); return std::unexpected("invalid interf");
esp_netif_ip_info_t ip; esp_netif_ip_info_t ip;
if (const auto result = esp_netif_get_ip_info(interf, &ip); result != ESP_OK) if (const auto result = esp_netif_get_ip_info(interf, &ip); result != ESP_OK)
return tl::make_unexpected(fmt::format("esp_netif_get_ip_info() failed with {}", esp_err_to_name(result))); return std::unexpected(fmt::format("esp_netif_get_ip_info() failed with {}", esp_err_to_name(result)));
return send(ip, port, buf); return send(ip, port, buf);
} }
tl::expected<void, std::string> UdpSender::send(const esp_netif_ip_info_t &ip, uint16_t port, std::string_view buf) std::expected<void, std::string> UdpSender::send(const esp_netif_ip_info_t &ip, uint16_t port, std::string_view buf)
{ {
struct sockaddr_in recipient; struct sockaddr_in recipient;
@@ -71,33 +71,33 @@ tl::expected<void, std::string> UdpSender::send(const esp_netif_ip_info_t &ip, u
return send(recipient, buf); return send(recipient, buf);
} }
tl::expected<void, std::string> UdpSender::send(const struct sockaddr_in &recipient, std::string_view buf) std::expected<void, std::string> UdpSender::send(const struct sockaddr_in &recipient, std::string_view buf)
{ {
if (!ready()) if (!ready())
return tl::make_unexpected("initializing failed, not ready to send"); return std::unexpected("initializing failed, not ready to send");
if (const ssize_t sent = sendto(m_udp_server, buf.data(), buf.size(), 0, (const struct sockaddr*)&recipient, sizeof(recipient)); sent < 0) if (const ssize_t sent = sendto(m_udp_server, buf.data(), buf.size(), 0, (const struct sockaddr*)&recipient, sizeof(recipient)); sent < 0)
return tl::make_unexpected(fmt::format("send failed with {} (errno={})", sent, errno)); return std::unexpected(fmt::format("send failed with {} (errno={})", sent, errno));
else if (sent != buf.size()) else if (sent != buf.size())
return tl::make_unexpected(fmt::format("sent bytes does not match, expected={}, sent={}", buf.size(), sent)); return std::unexpected(fmt::format("sent bytes does not match, expected={}, sent={}", buf.size(), sent));
return {}; return {};
} }
tl::expected<void, std::string> UdpSender::send(const struct sockaddr_in6 &recipient, std::string_view buf) std::expected<void, std::string> UdpSender::send(const struct sockaddr_in6 &recipient, std::string_view buf)
{ {
if (!ready()) if (!ready())
return tl::make_unexpected("initializing failed, not ready to send"); return std::unexpected("initializing failed, not ready to send");
if (const ssize_t sent = sendto(m_udp_server, buf.data(), buf.size(), 0, (const struct sockaddr*)&recipient, sizeof(recipient)); sent < 0) if (const ssize_t sent = sendto(m_udp_server, buf.data(), buf.size(), 0, (const struct sockaddr*)&recipient, sizeof(recipient)); sent < 0)
return tl::make_unexpected(fmt::format("send failed with {} (errno={})", sent, errno)); return std::unexpected(fmt::format("send failed with {} (errno={})", sent, errno));
else if (sent != buf.size()) else if (sent != buf.size())
return tl::make_unexpected(fmt::format("sent bytes does not match, expected={}, sent={}", buf.size(), sent)); return std::unexpected(fmt::format("sent bytes does not match, expected={}, sent={}", buf.size(), sent));
return {}; return {};
} }
tl::expected<void, std::string> UdpSender::send(ip_addr_t ip, uint16_t port, std::string_view buf) std::expected<void, std::string> UdpSender::send(ip_addr_t ip, uint16_t port, std::string_view buf)
{ {
switch (ip.type) switch (ip.type)
{ {
@@ -122,11 +122,11 @@ tl::expected<void, std::string> UdpSender::send(ip_addr_t ip, uint16_t port, std
return send(recipient, buf); return send(recipient, buf);
} }
default: default:
return tl::make_unexpected(fmt::format("unsupported ip type {}", ip.type)); return std::unexpected(fmt::format("unsupported ip type {}", ip.type));
} }
} }
tl::expected<void, std::string> UdpSender::send(esp_ip_addr_t ip, uint16_t port, std::string_view buf) std::expected<void, std::string> UdpSender::send(esp_ip_addr_t ip, uint16_t port, std::string_view buf)
{ {
switch (ip.type) switch (ip.type)
{ {
@@ -151,7 +151,7 @@ tl::expected<void, std::string> UdpSender::send(esp_ip_addr_t ip, uint16_t port,
return send(recipient, buf); return send(recipient, buf);
} }
default: default:
return tl::make_unexpected(fmt::format("unsupported ip type {}", ip.type)); return std::unexpected(fmt::format("unsupported ip type {}", ip.type));
} }
} }

View File

@@ -3,6 +3,7 @@
// system includes // system includes
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <expected>
// esp-idf includes // esp-idf includes
#include <esp_interface.h> #include <esp_interface.h>
@@ -10,9 +11,6 @@
#include <esp_netif_types.h> #include <esp_netif_types.h>
#include <lwip/sockets.h> #include <lwip/sockets.h>
// 3rdparty lib includes
#include <tl/expected.hpp>
namespace wifi_stack { namespace wifi_stack {
class UdpSender class UdpSender
@@ -23,13 +21,13 @@ public:
bool ready() const { return m_udp_server != -1; } bool ready() const { return m_udp_server != -1; }
tl::expected<void, std::string> send(esp_interface_t interf, uint16_t port, std::string_view buf); std::expected<void, std::string> send(esp_interface_t interf, uint16_t port, std::string_view buf);
tl::expected<void, std::string> send(esp_netif_t *interf, uint16_t port, std::string_view buf); std::expected<void, std::string> send(esp_netif_t *interf, uint16_t port, std::string_view buf);
tl::expected<void, std::string> send(const esp_netif_ip_info_t &ip, uint16_t port, std::string_view buf); std::expected<void, std::string> send(const esp_netif_ip_info_t &ip, uint16_t port, std::string_view buf);
tl::expected<void, std::string> send(const struct sockaddr_in &recipient, std::string_view buf); std::expected<void, std::string> send(const struct sockaddr_in &recipient, std::string_view buf);
tl::expected<void, std::string> send(const struct sockaddr_in6 &recipient, std::string_view buf); std::expected<void, std::string> send(const struct sockaddr_in6 &recipient, std::string_view buf);
tl::expected<void, std::string> send(ip_addr_t ip, uint16_t port, std::string_view buf); std::expected<void, std::string> send(ip_addr_t ip, uint16_t port, std::string_view buf);
tl::expected<void, std::string> send(esp_ip_addr_t ip, uint16_t port, std::string_view buf); std::expected<void, std::string> send(esp_ip_addr_t ip, uint16_t port, std::string_view buf);
private: private:
const int m_udp_server; const int m_udp_server;