Skip to content

Commit

Permalink
Update Code
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Nov 9, 2021
1 parent 6ec827c commit e9035be
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/app/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,19 @@ CommandHandler * CommandHandler::Handle::Get()
return (mMagic == InteractionModelEngine::GetInstance()->GetMagicNumber()) ? mpHandler : nullptr;
}

void CommandHandler::Handle::Release()
{
if (mpHandler != nullptr)
{
if (mMagic == InteractionModelEngine::GetInstance()->GetMagicNumber())
{
mpHandler->DecRef();
}
mpHandler = nullptr;
mMagic = 0;
}
}

CommandHandler::Handle::Handle(CommandHandler * handle)
{
if (handle != nullptr)
Expand Down
10 changes: 1 addition & 9 deletions src/app/CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,7 @@ class CommandHandler : public Command

CommandHandler * Get();

void Release()
{
if (mpHandler != nullptr)
{
mpHandler->DecRef();
mpHandler = nullptr;
mMagic = 0;
}
}
void Release();

private:
CommandHandler * mpHandler = nullptr;
Expand Down
56 changes: 56 additions & 0 deletions src/controller/tests/data_model/TestCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
using TestContext = chip::Test::AppContext;

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;

namespace {
Expand All @@ -54,9 +55,11 @@ enum ResponseDirective
kSendError,
kSendSuccessStatusCodeWithClusterStatus,
kSendErrorWithClusterStatus,
kAsync,
};

ResponseDirective responseDirective;
CommandHandler::Handle asyncHandle;

} // namespace

Expand Down Expand Up @@ -118,6 +121,10 @@ void DispatchSingleClusterCommand(const ConcreteCommandPath & aCommandPath, chip
{
apCommandObj->AddClusterSpecificFailure(aCommandPath, kTestFailureClusterStatus);
}
else if (responseDirective == kAsync)
{
asyncHandle = apCommandObj;
}
}
}

Expand Down Expand Up @@ -149,6 +156,7 @@ class TestCommandInteraction
TestCommandInteraction() {}
static void TestDataResponse(nlTestSuite * apSuite, void * apContext);
static void TestSuccessNoDataResponse(nlTestSuite * apSuite, void * apContext);
static void TestAsyncResponse(nlTestSuite * apSuite, void * apContext);
static void TestFailure(nlTestSuite * apSuite, void * apContext);
static void TestSuccessNoDataResponseWithClusterStatus(nlTestSuite * apSuite, void * apContext);
static void TestFailureWithClusterStatus(nlTestSuite * apSuite, void * apContext);
Expand Down Expand Up @@ -239,6 +247,53 @@ void TestCommandInteraction::TestSuccessNoDataResponse(nlTestSuite * apSuite, vo
NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0);
}

void TestCommandInteraction::TestAsyncResponse(nlTestSuite * apSuite, void * apContext)
{
TestContext & ctx = *static_cast<TestContext *>(apContext);
TestCluster::Commands::TestSimpleArgumentRequest::Type request;
auto sessionHandle = ctx.GetSessionBobToAlice();

bool onSuccessWasCalled = false;
bool onFailureWasCalled = false;
bool statusCheck = false;
request.arg1 = true;

// Passing of stack variables by reference is only safe because of synchronous completion of the interaction. Otherwise, it's
// not safe to do so.
auto onSuccessCb = [&onSuccessWasCalled, &statusCheck](const app::ConcreteCommandPath & commandPath,
const app::StatusIB & aStatus, const auto & dataResponse) {
statusCheck = (aStatus.mStatus == Protocols::InteractionModel::Status::Success);
onSuccessWasCalled = true;
};

// Passing of stack variables by reference is only safe because of synchronous completion of the interaction. Otherwise, it's
// not safe to do so.
auto onFailureCb = [&onFailureWasCalled](const app::StatusIB & aStatus, CHIP_ERROR aError) { onFailureWasCalled = true; };

responseDirective = kAsync;

chip::Controller::InvokeCommandRequest(&ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, request, onSuccessCb,
onFailureCb);

NL_TEST_ASSERT(apSuite, !onSuccessWasCalled && !onFailureWasCalled && !statusCheck);
NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 1);

CommandHandler * commandHandle = asyncHandle.Get();
NL_TEST_ASSERT(apSuite, commandHandle != nullptr);

if (commandHandle == nullptr)
{
return;
}

commandHandle->AddStatus(ConcreteCommandPath(kTestEndpointId, request.GetClusterId(), request.GetCommandId()),
Protocols::InteractionModel::Status::Success);
asyncHandle.Release();

NL_TEST_ASSERT(apSuite, onSuccessWasCalled && !onFailureWasCalled && statusCheck);
NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0);
}

void TestCommandInteraction::TestFailure(nlTestSuite * apSuite, void * apContext)
{
TestContext & ctx = *static_cast<TestContext *>(apContext);
Expand Down Expand Up @@ -348,6 +403,7 @@ const nlTest sTests[] =
{
NL_TEST_DEF("TestDataResponse", TestCommandInteraction::TestDataResponse),
NL_TEST_DEF("TestSuccessNoDataResponse", TestCommandInteraction::TestSuccessNoDataResponse),
NL_TEST_DEF("TestAsyncResponse", TestCommandInteraction::TestAsyncResponse),
NL_TEST_DEF("TestFailure", TestCommandInteraction::TestFailure),
NL_TEST_DEF("TestSuccessNoDataResponseWithClusterStatus", TestCommandInteraction::TestSuccessNoDataResponseWithClusterStatus),
NL_TEST_DEF("TestFailureWithClusterStatus", TestCommandInteraction::TestFailureWithClusterStatus),
Expand Down

0 comments on commit e9035be

Please sign in to comment.