Skip to content

Commit

Permalink
Update session handle for Group message (#11390)
Browse files Browse the repository at this point in the history
* Update session handle for Group message
  • Loading branch information
jepenven-silabs authored and pull[bot] committed Feb 8, 2023
1 parent c07cdf3 commit 2189783
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ ExchangeContext::ExchangeContext(ExchangeManager * em, uint16_t ExchangeId, Sess
SetDropAckDebug(false);
SetAckPending(false);
SetMsgRcvdFromPeer(false);
SetAutoRequestAck(true);

// Do not request Ack for multicast
SetAutoRequestAck(!session.IsGroupSession());

#if defined(CHIP_EXCHANGE_CONTEXT_DETAIL_LOGGING)
ChipLogDetail(ExchangeManager, "ec++ id: " ChipLogFormatExchange, ChipLogValueExchange(this));
Expand Down
43 changes: 24 additions & 19 deletions src/messaging/ExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,29 +213,34 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
msgFlags.Set(MessageFlagValues::kDuplicateMessage);
}

// Search for an existing exchange that the message applies to. If a match is found...
bool found = false;
mContextPool.ForEachActiveObject([&](auto * ec) {
if (ec->MatchExchange(session, packetHeader, payloadHeader))
{
// Found a matching exchange. Set flag for correct subsequent MRP
// retransmission timeout selection.
if (!ec->HasRcvdMsgFromPeer())
// Skip retrieval of exchange for group message since no exchange is stored
// for group msg (optimization)
if (!packetHeader.IsGroupSession())
{
// Search for an existing exchange that the message applies to. If a match is found...
bool found = false;
mContextPool.ForEachActiveObject([&](auto * ec) {
if (ec->MatchExchange(session, packetHeader, payloadHeader))
{
ec->SetMsgRcvdFromPeer(true);
// Found a matching exchange. Set flag for correct subsequent MRP
// retransmission timeout selection.
if (!ec->HasRcvdMsgFromPeer())
{
ec->SetMsgRcvdFromPeer(true);
}

// Matched ExchangeContext; send to message handler.
ec->HandleMessage(packetHeader.GetMessageCounter(), payloadHeader, source, msgFlags, std::move(msgBuf));
found = true;
return false;
}
return true;
});

// Matched ExchangeContext; send to message handler.
ec->HandleMessage(packetHeader.GetMessageCounter(), payloadHeader, source, msgFlags, std::move(msgBuf));
found = true;
return false;
if (found)
{
return;
}
return true;
});

if (found)
{
return;
}

// If it's not a duplicate message, search for an unsolicited message handler if it is marked as being sent by an initiator.
Expand Down
7 changes: 7 additions & 0 deletions src/transport/SessionHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class SessionHandle
mPeerSessionId.SetValue(peerSessionId);
}

SessionHandle(NodeId peerNodeId, GroupId groupId, FabricIndex fabric) : mPeerNodeId(peerNodeId), mFabric(fabric)
{
mGroupId.SetValue(groupId);
}

bool IsSecure() const { return !mUnauthenticatedSessionHandle.HasValue(); }

bool HasFabricIndex() const { return (mFabric != kUndefinedFabricIndex); }
Expand All @@ -68,6 +73,7 @@ class SessionHandle
}

NodeId GetPeerNodeId() const { return mPeerNodeId; }
bool IsGroupSession() const { return mGroupId.HasValue(); }
const Optional<uint16_t> & GetPeerSessionId() const { return mPeerSessionId; }
const Optional<uint16_t> & GetLocalSessionId() const { return mLocalSessionId; }

Expand All @@ -85,6 +91,7 @@ class SessionHandle
NodeId mPeerNodeId;
Optional<uint16_t> mLocalSessionId;
Optional<uint16_t> mPeerSessionId;
Optional<GroupId> mGroupId;
// TODO: Re-evaluate the storing of Fabric ID in SessionHandle
// The Fabric ID will not be available for PASE and group sessions. So need
// to identify an approach that'll allow looking up the corresponding information for
Expand Down
23 changes: 10 additions & 13 deletions src/transport/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & packetHeade
CHIP_ERROR err = CHIP_NO_ERROR;
PayloadHeader payloadHeader;
SessionManagerDelegate::DuplicateMessage isDuplicate = SessionManagerDelegate::DuplicateMessage::No;
FabricIndex fabricIndex = 0; // TODO : remove initialization once GroupDataProvider->Decrypt is implemented
// Credentials::GroupDataProvider * groups = Credentials::GetGroupDataProvider();

VerifyOrExit(!msg.IsNull(), ChipLogError(Inet, "Secure transport received NULL packet, discarding"));
Expand All @@ -471,6 +472,7 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & packetHeade
}

// Trial decryption with GroupDataProvider. TODO: Implement the GroupDataProvider Class
// TODO retrieve also the fabricIndex with the GroupDataProvider.
// VerifyOrExit(CHIP_NO_ERROR == groups->DecryptMessage(packetHeader, payloadHeader, msg),
// ChipLogError(Inet, "Secure transport received group message, but failed to decode it, discarding"));

Expand All @@ -488,29 +490,24 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & packetHeade

// TODO: Handle Group message counter here spec 4.7.3
// spec 4.5.1.2 for msg counter

if (isDuplicate == SessionManagerDelegate::DuplicateMessage::Yes && !payloadHeader.NeedsAck())
if (isDuplicate == SessionManagerDelegate::DuplicateMessage::Yes)
{
ChipLogDetail(Inet,
"Received a duplicate message with MessageCounter:" ChipLogFormatMessageCounter
" on exchange " ChipLogFormatExchangeId,
packetHeader.GetMessageCounter(), ChipLogValueExchangeIdFromReceivedHeader(payloadHeader));
if (!payloadHeader.NeedsAck())
{
// If it's a duplicate message, but doesn't require an ack, let's drop it right here to save CPU
// cycles on further message processing.
ExitNow(err = CHIP_NO_ERROR);
}
packetHeader.GetMessageCounter(), ChipLogValueExchangeIdFromSentHeader(payloadHeader));

// If it's a duplicate message, let's drop it right here to save CPU
// cycles on further message processing.
ExitNow(err = CHIP_NO_ERROR);
}

// TODO: Commit Group Message Counter

if (mCB != nullptr)
{
// TODO: Update Session Handle for Group messages.
// SessionHandle session(session->GetPeerNodeId(), session->GetLocalSessionId(), session->GetPeerSessionId(),
// session->GetFabricIndex());
// mCB->OnMessageReceived(packetHeader, payloadHeader, session, peerAddress, isDuplicate, std::move(msg));
SessionHandle session(packetHeader.GetSourceNodeId().Value(), packetHeader.GetDestinationGroupId().Value(), fabricIndex);
mCB->OnMessageReceived(packetHeader, payloadHeader, session, peerAddress, isDuplicate, std::move(msg));
}

exit:
Expand Down

0 comments on commit 2189783

Please sign in to comment.