Implemented safer config system

This commit is contained in:
CommanderRedYT
2024-01-15 20:32:30 +01:00
parent 379533571f
commit 2571e9ef48
2 changed files with 21 additions and 4 deletions

View File

@ -45,7 +45,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 +87,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 +119,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();

View File

@ -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);