Skip to content

Commit

Permalink
Remove default constructor of SessionHandle, reduce dangling session …
Browse files Browse the repository at this point in the history
…handle
  • Loading branch information
kghost committed Aug 25, 2021
1 parent dacc98b commit e9f4872
Show file tree
Hide file tree
Showing 25 changed files with 115 additions and 128 deletions.
11 changes: 2 additions & 9 deletions src/app/CommandSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using GeneralStatusCode = chip::Protocols::SecureChannel::GeneralStatusCode;
namespace chip {
namespace app {

CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * secureSession,
CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> secureSession,
uint32_t timeout)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -50,14 +50,7 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, FabricIndex aFabric
AbortExistingExchangeContext();

// Create a new exchange context.
if (secureSession == nullptr)
{
mpExchangeCtx = mpExchangeMgr->NewContext(SessionHandle(aNodeId, 0, 0, aFabricIndex), this);
}
else
{
mpExchangeCtx = mpExchangeMgr->NewContext(*secureSession, this);
}
mpExchangeCtx = mpExchangeMgr->NewContext(secureSession.ValueOr(SessionHandle(aNodeId, 0, 0, aFabricIndex)), this);
VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
mpExchangeCtx->SetResponseTimeout(timeout);

Expand Down
2 changes: 1 addition & 1 deletion src/app/CommandSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CommandSender : public Command, public Messaging::ExchangeDelegate
//
// If SendCommandRequest is never called, or the call fails, the API
// consumer is responsible for calling Shutdown on the CommandSender.
CHIP_ERROR SendCommandRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * secureSession,
CHIP_ERROR SendCommandRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> secureSession,
uint32_t timeout = kImMessageTimeoutMsec);

private:
Expand Down
6 changes: 3 additions & 3 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ void InteractionModelEngine::OnResponseTimeout(Messaging::ExchangeContext * ec)
ChipLogProgress(InteractionModel, "Time out! failed to receive echo response from Exchange: %d", ec->GetExchangeId());
}

CHIP_ERROR InteractionModelEngine::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
EventPathParams * apEventPathParamsList, size_t aEventPathParamsListSize,
AttributePathParams * apAttributePathParamsList,
CHIP_ERROR InteractionModelEngine::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex,
Optional<SessionHandle> apSecureSession, EventPathParams * apEventPathParamsList,
size_t aEventPathParamsListSize, AttributePathParams * apAttributePathParamsList,
size_t aAttributePathParamsListSize, EventNumber aEventNumber,
uint64_t aAppIdentifier)
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate
* @retval #CHIP_ERROR_NO_MEMORY If there is no ReadClient available
* @retval #CHIP_NO_ERROR On success.
*/
CHIP_ERROR SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
CHIP_ERROR SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> apSecureSession,
EventPathParams * apEventPathParamsList, size_t aEventPathParamsListSize,
AttributePathParams * apAttributePathParamsList, size_t aAttributePathParamsListSize,
EventNumber aEventNumber, uint64_t aAppIdentifier = 0);
Expand Down
11 changes: 2 additions & 9 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void ReadClient::MoveToState(const ClientState aTargetState)
GetStateStr());
}

CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> apSecureSession,
EventPathParams * apEventPathParamsList, size_t aEventPathParamsListSize,
AttributePathParams * apAttributePathParamsList, size_t aAttributePathParamsListSize,
EventNumber aEventNumber, uint32_t timeout)
Expand Down Expand Up @@ -134,14 +134,7 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex,
SuccessOrExit(err);
}

if (apSecureSession != nullptr)
{
mpExchangeCtx = mpExchangeMgr->NewContext(*apSecureSession, this);
}
else
{
mpExchangeCtx = mpExchangeMgr->NewContext(SessionHandle(aNodeId, 0, 0, aFabricIndex), this);
}
mpExchangeCtx = mpExchangeMgr->NewContext(apSecureSession.ValueOr(SessionHandle(aNodeId, 0, 0, aFabricIndex)), this);
VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
mpExchangeCtx->SetResponseTimeout(timeout);

Expand Down
2 changes: 1 addition & 1 deletion src/app/ReadClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ReadClient : public Messaging::ExchangeDelegate
* @retval #others fail to send read request
* @retval #CHIP_NO_ERROR On success.
*/
CHIP_ERROR SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * aSecureSession,
CHIP_ERROR SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> aSecureSession,
EventPathParams * apEventPathParamsList, size_t aEventPathParamsListSize,
AttributePathParams * apAttributePathParamsList, size_t aAttributePathParamsListSize,
EventNumber aEventNumber, uint32_t timeout = kImMessageTimeoutMsec);
Expand Down
13 changes: 3 additions & 10 deletions src/app/WriteClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void WriteClient::ClearState()
MoveToState(State::Uninitialized);
}

CHIP_ERROR WriteClient::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
CHIP_ERROR WriteClient::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> apSecureSession,
uint32_t timeout)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -263,14 +263,7 @@ CHIP_ERROR WriteClient::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricInde
ClearExistingExchangeContext();

