add support for mac_t and optional<mac_t>
This commit is contained in:
@@ -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<uint32_t>(value)); }
|
||||
inline esp_err_t nvs_set(nvs_handle handle, const char* key, double value) { return nvs_set(handle, key, std::bit_cast<uint64_t>(value)); }
|
||||
inline esp_err_t nvs_set(nvs_handle handle, const char* key, const double &value) { return nvs_set(handle, key, std::bit_cast<uint64_t>(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
|
||||
|
@@ -1,5 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
// system includes
|
||||
#include <optional>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
|
||||
// esp-idf includes
|
||||
#include <nvs.h>
|
||||
|
||||
@@ -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<uint8_t, 8> 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<wifi_stack::mac_t>* out_value)
|
||||
{
|
||||
union {
|
||||
uint64_t raw_value;
|
||||
struct {
|
||||
bool valid;
|
||||
std::array<uint8_t, 7> 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<uint8_t, 8> 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<wifi_stack::mac_t> &value)
|
||||
{
|
||||
union {
|
||||
uint64_t raw_value;
|
||||
struct {
|
||||
bool valid;
|
||||
std::array<uint8_t, 7> 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());
|
||||
|
@@ -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<wifi_stack::mac_t>)
|
||||
INSTANTIATE_CONFIGWRAPPER_TEMPLATES(wifi_stack::ip_address_t)
|
||||
|
Reference in New Issue
Block a user