From 3d962c88a144bf9008adb8b883818102a91d621a Mon Sep 17 00:00:00 2001 From: chendejin Date: Wed, 5 Jun 2024 15:15:31 +0800 Subject: [PATCH] ESP32: Support Thread and Wi-Fi network revert configuration --- .../ESP32/KeyValueStoreManagerImpl.cpp | 2 - .../ESP32/NetworkCommissioningDriver.cpp | 73 +++++++++++-------- .../ESP32/NetworkCommissioningDriver.h | 2 +- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/platform/ESP32/KeyValueStoreManagerImpl.cpp b/src/platform/ESP32/KeyValueStoreManagerImpl.cpp index 6617bb2cb11bf0..ed80ec80ab6b93 100644 --- a/src/platform/ESP32/KeyValueStoreManagerImpl.cpp +++ b/src/platform/ESP32/KeyValueStoreManagerImpl.cpp @@ -68,8 +68,6 @@ KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance; CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size, size_t offset_bytes) { - VerifyOrReturnError(value, CHIP_ERROR_INVALID_ARGUMENT); - // Offset and partial reads are not supported in nvs, for now just return NOT_IMPLEMENTED. Support can be added in the // future if this is needed. VerifyOrReturnError(offset_bytes == 0, CHIP_ERROR_NOT_IMPLEMENTED); diff --git a/src/platform/ESP32/NetworkCommissioningDriver.cpp b/src/platform/ESP32/NetworkCommissioningDriver.cpp index 2871c4c3f2031d..770f778d4d5779 100644 --- a/src/platform/ESP32/NetworkCommissioningDriver.cpp +++ b/src/platform/ESP32/NetworkCommissioningDriver.cpp @@ -95,39 +95,12 @@ CHIP_ERROR GetConfiguredNetwork(Network & network) CHIP_ERROR ESPWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeCallback) { - CHIP_ERROR err; - size_t ssidLen = 0; - size_t credentialsLen = 0; - - err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, mSavedNetwork.credentials, - sizeof(mSavedNetwork.credentials), &credentialsLen); - if (err == CHIP_ERROR_NOT_FOUND) - { - return CHIP_NO_ERROR; - } - - err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiSSIDKeyName, mSavedNetwork.ssid, sizeof(mSavedNetwork.ssid), &ssidLen); - if (err == CHIP_ERROR_NOT_FOUND) - { - return CHIP_NO_ERROR; - } - if (!CanCastTo(credentialsLen)) - { - return CHIP_ERROR_INCORRECT_STATE; - } - mSavedNetwork.credentialsLen = static_cast(credentialsLen); + LoadFromStorage(); - if (!CanCastTo(ssidLen)) - { - return CHIP_ERROR_INCORRECT_STATE; - } - mSavedNetwork.ssidLen = static_cast(ssidLen); - - mStagingNetwork = mSavedNetwork; mpScanCallback = nullptr; mpConnectCallback = nullptr; mpStatusChangeCallback = networkStatusChangeCallback; - return err; + return CHIP_NO_ERROR; } void ESPWiFiDriver::Shutdown() @@ -140,14 +113,25 @@ CHIP_ERROR ESPWiFiDriver::CommitConfiguration() ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kWiFiSSIDKeyName, mStagingNetwork.ssid, mStagingNetwork.ssidLen)); ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kWiFiCredentialsKeyName, mStagingNetwork.credentials, mStagingNetwork.credentialsLen)); - mSavedNetwork = mStagingNetwork; return CHIP_NO_ERROR; } CHIP_ERROR ESPWiFiDriver::RevertConfiguration() { - mStagingNetwork = mSavedNetwork; - return CHIP_NO_ERROR; + LoadFromStorage(); + + Network configuredNetwork; + if (CHIP_NO_ERROR == GetConfiguredNetwork(configuredNetwork)) + { + if (NetworkMatch(mStagingNetwork, ByteSpan(configuredNetwork.networkID, configuredNetwork.networkIDLen))) + { + return CHIP_NO_ERROR; + } + } + + // ConnectWiFiNetwork can work with empty mStagingNetwork + return ConnectWiFiNetwork(reinterpret_cast(mStagingNetwork.ssid), mStagingNetwork.ssidLen, + reinterpret_cast(mStagingNetwork.credentials), mStagingNetwork.credentialsLen); } bool ESPWiFiDriver::NetworkMatch(const WiFiNetwork & network, ByteSpan networkId) @@ -484,6 +468,31 @@ bool ESPWiFiDriver::WiFiNetworkIterator::Next(Network & item) return true; } +void ESPWiFiDriver::LoadFromStorage() +{ + WiFiNetwork network; + size_t ssidLen = 0; + size_t credentialsLen = 0; + mStagingNetwork = {}; + ReturnOnFailure(PersistedStorage::KeyValueStoreMgr().Get(kWiFiSSIDKeyName, network.ssid, sizeof(network.ssid), &ssidLen)); + + if (!CanCastTo(ssidLen)) + { + return; + } + + ReturnOnFailure(PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, network.credentials, + sizeof(network.credentials), &credentialsLen)); + if (!CanCastTo(credentialsLen)) + { + return; + } + + network.ssidLen = static_cast(ssidLen); + network.credentialsLen = static_cast(credentialsLen); + mStagingNetwork = network; +} + } // namespace NetworkCommissioning } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/ESP32/NetworkCommissioningDriver.h b/src/platform/ESP32/NetworkCommissioningDriver.h index 447d6346dcbd1c..f17937acd09483 100644 --- a/src/platform/ESP32/NetworkCommissioningDriver.h +++ b/src/platform/ESP32/NetworkCommissioningDriver.h @@ -133,8 +133,8 @@ class ESPWiFiDriver final : public WiFiDriver private: bool NetworkMatch(const WiFiNetwork & network, ByteSpan networkId); CHIP_ERROR StartScanWiFiNetworks(ByteSpan ssid); + void LoadFromStorage(); - WiFiNetwork mSavedNetwork; WiFiNetwork mStagingNetwork; ScanCallback * mpScanCallback; ConnectCallback * mpConnectCallback;