// Create a new exchange context.
if (apSecureSession == nullptr)
{
mpExchangeCtx = mpExchangeMgr->NewContext(SessionHandle(aNodeId, 0, 0, aFabricIndex), this);
}
else
{
mpExchangeCtx = mpExchangeMgr->NewContext(*apSecureSession, this);
}
mpExchangeCtx = mpExchangeMgr->NewContext(apSecureSession.ValueOr(SessionHandle(aNodeId, 0, 0, aFabricIndex)), this);
VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
mpExchangeCtx->SetResponseTimeout(timeout);

Expand Down Expand Up @@ -397,7 +390,7 @@ CHIP_ERROR WriteClient::ProcessAttributeStatusElement(AttributeStatusElement::Pa
return err;
}

CHIP_ERROR WriteClientHandle::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
CHIP_ERROR WriteClientHandle::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> apSecureSession,
uint32_t timeout)
{
CHIP_ERROR err = mpWriteClient->SendWriteRequest(aNodeId, aFabricIndex, apSecureSession, timeout);
Expand Down
5 changes: 3 additions & 2 deletions src/app/WriteClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class WriteClient : public Messaging::ExchangeDelegate
* If SendWriteRequest is never called, or the call fails, the API
* consumer is responsible for calling Shutdown on the WriteClient.
*/
CHIP_ERROR SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession, uint32_t timeout);
CHIP_ERROR SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> apSecureSession,
uint32_t timeout);

/**
* Initialize the client object. Within the lifetime
Expand Down Expand Up @@ -175,7 +176,7 @@ class WriteClientHandle
* Finalize the message and send it to the desired node. The underlying write object will always be released, and the user
* should not use this object after calling this function.
*/
CHIP_ERROR SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
CHIP_ERROR SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, Optional<SessionHandle> apSecureSession,
uint32_t timeout = kImMessageTimeoutMsec);

/**
Expand Down
4 changes: 2 additions & 2 deletions src/app/tests/TestCommandInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void TestCommandInteraction::TestCommandSenderWithWrongState(nlTestSuite * apSui
err = commandSender.Init(&gExchangeManager, nullptr);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

err = commandSender.SendCommandRequest(kTestDeviceNodeId, gFabricIndex, nullptr);
err = commandSender.SendCommandRequest(kTestDeviceNodeId, gFabricIndex, Optional<SessionHandle>::Missing());
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_INCORRECT_STATE);
}

Expand Down Expand Up @@ -278,7 +278,7 @@ void TestCommandInteraction::TestCommandSenderWithSendCommand(nlTestSuite * apSu
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

AddCommandDataElement(apSuite, apContext, &commandSender, false);
err = commandSender.SendCommandRequest(kTestDeviceNodeId, gFabricIndex, nullptr);
err = commandSender.SendCommandRequest(kTestDeviceNodeId, gFabricIndex, Optional<SessionHandle>::Missing());
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_NOT_CONNECTED);

GenerateReceivedCommand(apSuite, apContext, buf, true /*aNeedCommandData*/);
Expand Down
17 changes: 10 additions & 7 deletions src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ void TestReadInteraction::TestReadClient(nlTestSuite * apSuite, void * apContext
err = readClient.Init(&ctx.GetExchangeManager(), &delegate, 0 /* application identifier */);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
SessionHandle session = ctx.GetSessionLocalToPeer();
err = readClient.SendReadRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &session, nullptr /*apEventPathParamsList*/,
0 /*aEventPathParamsListSize*/, nullptr /*apAttributePathParamsList*/,
0 /*aAttributePathParamsListSize*/, eventNumber /*aEventNumber*/);
err = readClient.SendReadRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), Optional<SessionHandle>::Value(session),
nullptr /*apEventPathParamsList*/, 0 /*aEventPathParamsListSize*/,
nullptr /*apAttributePathParamsList*/, 0 /*aAttributePathParamsListSize*/,
eventNumber /*aEventNumber*/);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

GenerateReportData(apSuite, apContext, buf);
Expand Down Expand Up @@ -389,9 +390,10 @@ void TestReadInteraction::TestReadClientInvalidReport(nlTestSuite * apSuite, voi
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

SessionHandle session = ctx.GetSessionLocalToPeer();
err = readClient.SendReadRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &session, nullptr /*apEventPathParamsList*/,
0 /*aEventPathParamsListSize*/, nullptr /*apAttributePathParamsList*/,
0 /*aAttributePathParamsListSize*/, eventNumber /*aEventNumber*/);
err = readClient.SendReadRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), Optional<SessionHandle>::Value(session),
nullptr /*apEventPathParamsList*/, 0 /*aEventPathParamsListSize*/,
nullptr /*apAttributePathParamsList*/, 0 /*aAttributePathParamsListSize*/,
eventNumber /*aEventNumber*/);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

