Skip to content

Commit

Permalink
Rename messageId into messageCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Sep 14, 2021
1 parent 29e6bcd commit d7c4e2d
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/lib/support/CHIPFaultInjection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ DLL_EXPORT void FuzzExchangeHeader(uint8_t * p, int32_t arg)
1, // MessageType
2, // ExchangeId
4, // ProfileId
8 // AckMsgId
8 // AckMessageCounter
};
const uint8_t values[CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES] = { 0x1, 0x2, 0xFF };
size_t offsetIndex = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/messaging/ExchangeMessageDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ CHIP_ERROR ExchangeMessageDispatch::SendMessage(SessionHandle session, uint16_t
#if !defined(NDEBUG)
if (!payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::StandaloneAck))
{
ChipLogDetail(ExchangeManager, "Piggybacking Ack for MsgId:%08" PRIX32 " with msg", payloadHeader.GetAckId().Value());
ChipLogDetail(ExchangeManager, "Piggybacking Ack for MessageCounter:%08" PRIX32 " with msg",
payloadHeader.GetAckId().Value());
}
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions src/messaging/ExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
}

// Matched ExchangeContext; send to message handler.
ec->HandleMessage(packetHeader.GetMessageId(), payloadHeader, source, msgFlags, std::move(msgBuf));
ec->HandleMessage(packetHeader.GetMessageCounter(), payloadHeader, source, msgFlags, std::move(msgBuf));
found = true;
return false;
}
Expand Down Expand Up @@ -297,7 +297,7 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
return;
}

CHIP_ERROR err = ec->HandleMessage(packetHeader.GetMessageId(), payloadHeader, source, msgFlags, std::move(msgBuf));
CHIP_ERROR err = ec->HandleMessage(packetHeader.GetMessageCounter(), payloadHeader, source, msgFlags, std::move(msgBuf));
if (err != CHIP_NO_ERROR)
{
// Using same error message for all errors to reduce code size.
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/Flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ enum class MessageFlagValues : uint32_t
/**< Indicates that the message is a duplicate of a previously received message. */
kDuplicateMessage = 0x00004000,
/**< Indicates that the peer's group key message counter is not synchronized. */
kPeerGroupMsgIdNotSynchronized = 0x00008000,
kPeerGroupMessageCounterNotSynchronized = 0x00008000,
/**< Indicates that the source of the message is the initiator of the CHIP exchange. */
kFromInitiator = 0x00010000,
/**< Indicates that message is being sent/received via the local ephemeral UDP port. */
Expand Down
36 changes: 18 additions & 18 deletions src/messaging/ReliableMessageContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ CHIP_ERROR ReliableMessageContext::FlushAcks()
if (err == CHIP_NO_ERROR)
{
#if !defined(NDEBUG)
ChipLogDetail(ExchangeManager, "Flushed pending ack for MsgId:%08" PRIX32, mPendingPeerAckId);
ChipLogDetail(ExchangeManager, "Flushed pending ack for MessageCounter:%08" PRIX32, mPendingPeerAckId);
#endif
}
}
Expand All @@ -150,36 +150,36 @@ uint64_t ReliableMessageContext::GetActiveRetransmitTimeoutTick()
* @note
* This message is part of the CHIP Reliable Messaging protocol.
*
* @param[in] AckMsgId The msgId of incoming Ack message.
* @param[in] AckMessageCounter The messageCounter of incoming Ack message.
*
* @retval #CHIP_ERROR_INVALID_ACK_ID if the msgId of received Ack is not in the RetransTable.
* @retval #CHIP_ERROR_INVALID_ACK_ID if the messageCounter of received Ack is not in the RetransTable.
* @retval #CHIP_NO_ERROR if the context was removed.
*
*/
CHIP_ERROR ReliableMessageContext::HandleRcvdAck(uint32_t AckMsgId)
CHIP_ERROR ReliableMessageContext::HandleRcvdAck(uint32_t AckMessageCounter)
{
CHIP_ERROR err = CHIP_NO_ERROR;

// Msg is an Ack; Check Retrans Table and remove message context
if (!GetReliableMessageMgr()->CheckAndRemRetransTable(this, AckMsgId))
if (!GetReliableMessageMgr()->CheckAndRemRetransTable(this, AckMessageCounter))
{
#if !defined(NDEBUG)
ChipLogError(ExchangeManager, "CHIP MsgId:%08" PRIX32 " not in RetransTable", AckMsgId);
ChipLogError(ExchangeManager, "CHIP MessageCounter:%08" PRIX32 " not in RetransTable", AckMessageCounter);
#endif
err = CHIP_ERROR_INVALID_ACK_ID;
// Optionally call an application callback with this error.
}
else
{
#if !defined(NDEBUG)
ChipLogDetail(ExchangeManager, "Removed CHIP MsgId:%08" PRIX32 " from RetransTable", AckMsgId);
ChipLogDetail(ExchangeManager, "Removed CHIP MessageCounter:%08" PRIX32 " from RetransTable", AckMessageCounter);
#endif
}

return err;
}

