Skip to content

Commit 0003f04

Browse files
committed
[nrf toup] OpenThread: Clear SRP host and services during RevertConfiguration
* Postpone `RevertConfiguration` until OpenThread is initialized. * Clear SRP host and services before reverting dataset. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent 0f5af64 commit 0003f04

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp

+49-9
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ CHIP_ERROR GenericThreadDriver::Init(Internal::BaseDriver::NetworkStatusChangeCa
5151

5252
// If the network configuration backup exists, it means that the device has been rebooted with
5353
// the fail-safe armed. Since OpenThread persists all operational dataset changes, the backup
54-
// must be restored on the boot. If there's no backup, the below function is a no-op.
55-
RevertConfiguration();
54+
// must be restored.
55+
if (BackupExists())
56+
{
57+
// Set flag and postpone revert until OpenThread is initialized,
58+
// as we need to clear SRP host and services before restoring the backup.
59+
mRevertOnServerReady = true;
60+
}
5661

5762
CheckInterfaceEnabled();
5863

@@ -61,11 +66,28 @@ CHIP_ERROR GenericThreadDriver::Init(Internal::BaseDriver::NetworkStatusChangeCa
6166

6267
void GenericThreadDriver::OnThreadStateChangeHandler(const ChipDeviceEvent * event, intptr_t arg)
6368
{
64-
if ((event->Type == DeviceEventType::kThreadStateChange) &&
65-
(event->ThreadStateChange.OpenThread.Flags & OT_CHANGED_THREAD_PANID))
69+
auto & driver = *reinterpret_cast<GenericThreadDriver *>(arg);
70+
71+
switch (event->Type)
6672
{
67-
// Update the mStagingNetwork when thread panid changed
68-
ThreadStackMgrImpl().GetThreadProvision(reinterpret_cast<GenericThreadDriver *>(arg)->mStagingNetwork);
73+
case DeviceEventType::kThreadStateChange:
74+
if (event->ThreadStateChange.OpenThread.Flags & OT_CHANGED_THREAD_PANID)
75+
{
76+
// Update the mStagingNetwork when thread panid changed
77+
ThreadStackMgrImpl().GetThreadProvision(driver.mStagingNetwork);
78+
}
79+
break;
80+
81+
case DeviceEventType::kServerReady:
82+
if (driver.mRevertOnServerReady)
83+
{
84+
driver.mRevertOnServerReady = false;
85+
driver.RevertConfiguration();
86+
}
87+
break;
88+
89+
default:
90+
break;
6991
}
7092
}
7193

@@ -97,6 +119,8 @@ CHIP_ERROR GenericThreadDriver::RevertConfiguration()
97119
// since the fail-safe was armed, so return with no error.
98120
ReturnErrorCodeIf(error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, CHIP_NO_ERROR);
99121

122+
ThreadStackMgrImpl().ClearAllSrpHostAndServices();
123+
100124
if (!GetEnabled())
101125
{
102126
// When reverting configuration, set InterfaceEnabled to default value (true).
@@ -272,19 +296,35 @@ Status GenericThreadDriver::MatchesNetworkId(const Thread::OperationalDataset &
272296

273297
CHIP_ERROR GenericThreadDriver::BackupConfiguration()
274298
{
275-
// If configuration is already backed up, return with no error
276-
CHIP_ERROR err = KeyValueStoreMgr().Get(DefaultStorageKeyAllocator::FailSafeNetworkConfig().KeyName(), nullptr, 0);
277299

278-
if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
300+
// If configuration is already backed up and revert is not pending,
301+
// return with no error.
302+
if (BackupExists() && !mRevertOnServerReady)
279303
{
304+
// Configuration is already backed up, return with no error.
280305
return CHIP_NO_ERROR;
281306
}
282307

308+
// Clear the revert flag and overwrite the backup.
309+
mRevertOnServerReady = false;
310+
283311
ByteSpan dataset = mStagingNetwork.AsByteSpan();
284312

285313
return KeyValueStoreMgr().Put(DefaultStorageKeyAllocator::FailSafeNetworkConfig().KeyName(), dataset.data(), dataset.size());
286314
}
287315

316+
bool GenericThreadDriver::BackupExists()
317+
{
318+
CHIP_ERROR err = KeyValueStoreMgr().Get(DefaultStorageKeyAllocator::FailSafeNetworkConfig().KeyName(), nullptr, 0);
319+
320+
if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
321+
{
322+
return true;
323+
}
324+
325+
return false;
326+
}
327+
288328
void GenericThreadDriver::CheckInterfaceEnabled()
289329
{
290330
#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART

src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.h

+2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ class GenericThreadDriver final : public ThreadDriver
127127
static void OnThreadStateChangeHandler(const ChipDeviceEvent * event, intptr_t arg);
128128
Status MatchesNetworkId(const Thread::OperationalDataset & dataset, const ByteSpan & networkId) const;
129129
CHIP_ERROR BackupConfiguration();
130+
bool BackupExists();
130131
void CheckInterfaceEnabled();
131132

132133
ThreadNetworkIterator mThreadIterator = ThreadNetworkIterator(this);
133134
Thread::OperationalDataset mStagingNetwork = {};
135+
bool mRevertOnServerReady = false;
134136
};
135137

136138
} // namespace NetworkCommissioning

0 commit comments

Comments
 (0)