Skip to content

Commit

Permalink
Update the exchange match logic to align with the latest spec proposal (
Browse files Browse the repository at this point in the history
#8441)

* Update the exchange match logic to align with the latest spec proposal

* Rename SecureSessionHandle to SessionHandle

* Make mLocalKeyId and mPeerKeyId optional

* Rename match to matchIncomingSession
  • Loading branch information
yufengwangca authored and pull[bot] committed Aug 31, 2021
1 parent af1ac8a commit 1105583
Show file tree
Hide file tree
Showing 49 changed files with 285 additions and 166 deletions.
3 changes: 1 addition & 2 deletions examples/shell/shell_common/cmd_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ void StartPinging(streamer_t * stream, char * destination)
err = EstablishSecureSession(stream, GetEchoPeerAddress());
SuccessOrExit(err);

// TODO: temprary create a SecureSessionHandle from node id to unblock end-to-end test. Complete solution is tracked in PR:4451
err = gEchoClient.Init(&gExchangeManager, { kTestDeviceNodeId, 0, gFabricIndex });
err = gEchoClient.Init(&gExchangeManager, SessionHandle(kTestDeviceNodeId, 0, 0, gFabricIndex));
SuccessOrExit(err);

// Arrange to get a callback whenever an Echo Response is received.
Expand Down
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ CHIP_ERROR SendMessage(streamer_t * stream)
uint32_t payloadSize = gSendArguments.GetPayloadSize();

// Create a new exchange context.
auto * ec = gExchangeManager.NewContext({ kTestDeviceNodeId, 0, gFabricIndex }, &gMockAppDelegate);
auto * ec = gExchangeManager.NewContext(SessionHandle(kTestDeviceNodeId, 0, 0, gFabricIndex), &gMockAppDelegate);
VerifyOrExit(ec != nullptr, err = CHIP_ERROR_NO_MEMORY);

payloadBuf = MessagePacketBuffer::New(payloadSize);
Expand Down
6 changes: 2 additions & 4 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, SecureSessionHandle * secureSession)
CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * secureSession)
{
CHIP_ERROR err = CHIP_NO_ERROR;
System::PacketBufferHandle commandPacket;
Expand All @@ -49,11 +49,9 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, FabricIndex aFabric
AbortExistingExchangeContext();

// Create a new exchange context.
// TODO: temprary create a SecureSessionHandle from node id, will be fix in PR 3602
// TODO: Hard code keyID to 0 to unblock IM end-to-end test. Complete solution is tracked in issue:4451
if (secureSession == nullptr)
{
mpExchangeCtx = mpExchangeMgr->NewContext({ aNodeId, 0, aFabricIndex }, this);
mpExchangeCtx = mpExchangeMgr->NewContext(SessionHandle(aNodeId, 0, 0, aFabricIndex), this);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/CommandSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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, SecureSessionHandle * secureSession = nullptr);
CHIP_ERROR SendCommandRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * secureSession = nullptr);

private:
// ExchangeDelegate interface implementation. Private so people won't
Expand Down
2 changes: 1 addition & 1 deletion src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ void InteractionModelEngine::OnResponseTimeout(Messaging::ExchangeContext * ec)
ChipLogProgress(DataManagement, "Time out! failed to receive echo response from Exchange: %d", ec->GetExchangeId());
}

