@@ -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
6267void 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,10 +296,11 @@ Status GenericThreadDriver::MatchesNetworkId(const Thread::OperationalDataset &
272296
273297CHIP_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 ) ;
299+ // Abort pending revert
300+ mRevertOnServerReady = false ;
277301
278- if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
302+ // If configuration is already backed up, return with no error
303+ if (BackupExists ())
279304 {
280305 return CHIP_NO_ERROR;
281306 }
@@ -285,6 +310,18 @@ CHIP_ERROR GenericThreadDriver::BackupConfiguration()
285310 return KeyValueStoreMgr ().Put (DefaultStorageKeyAllocator::FailSafeNetworkConfig ().KeyName (), dataset.data (), dataset.size ());
286311}
287312
313+ bool GenericThreadDriver::BackupExists ()
314+ {
315+ CHIP_ERROR err = KeyValueStoreMgr ().Get (DefaultStorageKeyAllocator::FailSafeNetworkConfig ().KeyName (), nullptr , 0 );
316+
317+ if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
318+ {
319+ return true ;
320+ }
321+
322+ return false ;
323+ }
324+
288325void GenericThreadDriver::CheckInterfaceEnabled ()
289326{
290327#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
0 commit comments