feat: enhance gateway channel management with serial command handling and gateway ID updates

Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
Tony
2026-06-11 21:04:20 +08:00
parent 6ffca719d9
commit dceede8602
8 changed files with 298 additions and 14 deletions
@@ -76,11 +76,14 @@ class GatewaySettingsStore {
std::string getGatewayName(uint8_t gateway_id, std::string_view fallback) const;
bool setGatewayName(uint8_t gateway_id, std::string_view name);
uint8_t getChannelGatewayId(uint8_t channel_index, uint8_t fallback) const;
bool setChannelGatewayId(uint8_t channel_index, uint8_t gateway_id);
private:
std::optional<std::string> readString(std::string_view key) const;
bool writeString(std::string_view key, std::string_view value);
std::string makeGatewayNameKey(uint8_t gateway_id) const;
std::string makeChannelGatewayIdKey(uint8_t channel_index) const;
mutable nvs_handle_t handle_{0};
};
@@ -135,8 +138,11 @@ class GatewayRuntime {
bool setBleEnabled(bool enabled);
bool cacheEnabled() const;
bool setCacheEnabled(bool enabled);
uint8_t gatewayIdForChannel(uint8_t channel_index, uint8_t fallback) const;
bool setGatewayIdForChannel(uint8_t channel_index, uint8_t gateway_id);
std::string gatewayName(uint8_t gateway_id) const;
bool setGatewayName(uint8_t gateway_id, std::string_view name);
std::vector<uint8_t> serialNumberBytes() const;
std::string gatewaySerialHex(uint8_t gateway_id) const;
std::string bleMacHex() const;
std::string bleGatewayName(uint8_t gateway_id, std::string_view gateway_name) const;
@@ -162,6 +168,7 @@ class GatewayRuntime {
std::deque<std::vector<uint8_t>> normal_commands_;
std::deque<std::vector<uint8_t>> maintenance_commands_;
mutable std::map<uint8_t, std::string> gateway_names_;
mutable std::map<uint8_t, uint8_t> channel_gateway_ids_;
size_t gateway_count_{0};
bool ble_enabled_{false};
bool cache_enabled_{true};
@@ -172,4 +179,4 @@ class GatewayRuntime {
SemaphoreHandle_t command_lock_{nullptr};
};
} // namespace gateway
} // namespace gateway
@@ -57,7 +57,8 @@ esp_err_t InitializeRuntimeNvs() {
std::string ReadRuntimeSerialId() {
uint8_t mac[6] = {0};
if (esp_read_mac(mac, ESP_MAC_WIFI_STA) != ESP_OK) {
if (esp_read_mac(mac, ESP_MAC_BASE) != ESP_OK &&
esp_read_mac(mac, ESP_MAC_WIFI_STA) != ESP_OK) {
return "DALIGW";
}
@@ -188,6 +189,31 @@ bool GatewaySettingsStore::setGatewayName(uint8_t gateway_id,
return writeString(makeGatewayNameKey(gateway_id), name);
}
uint8_t GatewaySettingsStore::getChannelGatewayId(uint8_t channel_index,
uint8_t fallback) const {
if (handle_ == 0) {
return fallback;
}
uint8_t gateway_id = fallback;
if (nvs_get_u8(handle_, makeChannelGatewayIdKey(channel_index).c_str(), &gateway_id) !=
ESP_OK) {
return fallback;
}
return gateway_id;
}
bool GatewaySettingsStore::setChannelGatewayId(uint8_t channel_index,
uint8_t gateway_id) {
if (handle_ == 0) {
return false;
}
return nvs_set_u8(handle_, makeChannelGatewayIdKey(channel_index).c_str(), gateway_id) ==
ESP_OK &&
nvs_commit(handle_) == ESP_OK;
}
std::optional<std::string> GatewaySettingsStore::readString(std::string_view key) const {
if (handle_ == 0) {
return std::nullopt;
@@ -222,6 +248,12 @@ std::string GatewaySettingsStore::makeGatewayNameKey(uint8_t gateway_id) const {
return std::string(key);
}
std::string GatewaySettingsStore::makeChannelGatewayIdKey(uint8_t channel_index) const {
char key[24] = {0};
std::snprintf(key, sizeof(key), "dali_ch_id_%u", channel_index);
return std::string(key);
}
GatewayRuntime::GatewayRuntime(BootProfile profile, GatewayRuntimeConfig config,
DaliDomainService* dali_domain)
: profile_(profile),
@@ -323,7 +355,7 @@ GatewayRuntime::CommandPriority GatewayRuntime::classifyCommandPriority(
}
if (opcode == 0x00 || opcode == 0x01 || opcode == 0x03 || opcode == 0x04 || opcode == 0x07 ||
opcode == 0x08 || opcode == 0x10 || opcode == 0x11 || opcode == 0x12 || opcode == 0x13 ||
opcode == 0x17 || opcode == 0x18 || opcode == 0x37 || opcode == 0x38 ||
opcode == 0x0B || opcode == 0x17 || opcode == 0x18 || opcode == 0x37 || opcode == 0x38 ||
opcode == 0x60 || opcode == 0x61 || opcode == 0x62 || (opcode == 0x30 && addr == 0)) {
return CommandPriority::kControl;
}
@@ -531,6 +563,27 @@ bool GatewayRuntime::setCacheEnabled(bool enabled) {
return true;
}
uint8_t GatewayRuntime::gatewayIdForChannel(uint8_t channel_index, uint8_t fallback) const {
LockGuard guard(command_lock_);
const auto cached = channel_gateway_ids_.find(channel_index);
if (cached != channel_gateway_ids_.end()) {
return cached->second;
}
const uint8_t gateway_id = settings_.getChannelGatewayId(channel_index, fallback);
channel_gateway_ids_[channel_index] = gateway_id;
return gateway_id;
}
bool GatewayRuntime::setGatewayIdForChannel(uint8_t channel_index, uint8_t gateway_id) {
if (!settings_.setChannelGatewayId(channel_index, gateway_id)) {
return false;
}
LockGuard guard(command_lock_);
channel_gateway_ids_[channel_index] = gateway_id;
return true;
}
std::string GatewayRuntime::gatewayName(uint8_t gateway_id) const {
LockGuard guard(command_lock_);
const auto cached = gateway_names_.find(gateway_id);
@@ -577,6 +630,11 @@ std::string GatewayRuntime::gatewaySerialHex(uint8_t gateway_id) const {
return toHex(serial);
}
std::vector<uint8_t> GatewayRuntime::serialNumberBytes() const {
const auto bytes = serialBytes();
return {bytes[3], bytes[4], bytes[5]};
}
std::string GatewayRuntime::bleMacHex() const {
return toHex(serialBytes());
}