Skip to content

Commit a814f83

Browse files
committed
Update GenericThreadBorderRouterDelegate and add the function description for thread-br-delegate
1 parent dfd8c89 commit a814f83

File tree

3 files changed

+52
-85
lines changed

3 files changed

+52
-85
lines changed

src/app/clusters/thread-border-router-management-server/thread-br-delegate.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ class Delegate
6969

7070
virtual CHIP_ERROR GetDataset(Thread::OperationalDataset & dataset, DatasetType type) = 0;
7171

72+
// The Delegate implement should back up the old active dataset at the begining of function if no backup already exists.
7273
virtual CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, ActivateDatasetCallback * callback) = 0;
7374

74-
// The Thread BR should be able to revert the active dataset set by SetActiveDataset(), and restore
75-
// the previous status of Thread BR.
75+
// The Delegate implement should remove the backup active dataset in this function.
76+
virtual CHIP_ERROR CommitActiveDataset() = 0;
77+
78+
// The Delegate implement should restore the backup active dataset in this function.
79+
// Once RevertActiveDataset is called the ActivateDatasetCallback of previous SetActiveDataset should never be called.
7680
virtual CHIP_ERROR RevertActiveDataset() = 0;
7781

7882
virtual CHIP_ERROR SetPendingDataset(const Thread::OperationalDataset & pendingDataset) = 0;

src/platform/OpenThread/GenericThreadBorderRouterDelegate.cpp

+42-80
Original file line numberDiff line numberDiff line change
@@ -121,35 +121,16 @@ CHIP_ERROR GenericThreadBorderRouterDelegate::GetDataset(Thread::OperationalData
121121
CHIP_ERROR GenericThreadBorderRouterDelegate::SetActiveDataset(const Thread::OperationalDataset & activeDataset,
122122
ActivateDatasetCallback * callback)
123123
{
124-
otInstance * otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
125-
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
126-
127-
VerifyOrReturnError(callback, CHIP_ERROR_INVALID_ARGUMENT);
128-
otOperationalDatasetTlvs datasetTlvs;
129-
memcpy(datasetTlvs.mTlvs, activeDataset.AsByteSpan().data(), activeDataset.AsByteSpan().size());
130-
datasetTlvs.mLength = activeDataset.AsByteSpan().size();
131-
132-
ScopedThreadLock threadLock;
133-
// Save the previous thread state and dataset for reverting
134-
bool threadIsEnabled = GetThreadEnabled();
135-
ReturnErrorOnFailure(DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(kFailsafeThreadEnabledKey, threadIsEnabled));
136-
if (threadIsEnabled)
124+
CHIP_ERROR err = BackupActiveDataset();
125+
if (err == CHIP_NO_ERROR)
137126
{
138-
otOperationalDatasetTlvs stagingDataset;
139-
ReturnErrorCodeIf(otDatasetGetActiveTlvs(otInst, &stagingDataset) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
140-
if (activeDataset.AsByteSpan().data_equal(ByteSpan(stagingDataset.mTlvs, stagingDataset.mLength)))
141-
{
142-
callback->OnActivateDatasetComplete(CHIP_NO_ERROR);
143-
return CHIP_NO_ERROR;
144-
}
145-
ReturnErrorOnFailure(DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(kFailsafeThreadDatasetTlvsKey,
146-
stagingDataset.mTlvs, stagingDataset.mLength));
127+
err = DeviceLayer::ThreadStackMgrImpl().AttachToThreadNetwork(activeDataset, nullptr);
147128
}
148-
SetThreadEnabled(false);
149-
ReturnErrorCodeIf(otDatasetSetActiveTlvs(otInst, &datasetTlvs) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
150-
SetThreadEnabled(true);
151-
mCallback = callback;
152-
return CHIP_NO_ERROR;
129+
if (err == CHIP_NO_ERROR)
130+
{
131+
mCallback = callback;
132+
}
133+
return err;
153134
}
154135

155136
void GenericThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
@@ -163,45 +144,53 @@ void GenericThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceLayer
163144
delegate->mCallback->OnActivateDatasetComplete(CHIP_NO_ERROR);
164145
// Delete Failsafe Keys after activating dataset is completed
165146
DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kFailsafeThreadDatasetTlvsKey);
166-
DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kFailsafeThreadEnabledKey);
167147
delegate->mCallback = nullptr;
168148
}
169149
}
170150
}
171151

172-
CHIP_ERROR GenericThreadBorderRouterDelegate::RevertActiveDataset()
152+
CHIP_ERROR GenericThreadBorderRouterDelegate::BackupActiveDataset()
173153
{
174-
otInstance * otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
175-
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
176-
177-
bool threadIsEnabled = false;
178-
CHIP_ERROR err = DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(kFailsafeThreadEnabledKey, &threadIsEnabled);
179-
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
154+
// If active dataset is already backed up, return with no error
155+
CHIP_ERROR err = DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(kFailsafeThreadDatasetTlvsKey, nullptr, 0);
156+
if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL)
180157
{
181158
return CHIP_NO_ERROR;
182159
}
183-
ReturnErrorOnFailure(err);
160+
GetDataset(mStagingDataset, DatasetType::kActive);
161+
ByteSpan dataset = mStagingDataset.AsByteSpan();
162+
return DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(kFailsafeThreadDatasetTlvsKey, dataset.data(), dataset.size());
163+
}
184164

