Skip to content

Commit

Permalink
add logic of no callbacks for an activate-before-revert happening aft…
Browse files Browse the repository at this point in the history
…er the revert
  • Loading branch information
wqx6 committed Jun 27, 2024
1 parent 58cdaeb commit 80eac4f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Delegate
// If the dataset is set successfully, OnActivateDatasetComplete should be called with CHIP_NO_ERROR when the
// Border Router is attached to the Thread network.
// If an error occurs while setting the active dataset, this callback should be called with the error.
virtual void OnActivateDatasetComplete(CHIP_ERROR error) = 0;
virtual void OnActivateDatasetComplete(uint32_t randomNumber, CHIP_ERROR error) = 0;
};

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

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

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

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

virtual CHIP_ERROR SetPendingDataset(const Thread::OperationalDataset & pendingDataset) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "app/clusters/general-commissioning-server/general-commissioning-server.h"
#include "app/data-model/Nullable.h"
#include "app/server/Server.h"
#include "crypto/RandUtils.h"
#include "lib/core/CHIPError.h"
#include "lib/support/CodeUtils.h"
#include "lib/support/Span.h"
Expand Down Expand Up @@ -102,7 +103,12 @@ Status ServerInstance::HandleSetActiveDatasetRequest(bool failSafeArmed,
}

mBreadcrumb = req.breadcrumb;
CHIP_ERROR err = mDelegate->SetActiveDataset(activeDataset, this);
mRandomNumber = Crypto::GetRandU32();
CHIP_ERROR err = mDelegate->SetActiveDataset(activeDataset, mRandomNumber, this);
if (err != CHIP_NO_ERROR)
{
mRandomNumber = 0;
}
return StatusIB(err).mStatus;
}

Expand Down Expand Up @@ -318,14 +324,20 @@ void ServerInstance::CommitSavedBreadcrumb()
mBreadcrumb.ClearValue();
}

void ServerInstance::OnActivateDatasetComplete(CHIP_ERROR error)
void ServerInstance::OnActivateDatasetComplete(uint32_t randomNumber, CHIP_ERROR error)
{
auto commandHandleRef = std::move(mAsyncCommandHandle);
auto commandHandle = commandHandleRef.Get();
if (commandHandle == nullptr)
{
return;
}
if (mRandomNumber != randomNumber)
{
// Previous SetActiveDatasetRequest was handled.
return;
}
mRandomNumber = 0;
if (error == CHIP_NO_ERROR)
{
// The successful completion of the activation process SHALL disarm the fail-safe timer.
Expand All @@ -352,6 +364,8 @@ void ServerInstance::OnFailSafeTimerExpired()
{
return;
}
// Reset the RandomNumeber so that the OnActivateDatasetComplete will not handle the previous SetActiveDatasetRequest command.
mRandomNumber = 0;
commandHandle->AddStatus(mPath, Status::Timeout);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ServerInstance : public CommandHandlerInterface, public AttributeAccessInt
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;

// ActivateDatasetCallbackInterface
void OnActivateDatasetComplete(CHIP_ERROR error) override;
void OnActivateDatasetComplete(uint32_t randomNum, CHIP_ERROR error) override;

private:
friend class TestThreadBorderRouterManagementCluster;
Expand Down Expand Up @@ -86,6 +86,7 @@ class ServerInstance : public CommandHandlerInterface, public AttributeAccessInt
app::CommandHandler::Handle mAsyncCommandHandle;
ConcreteCommandPath mPath = ConcreteCommandPath(0, 0, 0);
Optional<uint64_t> mBreadcrumb;
uint32_t mRandomNumber;
};

} // namespace ThreadBorderRouterManagement
Expand Down
3 changes: 1 addition & 2 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,10 @@ source_set("operational-state-test-srcs") {
source_set("thread-border-router-management-test-srcs") {
sources = [ "${chip_root}/src/app/clusters/thread-border-router-management-server/thread-br-mgmt-server.cpp" ]

deps = [ "${chip_root}/src/app/server" ]

public_deps = [
"${chip_root}/src/app",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/app/server",
"${chip_root}/src/app/util/mock:mock_ember",
"${chip_root}/src/lib/core",
]
Expand Down
3 changes: 2 additions & 1 deletion src/app/tests/TestThreadBorderRouterManagementCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class TestDelegate : public Delegate
return CHIP_IM_GLOBAL_STATUS(NotFound);
}

CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, ActivateDatasetCallback * callback) override
CHIP_ERROR SetActiveDataset(const Thread::OperationalDataset & activeDataset, uint32_t randomNumber,
ActivateDatasetCallback * callback) override
{
memcpy(mActiveDataset, activeDataset.AsByteSpan().data(), activeDataset.AsByteSpan().size());
mActiveDatasetLen = activeDataset.AsByteSpan().size();
Expand Down

0 comments on commit 80eac4f

Please sign in to comment.