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:
Tony
2026-05-27 18:08:53 +08:00
parent 7a820e700c
commit 6d0b36b60a
8 changed files with 1579 additions and 161 deletions
@@ -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");
}
@@ -742,6 +742,14 @@ void GatewayController::dispatchCommand(const std::vector<uint8_t>& command) {
const uint8_t opcode = command[3];
const uint8_t addr = command[4];
const uint8_t data = command[5];
if (opcode == 0x09 && addr == 0x00) {
const auto ids = gatewayIds();
const auto count = std::min<size_t>(ids.size(), 16);
std::vector<uint8_t> payload{0x09, static_cast<uint8_t>(count)};
payload.insert(payload.end(), ids.begin(), ids.begin() + count);
publishPayload(gateway_id, payload);
return;
}
if (!hasGateway(gateway_id)) {
ESP_LOGW(kTag, "command for unknown gateway=%u opcode=0x%02x", gateway_id, opcode);
return;
@@ -72,8 +72,9 @@ inline constexpr uint8_t kReg1DaliProgramVersion[5] = {
static_cast<uint8_t>(kReg1DaliApplicationNumber & 0xff),
kReg1DaliApplicationVersion};
inline constexpr uint32_t kDaliMaxKnxInstanceCount = 16;
inline constexpr uint32_t kReg1DaliSerialMacIncrement = 0;
inline constexpr uint32_t kOamRouterSerialMacIncrement = 1;
inline constexpr uint32_t kOamRouterSerialMacIncrement = kDaliMaxKnxInstanceCount;
inline constexpr uint16_t kOamRouterDeviceDescriptor = 0x091A;
inline constexpr uint16_t kOamRouterManufacturerId =
static_cast<uint16_t>(CONFIG_GATEWAY_KNX_OAM_ROUTER_OEM_MANUFACTURER_ID);