@@ -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
VerifyOrReturnError (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,10 +296,11 @@ 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 ) ;
299
+ // Abort pending revert
300
+ mRevertOnServerReady = false ;
277
301
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 ())
279
304
{
280
305
return CHIP_NO_ERROR;
281
306
}
@@ -285,6 +310,18 @@ CHIP_ERROR GenericThreadDriver::BackupConfiguration()
285
310
return KeyValueStoreMgr ().Put (DefaultStorageKeyAllocator::FailSafeNetworkConfig ().KeyName (), dataset.data (), dataset.size ());
286
311
}
287
312
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
+
288
325
void GenericThreadDriver::CheckInterfaceEnabled ()
289
326
{
290
327
#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
0 commit comments