diff --git a/src/messaging/ExchangeContext.cpp b/src/messaging/ExchangeContext.cpp index 1d358a95c1e7bc..6dd1bf49db73b6 100644 --- a/src/messaging/ExchangeContext.cpp +++ b/src/messaging/ExchangeContext.cpp @@ -105,15 +105,6 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp reliableTransmissionRequested = !sendFlags.Has(SendMessageFlags::kNoAutoRequestAck); } - ExchangeMessageDispatch * dispatch = GetMessageDispatch(); - ApplicationExchangeDispatch defaultDispatch; - - if (dispatch == nullptr) - { - defaultDispatch.Init(mExchangeMgr->GetReliableMessageMgr(), mExchangeMgr->GetSessionMgr()); - dispatch = &defaultDispatch; - } - // If a response message is expected... if (sendFlags.Has(SendMessageFlags::kExpectResponse)) { @@ -130,8 +121,8 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp } } - err = dispatch->SendMessage(mSecureSession, mExchangeId, IsInitiator(), GetReliableMessageContext(), - reliableTransmissionRequested, protocolId, msgType, std::move(msgBuf)); + err = mDispatch->SendMessage(mSecureSession, mExchangeId, IsInitiator(), GetReliableMessageContext(), + reliableTransmissionRequested, protocolId, msgType, std::move(msgBuf)); exit: if (err != CHIP_NO_ERROR && IsResponseExpected()) @@ -217,18 +208,8 @@ void ExchangeContextDeletor::Release(ExchangeContext * ec) ec->mExchangeMgr->ReleaseContext(ec); } -ExchangeMessageDispatch * ExchangeContext::GetMessageDispatch() -{ - if (mDelegate != nullptr) - { - return mDelegate->GetMessageDispatch(mExchangeMgr->GetReliableMessageMgr(), mExchangeMgr->GetSessionMgr()); - } - - return nullptr; -} - ExchangeContext::ExchangeContext(ExchangeManager * em, uint16_t ExchangeId, SecureSessionHandle session, bool Initiator, - ExchangeDelegateBase * delegate) + ExchangeDelegate * delegate) { VerifyOrDie(mExchangeMgr == nullptr); @@ -238,6 +219,22 @@ ExchangeContext::ExchangeContext(ExchangeManager * em, uint16_t ExchangeId, Secu mFlags.Set(Flags::kFlagInitiator, Initiator); mDelegate = delegate; + ExchangeMessageDispatch * dispatch = nullptr; + if (delegate != nullptr) + { + dispatch = delegate->GetMessageDispatch(em->GetReliableMessageMgr(), em->GetSessionMgr()); + if (dispatch == nullptr) + { + dispatch = &em->mDefaultExchangeDispatch; + } + } + else + { + dispatch = &em->mDefaultExchangeDispatch; + } + VerifyOrDie(dispatch != nullptr); + mDispatch = dispatch->Retain(); + SetDropAckDebug(false); SetAckPending(false); SetPeerRequestedAck(false); @@ -267,6 +264,12 @@ ExchangeContext::~ExchangeContext() mExchangeACL = nullptr; } + if (mDispatch != nullptr) + { + mDispatch->Release(); + mDispatch = nullptr; + } + #if defined(CHIP_EXCHANGE_CONTEXT_DETAIL_LOGGING) ChipLogDetail(ExchangeManager, "ec-- id: %d", mExchangeId); #endif @@ -331,7 +334,7 @@ void ExchangeContext::HandleResponseTimeout(System::Layer * aSystemLayer, void * // NOTE: we don't set mResponseExpected to false here because the response could still arrive. If the user // wants to never receive the response, they must close the exchange context. - ExchangeDelegateBase * delegate = ec->GetDelegate(); + ExchangeDelegate * delegate = ec->GetDelegate(); // Call the user's timeout handler. if (delegate != nullptr) @@ -347,17 +350,8 @@ CHIP_ERROR ExchangeContext::HandleMessage(const PacketHeader & packetHeader, con // layer has completed its work on the ExchangeContext. Retain(); - ExchangeMessageDispatch * dispatch = GetMessageDispatch(); - ApplicationExchangeDispatch defaultDispatch; - - if (dispatch == nullptr) - { - defaultDispatch.Init(mExchangeMgr->GetReliableMessageMgr(), mExchangeMgr->GetSessionMgr()); - dispatch = &defaultDispatch; - } - CHIP_ERROR err = - dispatch->OnMessageReceived(payloadHeader, packetHeader.GetMessageId(), peerAddress, GetReliableMessageContext()); + mDispatch->OnMessageReceived(payloadHeader, packetHeader.GetMessageId(), peerAddress, GetReliableMessageContext()); SuccessOrExit(err); // The SecureChannel::StandaloneAck message type is only used for CRMP; do not pass such messages to the application layer. diff --git a/src/messaging/ExchangeContext.h b/src/messaging/ExchangeContext.h index 0eea79b3ad2e16..c2aa6ea944b62a 100644 --- a/src/messaging/ExchangeContext.h +++ b/src/messaging/ExchangeContext.h @@ -63,7 +63,7 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen typedef uint32_t Timeout; // Type used to express the timeout in this ExchangeContext, in milliseconds ExchangeContext(ExchangeManager * em, uint16_t ExchangeId, SecureSessionHandle session, bool Initiator, - ExchangeDelegateBase * delegate); + ExchangeDelegate * delegate); ~ExchangeContext(); @@ -128,14 +128,14 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen CHIP_ERROR HandleMessage(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msgBuf); - ExchangeDelegateBase * GetDelegate() const { return mDelegate; } - void SetDelegate(ExchangeDelegateBase * delegate) { mDelegate = delegate; } + ExchangeDelegate * GetDelegate() const { return mDelegate; } + void SetDelegate(ExchangeDelegate * delegate) { mDelegate = delegate; } ExchangeManager * GetExchangeMgr() const { return mExchangeMgr; } ReliableMessageContext * GetReliableMessageContext() { return static_cast(this); }; - ExchangeMessageDispatch * GetMessageDispatch(); + ExchangeMessageDispatch * GetMessageDispatch() { return mDispatch; } ExchangeACL * GetExchangeACL(Transport::AdminPairingTable & table) { @@ -167,9 +167,11 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen private: Timeout mResponseTimeout; // Maximum time to wait for response (in milliseconds); 0 disables response timeout. - ExchangeDelegateBase * mDelegate = nullptr; - ExchangeManager * mExchangeMgr = nullptr; - ExchangeACL * mExchangeACL = nullptr; + ExchangeDelegate * mDelegate = nullptr; + ExchangeManager * mExchangeMgr = nullptr; + ExchangeACL * mExchangeACL = nullptr; + + ExchangeMessageDispatch * mDispatch = nullptr; SecureSessionHandle mSecureSession; // The connection state uint16_t mExchangeId; // Assigned exchange ID. diff --git a/src/messaging/ExchangeDelegate.h b/src/messaging/ExchangeDelegate.h index cc9d0ccf8b9498..7656e83c460de1 100644 --- a/src/messaging/ExchangeDelegate.h +++ b/src/messaging/ExchangeDelegate.h @@ -42,10 +42,10 @@ class ExchangeContext; * is interested in receiving these callbacks, they can specialize this class and handle * each trigger in their implementation of this class. */ -class DLL_EXPORT ExchangeDelegateBase +class DLL_EXPORT ExchangeDelegate { public: - virtual ~ExchangeDelegateBase() {} + virtual ~ExchangeDelegate() {} /** * @brief @@ -77,22 +77,10 @@ class DLL_EXPORT ExchangeDelegateBase */ virtual void OnExchangeClosing(ExchangeContext * ec) {} - virtual ExchangeMessageDispatch * GetMessageDispatch(ReliableMessageMgr * rmMgr, SecureSessionMgr * sessionMgr) = 0; -}; - -class DLL_EXPORT ExchangeDelegate : public ExchangeDelegateBase -{ -public: - virtual ~ExchangeDelegate() {} - - virtual ExchangeMessageDispatch * GetMessageDispatch(ReliableMessageMgr * rmMgr, SecureSessionMgr * sessionMgr) + virtual ExchangeMessageDispatch * GetMessageDispatch(ReliableMessageMgr * reliableMessageMgr, SecureSessionMgr * sessionMgr) { - mMessageDispatch.Init(rmMgr, sessionMgr); - return &mMessageDispatch; + return nullptr; } - -private: - ApplicationExchangeDispatch mMessageDispatch; }; } // namespace Messaging diff --git a/src/messaging/ExchangeMessageDispatch.h b/src/messaging/ExchangeMessageDispatch.h index 95fd256294b562..57179710d84017 100644 --- a/src/messaging/ExchangeMessageDispatch.h +++ b/src/messaging/ExchangeMessageDispatch.h @@ -23,6 +23,7 @@ #pragma once +#include #include namespace chip { @@ -31,7 +32,7 @@ namespace Messaging { class ReliableMessageMgr; class ReliableMessageContext; -class ExchangeMessageDispatch +class ExchangeMessageDispatch : public ReferenceCounted { public: ExchangeMessageDispatch() {} diff --git a/src/messaging/ExchangeMgr.cpp b/src/messaging/ExchangeMgr.cpp index ebf4a50033a069..1215751347af48 100644 --- a/src/messaging/ExchangeMgr.cpp +++ b/src/messaging/ExchangeMgr.cpp @@ -86,6 +86,7 @@ CHIP_ERROR ExchangeManager::Init(SecureSessionMgr * sessionMgr) sessionMgr->SetDelegate(this); mReliableMessageMgr.Init(sessionMgr->SystemLayer(), sessionMgr); + ReturnErrorOnFailure(mDefaultExchangeDispatch.Init(&mReliableMessageMgr, mSessionMgr)); mState = State::kState_Initialized; @@ -113,18 +114,18 @@ CHIP_ERROR ExchangeManager::Shutdown() return CHIP_NO_ERROR; } -ExchangeContext * ExchangeManager::NewContext(SecureSessionHandle session, ExchangeDelegateBase * delegate) +ExchangeContext * ExchangeManager::NewContext(SecureSessionHandle session, ExchangeDelegate * delegate) { return mContextPool.CreateObject(this, mNextExchangeId++, session, true, delegate); } -CHIP_ERROR ExchangeManager::RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegateBase * delegate) +CHIP_ERROR ExchangeManager::RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegate * delegate) { return RegisterUMH(protocolId, kAnyMessageType, delegate); } CHIP_ERROR ExchangeManager::RegisterUnsolicitedMessageHandlerForType(Protocols::Id protocolId, uint8_t msgType, - ExchangeDelegateBase * delegate) + ExchangeDelegate * delegate) { return RegisterUMH(protocolId, static_cast(msgType), delegate); } @@ -144,7 +145,7 @@ void ExchangeManager::OnReceiveError(CHIP_ERROR error, const Transport::PeerAddr ChipLogError(ExchangeManager, "Accept FAILED, err = %s", ErrorStr(error)); } -CHIP_ERROR ExchangeManager::RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegateBase * delegate) +CHIP_ERROR ExchangeManager::RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegate * delegate) { UnsolicitedMessageHandler * selected = nullptr; @@ -337,7 +338,7 @@ void ExchangeManager::OnMessageReceived(const Transport::PeerAddress & source, S } } -void ExchangeManager::CloseAllContextsForDelegate(const ExchangeDelegateBase * delegate) +void ExchangeManager::CloseAllContextsForDelegate(const ExchangeDelegate * delegate) { mContextPool.ForEachActiveObject([&](auto * ec) { if (ec->GetDelegate() == delegate) diff --git a/src/messaging/ExchangeMgr.h b/src/messaging/ExchangeMgr.h index 03c496b7a01fdb..25ede5e4901836 100644 --- a/src/messaging/ExchangeMgr.h +++ b/src/messaging/ExchangeMgr.h @@ -39,7 +39,7 @@ namespace chip { namespace Messaging { class ExchangeContext; -class ExchangeDelegateBase; +class ExchangeDelegate; static constexpr int16_t kAnyMessageType = -1; @@ -99,7 +99,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans * @return A pointer to the created ExchangeContext object On success. Otherwise NULL if no object * can be allocated or is available. */ - ExchangeContext * NewContext(SecureSessionHandle session, ExchangeDelegateBase * delegate); + ExchangeContext * NewContext(SecureSessionHandle session, ExchangeDelegate * delegate); void ReleaseContext(ExchangeContext * ec) { mContextPool.ReleaseObject(ec); } @@ -115,7 +115,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans * is full and a new one cannot be allocated. * @retval #CHIP_NO_ERROR On success. */ - CHIP_ERROR RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegateBase * delegate); + CHIP_ERROR RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegate * delegate); /** * Register an unsolicited message handler for a given protocol identifier and message type. @@ -130,13 +130,13 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans * is full and a new one cannot be allocated. * @retval #CHIP_NO_ERROR On success. */ - CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(Protocols::Id protocolId, uint8_t msgType, ExchangeDelegateBase * delegate); + CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(Protocols::Id protocolId, uint8_t msgType, ExchangeDelegate * delegate); /** * A strongly-message-typed version of RegisterUnsolicitedMessageHandlerForType. */ template ::value>> - CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(MessageType msgType, ExchangeDelegateBase * delegate) + CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(MessageType msgType, ExchangeDelegate * delegate) { static_assert(std::is_same, uint8_t>::value, "Enum is wrong size; cast is not safe"); return RegisterUnsolicitedMessageHandlerForType(Protocols::MessageTypeTraits::ProtocolId(), @@ -183,7 +183,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans * their delegate. To be used if the delegate is being destroyed. This * method will guarantee that it does not call into the delegate. */ - void CloseAllContextsForDelegate(const ExchangeDelegateBase * delegate); + void CloseAllContextsForDelegate(const ExchangeDelegate * delegate); void SetDelegate(ExchangeMgrDelegate * delegate) { mDelegate = delegate; } @@ -214,7 +214,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans return ProtocolId == aProtocolId && MessageType == aMessageType; } - ExchangeDelegateBase * Delegate; + ExchangeDelegate * Delegate; Protocols::Id ProtocolId; // Message types are normally 8-bit unsigned ints, but we use // kAnyMessageType, which is negative, to represent a wildcard handler, @@ -231,13 +231,15 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans SecureSessionMgr * mSessionMgr; ReliableMessageMgr mReliableMessageMgr; + ApplicationExchangeDispatch mDefaultExchangeDispatch; + Transport::AdminId mAdminId = 0; BitMapObjectPool mContextPool; UnsolicitedMessageHandler UMHandlerPool[CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS]; - CHIP_ERROR RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegateBase * delegate); + CHIP_ERROR RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegate * delegate); CHIP_ERROR UnregisterUMH(Protocols::Id protocolId, int16_t msgType); void OnReceiveError(CHIP_ERROR error, const Transport::PeerAddress & source, SecureSessionMgr * msgLayer) override; diff --git a/src/messaging/tests/MessagingContext.cpp b/src/messaging/tests/MessagingContext.cpp index 3e67f870ce42ad..fdd44dfdf0225c 100644 --- a/src/messaging/tests/MessagingContext.cpp +++ b/src/messaging/tests/MessagingContext.cpp @@ -67,13 +67,13 @@ SecureSessionHandle MessagingContext::GetSessionPeerToLocal() return { GetSourceNodeId(), GetLocalKeyId(), GetAdminId() }; } -Messaging::ExchangeContext * MessagingContext::NewExchangeToPeer(Messaging::ExchangeDelegateBase * delegate) +Messaging::ExchangeContext * MessagingContext::NewExchangeToPeer(Messaging::ExchangeDelegate * delegate) { // TODO: temprary create a SecureSessionHandle from node id, will be fix in PR 3602 return mExchangeManager.NewContext(GetSessionLocalToPeer(), delegate); } -Messaging::ExchangeContext * MessagingContext::NewExchangeToLocal(Messaging::ExchangeDelegateBase * delegate) +Messaging::ExchangeContext * MessagingContext::NewExchangeToLocal(Messaging::ExchangeDelegate * delegate) { // TODO: temprary create a SecureSessionHandle from node id, will be fix in PR 3602 return mExchangeManager.NewContext(GetSessionPeerToLocal(), delegate); diff --git a/src/messaging/tests/MessagingContext.h b/src/messaging/tests/MessagingContext.h index 13acc1185c36fc..79a5286ae17f1c 100644 --- a/src/messaging/tests/MessagingContext.h +++ b/src/messaging/tests/MessagingContext.h @@ -79,8 +79,8 @@ class MessagingContext : public IOContext SecureSessionHandle GetSessionLocalToPeer(); SecureSessionHandle GetSessionPeerToLocal(); - Messaging::ExchangeContext * NewExchangeToPeer(Messaging::ExchangeDelegateBase * delegate); - Messaging::ExchangeContext * NewExchangeToLocal(Messaging::ExchangeDelegateBase * delegate); + Messaging::ExchangeContext * NewExchangeToPeer(Messaging::ExchangeDelegate * delegate); + Messaging::ExchangeContext * NewExchangeToLocal(Messaging::ExchangeDelegate * delegate); Credentials::OperationalCredentialSet & GetOperationalCredentialSet() { return mOperationalCredentialSet; } diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index b45dfd2c8173aa..818335e3c46fa3 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -161,7 +161,7 @@ void CheckFailRetrans(nlTestSuite * inSuite, void * inContext) exchange->Close(); } -void CheckResendMessage(nlTestSuite * inSuite, void * inContext) +void CheckResendApplicationMessage(nlTestSuite * inSuite, void * inContext) { TestContext & ctx = *reinterpret_cast(inContext); @@ -170,13 +170,10 @@ void CheckResendMessage(nlTestSuite * inSuite, void * inContext) chip::System::PacketBufferHandle buffer = chip::MessagePacketBuffer::NewWithData(PAYLOAD, sizeof(PAYLOAD)); NL_TEST_ASSERT(inSuite, !buffer.IsNull()); - IPAddress addr; - IPAddress::FromString("127.0.0.1", addr); - CHIP_ERROR err = CHIP_NO_ERROR; MockAppDelegate mockSender; - // TODO: temprary create a SecureSessionHandle from node id, will be fix in PR 3602 + // TODO: temporarily create a SecureSessionHandle from node id, will be fix in PR 3602 ExchangeContext * exchange = ctx.NewExchangeToPeer(&mockSender); NL_TEST_ASSERT(inSuite, exchange != nullptr); @@ -190,11 +187,18 @@ void CheckResendMessage(nlTestSuite * inSuite, void * inContext) 1, // CHIP_CONFIG_RMP_DEFAULT_ACTIVE_RETRY_INTERVAL }); - gLoopback.mSendMessageCount = 0; - gLoopback.mNumMessagesToDrop = 2; + // Let's drop the initial message + gLoopback.mSendMessageCount = 0; + gLoopback.mNumMessagesToDrop = 2; + gLoopback.mDroppedMessageCount = 0; + + // Ensure the retransmit table is empty right now + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); err = exchange->SendMessage(Echo::MsgType::EchoRequest, std::move(buffer)); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + // Ensure the message was dropped, and was added to retransmit table NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 1); NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 1); NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1); @@ -202,6 +206,8 @@ void CheckResendMessage(nlTestSuite * inSuite, void * inContext) // 1 tick is 64 ms, sleep 65 ms to trigger first re-transmit test_os_sleep_ms(65); ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_SYSTEM_NO_ERROR); + + // Ensure the retransmit message was dropped, and is still there in the retransmit table NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 2); NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 0); NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2); @@ -210,15 +216,79 @@ void CheckResendMessage(nlTestSuite * inSuite, void * inContext) // sleep another 65 ms to trigger second re-transmit test_os_sleep_ms(65); ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_SYSTEM_NO_ERROR); + + // Ensure the retransmit message was NOT dropped, and the retransmit table is empty, as we should have gotten an ack NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount >= 3); NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2); + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); + + rm->ClearRetransTable(rc); + exchange->Close(); +} + +void CheckCloseExchangeAndResendApplicationMessage(nlTestSuite * inSuite, void * inContext) +{ + TestContext & ctx = *reinterpret_cast(inContext); + + ctx.GetInetLayer().SystemLayer()->Init(nullptr); - ChipLogProgress(ExchangeManager, "Checking that retransmit table is empty now"); + chip::System::PacketBufferHandle buffer = chip::MessagePacketBuffer::NewWithData(PAYLOAD, sizeof(PAYLOAD)); + NL_TEST_ASSERT(inSuite, !buffer.IsNull()); + + CHIP_ERROR err = CHIP_NO_ERROR; + + MockAppDelegate mockSender; + // TODO: temporarily create a SecureSessionHandle from node id, will be fixed in PR 3602 + ExchangeContext * exchange = ctx.NewExchangeToPeer(&mockSender); + NL_TEST_ASSERT(inSuite, exchange != nullptr); + + ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr(); + ReliableMessageContext * rc = exchange->GetReliableMessageContext(); + NL_TEST_ASSERT(inSuite, rm != nullptr); + NL_TEST_ASSERT(inSuite, rc != nullptr); + + rc->SetConfig({ + 1, // CHIP_CONFIG_RMP_DEFAULT_INITIAL_RETRY_INTERVAL + 1, // CHIP_CONFIG_RMP_DEFAULT_ACTIVE_RETRY_INTERVAL + }); + + // Let's drop the initial message + gLoopback.mSendMessageCount = 0; + gLoopback.mNumMessagesToDrop = 2; + gLoopback.mDroppedMessageCount = 0; + + // Ensure the retransmit table is empty right now + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); + + err = exchange->SendMessage(Echo::MsgType::EchoRequest, std::move(buffer)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + exchange->Close(); + + // Ensure the message was dropped, and was added to retransmit table + NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 1); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 1); + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1); + + // 1 tick is 64 ms, sleep 65 ms to trigger first re-transmit test_os_sleep_ms(65); + ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_SYSTEM_NO_ERROR); + + // Ensure the retransmit message was dropped, and is still there in the retransmit table + NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount == 2); + NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 0); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2); + NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1); + + // sleep another 65 ms to trigger second re-transmit + test_os_sleep_ms(65); + ReliableMessageMgr::Timeout(&ctx.GetSystemLayer(), rm, CHIP_SYSTEM_NO_ERROR); + + // Ensure the retransmit message was NOT dropped, and the retransmit table is empty, as we should have gotten an ack + NL_TEST_ASSERT(inSuite, gLoopback.mSendMessageCount >= 3); + NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2); NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); rm->ClearRetransTable(rc); - exchange->Close(); } void CheckSendStandaloneAckMessage(nlTestSuite * inSuite, void * inContext) @@ -251,7 +321,8 @@ const nlTest sTests[] = { NL_TEST_DEF("Test ReliableMessageMgr::CheckAddClearRetrans", CheckAddClearRetrans), NL_TEST_DEF("Test ReliableMessageMgr::CheckFailRetrans", CheckFailRetrans), - NL_TEST_DEF("Test ReliableMessageMgr::CheckResendMessage", CheckResendMessage), + NL_TEST_DEF("Test ReliableMessageMgr::CheckResendApplicationMessage", CheckResendApplicationMessage), + NL_TEST_DEF("Test ReliableMessageMgr::CheckCloseExchangeAndResendApplicationMessage", CheckCloseExchangeAndResendApplicationMessage), NL_TEST_DEF("Test ReliableMessageMgr::CheckSendStandaloneAckMessage", CheckSendStandaloneAckMessage), NL_TEST_SENTINEL() diff --git a/src/protocols/secure_channel/CASEServer.cpp b/src/protocols/secure_channel/CASEServer.cpp index a6d9104fb11b7c..25adca9cbf1794 100644 --- a/src/protocols/secure_channel/CASEServer.cpp +++ b/src/protocols/secure_channel/CASEServer.cpp @@ -43,7 +43,7 @@ CHIP_ERROR CASEServer::ListenForSessionEstablishment(Messaging::ExchangeManager ReturnErrorOnFailure(mPairingSession.MessageDispatch().Init(transportMgr)); - ExchangeDelegateBase * delegate = this; + ExchangeDelegate * delegate = this; ReturnErrorOnFailure( mExchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::CASE_SigmaR1, delegate)); return CHIP_NO_ERROR; diff --git a/src/protocols/secure_channel/CASEServer.h b/src/protocols/secure_channel/CASEServer.h index 30b14feeb5bc5c..5a976b79558525 100644 --- a/src/protocols/secure_channel/CASEServer.h +++ b/src/protocols/secure_channel/CASEServer.h @@ -23,7 +23,7 @@ namespace chip { -class CASEServer : public SessionEstablishmentDelegate, public Messaging::ExchangeDelegateBase +class CASEServer : public SessionEstablishmentDelegate, public Messaging::ExchangeDelegate { public: CASEServer() {} diff --git a/src/protocols/secure_channel/CASESession.h b/src/protocols/secure_channel/CASESession.h index 19e684c1395ee2..47afdddb0e49dd 100644 --- a/src/protocols/secure_channel/CASESession.h +++ b/src/protocols/secure_channel/CASESession.h @@ -71,7 +71,7 @@ struct CASESessionSerializable uint16_t mPeerKeyId; }; -class DLL_EXPORT CASESession : public Messaging::ExchangeDelegateBase, public PairingSession +class DLL_EXPORT CASESession : public Messaging::ExchangeDelegate, public PairingSession { public: CASESession(); diff --git a/src/protocols/secure_channel/PASESession.h b/src/protocols/secure_channel/PASESession.h index bc814f0424b183..fc925a32f30acf 100644 --- a/src/protocols/secure_channel/PASESession.h +++ b/src/protocols/secure_channel/PASESession.h @@ -68,7 +68,7 @@ struct PASESessionSerializable typedef uint8_t PASEVerifier[2][kSpake2p_WS_Length]; -class DLL_EXPORT PASESession : public Messaging::ExchangeDelegateBase, public PairingSession +class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public PairingSession { public: PASESession();