@@ -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
+ mRevertOnServerReady = 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.mRevertOnServerReady )
83
+ {
84
+ driver.mRevertOnServerReady = 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).
@@ -272,19 +296,35 @@ Status GenericThreadDriver::MatchesNetworkId(const Thread::OperationalDataset &
272
296
273
297
CHIP_ERROR GenericThreadDriver::BackupConfiguration ()
274
298
{
275
- // If configuration is already backed up, return with no error
276
- CHIP_ERROR err = KeyValueStoreMgr ().Get (DefaultStorageKeyAllocator::FailSafeNetworkConfig ().KeyName (), nullptr , 0 );
277
299
278
- if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
300
+ // If configuration is already backed up and revet is not pending,
301
+ // return with no error.
302
+ if (BackupExists () && !mRevertOnServerReady )
279
303
{
304
+ // Configuration is already backed up, return with no error.
280
305
return CHIP_NO_ERROR;
281
306
}
282
307
308
+ // Clear the revert flag and overwrite the backup.
309
+ mRevertOnServerReady = false ;
310
+
283
311
ByteSpan dataset = mStagingNetwork .AsByteSpan ();
284
312
285
313
return KeyValueStoreMgr ().Put (DefaultStorageKeyAllocator::FailSafeNetworkConfig ().KeyName (), dataset.data (), dataset.size ());
286
314
}
287
315
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
+
288
328
void GenericThreadDriver::CheckInterfaceEnabled ()
289
329
{
290
330
#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
0 commit comments