185-
ScopedThreadLock threadLock;
186-
if (threadIsEnabled)
165+
CHIP_ERROR GenericThreadBorderRouterDelegate::CommitActiveDataset()
166+
{
167+
// Delete Failsafe Key when committing.
168+
DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kFailsafeThreadDatasetTlvsKey);
169+
return CHIP_NO_ERROR;
170+
}
171+
172+
CHIP_ERROR GenericThreadBorderRouterDelegate::RevertActiveDataset()
173+
{
174+
// The FailSafe Timer is triggered and the previous command request should be handled, so reset the callback.
175+
mCallback = nullptr;
176+
uint8_t datasetBytes[Thread::kSizeOperationalDataset];
177+
size_t datasetLength;
178+
CHIP_ERROR err = DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(kFailsafeThreadDatasetTlvsKey, datasetBytes,
179+
sizeof(datasetBytes), &datasetLength);
180+
// If no backup could be found, it means the active datset has not been modified since the fail-safe was armed,
181+
// so return with no error.
182+
ReturnErrorCodeIf(err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, CHIP_NO_ERROR);
183+
if (err == CHIP_NO_ERROR)
187184
{
188-
otOperationalDatasetTlvs stagingDataset;
189-
size_t datasetTlvslen = 0;
190-
err = DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(kFailsafeThreadDatasetTlvsKey, stagingDataset.mTlvs,
191-
sizeof(stagingDataset.mTlvs), &datasetTlvslen);
192-
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
193-
{
194-
return CHIP_NO_ERROR;
195-
}
196-
ReturnErrorOnFailure(err);
197-
stagingDataset.mLength = datasetTlvslen;
198-
ReturnErrorOnFailure(SetThreadEnabled(false));
199-
ReturnErrorCodeIf(otDatasetSetActiveTlvs(otInst, &stagingDataset) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
185+
err = mStagingDataset.Init(ByteSpan(datasetBytes, datasetLength));
186+
}
187+
if (err == CHIP_NO_ERROR)
188+
{
189+
err = DeviceLayer::ThreadStackMgrImpl().AttachToThreadNetwork(mStagingDataset, nullptr);
200190
}
201-
ReturnErrorOnFailure(SetThreadEnabled(threadIsEnabled));
202-
// Delete Failsafe Keys after reverting.
191+
192+
// Always delete the backup, regardless if it can be successfully restored.
203193
DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kFailsafeThreadDatasetTlvsKey);
204-
DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kFailsafeThreadEnabledKey);
205194
return CHIP_NO_ERROR;
206195
}
207196

@@ -218,33 +207,6 @@ CHIP_ERROR GenericThreadBorderRouterDelegate::SetPendingDataset(const Thread::Op
218207
return CHIP_NO_ERROR;
219208
}
220209

221-
CHIP_ERROR GenericThreadBorderRouterDelegate::SetThreadEnabled(bool enabled)
222-
{
223-
otInstance * otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
224-
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
225-
bool isEnabled = (otThreadGetDeviceRole(otInst) != OT_DEVICE_ROLE_DISABLED);
226-
bool isIp6Enabled = otIp6IsEnabled(otInst);
227-
if (enabled && !isIp6Enabled)
228-
{
229-
ReturnErrorCodeIf(otIp6SetEnabled(otInst, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
230-
}
231-
if (enabled != isEnabled)
232-
{
233-
ReturnErrorCodeIf(otThreadSetEnabled(otInst, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
234-
}
235-
if (!enabled && isIp6Enabled)
236-
{
237-
ReturnErrorCodeIf(otIp6SetEnabled(otInst, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
238-
}
239-
return CHIP_NO_ERROR;
240-
}
241-
242-
bool GenericThreadBorderRouterDelegate::GetThreadEnabled()
243-
{
244-
otInstance * otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
245-
return otInst && otIp6IsEnabled(otInst) && (otThreadGetDeviceRole(otInst) != OT_DEVICE_ROLE_DISABLED);
246-
}
247-
248210
} // namespace ThreadBorderRouterManagement
249211
} // namespace Clusters
250212
} // namespace app

src/platform/OpenThread/GenericThreadBorderRouterDelegate.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class GenericThreadBorderRouterDelegate : public Delegate
3434
public:
3535
static constexpr char kThreadBorderRourterName[] = "Espressif-ThreadBR";
3636
static constexpr char kFailsafeThreadDatasetTlvsKey[] = "g/fs/td";
37-
static constexpr char kFailsafeThreadEnabledKey[] = "g/fs/te";
3837

3938
GenericThreadBorderRouterDelegate() = default;
4039
~GenericThreadBorderRouterDelegate() = default;
@@ -70,14 +69,16 @@ class GenericThreadBorderRouterDelegate : public Delegate
7069

7170
CHIP_ERROR RevertActiveDataset() override;
7271

72+
CHIP_ERROR CommitActiveDataset() override;
73+
7374
CHIP_ERROR SetPendingDataset(const Thread::OperationalDataset & pendingDataset) override;
7475

7576
static void OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
7677

7778
private:
78-
CHIP_ERROR SetThreadEnabled(bool enabled);
79-
bool GetThreadEnabled();
79+
CHIP_ERROR BackupActiveDataset();
8080
ActivateDatasetCallback * mCallback = nullptr;
81+
Thread::OperationalDataset mStagingDataset = {};
8182
};
8283
} // namespace ThreadBorderRouterManagement
8384
} // namespace Clusters

0 commit comments

Comments
 (0)