@@ -51,8 +51,13 @@ CHIP_ERROR GenericThreadDriver::Init(Internal::BaseDriver::NetworkStatusChangeCa
51
51
52
52
// If the network configuration backup exists, it means that the device has been rebooted with
53
53
// 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
+ mRevertOnStartup = true ;
60
+ }
56
61
57
62
CheckInterfaceEnabled ();
58
63
@@ -61,11 +66,28 @@ CHIP_ERROR GenericThreadDriver::Init(Internal::BaseDriver::NetworkStatusChangeCa
61
66
62
67
void GenericThreadDriver::OnThreadStateChangeHandler (const ChipDeviceEvent * event, intptr_t arg)
63
68
{
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 )
66
72
{
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.mRevertOnStartup )
83
+ {
84
+ driver.mRevertOnStartup = false ;
85
+ driver.RevertConfiguration ();
86
+ }
87
+ break ;
88
+
89
+ default :
90
+ break ;
69
91
}
70
92
}
71
93
@@ -97,6 +119,8 @@ CHIP_ERROR GenericThreadDriver::RevertConfiguration()
97
119
// since the fail-safe was armed, so return with no error.
98
120
ReturnErrorCodeIf (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, CHIP_NO_ERROR);
99
121
122
+ ThreadStackMgrImpl ().ClearAllSrpHostAndServices ();
123
+
100
124
if (!GetEnabled ())
101
125
{
102
126
// When reverting configuration, set InterfaceEnabled to default value (true).
@@ -273,9 +297,7 @@ Status GenericThreadDriver::MatchesNetworkId(const Thread::OperationalDataset &
273
297
CHIP_ERROR GenericThreadDriver::BackupConfiguration ()
274
298
{
275
299
// If configuration is already backed up, return with no error
276
- CHIP_ERROR err = KeyValueStoreMgr ().Get (DefaultStorageKeyAllocator::FailSafeNetworkConfig ().KeyName (), nullptr , 0 );
277
-
278
- if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
300
+ if (BackupExists ())
279
301
{
280
302
return CHIP_NO_ERROR;
281
303
}
@@ -285,6 +307,18 @@ CHIP_ERROR GenericThreadDriver::BackupConfiguration()
285
307
return KeyValueStoreMgr ().Put (DefaultStorageKeyAllocator::FailSafeNetworkConfig ().KeyName (), dataset.data (), dataset.size ());
286
308
}
287
309
310
+ bool GenericThreadDriver::BackupExists ()
311
+ {
312
+ CHIP_ERROR err = KeyValueStoreMgr ().Get (DefaultStorageKeyAllocator::FailSafeNetworkConfig ().KeyName (), nullptr , 0 );
313
+
314
+ if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
315
+ {
316
+ return true ;
317
+ }
318
+
319
+ return false ;
320
+ }
321
+
288
322
void GenericThreadDriver::CheckInterfaceEnabled ()
289
323
{
290
324
#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
0 commit comments