GenerateReportData(apSuite, apContext, buf, true /*aNeedInvalidReport*/);
Expand Down Expand Up @@ -579,7 +581,8 @@ void TestReadInteraction::TestReadEventRoundtrip(nlTestSuite * apSuite, void * a

SessionHandle session = ctx.GetSessionLocalToPeer();
err = chip::app::InteractionModelEngine::GetInstance()->SendReadRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(),
&session, eventPathParams, 2, nullptr, 1, 0);
Optional<SessionHandle>::Value(session),
eventPathParams, 2, nullptr, 1, 0);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

InteractionModelEngine::GetInstance()->GetReportingEngine().Run();
Expand Down
5 changes: 3 additions & 2 deletions src/app/tests/TestWriteInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ void TestWriteInteraction::TestWriteClient(nlTestSuite * apSuite, void * apConte
AddAttributeDataElement(apSuite, apContext, writeClientHandle);

SessionHandle session = ctx.GetSessionLocalToPeer();
err = writeClientHandle.SendWriteRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &session);
err = writeClientHandle.SendWriteRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(),
Optional<SessionHandle>::Value(session));
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
// The internal WriteClient should be nullptr once we SendWriteRequest.
NL_TEST_ASSERT(apSuite, nullptr == writeClientHandle.mpWriteClient);
Expand Down Expand Up @@ -306,7 +307,7 @@ void TestWriteInteraction::TestWriteRoundtrip(nlTestSuite * apSuite, void * apCo

SessionHandle session = ctx.GetSessionLocalToPeer();

err = writeClient.SendWriteRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &session);
err = writeClient.SendWriteRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), Optional<SessionHandle>::Value(session));
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

NL_TEST_ASSERT(apSuite, delegate.mGotResponse);
Expand Down
12 changes: 8 additions & 4 deletions src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ CHIP_ERROR SendCommandRequest(chip::app::CommandSender * commandSender)
err = commandSender->FinishCommand();
SuccessOrExit(err);

err = commandSender->SendCommandRequest(chip::kTestDeviceNodeId, gFabricIndex, nullptr, gMessageTimeoutMsec);
err = commandSender->SendCommandRequest(chip::kTestDeviceNodeId, gFabricIndex, chip::Optional<chip::SessionHandle>::Missing(),
gMessageTimeoutMsec);
SuccessOrExit(err);

exit:
Expand Down Expand Up @@ -163,7 +164,8 @@ CHIP_ERROR SendBadCommandRequest(chip::app::CommandSender * commandSender)
err = commandSender->FinishCommand();
SuccessOrExit(err);

err = commandSender->SendCommandRequest(chip::kTestDeviceNodeId, gFabricIndex, nullptr, gMessageTimeoutMsec);
err = commandSender->SendCommandRequest(chip::kTestDeviceNodeId, gFabricIndex, chip::Optional<chip::SessionHandle>::Missing(),
gMessageTimeoutMsec);
SuccessOrExit(err);

exit:
Expand Down Expand Up @@ -199,7 +201,8 @@ CHIP_ERROR SendReadRequest()
printf("\nSend read request message to Node: %" PRIu64 "\n", chip::kTestDeviceNodeId);

err = chip::app::InteractionModelEngine::GetInstance()->SendReadRequest(
chip::kTestDeviceNodeId, gFabricIndex, nullptr, eventPathParams, 2, &attributePathParams, 1, number, gMessageTimeoutMsec);
chip::kTestDeviceNodeId, gFabricIndex, chip::Optional<chip::SessionHandle>::Missing(), eventPathParams, 2,
&attributePathParams, 1, number, gMessageTimeoutMsec);
SuccessOrExit(err);

exit:
Expand Down Expand Up @@ -236,7 +239,8 @@ CHIP_ERROR SendWriteRequest(chip::app::WriteClientHandle & apWriteClient)

SuccessOrExit(err = writer->PutBoolean(chip::TLV::ContextTag(chip::app::AttributeDataElement::kCsTag_Data), true));
SuccessOrExit(err = apWriteClient->FinishAttribute());
SuccessOrExit(err = apWriteClient.SendWriteRequest(chip::kTestDeviceNodeId, gFabricIndex, nullptr, gMessageTimeoutMsec));
SuccessOrExit(err = apWriteClient.SendWriteRequest(chip::kTestDeviceNodeId, gFabricIndex,
chip::Optional<chip::SessionHandle>::Missing(), gMessageTimeoutMsec));

gWriteCount++;

Expand Down
3 changes: 2 additions & 1 deletion src/channel/ChannelContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ void ChannelContext::EnterCasePairingState()
auto & prepare = GetPrepareVars();
prepare.mCasePairingSession = Platform::New<CASESession>();

ExchangeContext * ctxt = mExchangeManager->NewContext(SessionHandle(), prepare.mCasePairingSession);
ExchangeContext * ctxt =
mExchangeManager->NewContext(SessionHandle::TemporaryUnauthenticatedSession(), prepare.mCasePairingSession);
VerifyOrReturn(ctxt != nullptr);

// TODO: currently only supports IP/UDP paring
Expand Down
Loading

0 comments on commit e9f4872

Please sign in to comment.