Skip to content

Commit 884aea2

Browse files
committed
some review changes
1 parent 74824cb commit 884aea2

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

src/app/chip_data_model.gni

+2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ template("chip_data_model") {
393393
} else if (cluster == "thread-border-router-management-server") {
394394
sources += [
395395
"${_app_root}/clusters/${cluster}/${cluster}.cpp",
396+
"${_app_root}/clusters/${cluster}/${cluster}.h",
396397
"${_app_root}/clusters/${cluster}/thread-border-router-management-server-utils.cpp",
398+
"${_app_root}/clusters/${cluster}/thread-br-delegate.h",
397399
]
398400
} else {
399401
sources += [ "${_app_root}/clusters/${cluster}/${cluster}.cpp" ]

src/platform/OpenThread/GenericThreadBorderRouterDelegate.cpp

+13-32
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ CHIP_ERROR GenericOpenThreadBorderRouterDelegate::Init()
5656
{
5757
mCallback = nullptr;
5858
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.
5960
ReturnErrorOnFailure(RevertActiveDataset());
6061
return CHIP_NO_ERROR;
6162
}
@@ -121,7 +122,8 @@ CHIP_ERROR GenericOpenThreadBorderRouterDelegate::GetDataset(Thread::Operational
121122
CHIP_ERROR GenericOpenThreadBorderRouterDelegate::SetActiveDataset(const Thread::OperationalDataset & activeDataset,
122123
uint32_t sequenceNum, ActivateDatasetCallback * callback)
123124
{
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);
125127
if (err == CHIP_NO_ERROR)
126128
{
127129
err = DeviceLayer::ThreadStackMgrImpl().AttachToThreadNetwork(activeDataset, nullptr);
@@ -145,55 +147,34 @@ void GenericOpenThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceL
145147
event->ThreadConnectivityChange.Result == DeviceLayer::kConnectivity_Established)
146148
{
147149
delegate->mCallback->OnActivateDatasetComplete(delegate->mSequenceNum, CHIP_NO_ERROR);
148-
// Delete Failsafe Keys after activating dataset is completed
149-
DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kFailsafeThreadDatasetTlvsKey);
150150
delegate->mCallback = nullptr;
151151
}
152152
}
153153
}
154154

155-
CHIP_ERROR GenericOpenThreadBorderRouterDelegate::BackupActiveDataset()
155+
CHIP_ERROR GenericOpenThreadBorderRouterDelegate::SaveThreadBorderRouterCommissioned(bool commissioned)
156156
{
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);
166158
}
167159

168160
CHIP_ERROR GenericOpenThreadBorderRouterDelegate::CommitActiveDataset()
169161
{
170-
// Delete Failsafe Key when committing.
171-
DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kFailsafeThreadDatasetTlvsKey);
172-
return CHIP_NO_ERROR;
162+
return SaveThreadBorderRouterCommissioned(true);
173163
}
174164

175165
CHIP_ERROR GenericOpenThreadBorderRouterDelegate::RevertActiveDataset()
176166
{
177167
// The FailSafe Timer is triggered and the previous command request should be handled, so reset the callback.
178168
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)
187172
{
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);
189177
}
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);
197178
return CHIP_NO_ERROR;
198179
}
199180

src/platform/OpenThread/GenericThreadBorderRouterDelegate.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ namespace ThreadBorderRouterManagement {
3232
class GenericOpenThreadBorderRouterDelegate : public Delegate
3333
{
3434
public:
35-
static constexpr char kFailsafeThreadDatasetTlvsKey[] = "g/fs/td";
35+
static constexpr char kFailsafeThreadBorderRouterCommissioned[] = "g/fs/tbrc";
3636

37-
GenericOpenThreadBorderRouterDelegate(const CharSpan & name)
37+
GenericOpenThreadBorderRouterDelegate(const char *name)
3838
{
39-
strncpy(mThreadBorderRouterName, name.data(), std::min(name.size(), kBorderRouterNameMaxLength));
39+
strncpy(mThreadBorderRouterName, name, kBorderRouterNameMaxLength);
4040
}
4141
~GenericOpenThreadBorderRouterDelegate() = default;
4242

@@ -79,9 +79,8 @@ class GenericOpenThreadBorderRouterDelegate : public Delegate
7979
static void OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
8080

8181
private:
82-
CHIP_ERROR BackupActiveDataset();
82+
CHIP_ERROR SaveThreadBorderRouterCommissioned(bool commissioned);
8383
ActivateDatasetCallback * mCallback = nullptr;
84-
Thread::OperationalDataset mStagingDataset = {};
8584
uint32_t mSequenceNum;
8685
char mThreadBorderRouterName[kBorderRouterNameMaxLength + 1];
8786
};

0 commit comments

Comments
 (0)