Skip to content

Commit 80eac4f

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

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
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

+16-2
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

@@ -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();

0 commit comments

Comments
 (0)