Skip to content

Commit 6f81382

Browse files
committed
add logic of no callbacks for an activate-before-revert happening after the revert
1 parent 58cdaeb commit 6f81382

7 files changed

+33
-16
lines changed

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Delegate
4848
// If the dataset is set successfully, OnActivateDatasetComplete should be called with CHIP_NO_ERROR when the
4949
// Border Router is attached to the Thread network.
5050
// If an error occurs while setting the active dataset, this callback should be called with the error.
51-
virtual void OnActivateDatasetComplete(CHIP_ERROR error) = 0;
51+
virtual void OnActivateDatasetComplete(uint32_t randomNumber, CHIP_ERROR error) = 0;
5252
};
5353

5454
enum class DatasetType : uint8_t
@@ -72,15 +72,14 @@ class Delegate
7272
virtual CHIP_ERROR GetDataset(Thread::OperationalDataset & dataset, DatasetType type) = 0;
7373

7474
// The Delegate implementation should back up the old active dataset at the beginning of function if no backup already exists.
75-
virtual CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, ActivateDatasetCallback * callback) = 0;
75+
// The Delegate implementation should store the random number and pass it to OnActivateDatasetComplete.
76+
virtual CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, uint32_t randomNum,
77+
ActivateDatasetCallback * callback) = 0;
7678

7779
// The Delegate implementation should remove the backup active dataset in this function.
7880
virtual CHIP_ERROR CommitActiveDataset() = 0;
7981

8082
// The Delegate implementation should restore the backup active dataset in this function.
81-
// If RevertActiveDataset is called before the ActivateDatasetCallback that would result from a
82-
// previous SetActiveDataset, the delegate must ensure that the ActivateDatasetCallback
83-
// for that previous SetActiveDataset call will not happen.
8483
virtual CHIP_ERROR RevertActiveDataset() = 0;
8584

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

src/app/clusters/thread-border-router-management-server/thread-br-mgmt-server.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "app/clusters/general-commissioning-server/general-commissioning-server.h"
3131
#include "app/data-model/Nullable.h"
3232
#include "app/server/Server.h"
33+
#include "crypto/RandUtils.h"
3334
#include "lib/core/CHIPError.h"
3435
#include "lib/support/CodeUtils.h"
3536
#include "lib/support/Span.h"
@@ -102,7 +103,12 @@ Status ServerInstance::HandleSetActiveDatasetRequest(bool failSafeArmed,
102103
}
103104

104105
mBreadcrumb = req.breadcrumb;
105-
CHIP_ERROR err = mDelegate->SetActiveDataset(activeDataset, this);
106+
mRandomNumber = Crypto::GetRandU32();
107+
CHIP_ERROR err = mDelegate->SetActiveDataset(activeDataset, mRandomNumber, this);
108+
if (err != CHIP_NO_ERROR)
109+
{
110+
mRandomNumber = 0;
111+
}
106112
return StatusIB(err).mStatus;
107113
}
108114

@@ -162,7 +168,7 @@ void ServerInstance::InvokeCommand(HandlerContext & ctxt)
162168
Status status = HandleSetActiveDatasetRequest(IsFailSafeArmed(ctx), req);
163169
if (status != Status::Success)
164170
{
165-
OnActivateDatasetComplete(ChipError(ChipError::SdkPart::kIMGlobalStatus, to_underlying(status)));
171+
OnActivateDatasetComplete(mRandomNumber, ChipError(ChipError::SdkPart::kIMGlobalStatus, to_underlying(status)));
166172
}
167173
}
168174
else
@@ -318,14 +324,20 @@ void ServerInstance::CommitSavedBreadcrumb()
318324
mBreadcrumb.ClearValue();
319325
}
320326

321-
void ServerInstance::OnActivateDatasetComplete(CHIP_ERROR error)
327+
void ServerInstance::OnActivateDatasetComplete(uint32_t randomNumber, CHIP_ERROR error)
322328
{
323329
auto commandHandleRef = std::move(mAsyncCommandHandle);
324330
auto commandHandle = commandHandleRef.Get();
325331
if (commandHandle == nullptr)
326332
{
327333
return;
328334
}
335+
if (mRandomNumber != randomNumber)
336+
{
337+
// Previous SetActiveDatasetRequest was handled.
338+
return;
339+
}
340+
mRandomNumber = 0;
329341
if (error == CHIP_NO_ERROR)
330342
{
331343
// The successful completion of the activation process SHALL disarm the fail-safe timer.
@@ -352,6 +364,8 @@ void ServerInstance::OnFailSafeTimerExpired()
352364
{
353365
return;
354366
}
367+
// Reset the RandomNumeber so that the OnActivateDatasetComplete will not handle the previous SetActiveDatasetRequest command.
368+
mRandomNumber = 0;
355369
commandHandle->AddStatus(mPath, Status::Timeout);
356370
}
357371

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ServerInstance : public CommandHandlerInterface, public AttributeAccessInt
5353
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
5454

5555
// ActivateDatasetCallbackInterface
56-
void OnActivateDatasetComplete(CHIP_ERROR error) override;
56+
void OnActivateDatasetComplete(uint32_t randomNum, CHIP_ERROR error) override;
5757

5858
private:
5959
friend class TestThreadBorderRouterManagementCluster;
@@ -86,6 +86,7 @@ class ServerInstance : public CommandHandlerInterface, public AttributeAccessInt
8686
app::CommandHandler::Handle mAsyncCommandHandle;
8787
ConcreteCommandPath mPath = ConcreteCommandPath(0, 0, 0);
8888
Optional<uint64_t> mBreadcrumb;
89+
uint32_t mRandomNumber;
8990
};
9091

