feat(gateway): add ESP-Touch smartconfig provisioning support and enhance network management

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Tony
2026-04-30 18:49:38 +08:00
parent 4ce3513dd2
commit ae4669e1b3
10 changed files with 338 additions and 43 deletions
@@ -4,6 +4,7 @@
#include <cstdint>
#include <deque>
#include <functional>
#include <map>
#include <optional>
#include <string>
#include <string_view>
@@ -127,6 +128,7 @@ class GatewayRuntime {
GatewaySettingsStore settings_;
std::optional<std::vector<uint8_t>> current_command_;
std::deque<std::vector<uint8_t>> pending_commands_;
mutable std::map<uint8_t, std::string> gateway_names_;
size_t gateway_count_{0};
bool ble_enabled_{false};
CommandDropReason last_enqueue_drop_reason_{CommandDropReason::kNone};
@@ -119,7 +119,14 @@ std::optional<std::string> GatewaySettingsStore::getWifiPassword() const {
bool GatewaySettingsStore::setWifiCredentials(std::string_view ssid,
std::string_view password) {
return writeString(kWifiSsidKey, ssid) && writeString(kWifiPasswordKey, password);
if (handle_ == 0) {
return false;
}
const esp_err_t ssid_err = nvs_set_str(handle_, kWifiSsidKey, std::string(ssid).c_str());
const esp_err_t password_err =
nvs_set_str(handle_, kWifiPasswordKey, std::string(password).c_str());
return ssid_err == ESP_OK && password_err == ESP_OK && nvs_commit(handle_) == ESP_OK;
}
bool GatewaySettingsStore::clearWifiCredentials() {
@@ -129,12 +136,17 @@ bool GatewaySettingsStore::clearWifiCredentials() {
esp_err_t ssid_err = nvs_erase_key(handle_, kWifiSsidKey);
esp_err_t password_err = nvs_erase_key(handle_, kWifiPasswordKey);
const bool ssid_missing = ssid_err == ESP_ERR_NVS_NOT_FOUND;
const bool password_missing = password_err == ESP_ERR_NVS_NOT_FOUND;
if (ssid_err == ESP_ERR_NVS_NOT_FOUND) {
ssid_err = ESP_OK;
}
if (password_err == ESP_ERR_NVS_NOT_FOUND) {
password_err = ESP_OK;
}
if (ssid_missing && password_missing) {
return true;
}
return ssid_err == ESP_OK && password_err == ESP_OK && nvs_commit(handle_) == ESP_OK;
}
@@ -348,10 +360,16 @@ void GatewayRuntime::setWirelessInfo(WirelessInfo info) {
}
bool GatewayRuntime::clearWirelessInfo() {
bool had_credentials = false;
{
LockGuard guard(command_lock_);
had_credentials = wireless_info_.has_value() &&
(!wireless_info_->ssid.empty() || !wireless_info_->password.empty());
wireless_info_.reset();
}
if (!had_credentials) {
return true;
}
return settings_.clearWifiCredentials();
}
@@ -379,19 +397,37 @@ GatewayDeviceInfo GatewayRuntime::deviceInfo() const {
}
bool GatewayRuntime::bleEnabled() const {
LockGuard guard(command_lock_);
return ble_enabled_;
}
bool GatewayRuntime::setBleEnabled(bool enabled) {
{
LockGuard guard(command_lock_);
if (ble_enabled_ == enabled) {
return true;
}
}
if (!settings_.setBleEnabled(enabled)) {
return false;
}
LockGuard guard(command_lock_);
ble_enabled_ = enabled;
return true;
}
std::string GatewayRuntime::gatewayName(uint8_t gateway_id) const {
return settings_.getGatewayName(gateway_id, defaultGatewayName(gateway_id));
LockGuard guard(command_lock_);
const auto cached = gateway_names_.find(gateway_id);
if (cached != gateway_names_.end()) {
return cached->second;
}
auto name = settings_.getGatewayName(gateway_id, defaultGatewayName(gateway_id));
if (name.size() > kMaxGatewayNameBytes) {
name.resize(kMaxGatewayNameBytes);
}
gateway_names_[gateway_id] = name;
return name;
}
bool GatewayRuntime::setGatewayName(uint8_t gateway_id, std::string_view name) {
@@ -403,7 +439,17 @@ bool GatewayRuntime::setGatewayName(uint8_t gateway_id, std::string_view name) {
normalized = defaultGatewayName(gateway_id);
}
return settings_.setGatewayName(gateway_id, normalized);
if (gatewayName(gateway_id) == normalized) {
return true;
}
if (!settings_.setGatewayName(gateway_id, normalized)) {
return false;
}
LockGuard guard(command_lock_);
gateway_names_[gateway_id] = normalized;
return true;
}
std::string GatewayRuntime::gatewaySerialHex(uint8_t gateway_id) const {