CHIP_ERROR InteractionModelEngine::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SecureSessionHandle * apSecureSession,
CHIP_ERROR InteractionModelEngine::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
EventPathParams * apEventPathParamsList, size_t aEventPathParamsListSize,
AttributePathParams * apAttributePathParamsList,
size_t aAttributePathParamsListSize, EventNumber aEventNumber,
Expand Down
2 changes: 1 addition & 1 deletion src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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, SecureSessionHandle * apSecureSession,
CHIP_ERROR SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
EventPathParams * apEventPathParamsList, size_t aEventPathParamsListSize,
AttributePathParams * apAttributePathParamsList, size_t aAttributePathParamsListSize,
EventNumber aEventNumber, uint64_t aAppIdentifier = 0);
Expand Down
4 changes: 2 additions & 2 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, SecureSessionHandle * apSecureSession,
CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession,
EventPathParams * apEventPathParamsList, size_t aEventPathParamsListSize,
AttributePathParams * apAttributePathParamsList, size_t aAttributePathParamsListSize,
EventNumber aEventNumber)
Expand Down Expand Up @@ -140,7 +140,7 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex,
}
else
{
mpExchangeCtx = mpExchangeMgr->NewContext({ aNodeId, 0, aFabricIndex }, this);
mpExchangeCtx = mpExchangeMgr->NewContext(SessionHandle(aNodeId, 0, 0, aFabricIndex), this);
}
VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
mpExchangeCtx->SetResponseTimeout(kImMessageTimeoutMsec);
Expand Down
2 changes: 1 addition & 1 deletion src/app/ReadClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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, SecureSessionHandle * aSecureSession,
CHIP_ERROR SendReadRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * aSecureSession,
EventPathParams * apEventPathParamsList, size_t aEventPathParamsListSize,
AttributePathParams * apAttributePathParamsList, size_t aAttributePathParamsListSize,
EventNumber aEventNumber);
Expand Down
8 changes: 3 additions & 5 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, SecureSessionHandle * apSecureSession)
CHIP_ERROR WriteClient::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession)
{
CHIP_ERROR err = CHIP_NO_ERROR;
System::PacketBufferHandle packet;
Expand All @@ -262,11 +262,9 @@ CHIP_ERROR WriteClient::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricInde
ClearExistingExchangeContext();

// Create a new exchange context.
// TODO: we temporarily create a SecureSessionHandle from node id, this will be fixed in PR 3602
// TODO: Hard code keyID to 0 to unblock IM end-to-end test. Complete solution is tracked in issue:4451
if (apSecureSession == nullptr)
{
mpExchangeCtx = mpExchangeMgr->NewContext({ aNodeId, 0, aFabricIndex }, this);
mpExchangeCtx = mpExchangeMgr->NewContext(SessionHandle(aNodeId, 0, 0, aFabricIndex), this);
}
else
{
Expand Down Expand Up @@ -398,7 +396,7 @@ CHIP_ERROR WriteClient::ProcessAttributeStatusElement(AttributeStatusElement::Pa
return err;
}

CHIP_ERROR WriteClientHandle::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SecureSessionHandle * apSecureSession)
CHIP_ERROR WriteClientHandle::SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession)
{
CHIP_ERROR err = mpWriteClient->SendWriteRequest(aNodeId, aFabricIndex, apSecureSession);

Expand Down
4 changes: 2 additions & 2 deletions src/app/WriteClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ 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, SecureSessionHandle * apSecureSession);
CHIP_ERROR SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession);