9192
} // namespace ThreadBorderRouterManagement

src/app/tests/BUILD.gn

+1-2
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,10 @@ source_set("operational-state-test-srcs") {
123123
source_set("thread-border-router-management-test-srcs") {
124124
sources = [ "${chip_root}/src/app/clusters/thread-border-router-management-server/thread-br-mgmt-server.cpp" ]
125125

126-
deps = [ "${chip_root}/src/app/server" ]
127-
128126
public_deps = [
129127
"${chip_root}/src/app",
130128
"${chip_root}/src/app/common:cluster-objects",
129+
"${chip_root}/src/app/server",
131130
"${chip_root}/src/app/util/mock:mock_ember",
132131
"${chip_root}/src/lib/core",
133132
]

src/app/tests/TestThreadBorderRouterManagementCluster.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ class TestDelegate : public Delegate
103103
return CHIP_IM_GLOBAL_STATUS(NotFound);
104104
}
105105

106-
CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, ActivateDatasetCallback * callback) override
106+
CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, uint32_t randomNumber,
107+
ActivateDatasetCallback * callback) override
107108
{
108109
memcpy(mActiveDataset, activeDataset.AsByteSpan().data(), activeDataset.AsByteSpan().size());
109110
mActiveDatasetLen = activeDataset.AsByteSpan().size();

src/platform/OpenThread/GenericThreadBorderRouterDelegate.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ CHIP_ERROR GenericThreadBorderRouterDelegate::GetDataset(Thread::OperationalData
119119
}
120120

121121
CHIP_ERROR GenericThreadBorderRouterDelegate::SetActiveDataset(const Thread::OperationalDataset & activeDataset,
122-
ActivateDatasetCallback * callback)
122+
uint32_t randomNumber, ActivateDatasetCallback * callback)
123123
{
124124
CHIP_ERROR err = BackupActiveDataset();
125125
if (err == CHIP_NO_ERROR)
@@ -128,7 +128,8 @@ CHIP_ERROR GenericThreadBorderRouterDelegate::SetActiveDataset(const Thread::Ope
128128
}
129129
if (err == CHIP_NO_ERROR)
130130
{
131-
mCallback = callback;
131+
mRandomNumber = randomNumber;
132+
mCallback = callback;
132133
}
133134
return err;
134135
}
@@ -141,7 +142,7 @@ void GenericThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceLayer
141142
if (event->Type == DeviceLayer::DeviceEventType::kThreadConnectivityChange &&
142143
event->ThreadConnectivityChange.Result == DeviceLayer::kConnectivity_Established)
143144
{
144-
delegate->mCallback->OnActivateDatasetComplete(CHIP_NO_ERROR);
145+
delegate->mCallback->OnActivateDatasetComplete(delegate->mRandomNumber, CHIP_NO_ERROR);
145146
// Delete Failsafe Keys after activating dataset is completed
146147
DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kFailsafeThreadDatasetTlvsKey);
147148
delegate->mCallback = nullptr;

src/platform/OpenThread/GenericThreadBorderRouterDelegate.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class GenericThreadBorderRouterDelegate : public Delegate
6565

6666
CHIP_ERROR GetDataset(Thread::OperationalDataset & dataset, DatasetType type) override;
6767

68-
CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, ActivateDatasetCallback * callback) override;
68+
CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, uint32_t randomNumber,
69+
ActivateDatasetCallback * callback) override;
6970

7071
CHIP_ERROR RevertActiveDataset() override;
7172

@@ -79,6 +80,7 @@ class GenericThreadBorderRouterDelegate : public Delegate
7980
CHIP_ERROR BackupActiveDataset();
8081
ActivateDatasetCallback * mCallback = nullptr;
8182
Thread::OperationalDataset mStagingDataset = {};
83+
uint32_t mRandomNumber;
8284
};
8385
} // namespace ThreadBorderRouterManagement
8486
} // namespace Clusters

0 commit comments

Comments
 (0)