diff --git a/src/configutils_base.h b/src/configutils_base.h index a8a20d0..9cc4259 100644 --- a/src/configutils_base.h +++ b/src/configutils_base.h @@ -67,11 +67,11 @@ inline esp_err_t nvs_set(nvs_handle handle, const char* key, int16_t value) inline esp_err_t nvs_set(nvs_handle handle, const char* key, uint16_t value) { return nvs_set_u16(handle, key, value); } inline esp_err_t nvs_set(nvs_handle handle, const char* key, int32_t value) { return nvs_set_i32(handle, key, value); } inline esp_err_t nvs_set(nvs_handle handle, const char* key, uint32_t value) { return nvs_set_u32(handle, key, value); } -inline esp_err_t nvs_set(nvs_handle handle, const char* key, int64_t value) { return nvs_set_i64(handle, key, value); } -inline esp_err_t nvs_set(nvs_handle handle, const char* key, uint64_t value) { return nvs_set_u64(handle, key, value); } +inline esp_err_t nvs_set(nvs_handle handle, const char* key, const int64_t &value) { return nvs_set_i64(handle, key, value); } +inline esp_err_t nvs_set(nvs_handle handle, const char* key, const uint64_t &value) { return nvs_set_u64(handle, key, value); } inline esp_err_t nvs_set(nvs_handle handle, const char* key, bool value) { return nvs_set_u8 (handle, key, value); } inline esp_err_t nvs_set(nvs_handle handle, const char* key, float value) { return nvs_set(handle, key, std::bit_cast(value)); } -inline esp_err_t nvs_set(nvs_handle handle, const char* key, double value) { return nvs_set(handle, key, std::bit_cast(value)); } +inline esp_err_t nvs_set(nvs_handle handle, const char* key, const double &value) { return nvs_set(handle, key, std::bit_cast(value)); } inline esp_err_t nvs_set(nvs_handle handle, const char* key, const std::string &value) { return nvs_set_str(handle, key, value.c_str()); } } // namespace espconfig diff --git a/src/configutils_espwifistack.h b/src/configutils_espwifistack.h index a45c9b9..1a145c7 100644 --- a/src/configutils_espwifistack.h +++ b/src/configutils_espwifistack.h @@ -1,5 +1,10 @@ #pragma once +// system includes +#include +#include +#include + // esp-idf includes #include @@ -9,6 +14,52 @@ namespace espconfig { +inline esp_err_t nvs_get(nvs_handle handle, const char* key, wifi_stack::mac_t* out_value) +{ + union { + uint64_t raw_value; + std::array bytes; + static_assert(sizeof(raw_value) == sizeof(bytes)); + }; + + const auto result = nvs_get(handle, key, &raw_value); + if (result == ESP_OK) + { + if (bytes[6] != 0x00 || bytes[7] != 0x00) + return ESP_ERR_INVALID_MAC; + std::copy(std::cbegin(bytes), std::cbegin(bytes) + wifi_stack::mac_t::static_size, std::begin(*out_value)); + } + return result; +} + +inline esp_err_t nvs_get(nvs_handle handle, const char* key, std::optional* out_value) +{ + union { + uint64_t raw_value; + struct { + bool valid; + std::array bytes; + } combined; + static_assert(sizeof(raw_value) == sizeof(combined)); + }; + + const auto result = nvs_get(handle, key, &raw_value); + if (result == ESP_OK) + { + if (combined.valid) + { + if (combined.bytes[6] != 0x00) + return ESP_ERR_INVALID_MAC; + + *out_value = wifi_stack::mac_t{}; + std::copy(std::cbegin(combined.bytes), std::cbegin(combined.bytes) + wifi_stack::mac_t::static_size, std::begin(**out_value)); + } + else + *out_value = std::nullopt; + } + return result; +} + inline esp_err_t nvs_get(nvs_handle handle, const char* key, wifi_stack::ip_address_t* out_value) { wifi_stack::ip_address_t::value_t temp; @@ -20,6 +71,42 @@ inline esp_err_t nvs_get(nvs_handle handle, const char* key, wifi_stack::ip_addr +inline esp_err_t nvs_set(nvs_handle handle, const char* key, const wifi_stack::mac_t &value) +{ + union { + uint64_t raw_value; + std::array bytes; + static_assert(sizeof(raw_value) == sizeof(bytes)); + }; + std::copy(std::begin(value), std::end(value), std::begin(bytes)); + bytes[6] = 0x00; + bytes[7] = 0x00; + return nvs_set(handle, key, raw_value); +} + +inline esp_err_t nvs_set(nvs_handle handle, const char* key, const std::optional &value) +{ + union { + uint64_t raw_value; + struct { + bool valid; + std::array bytes; + } combined; + static_assert(sizeof(raw_value) == sizeof(combined)); + }; + + if (value) + { + combined.valid = true; + std::copy(std::begin(*value), std::end(*value), std::begin(combined.bytes)); + combined.bytes[6] = 0x00; + } + else + combined.valid = false; + + return nvs_set(handle, key, raw_value); +} + inline esp_err_t nvs_set(nvs_handle handle, const char* key, wifi_stack::ip_address_t value) { return nvs_set(handle, key, value.value()); diff --git a/src/configwrapper_espwifistack.cpp b/src/configwrapper_espwifistack.cpp index 58024b1..d41cd44 100644 --- a/src/configwrapper_espwifistack.cpp +++ b/src/configwrapper_espwifistack.cpp @@ -2,4 +2,6 @@ #define CONFIGWRAPPER_TOSTRING_USINGS using ::wifi_stack::toString; #include "configwrapper_priv.h" +INSTANTIATE_CONFIGWRAPPER_TEMPLATES(wifi_stack::mac_t) +INSTANTIATE_CONFIGWRAPPER_TEMPLATES(std::optional) INSTANTIATE_CONFIGWRAPPER_TEMPLATES(wifi_stack::ip_address_t)