Sync from fork #1
@@ -36,7 +36,6 @@ set(dependencies
|
|||||||
espwifistack
|
espwifistack
|
||||||
date
|
date
|
||||||
espchrono
|
espchrono
|
||||||
fmt
|
|
||||||
nvs_flash
|
nvs_flash
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -3,9 +3,7 @@
|
|||||||
// system includes
|
// system includes
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
|
#include <format>
|
||||||
// 3rdparty lib includes
|
|
||||||
#include <fmt/core.h>
|
|
||||||
|
|
||||||
// local includes
|
// local includes
|
||||||
#include "configwrapper.h"
|
#include "configwrapper.h"
|
||||||
@@ -16,7 +14,7 @@ template<int MAX_LENGTH>
|
|||||||
ConfigConstraintReturnType StringMaxSize(const std::string &str)
|
ConfigConstraintReturnType StringMaxSize(const std::string &str)
|
||||||
{
|
{
|
||||||
if (str.size() > MAX_LENGTH)
|
if (str.size() > MAX_LENGTH)
|
||||||
return std::unexpected(fmt::format("String length {} exceeds maximum {}", str.size(), MAX_LENGTH));
|
return std::unexpected(std::format("String length {} exceeds maximum {}", str.size(), MAX_LENGTH));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,7 +22,7 @@ template<int MIN_LENGTH, int MAX_LENGTH>
|
|||||||
ConfigConstraintReturnType StringMinMaxSize(const std::string &str)
|
ConfigConstraintReturnType StringMinMaxSize(const std::string &str)
|
||||||
{
|
{
|
||||||
if (str.size() < MIN_LENGTH || str.size() > MAX_LENGTH)
|
if (str.size() < MIN_LENGTH || str.size() > MAX_LENGTH)
|
||||||
return std::unexpected(fmt::format("String length {} exceeds range {} to {}", str.size(), MIN_LENGTH, MAX_LENGTH));
|
return std::unexpected(std::format("String length {} exceeds range {} to {}", str.size(), MIN_LENGTH, MAX_LENGTH));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +47,7 @@ ConfigConstraintReturnType StringOr(const std::string &str)
|
|||||||
const auto result1 = callback1(str);
|
const auto result1 = callback1(str);
|
||||||
if (result1)
|
if (result1)
|
||||||
return {};
|
return {};
|
||||||
return std::unexpected(fmt::format("None of the following 2 constraints succeded: {} | {}", result0.error(), result1.error()));
|
return std::unexpected(std::format("None of the following 2 constraints succeded: {} | {}", result0.error(), result1.error()));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<ConfigWrapper<std::string>::ConstraintCallback callback0, ConfigWrapper<std::string>::ConstraintCallback callback1, ConfigWrapper<std::string>::ConstraintCallback callback2>
|
template<ConfigWrapper<std::string>::ConstraintCallback callback0, ConfigWrapper<std::string>::ConstraintCallback callback1, ConfigWrapper<std::string>::ConstraintCallback callback2>
|
||||||
@@ -64,7 +62,7 @@ ConfigConstraintReturnType StringOr(const std::string &str)
|
|||||||
const auto result2 = callback2(str);
|
const auto result2 = callback2(str);
|
||||||
if (result2)
|
if (result2)
|
||||||
return {};
|
return {};
|
||||||
return std::unexpected(fmt::format("None of the following 3 constraints succeded: {} | {} | {}", result0.error(), result1.error(), result2.error()));
|
return std::unexpected(std::format("None of the following 3 constraints succeded: {} | {} | {}", result0.error(), result1.error(), result2.error()));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<ConfigWrapper<std::string>::ConstraintCallback callback0, ConfigWrapper<std::string>::ConstraintCallback callback1>
|
template<ConfigWrapper<std::string>::ConstraintCallback callback0, ConfigWrapper<std::string>::ConstraintCallback callback1>
|
||||||
@@ -101,7 +99,7 @@ template<typename T, T MIN_VALUE>
|
|||||||
ConfigConstraintReturnType MinValue(typename ConfigWrapper<T>::value_t val)
|
ConfigConstraintReturnType MinValue(typename ConfigWrapper<T>::value_t val)
|
||||||
{
|
{
|
||||||
if (val < MIN_VALUE)
|
if (val < MIN_VALUE)
|
||||||
return std::unexpected(fmt::format("Value {} exceeds minimum {}", val, MIN_VALUE));
|
return std::unexpected(std::format("Value {} exceeds minimum {}", val, MIN_VALUE));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +107,7 @@ template<typename T, T MAX_VALUE>
|
|||||||
ConfigConstraintReturnType MaxValue(typename ConfigWrapper<T>::value_t val)
|
ConfigConstraintReturnType MaxValue(typename ConfigWrapper<T>::value_t val)
|
||||||
{
|
{
|
||||||
if (val > MAX_VALUE)
|
if (val > MAX_VALUE)
|
||||||
return std::unexpected(fmt::format("Value {} exceeds maximum {}", val, MAX_VALUE));
|
return std::unexpected(std::format("Value {} exceeds maximum {}", val, MAX_VALUE));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +115,7 @@ template<typename T, T MIN_VALUE, T MAX_VALUE>
|
|||||||
ConfigConstraintReturnType MinMaxValue(typename ConfigWrapper<T>::value_t val)
|
ConfigConstraintReturnType MinMaxValue(typename ConfigWrapper<T>::value_t val)
|
||||||
{
|
{
|
||||||
if (val < MIN_VALUE || val > MAX_VALUE)
|
if (val < MIN_VALUE || val > MAX_VALUE)
|
||||||
return std::unexpected(fmt::format("Value {} exceeds range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
return std::unexpected(std::format("Value {} exceeds range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +123,7 @@ template<typename T>
|
|||||||
ConfigConstraintReturnType MinMaxValue(typename ConfigWrapper<T>::value_t val, T MIN_VALUE, T MAX_VALUE)
|
ConfigConstraintReturnType MinMaxValue(typename ConfigWrapper<T>::value_t val, T MIN_VALUE, T MAX_VALUE)
|
||||||
{
|
{
|
||||||
if (val < MIN_VALUE || val > MAX_VALUE)
|
if (val < MIN_VALUE || val > MAX_VALUE)
|
||||||
return std::unexpected(fmt::format("Value {} exceeds range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
return std::unexpected(std::format("Value {} exceeds range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +131,7 @@ template<typename T, T MIN_VALUE, T MAX_VALUE>
|
|||||||
ConfigConstraintReturnType MinMaxOrZeroValue(typename ConfigWrapper<T>::value_t val)
|
ConfigConstraintReturnType MinMaxOrZeroValue(typename ConfigWrapper<T>::value_t val)
|
||||||
{
|
{
|
||||||
if (val != 0 && (val < MIN_VALUE || val > MAX_VALUE))
|
if (val != 0 && (val < MIN_VALUE || val > MAX_VALUE))
|
||||||
return std::unexpected(fmt::format("Value {} exceeds constraint 0 or range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
return std::unexpected(std::format("Value {} exceeds constraint 0 or range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
} // namespace espconfig
|
} // namespace espconfig
|
||||||
|
@@ -1,12 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// system includes
|
// system includes
|
||||||
#include <string>
|
|
||||||
#include <expected>
|
#include <expected>
|
||||||
|
|
||||||
// 3rdparty lib includes
|
|
||||||
#include <fmt/core.h>
|
|
||||||
|
|
||||||
// local includes
|
// local includes
|
||||||
#include "configwrapper.h"
|
#include "configwrapper.h"
|
||||||
#include "espchrono.h"
|
#include "espchrono.h"
|
||||||
|
@@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
#include "configmanager.h"
|
#include "configmanager.h"
|
||||||
|
|
||||||
|
// system includes
|
||||||
|
#include <format>
|
||||||
|
|
||||||
// esp-idf includes
|
// esp-idf includes
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
|
|
||||||
// local includes
|
// 3rdparty lib includes
|
||||||
#include "espchrono.h"
|
#include <espchrono.h>
|
||||||
|
|
||||||
#define INSTANTIATE_CONFIGMANAGER_TEMPLATES(Type) \
|
#define INSTANTIATE_CONFIGMANAGER_TEMPLATES(Type) \
|
||||||
namespace espconfig { \
|
namespace espconfig { \
|
||||||
@@ -45,7 +48,7 @@ esp_err_t ConfigManager<ConfigContainer>::init(const char *ns)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else // CONFIG_NVS_ENCRYPTION
|
||||||
const esp_partition_t *key_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS, NULL);
|
const esp_partition_t *key_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS, NULL);
|
||||||
if (!key_part)
|
if (!key_part)
|
||||||
{
|
{
|
||||||
@@ -87,7 +90,7 @@ esp_err_t ConfigManager<ConfigContainer>::init(const char *ns)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif // CONFIG_NVS_ENCRYPTION
|
||||||
|
|
||||||
{
|
{
|
||||||
const auto result = nvs_open_from_partition("nvs", ns, NVS_READWRITE, &nvs_handle_user);
|
const auto result = nvs_open_from_partition("nvs", ns, NVS_READWRITE, &nvs_handle_user);
|
||||||
@@ -119,12 +122,19 @@ esp_err_t ConfigManager<ConfigContainer>::loadFromFlash()
|
|||||||
|
|
||||||
bool success = true;
|
bool success = true;
|
||||||
ConfigContainer::callForEveryConfig([&](ConfigWrapperInterface &config){
|
ConfigContainer::callForEveryConfig([&](ConfigWrapperInterface &config){
|
||||||
|
if (strlen(config.nvsName()) > 15)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "config key '%s' is longer than 15 characters", config.nvsName());
|
||||||
|
success = false;
|
||||||
|
return false; // don't abort loop
|
||||||
|
}
|
||||||
|
|
||||||
if (const auto result = config.loadFromFlash(nvs_handle_user); !result)
|
if (const auto result = config.loadFromFlash(nvs_handle_user); !result)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "config parameter %s failed to load: %.*s", config.nvsName(), result.error().size(), result.error().data());
|
ESP_LOGE(TAG, "config parameter %s failed to load: %.*s", config.nvsName(), result.error().size(), result.error().data());
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
return false; // dont abort the loop
|
return false; // don't abort the loop
|
||||||
});
|
});
|
||||||
|
|
||||||
const auto after = espchrono::millis_clock::now();
|
const auto after = espchrono::millis_clock::now();
|
||||||
@@ -160,7 +170,7 @@ ConfigStatusReturnType ConfigManager<ConfigContainer>::reset()
|
|||||||
{
|
{
|
||||||
if (!message.empty())
|
if (!message.empty())
|
||||||
message.append(", ");
|
message.append(", ");
|
||||||
message.append(fmt::format("reset of {} failed: {}", config.nvsName(), result.error()));
|
message.append(std::format("reset of {} failed: {}", config.nvsName(), result.error()));
|
||||||
}
|
}
|
||||||
return false; // dont abort loop
|
return false; // dont abort loop
|
||||||
});
|
});
|
||||||
|
@@ -39,7 +39,17 @@ public:
|
|||||||
|
|
||||||
virtual ConfigConstraintReturnType checkValue(value_t value) const = 0;
|
virtual ConfigConstraintReturnType checkValue(value_t value) const = 0;
|
||||||
|
|
||||||
const T &value() const { return m_value; }
|
const T &value() const
|
||||||
|
{
|
||||||
|
#if defined(CONFIG_COMPILER_CXX_EXCEPTIONS) && CONFIG_COMPILER_CXX_EXCEPTIONS != 0
|
||||||
|
if (!m_loaded)
|
||||||
|
throw std::runtime_error("ConfigWrapper::value() called without loading first");
|
||||||
|
#else
|
||||||
|
#warning "COMPILER_CXX_EXCEPTIONS disabled, ConfigWrapper::value() called without loading first will assert"
|
||||||
|
assert(m_loaded);
|
||||||
|
#endif
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ConfigStatusReturnType writeToFlash(nvs_handle_t nvsHandle, value_t value);
|
ConfigStatusReturnType writeToFlash(nvs_handle_t nvsHandle, value_t value);
|
||||||
|
@@ -9,13 +9,11 @@
|
|||||||
// system includes
|
// system includes
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <format>
|
||||||
|
|
||||||
// esp-idf includes
|
// esp-idf includes
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
|
|
||||||
// 3rdparty lib includes
|
|
||||||
#include <fmt/core.h>
|
|
||||||
|
|
||||||
// local includes
|
// local includes
|
||||||
#include "configwrapper.h"
|
#include "configwrapper.h"
|
||||||
#include "cpputils.h"
|
#include "cpputils.h"
|
||||||
|
Reference in New Issue
Block a user