Fixed possible stack overflow and improved config registering

This commit is contained in:
2022-01-26 17:15:02 +01:00
parent ae5ee03396
commit fdb4ef6dbf

View File

@ -111,12 +111,14 @@ esp_err_t ConfigManager<ConfigContainer>::init(const char *ns)
before = espchrono::millis_clock::now(); before = espchrono::millis_clock::now();
bool success = true; bool success = true;
for (ConfigWrapperInterface &config : ConfigContainer::getAllConfigParams()) ConfigContainer::callForEveryConfig([&](ConfigWrapperInterface &config){
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
});
after = espchrono::millis_clock::now(); after = espchrono::millis_clock::now();
@ -146,13 +148,15 @@ ConfigStatusReturnType ConfigManager<ConfigContainer>::reset()
std::string message; std::string message;
for (ConfigWrapperInterface &config : ConfigContainer::getAllConfigParams()) ConfigContainer::callForEveryConfig([&](ConfigWrapperInterface &config){
if (const auto result = reset_config(config); !result) if (const auto result = reset_config(config); !result)
{ {
if (!message.empty()) if (!message.empty())
message.append(", "); message.append(", ");
message.append(fmt::format("reset of {} failed: {}", config.nvsName(), result.error())); message.append(fmt::format("reset of {} failed: {}", config.nvsName(), result.error()));
} }
return false; // dont abort loop
});
if (!message.empty()) if (!message.empty())
return tl::make_unexpected(std::move(message)); return tl::make_unexpected(std::move(message));
@ -163,10 +167,16 @@ ConfigStatusReturnType ConfigManager<ConfigContainer>::reset()
template<typename ConfigContainer> template<typename ConfigContainer>
ConfigWrapperInterface *ConfigManager<ConfigContainer>::findConfigByKey(std::string_view key) ConfigWrapperInterface *ConfigManager<ConfigContainer>::findConfigByKey(std::string_view key)
{ {
const auto configParams = ConfigContainer::getAllConfigParams(); ConfigWrapperInterface *result{};
const auto iter = std::find_if(std::cbegin(configParams), std::cend(configParams), ConfigContainer::callForEveryConfig([&](ConfigWrapperInterface &config){
[&key](const ConfigWrapperInterface &config){ return key == config.nvsName(); }); if (key == config.nvsName())
return iter != std::cend(configParams) ? &iter->get() : nullptr; {
result = &config;
return true; // abort the loop
}
return false; // dont abort loop
});
return result;
} }
} // namespace espconfig } // namespace espconfig