Skip to content

Commit

Permalink
ESP32: Support Thread and Wi-Fi network revert configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
DejinChen committed Jun 5, 2024
1 parent 923854b commit 3d962c8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/platform/ESP32/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
73 changes: 41 additions & 32 deletions src/platform/ESP32/NetworkCommissioningDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(credentialsLen))
{
return CHIP_ERROR_INCORRECT_STATE;
}
mSavedNetwork.credentialsLen = static_cast<uint8_t>(credentialsLen);
LoadFromStorage();

if (!CanCastTo<uint8_t>(ssidLen))
{
return CHIP_ERROR_INCORRECT_STATE;
}
mSavedNetwork.ssidLen = static_cast<uint8_t>(ssidLen);

mStagingNetwork = mSavedNetwork;
mpScanCallback = nullptr;
mpConnectCallback = nullptr;
mpStatusChangeCallback = networkStatusChangeCallback;
return err;
return CHIP_NO_ERROR;
}

void ESPWiFiDriver::Shutdown()
Expand All @@ -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<const char *>(mStagingNetwork.ssid), mStagingNetwork.ssidLen,
reinterpret_cast<const char *>(mStagingNetwork.credentials), mStagingNetwork.credentialsLen);
}

bool ESPWiFiDriver::NetworkMatch(const WiFiNetwork & network, ByteSpan networkId)
Expand Down Expand Up @@ -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<uint8_t>(ssidLen))
{
return;
}

ReturnOnFailure(PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, network.credentials,
sizeof(network.credentials), &credentialsLen));
if (!CanCastTo<uint8_t>(credentialsLen))
{
return;
}

network.ssidLen = static_cast<uint8_t>(ssidLen);
network.credentialsLen = static_cast<uint8_t>(credentialsLen);
mStagingNetwork = network;
}

} // namespace NetworkCommissioning
} // namespace DeviceLayer
} // namespace chip
2 changes: 1 addition & 1 deletion src/platform/ESP32/NetworkCommissioningDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3d962c8

Please sign in to comment.