/**
* Initialize the client object. Within the lifetime
Expand Down Expand Up @@ -173,7 +173,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, SecureSessionHandle * apSecureSession);
CHIP_ERROR SendWriteRequest(NodeId aNodeId, FabricIndex aFabricIndex, SessionHandle * apSecureSession);

/**
* Encode an attribute value that can be directly encoded using TLVWriter::Put
Expand Down
6 changes: 3 additions & 3 deletions src/app/tests/TestCommandInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void TestCommandInteraction::TestCommandHandlerWithWrongState(nlTestSuite * apSu

err = commandHandler.Init(&chip::gExchangeManager, nullptr);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
commandHandler.mpExchangeCtx = gExchangeManager.NewContext({ 0, 0, 0 }, nullptr);
commandHandler.mpExchangeCtx = gExchangeManager.NewContext(SessionHandle(0, 0, 0, 0), nullptr);
TestExchangeDelegate delegate;
commandHandler.mpExchangeCtx->SetDelegate(&delegate);
err = commandHandler.SendCommandResponse();
Expand Down Expand Up @@ -300,7 +300,7 @@ void TestCommandInteraction::TestCommandHandlerWithSendEmptyCommand(nlTestSuite
err = commandHandler.Init(&chip::gExchangeManager, nullptr);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

commandHandler.mpExchangeCtx = gExchangeManager.NewContext({ 0, 0, 0 }, nullptr);
commandHandler.mpExchangeCtx = gExchangeManager.NewContext(SessionHandle(0, 0, 0, 0), nullptr);

TestExchangeDelegate delegate;
commandHandler.mpExchangeCtx->SetDelegate(&delegate);
Expand Down Expand Up @@ -337,7 +337,7 @@ void TestCommandInteraction::ValidateCommandHandlerWithSendCommand(nlTestSuite *
err = commandHandler.Init(&chip::gExchangeManager, nullptr);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

commandHandler.mpExchangeCtx = gExchangeManager.NewContext({ 0, 0, 0 }, nullptr);
commandHandler.mpExchangeCtx = gExchangeManager.NewContext(SessionHandle(0, 0, 0, 0), nullptr);
TestExchangeDelegate delegate;
commandHandler.mpExchangeCtx->SetDelegate(&delegate);

Expand Down
6 changes: 3 additions & 3 deletions src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void TestReadInteraction::TestReadClient(nlTestSuite * apSuite, void * apContext
System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
err = readClient.Init(&ctx.GetExchangeManager(), &delegate, 0 /* application identifier */);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
SecureSessionHandle session = ctx.GetSessionLocalToPeer();
SessionHandle session = ctx.GetSessionLocalToPeer();
err = readClient.SendReadRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &session, nullptr /*apEventPathParamsList*/,
0 /*aEventPathParamsListSize*/, nullptr /*apAttributePathParamsList*/,
0 /*aAttributePathParamsListSize*/, eventNumber /*aEventNumber*/);
Expand Down Expand Up @@ -389,7 +389,7 @@ void TestReadInteraction::TestReadClientInvalidReport(nlTestSuite * apSuite, voi
err = readClient.Init(&ctx.GetExchangeManager(), &delegate, 0 /* application identifier */);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

SecureSessionHandle session = ctx.GetSessionLocalToPeer();
SessionHandle session = ctx.GetSessionLocalToPeer();
err = readClient.SendReadRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &session, nullptr /*apEventPathParamsList*/,
0 /*aEventPathParamsListSize*/, nullptr /*apAttributePathParamsList*/,
0 /*aAttributePathParamsListSize*/, eventNumber /*aEventNumber*/);
Expand Down Expand Up @@ -578,7 +578,7 @@ void TestReadInteraction::TestReadEventRoundtrip(nlTestSuite * apSuite, void * a
eventPathParams[1].mClusterId = kTestClusterId;
eventPathParams[1].mEventId = kTestEventIdCritical;

SecureSessionHandle session = ctx.GetSessionLocalToPeer();
SessionHandle session = ctx.GetSessionLocalToPeer();
err = chip::app::InteractionModelEngine::GetInstance()->SendReadRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(),
&session, eventPathParams, 2, nullptr, 1, 0);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/TestReportingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void TestReportingEngine::TestBuildAndSendSingleReportData(nlTestSuite * apSuite

err = InteractionModelEngine::GetInstance()->Init(&gExchangeManager, nullptr);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
Messaging::ExchangeContext * exchangeCtx = gExchangeManager.NewContext({ 0, 0, 0 }, nullptr);
Messaging::ExchangeContext * exchangeCtx = gExchangeManager.NewContext(SessionHandle(0, 0, 0, 0), nullptr);
TestExchangeDelegate delegate;
exchangeCtx->SetDelegate(&delegate);

Expand Down
6 changes: 3 additions & 3 deletions src/app/tests/TestWriteInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ void TestWriteInteraction::TestWriteClient(nlTestSuite * apSuite, void * apConte
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
AddAttributeDataElement(apSuite, apContext, writeClientHandle);

SecureSessionHandle session = ctx.GetSessionLocalToPeer();
err = writeClientHandle.SendWriteRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &session);
SessionHandle session = ctx.GetSessionLocalToPeer();
err = writeClientHandle.SendWriteRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &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 @@ -304,7 +304,7 @@ void TestWriteInteraction::TestWriteRoundtrip(nlTestSuite * apSuite, void * apCo

NL_TEST_ASSERT(apSuite, !delegate.mGotResponse);

SecureSessionHandle session = ctx.GetSessionLocalToPeer();
SessionHandle session = ctx.GetSessionLocalToPeer();

err = writeClient.SendWriteRequest(ctx.GetDestinationNodeId(), ctx.GetFabricIndex(), &session);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
Expand Down
3 changes: 2 additions & 1 deletion src/app/util/chip-message-send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ EmberStatus chipSendUnicast(NodeId destination, EmberApsFrame * apsFrame, uint16
return EMBER_DELIVERY_FAILED;
}

Messaging::ExchangeContext * exchange = exchangeMgr->NewContext({ destination, Transport::kAnyKeyId, 0 }, nullptr);
Messaging::ExchangeContext * exchange =
exchangeMgr->NewContext(SessionHandle(destination, 0, Transport::kAnyKeyId, 0), nullptr);
if (exchange == nullptr)
{
return EMBER_DELIVERY_FAILED;
Expand Down
10 changes: 5 additions & 5 deletions src/channel/ChannelContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bool ChannelContext::IsCasePairing()
return mState == ChannelState::kPreparing && GetPrepareVars().mState == PrepareState::kCasePairing;
}

bool ChannelContext::MatchesSession(SecureSessionHandle session, SecureSessionMgr * ssm)
bool ChannelContext::MatchesSession(SessionHandle session, SecureSessionMgr * ssm)
{
switch (mState)
{
Expand Down Expand Up @@ -258,7 +258,7 @@ void ChannelContext::EnterCasePairingState()
auto & prepare = GetPrepareVars();
prepare.mCasePairingSession = Platform::New<CASESession>();

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

// TODO: currently only supports IP/UDP paring
Expand Down Expand Up @@ -313,7 +313,7 @@ void ChannelContext::OnSessionEstablished()
}
}

void ChannelContext::OnNewConnection(SecureSessionHandle session)
void ChannelContext::OnNewConnection(SessionHandle session)
{
if (mState != ChannelState::kPreparing)
return;
Expand All @@ -324,14 +324,14 @@ void ChannelContext::OnNewConnection(SecureSessionHandle session)
EnterReadyState(session);
}

void ChannelContext::EnterReadyState(SecureSessionHandle session)
void ChannelContext::EnterReadyState(SessionHandle session)
{
mState = ChannelState::kReady;
mStateVars.Set<ReadyVars>(session);
mChannelManager->NotifyChannelEvent(this, [](ChannelDelegate * delegate) { delegate->OnEstablished(); });
}

void ChannelContext::OnConnectionExpired(SecureSessionHandle session)
void ChannelContext::OnConnectionExpired(SessionHandle session)
{
if (mState != ChannelState::kReady)
return;
Expand Down
12 changes: 6 additions & 6 deletions src/channel/ChannelContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ class ChannelContext : public ReferenceCounted<ChannelContext, ChannelContextDel
bool IsCasePairing();

bool MatchesBuilder(const ChannelBuilder & builder);
bool MatchesSession(SecureSessionHandle session, SecureSessionMgr * ssm);
bool MatchesSession(SessionHandle session, SecureSessionMgr * ssm);

// events of ResolveDelegate, propagated from ExchangeManager
void HandleNodeIdResolve(CHIP_ERROR error, uint64_t nodeId, const Mdns::MdnsService & address);

// events of SecureSessionManager, propagated from ExchangeManager
void OnNewConnection(SecureSessionHandle session);
void OnConnectionExpired(SecureSessionHandle session);
void OnNewConnection(SessionHandle session);
void OnConnectionExpired(SessionHandle session);

// Pairing callbacks
void OnSessionEstablishmentError(CHIP_ERROR error) override;
Expand Down Expand Up @@ -147,8 +147,8 @@ class ChannelContext : public ReferenceCounted<ChannelContext, ChannelContextDel
struct ReadyVars
{
static constexpr const size_t VariantId = 2;
ReadyVars(SecureSessionHandle session) : mSession(session) {}
const SecureSessionHandle mSession;
ReadyVars(SessionHandle session) : mSession(session) {}
const SessionHandle mSession;
};

Variant<PrepareVars, ReadyVars> mStateVars;
Expand All @@ -160,7 +160,7 @@ class ChannelContext : public ReferenceCounted<ChannelContext, ChannelContextDel
void EnterPreparingState(const ChannelBuilder & builder);
void ExitPreparingState();

void EnterReadyState(SecureSessionHandle session);
void EnterReadyState(SessionHandle session);
void ExitReadyState();

void EnterFailedState(CHIP_ERROR error);
Expand Down
4 changes: 2 additions & 2 deletions src/channel/Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DLL_EXPORT ChannelManager : public ExchangeMgrDelegate
});
}

void OnNewConnection(SecureSessionHandle session, ExchangeManager * mgr) override
void OnNewConnection(SessionHandle session, ExchangeManager * mgr) override
{
mChannelContexts.ForEachActiveObject([&](ChannelContext * context) {
if (context->MatchesSession(session, mgr->GetSessionMgr()))
Expand All @@ -70,7 +70,7 @@ class DLL_EXPORT ChannelManager : public ExchangeMgrDelegate
});
}

void OnConnectionExpired(SecureSessionHandle session, ExchangeManager * mgr) override
void OnConnectionExpired(SessionHandle session, ExchangeManager * mgr) override
{
mChannelContexts.ForEachActiveObject([&](ChannelContext * context) {
if (context->MatchesSession(session, mgr->GetSessionMgr()))
Expand Down
Loading

0 comments on commit 1105583

Please sign in to comment.