CHIP_ERROR ReliableMessageContext::HandleNeedsAck(uint32_t messageId, BitFlags<MessageFlagValues> messageFlags)
CHIP_ERROR ReliableMessageContext::HandleNeedsAck(uint32_t messageCounter, BitFlags<MessageFlagValues> messageFlags)

{
// Skip processing ack if drop ack debug is enabled.
Expand All @@ -189,32 +189,32 @@ CHIP_ERROR ReliableMessageContext::HandleNeedsAck(uint32_t messageId, BitFlags<M
// Expire any virtual ticks that have expired so all wakeup sources reflect the current time
GetReliableMessageMgr()->ExpireTicks();

CHIP_ERROR err = HandleNeedsAckInner(messageId, messageFlags);
CHIP_ERROR err = HandleNeedsAckInner(messageCounter, messageFlags);

// Schedule next physical wakeup on function exit
GetReliableMessageMgr()->StartTimer();

return err;
}

CHIP_ERROR ReliableMessageContext::HandleNeedsAckInner(uint32_t messageId, BitFlags<MessageFlagValues> messageFlags)
CHIP_ERROR ReliableMessageContext::HandleNeedsAckInner(uint32_t messageCounter, BitFlags<MessageFlagValues> messageFlags)

{
// If the message IS a duplicate there will never be a response to it, so we
// should not wait for one and just immediately send a standalone ack.
if (messageFlags.Has(MessageFlagValues::kDuplicateMessage))
{
#if !defined(NDEBUG)
ChipLogDetail(ExchangeManager, "Forcing tx of solitary ack for duplicate MsgId:%08" PRIX32, messageId);
ChipLogDetail(ExchangeManager, "Forcing tx of solitary ack for duplicate MessageCounter:%08" PRIX32, messageCounter);
#endif
// Is there pending ack for a different message id.
bool wasAckPending = IsAckPending() && mPendingPeerAckId != messageId;
bool wasAckPending = IsAckPending() && mPendingPeerAckId != messageCounter;

// Temporary store currently pending ack id (even if there is none).
uint32_t tempAckId = mPendingPeerAckId;

// Set the pending ack id.
SetPendingPeerAckId(messageId);
SetPendingPeerAckId(messageCounter);

// Send the Ack for the duplication message in a SecureChannel::StandaloneAck message.
CHIP_ERROR err = SendStandaloneAckMessage();
Expand All @@ -234,15 +234,15 @@ CHIP_ERROR ReliableMessageContext::HandleNeedsAckInner(uint32_t messageId, BitFl
if (IsAckPending())
{
#if !defined(NDEBUG)
ChipLogDetail(ExchangeManager, "Pending ack queue full; forcing tx of solitary ack for MsgId:%08" PRIX32,
ChipLogDetail(ExchangeManager, "Pending ack queue full; forcing tx of solitary ack for MessageCounter:%08" PRIX32,
mPendingPeerAckId);
#endif
// Send the Ack for the currently pending Ack in a SecureChannel::StandaloneAck message.
ReturnErrorOnFailure(SendStandaloneAckMessage());
}

// Replace the Pending ack id.
SetPendingPeerAckId(messageId);
SetPendingPeerAckId(messageCounter);
mNextAckTimeTick =
static_cast<uint16_t>(CHIP_CONFIG_RMP_DEFAULT_ACK_TIMEOUT_TICK +
GetReliableMessageMgr()->GetTickCounterFromTimeDelta(System::Clock::GetMonotonicMilliseconds()));
Expand All @@ -261,7 +261,7 @@ CHIP_ERROR ReliableMessageContext::SendStandaloneAckMessage()

// Send the null message
#if !defined(NDEBUG)
ChipLogDetail(ExchangeManager, "Sending Standalone Ack for MsgId:%08" PRIX32, mPendingPeerAckId);
ChipLogDetail(ExchangeManager, "Sending Standalone Ack for MessageCounter:%08" PRIX32, mPendingPeerAckId);
#endif

CHIP_ERROR err = GetExchangeContext()->SendMessage(Protocols::SecureChannel::MsgType::StandaloneAck, std::move(msgBuf),
Expand All @@ -273,8 +273,8 @@ CHIP_ERROR ReliableMessageContext::SendStandaloneAckMessage()
}
if (err != CHIP_NO_ERROR)
{
ChipLogError(ExchangeManager, "Failed to send Solitary ack for MsgId:%08" PRIX32 ":%" CHIP_ERROR_FORMAT, mPendingPeerAckId,
err.Format());
ChipLogError(ExchangeManager, "Failed to send Solitary ack for MessageCounter:%08" PRIX32 ":%" CHIP_ERROR_FORMAT,
mPendingPeerAckId, err.Format());
}

return err;
Expand Down
6 changes: 3 additions & 3 deletions src/messaging/ReliableMessageContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ class ReliableMessageContext
private:
void RetainContext();
void ReleaseContext();
CHIP_ERROR HandleRcvdAck(uint32_t AckMsgId);
CHIP_ERROR HandleNeedsAck(uint32_t messageId, BitFlags<MessageFlagValues> messageFlags);
CHIP_ERROR HandleNeedsAckInner(uint32_t messageId, BitFlags<MessageFlagValues> messageFlags);
CHIP_ERROR HandleRcvdAck(uint32_t AckMessageCounter);
CHIP_ERROR HandleNeedsAck(uint32_t messageCounter, BitFlags<MessageFlagValues> messageFlags);
CHIP_ERROR HandleNeedsAckInner(uint32_t messageCounter, BitFlags<MessageFlagValues> messageFlags);
ExchangeContext * GetExchangeContext();

/**
Expand Down
30 changes: 16 additions & 14 deletions src/messaging/ReliableMessageMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ void ReliableMessageMgr::TicklessDebugDumpRetransTable(const char * log)
{
if (entry.rc)
{
ChipLogDetail(ExchangeManager, "EC:%04" PRIX16 " MsgId:%08" PRIX32 " NextRetransTimeCtr:%04" PRIX16, entry.rc,
entry.msgId, entry.nextRetransTimeTick);
ChipLogDetail(ExchangeManager, "EC:%04" PRIX16 " MessageCounter:%08" PRIX32 " NextRetransTimeCtr:%04" PRIX16, entry.rc,
entry.messageCounter, entry.nextRetransTimeTick);
}
}
}
Expand Down Expand Up @@ -143,15 +143,15 @@ void ReliableMessageMgr::ExecuteActions()
continue;
}

uint8_t sendCount = entry.sendCount;
uint32_t msgId = entry.retainedBuf.GetMsgId();
uint8_t sendCount = entry.sendCount;
uint32_t messageCounter = entry.retainedBuf.GetMessageCounter();

if (sendCount == CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS)
{
err = CHIP_ERROR_MESSAGE_NOT_ACKNOWLEDGED;

ChipLogError(ExchangeManager, "Failed to Send CHIP MsgId:%08" PRIX32 " sendCount: %" PRIu8 " max retries: %d", msgId,
sendCount, CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS);
ChipLogError(ExchangeManager, "Failed to Send CHIP MessageCounter:%08" PRIX32 " sendCount: %" PRIu8 " max retries: %d",
messageCounter, sendCount, CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS);

// Remove from Table
ClearRetransTable(entry);
Expand All @@ -166,7 +166,7 @@ void ReliableMessageMgr::ExecuteActions()
// If the retransmission was successful, update the passive timer
entry.nextRetransTimeTick = static_cast<uint16_t>(rc->GetActiveRetransmitTimeoutTick());
#if !defined(NDEBUG)
ChipLogDetail(ExchangeManager, "Retransmit MsgId:%08" PRIX32 " Send Cnt %d", msgId, entry.sendCount);
ChipLogDetail(ExchangeManager, "Retransmit MessageCounter:%08" PRIX32 " Send Cnt %d", messageCounter, entry.sendCount);
#endif
}
}
Expand Down Expand Up @@ -329,17 +329,17 @@ void ReliableMessageMgr::ResumeRetransmision(ReliableMessageContext * rc)
}
}

bool ReliableMessageMgr::CheckAndRemRetransTable(ReliableMessageContext * rc, uint32_t ackMsgId)
bool ReliableMessageMgr::CheckAndRemRetransTable(ReliableMessageContext * rc, uint32_t ackMessageCounter)
{
for (RetransTableEntry & entry : mRetransTable)
{
if ((entry.rc == rc) && entry.retainedBuf.GetMsgId() == ackMsgId)
if ((entry.rc == rc) && entry.retainedBuf.GetMessageCounter() == ackMessageCounter)
{
// Clear the entry from the retransmision table.
ClearRetransTable(entry);

#if !defined(NDEBUG)
ChipLogDetail(ExchangeManager, "Rxd Ack; Removing MsgId:%08" PRIX32 " from Retrans Table", ackMsgId);
ChipLogDetail(ExchangeManager, "Rxd Ack; Removing MessageCounter:%08" PRIX32 " from Retrans Table", ackMessageCounter);
#endif
return true;
}
Expand All @@ -360,8 +360,9 @@ CHIP_ERROR ReliableMessageMgr::SendFromRetransTable(RetransTableEntry * entry)
if (dispatcher == nullptr || !rc->GetExchangeContext()->HasSecureSession())
{
// Using same error message for all errors to reduce code size.
ChipLogError(ExchangeManager, "Crit-err %" CHIP_ERROR_FORMAT " when sending CHIP MsgId:%08" PRIX32 ", send tries: %d",
CHIP_ERROR_INCORRECT_STATE.Format(), entry->retainedBuf.GetMsgId(), entry->sendCount);
ChipLogError(ExchangeManager,
"Crit-err %" CHIP_ERROR_FORMAT " when sending CHIP MessageCounter:%08" PRIX32 ", send tries: %d",
CHIP_ERROR_INCORRECT_STATE.Format(), entry->retainedBuf.GetMessageCounter(), entry->sendCount);
ClearRetransTable(*entry);
return CHIP_ERROR_INCORRECT_STATE;
}
Expand All @@ -377,8 +378,9 @@ CHIP_ERROR ReliableMessageMgr::SendFromRetransTable(RetransTableEntry * entry)
{
// Remove from table
// Using same error message for all errors to reduce code size.
ChipLogError(ExchangeManager, "Crit-err %" CHIP_ERROR_FORMAT " when sending CHIP MsgId:%08" PRIX32 ", send tries: %d",
err.Format(), entry->retainedBuf.GetMsgId(), entry->sendCount);
ChipLogError(ExchangeManager,
"Crit-err %" CHIP_ERROR_FORMAT " when sending CHIP MessageCounter:%08" PRIX32 ", send tries: %d", err.Format(),
entry->retainedBuf.GetMessageCounter(), entry->sendCount);

ClearRetransTable(*entry);
}
Expand Down
4 changes: 2 additions & 2 deletions src/messaging/ReliableMessageMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ class ReliableMessageMgr
*
* @param[in] rc A pointer to the ExchangeContext object.
*
* @param[in] msgId message ID which has been acked.
* @param[in] messageCounter message ID which has been acked.
*
* @retval #CHIP_NO_ERROR On success.
*/
bool CheckAndRemRetransTable(ReliableMessageContext * rc, uint32_t msgId);
bool CheckAndRemRetransTable(ReliableMessageContext * rc, uint32_t messageCounter);

/**
* Send the specified entry from the retransmission table.
Expand Down
6 changes: 3 additions & 3 deletions src/transport/SecureMessageCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ CHIP_ERROR Encode(Transport::PeerConnectionState * state, PayloadHeader & payloa
VerifyOrReturnError(!msgBuf->HasChainedBuffer(), CHIP_ERROR_INVALID_MESSAGE_LENGTH);
VerifyOrReturnError(msgBuf->TotalLength() <= kMaxAppMessageLen, CHIP_ERROR_MESSAGE_TOO_LONG);

uint32_t msgId = counter.Value();
uint32_t messageCounter = counter.Value();

static_assert(std::is_same<decltype(msgBuf->TotalLength()), uint16_t>::value,
"Addition to generate payloadLength might overflow");

packetHeader
.SetMessageId(msgId) //
.SetMessageCounter(messageCounter) //
.SetEncryptionKeyID(state->GetPeerKeyID());

packetHeader.GetFlags().Set(Header::FlagValues::kEncryptedMessage);
Expand All @@ -68,7 +68,7 @@ CHIP_ERROR Encode(Transport::PeerConnectionState * state, PayloadHeader & payloa
VerifyOrReturnError(CanCastTo<uint16_t>(totalLen + taglen), CHIP_ERROR_INTERNAL);
msgBuf->SetDataLength(static_cast<uint16_t>(totalLen + taglen));

ChipLogDetail(Inet, "Secure message was encrypted: Msg ID %" PRIu32, msgId);
ChipLogDetail(Inet, "Secure message was encrypted: Msg ID %" PRIu32, messageCounter);

ReturnErrorOnFailure(counter.Advance());
return CHIP_NO_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion src/transport/SecureSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ CHIP_ERROR SecureSession::GetIV(const PacketHeader & header, uint8_t * iv, size_
Encoding::LittleEndian::BufferWriter bbuf(iv, len);

bbuf.Put64(header.GetSourceNodeId().ValueOr(0));
bbuf.Put32(header.GetMessageId());
bbuf.Put32(header.GetMessageCounter());

return bbuf.Fit() ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY;
}
Expand Down
Loading

0 comments on commit d7c4e2d

Please sign in to comment.