Refactor channel binding configuration and validation
- Introduced `NativeChannelConfig` and `SerialChannelConfig` functions to streamline the creation of `ChannelBindingConfig` instances. - Replaced manual channel configuration with macros `GATEWAY_CONFIGURE_NATIVE_CHANNEL` and `GATEWAY_CONFIGURE_SERIAL_CHANNEL` for better maintainability. - Updated `BuildChannelBindings` to utilize the new configuration functions, reducing redundancy. - Enhanced `ValidateChannelBindings` to check for required TX and RX GPIO pins for native channels. - Simplified `BindConfiguredChannels` to iterate over the built channel bindings, improving readability and reducing code duplication. - Adjusted the handling of reserved UART ports in `app_main` to dynamically include all serial channels. - Updated SDK configuration for channel 1 gateway ID and modified KNX-related configurations for enhanced functionality. - Removed obsolete NVS flash initialization code and replaced it with a more streamlined approach for managing runtime data. - Added a new command in `GatewayController` to publish gateway IDs when requested. - Defined a constant for maximum KNX instance count to improve clarity in the codebase. Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "gateway_modbus.hpp"
|
||||
#include "gateway_provisioning.hpp"
|
||||
#include "log.hpp"
|
||||
#include "esp_idf_platform.h"
|
||||
#include "security_storage.h"
|
||||
|
||||
#include "cJSON.h"
|
||||
@@ -25,7 +26,6 @@
|
||||
#endif
|
||||
#include "esp_log.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "lwip/inet.h"
|
||||
@@ -415,32 +415,11 @@ std::optional<uint32_t> QueryKnxInstanceId(std::string_view query) {
|
||||
return ValidKnxInstanceId(QueryInt(query, "instanceId", "instance").value_or(0));
|
||||
}
|
||||
|
||||
bool EnsureBridgeNvsReady() {
|
||||
const esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
if (nvs_flash_erase() != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
return nvs_flash_init() == ESP_OK;
|
||||
}
|
||||
return err == ESP_OK || err == ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
bool EraseOpenKnxEeprom(const std::string& nvs_namespace) {
|
||||
if (nvs_namespace.empty() || !EnsureBridgeNvsReady()) {
|
||||
bool EraseOpenKnxRuntimeData(const std::string& nvs_namespace) {
|
||||
if (nvs_namespace.empty()) {
|
||||
return false;
|
||||
}
|
||||
nvs_handle_t handle = 0;
|
||||
esp_err_t err = nvs_open(nvs_namespace.c_str(), NVS_READWRITE, &handle);
|
||||
if (err != ESP_OK) {
|
||||
return err == ESP_ERR_NVS_NOT_FOUND;
|
||||
}
|
||||
err = nvs_erase_key(handle, "eeprom");
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(handle);
|
||||
}
|
||||
nvs_close(handle);
|
||||
return err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND;
|
||||
return openknx::EraseOpenKnxPropertyStorage(nvs_namespace);
|
||||
}
|
||||
|
||||
std::optional<int> JsonIntAny(const cJSON* parent, const char* primary, const char* fallback) {
|
||||
@@ -4975,7 +4954,7 @@ GatewayBridgeHttpResponse GatewayBridgeService::handlePost(
|
||||
if (instance_id.value() != 0) {
|
||||
nvs_namespace += "_" + std::to_string(instance_id.value());
|
||||
}
|
||||
if (!EraseOpenKnxEeprom(nvs_namespace)) {
|
||||
if (!EraseOpenKnxRuntimeData(nvs_namespace)) {
|
||||
return ErrorResponse(ESP_FAIL, "failed to erase KNX runtime data");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user