@@ -56,6 +56,7 @@ CHIP_ERROR GenericOpenThreadBorderRouterDelegate::Init()
56
56
{
57
57
mCallback = nullptr ;
58
58
ReturnErrorOnFailure (DeviceLayer::PlatformMgrImpl ().AddEventHandler (OnPlatformEventHandler, reinterpret_cast <intptr_t >(this )));
59
+ // When the Thread Border Router is reboot during SetActiveDataset, we need to revert the active dateset.
59
60
ReturnErrorOnFailure (RevertActiveDataset ());
60
61
return CHIP_NO_ERROR;
61
62
}
@@ -121,7 +122,8 @@ CHIP_ERROR GenericOpenThreadBorderRouterDelegate::GetDataset(Thread::Operational
121
122
CHIP_ERROR GenericOpenThreadBorderRouterDelegate::SetActiveDataset (const Thread::OperationalDataset & activeDataset,
122
123
uint32_t sequenceNum, ActivateDatasetCallback * callback)
123
124
{
124
- CHIP_ERROR err = BackupActiveDataset ();
125
+ // This function will never be invoked when there is an Active Dataset already configured.
126
+ CHIP_ERROR err = SaveThreadBorderRouterCommissioned (false );
125
127
if (err == CHIP_NO_ERROR)
126
128
{
127
129
err = DeviceLayer::ThreadStackMgrImpl ().AttachToThreadNetwork (activeDataset, nullptr );
@@ -145,55 +147,34 @@ void GenericOpenThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceL
145
147
event->ThreadConnectivityChange .Result == DeviceLayer::kConnectivity_Established )
146
148
{
147
149
delegate->mCallback ->OnActivateDatasetComplete (delegate->mSequenceNum , CHIP_NO_ERROR);
148
- // Delete Failsafe Keys after activating dataset is completed
149
- DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Delete (kFailsafeThreadDatasetTlvsKey );
150
150
delegate->mCallback = nullptr ;
151
151
}
152
152
}
153
153
}
154
154
155
- CHIP_ERROR GenericOpenThreadBorderRouterDelegate::BackupActiveDataset ( )
155
+ CHIP_ERROR GenericOpenThreadBorderRouterDelegate::SaveThreadBorderRouterCommissioned ( bool commissioned )
156
156
{
157
- // If active dataset is already backed up, return with no error
158
- CHIP_ERROR err = DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Get (kFailsafeThreadDatasetTlvsKey , nullptr , 0 );
159
- if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
160
- {
161
- return CHIP_NO_ERROR;
162
- }
163
- GetDataset (mStagingDataset , DatasetType::kActive );
164
- ByteSpan dataset = mStagingDataset .AsByteSpan ();
165
- return DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Put (kFailsafeThreadDatasetTlvsKey , dataset.data (), dataset.size ());
157
+ return DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Put (kFailsafeThreadBorderRouterCommissioned , commissioned);
166
158
}
167
159
168
160
CHIP_ERROR GenericOpenThreadBorderRouterDelegate::CommitActiveDataset ()
169
161
{
170
- // Delete Failsafe Key when committing.
171
- DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Delete (kFailsafeThreadDatasetTlvsKey );
172
- return CHIP_NO_ERROR;
162
+ return SaveThreadBorderRouterCommissioned (true );
173
163
}
174
164
175
165
CHIP_ERROR GenericOpenThreadBorderRouterDelegate::RevertActiveDataset ()
176
166
{
177
167
// The FailSafe Timer is triggered and the previous command request should be handled, so reset the callback.
178
168
mCallback = nullptr ;
179
- uint8_t datasetBytes[Thread::kSizeOperationalDataset ];
180
- size_t datasetLength;
181
- CHIP_ERROR err = DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Get (kFailsafeThreadDatasetTlvsKey , datasetBytes,
182
- sizeof (datasetBytes), &datasetLength);
183
- // If no backup could be found, it means the active datset has not been modified since the fail-safe was armed,
184
- // so return with no error.
185
- ReturnErrorCodeIf (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, CHIP_NO_ERROR);
186
- if (err == CHIP_NO_ERROR)
169
+ bool threadCommissioned = true ;
170
+ DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Get (kFailsafeThreadBorderRouterCommissioned , &threadCommissioned);
171
+ if (!threadCommissioned)
187
172
{
188
- err = mStagingDataset .Init (ByteSpan (datasetBytes, datasetLength));
173
+ // If Thread is not commissioned, we will try to attach an empty Thread dataset and the Thread Border Router
174
+ // will stay uncommissioned
175
+ Thread::OperationalDataset emptyDataset = {};
176
+ return DeviceLayer::ThreadStackMgrImpl ().AttachToThreadNetwork (emptyDataset, nullptr );
189
177
}
190
- if (err == CHIP_NO_ERROR)
191
- {
192
- err = DeviceLayer::ThreadStackMgrImpl ().AttachToThreadNetwork (mStagingDataset , nullptr );
193
- }
194
-
195
- // Always delete the backup, regardless if it can be successfully restored.
196
- DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Delete (kFailsafeThreadDatasetTlvsKey );
197
178
return CHIP_NO_ERROR;
198
179
}
199
180
0 commit comments