diff --git a/examples/all-clusters-app/ameba/chip_main.cmake b/examples/all-clusters-app/ameba/chip_main.cmake index 9cd9ce4725913b..fa03acfbd1673c 100755 --- a/examples/all-clusters-app/ameba/chip_main.cmake +++ b/examples/all-clusters-app/ameba/chip_main.cmake @@ -129,6 +129,7 @@ list( ${chip_dir}/examples/all-clusters-app/ameba/main/CHIPDeviceManager.cpp ${chip_dir}/examples/all-clusters-app/ameba/main/Globals.cpp ${chip_dir}/examples/all-clusters-app/ameba/main/LEDWidget.cpp + ${chip_dir}/examples/all-clusters-app/ameba/main/DsoHack.cpp ) add_library( diff --git a/examples/all-clusters-app/ameba/main/DsoHack.cpp b/examples/all-clusters-app/ameba/main/DsoHack.cpp new file mode 100644 index 00000000000000..6fc5d7a63307c5 --- /dev/null +++ b/examples/all-clusters-app/ameba/main/DsoHack.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// This hack is needed because Ameba SDK is not linking against libstdc++ correctly. +extern "C" { +void * __dso_handle = 0; +} diff --git a/examples/all-clusters-app/ameba/main/chipinterface.cpp b/examples/all-clusters-app/ameba/main/chipinterface.cpp index c2f2fcec38388a..c5d0071dd12ade 100644 --- a/examples/all-clusters-app/ameba/main/chipinterface.cpp +++ b/examples/all-clusters-app/ameba/main/chipinterface.cpp @@ -40,10 +40,6 @@ #include "Rpc.h" #endif -extern "C" { -void * __dso_handle = 0; -} - using namespace ::chip; using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; diff --git a/examples/lighting-app/ameba/chip_main.cmake b/examples/lighting-app/ameba/chip_main.cmake index f10e7a2ccf9de9..38b08742a0b5fe 100755 --- a/examples/lighting-app/ameba/chip_main.cmake +++ b/examples/lighting-app/ameba/chip_main.cmake @@ -25,6 +25,7 @@ list( ${chip_dir}/examples/lighting-app/ameba/main/CHIPDeviceManager.cpp ${chip_dir}/examples/lighting-app/ameba/main/Globals.cpp ${chip_dir}/examples/lighting-app/ameba/main/LEDWidget.cpp + ${chip_dir}/examples/lighting-app/ameba/main/DsoHack.cpp ) add_library( diff --git a/examples/lighting-app/ameba/main/DsoHack.cpp b/examples/lighting-app/ameba/main/DsoHack.cpp new file mode 100644 index 00000000000000..6fc5d7a63307c5 --- /dev/null +++ b/examples/lighting-app/ameba/main/DsoHack.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// This hack is needed because Ameba SDK is not linking against libstdc++ correctly. +extern "C" { +void * __dso_handle = 0; +} diff --git a/examples/lighting-app/ameba/main/chipinterface.cpp b/examples/lighting-app/ameba/main/chipinterface.cpp index 9300f960b487fb..6c9536c23fd2c9 100755 --- a/examples/lighting-app/ameba/main/chipinterface.cpp +++ b/examples/lighting-app/ameba/main/chipinterface.cpp @@ -38,10 +38,6 @@ #include -extern "C" { -void * __dso_handle = 0; -} - using namespace ::chip; using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; diff --git a/src/app/CASEClient.cpp b/src/app/CASEClient.cpp index 7fc9248a9378b2..9e05560030e194 100644 --- a/src/app/CASEClient.cpp +++ b/src/app/CASEClient.cpp @@ -38,8 +38,6 @@ CHIP_ERROR CASEClient::EstablishSession(PeerId peer, const Transport::PeerAddres Messaging::ExchangeContext * exchange = mInitParams.exchangeMgr->NewContext(session.Value(), &mCASESession); VerifyOrReturnError(exchange != nullptr, CHIP_ERROR_INTERNAL); - ReturnErrorOnFailure(mCASESession.MessageDispatch().Init(mInitParams.sessionManager)); - uint16_t keyID = 0; ReturnErrorOnFailure(mInitParams.idAllocator->Allocate(keyID)); diff --git a/src/app/server/CommissioningWindowManager.cpp b/src/app/server/CommissioningWindowManager.cpp index 9c5c4f9f04b125..d5d519581d568a 100644 --- a/src/app/server/CommissioningWindowManager.cpp +++ b/src/app/server/CommissioningWindowManager.cpp @@ -161,7 +161,6 @@ CHIP_ERROR CommissioningWindowManager::OpenCommissioningWindow() ReturnErrorOnFailure(mIDAllocator->Allocate(keyID)); mPairingSession.Clear(); - ReturnErrorOnFailure(mPairingSession.MessageDispatch().Init(&mServer->GetSecureSessionManager())); if (mCommissioningTimeoutSeconds != kNoCommissioningTimeout) { diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index c7dda147f81b0a..4111fcd3e04b3c 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -816,9 +816,6 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re device->Init(GetControllerDeviceInitParams(), remoteDeviceId, peerAddress, fabric->GetFabricIndex()); - err = device->GetPairing().MessageDispatch().Init(mSystemState->SessionMgr()); - SuccessOrExit(err); - if (params.GetPeerAddress().GetTransportType() != Transport::Type::kBle) { device->SetAddress(params.GetPeerAddress().GetIPAddress()); diff --git a/src/messaging/ApplicationExchangeDispatch.cpp b/src/messaging/ApplicationExchangeDispatch.cpp index 584a3c8603e57d..3e2ad293b7d34b 100644 --- a/src/messaging/ApplicationExchangeDispatch.cpp +++ b/src/messaging/ApplicationExchangeDispatch.cpp @@ -26,19 +26,6 @@ namespace chip { namespace Messaging { -CHIP_ERROR ApplicationExchangeDispatch::PrepareMessage(const SessionHandle & session, PayloadHeader & payloadHeader, - System::PacketBufferHandle && message, - EncryptedPacketBufferHandle & preparedMessage) -{ - return mSessionManager->PrepareMessage(session, payloadHeader, std::move(message), preparedMessage); -} - -CHIP_ERROR ApplicationExchangeDispatch::SendPreparedMessage(const SessionHandle & session, - const EncryptedPacketBufferHandle & preparedMessage) const -{ - return mSessionManager->SendPreparedMessage(session, preparedMessage); -} - bool ApplicationExchangeDispatch::MessagePermitted(uint16_t protocol, uint8_t type) { // TODO: Change this check to only include the protocol and message types that are allowed diff --git a/src/messaging/ApplicationExchangeDispatch.h b/src/messaging/ApplicationExchangeDispatch.h index 86b27936237b5f..d1fb76aa019ec6 100644 --- a/src/messaging/ApplicationExchangeDispatch.h +++ b/src/messaging/ApplicationExchangeDispatch.h @@ -26,7 +26,6 @@ #include #include -#include namespace chip { namespace Messaging { @@ -34,28 +33,17 @@ namespace Messaging { class ApplicationExchangeDispatch : public ExchangeMessageDispatch { public: - ApplicationExchangeDispatch() {} - - virtual ~ApplicationExchangeDispatch() {} - - CHIP_ERROR Init(SessionManager * sessionManager) + static ExchangeMessageDispatch & Instance() { - ReturnErrorCodeIf(sessionManager == nullptr, CHIP_ERROR_INVALID_ARGUMENT); - mSessionManager = sessionManager; - return ExchangeMessageDispatch::Init(); + static ApplicationExchangeDispatch instance; + return instance; } - CHIP_ERROR PrepareMessage(const SessionHandle & session, PayloadHeader & payloadHeader, System::PacketBufferHandle && message, - EncryptedPacketBufferHandle & preparedMessage) override; - CHIP_ERROR SendPreparedMessage(const SessionHandle & session, const EncryptedPacketBufferHandle & message) const override; - - SessionManager * GetSessionManager() const { return mSessionManager; } + ApplicationExchangeDispatch() {} + virtual ~ApplicationExchangeDispatch() {} protected: bool MessagePermitted(uint16_t protocol, uint8_t type) override; - -private: - SessionManager * mSessionManager = nullptr; }; } // namespace Messaging diff --git a/src/messaging/ExchangeContext.cpp b/src/messaging/ExchangeContext.cpp index b51980389dfce7..5dc287c1438aa5 100644 --- a/src/messaging/ExchangeContext.cpp +++ b/src/messaging/ExchangeContext.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -161,8 +162,9 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp } // Create a new scope for `err`, to avoid shadowing warning previous `err`. - CHIP_ERROR err = mDispatch->SendMessage(mSession.Get(), mExchangeId, IsInitiator(), GetReliableMessageContext(), - reliableTransmissionRequested, protocolId, msgType, std::move(msgBuf)); + CHIP_ERROR err = mDispatch.SendMessage(GetExchangeMgr()->GetSessionManager(), mSession.Get(), mExchangeId, IsInitiator(), + GetReliableMessageContext(), reliableTransmissionRequested, protocolId, msgType, + std::move(msgBuf)); if (err != CHIP_NO_ERROR && IsResponseExpected()) { CancelResponseTimer(); @@ -249,7 +251,8 @@ void ExchangeContextDeletor::Release(ExchangeContext * ec) } ExchangeContext::ExchangeContext(ExchangeManager * em, uint16_t ExchangeId, const SessionHandle & session, bool Initiator, - ExchangeDelegate * delegate) + ExchangeDelegate * delegate) : + mDispatch((delegate != nullptr) ? delegate->GetMessageDispatch() : ApplicationExchangeDispatch::Instance()) { VerifyOrDie(mExchangeMgr == nullptr); @@ -259,22 +262,6 @@ ExchangeContext::ExchangeContext(ExchangeManager * em, uint16_t ExchangeId, cons mFlags.Set(Flags::kFlagInitiator, Initiator); mDelegate = delegate; - ExchangeMessageDispatch * dispatch = nullptr; - if (delegate != nullptr) - { - dispatch = delegate->GetMessageDispatch(em->GetReliableMessageMgr(), em->GetSessionManager()); - if (dispatch == nullptr) - { - dispatch = &em->mDefaultExchangeDispatch; - } - } - else - { - dispatch = &em->mDefaultExchangeDispatch; - } - VerifyOrDie(dispatch != nullptr); - mDispatch = dispatch->Retain(); - SetDropAckDebug(false); SetAckPending(false); SetMsgRcvdFromPeer(false); @@ -300,12 +287,6 @@ ExchangeContext::~ExchangeContext() DoClose(false); mExchangeMgr = nullptr; - if (mDispatch != nullptr) - { - mDispatch->Release(); - mDispatch = nullptr; - } - #if defined(CHIP_EXCHANGE_CONTEXT_DETAIL_LOGGING) ChipLogDetail(ExchangeManager, "ec-- id: " ChipLogFormatExchange, ChipLogValueExchange(this)); #endif @@ -451,7 +432,7 @@ CHIP_ERROR ExchangeContext::HandleMessage(uint32_t messageCounter, const Payload if (!IsGroupExchangeContext()) { ReturnErrorOnFailure( - mDispatch->OnMessageReceived(messageCounter, payloadHeader, peerAddress, msgFlags, GetReliableMessageContext())); + mDispatch.OnMessageReceived(messageCounter, payloadHeader, peerAddress, msgFlags, GetReliableMessageContext())); } if (IsAckPending() && !mDelegate) diff --git a/src/messaging/ExchangeContext.h b/src/messaging/ExchangeContext.h index b4507ebd0f3f1a..57c704ce12185e 100644 --- a/src/messaging/ExchangeContext.h +++ b/src/messaging/ExchangeContext.h @@ -75,7 +75,7 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen */ bool IsInitiator() const; - bool IsEncryptionRequired() const { return mDispatch->IsEncryptionRequired(); } + bool IsEncryptionRequired() const { return mDispatch.IsEncryptionRequired(); } bool IsGroupExchangeContext() const { return (mSession && mSession.Get().IsGroupSession()); } @@ -150,7 +150,7 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen ReliableMessageContext * GetReliableMessageContext() { return static_cast(this); }; - ExchangeMessageDispatch * GetMessageDispatch() { return mDispatch; } + ExchangeMessageDispatch & GetMessageDispatch() { return mDispatch; } SessionHandle GetSessionHandle() const { return mSession.Get(); } bool HasSessionHandle() const { return mSession; } @@ -184,7 +184,7 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen ExchangeDelegate * mDelegate = nullptr; ExchangeManager * mExchangeMgr = nullptr; - ExchangeMessageDispatch * mDispatch = nullptr; + ExchangeMessageDispatch & mDispatch; SessionHolder mSession; // The connection state uint16_t mExchangeId; // Assigned exchange ID. diff --git a/src/messaging/ExchangeDelegate.h b/src/messaging/ExchangeDelegate.h index 80566d850770ec..3e578ef48032a0 100644 --- a/src/messaging/ExchangeDelegate.h +++ b/src/messaging/ExchangeDelegate.h @@ -97,10 +97,7 @@ class DLL_EXPORT ExchangeDelegate */ virtual void OnExchangeClosing(ExchangeContext * ec) {} - virtual ExchangeMessageDispatch * GetMessageDispatch(ReliableMessageMgr * reliableMessageMgr, SessionManager * sessionManager) - { - return nullptr; - } + virtual ExchangeMessageDispatch & GetMessageDispatch() { return ApplicationExchangeDispatch::Instance(); } }; } // namespace Messaging diff --git a/src/messaging/ExchangeMessageDispatch.cpp b/src/messaging/ExchangeMessageDispatch.cpp index 8720e42f709088..7eb4bc7ad4076a 100644 --- a/src/messaging/ExchangeMessageDispatch.cpp +++ b/src/messaging/ExchangeMessageDispatch.cpp @@ -41,9 +41,10 @@ namespace chip { namespace Messaging { -CHIP_ERROR ExchangeMessageDispatch::SendMessage(const SessionHandle & session, uint16_t exchangeId, bool isInitiator, - ReliableMessageContext * reliableMessageContext, bool isReliableTransmission, - Protocols::Id protocol, uint8_t type, System::PacketBufferHandle && message) +CHIP_ERROR ExchangeMessageDispatch::SendMessage(SessionManager * sessionManager, const SessionHandle & session, uint16_t exchangeId, + bool isInitiator, ReliableMessageContext * reliableMessageContext, + bool isReliableTransmission, Protocols::Id protocol, uint8_t type, + System::PacketBufferHandle && message) { ReturnErrorCodeIf(!MessagePermitted(protocol.GetProtocolId(), type), CHIP_ERROR_INVALID_ARGUMENT); @@ -82,8 +83,8 @@ CHIP_ERROR ExchangeMessageDispatch::SendMessage(const SessionHandle & session, u }; std::unique_ptr entryOwner(entry, deleter); - ReturnErrorOnFailure(PrepareMessage(session, payloadHeader, std::move(message), entryOwner->retainedBuf)); - CHIP_ERROR err = SendPreparedMessage(session, entryOwner->retainedBuf); + ReturnErrorOnFailure(sessionManager->PrepareMessage(session, payloadHeader, std::move(message), entryOwner->retainedBuf)); + CHIP_ERROR err = sessionManager->SendPreparedMessage(session, entryOwner->retainedBuf); if (err == CHIP_ERROR_POSIX(ENOBUFS)) { // sendmsg on BSD-based systems never blocks, no matter how the @@ -105,8 +106,8 @@ CHIP_ERROR ExchangeMessageDispatch::SendMessage(const SessionHandle & session, u // If the channel itself is providing reliability, let's not request MRP acks payloadHeader.SetNeedsAck(false); EncryptedPacketBufferHandle preparedMessage; - ReturnErrorOnFailure(PrepareMessage(session, payloadHeader, std::move(message), preparedMessage)); - ReturnErrorOnFailure(SendPreparedMessage(session, preparedMessage)); + ReturnErrorOnFailure(sessionManager->PrepareMessage(session, payloadHeader, std::move(message), preparedMessage)); + ReturnErrorOnFailure(sessionManager->SendPreparedMessage(session, preparedMessage)); } return CHIP_NO_ERROR; diff --git a/src/messaging/ExchangeMessageDispatch.h b/src/messaging/ExchangeMessageDispatch.h index 6b8f91daebe819..464387f4e05ff9 100644 --- a/src/messaging/ExchangeMessageDispatch.h +++ b/src/messaging/ExchangeMessageDispatch.h @@ -23,47 +23,28 @@ #pragma once -#include #include #include namespace chip { namespace Messaging { -class ReliableMessageMgr; class ReliableMessageContext; -class ExchangeMessageDispatch : public ReferenceCounted +class ExchangeMessageDispatch { public: ExchangeMessageDispatch() {} virtual ~ExchangeMessageDispatch() {} - CHIP_ERROR Init() { return CHIP_NO_ERROR; } - virtual bool IsEncryptionRequired() const { return true; } - CHIP_ERROR SendMessage(const SessionHandle & session, uint16_t exchangeId, bool isInitiator, + CHIP_ERROR SendMessage(SessionManager * sessionManager, const SessionHandle & session, uint16_t exchangeId, bool isInitiator, ReliableMessageContext * reliableMessageContext, bool isReliableTransmission, Protocols::Id protocol, uint8_t type, System::PacketBufferHandle && message); - - /** - * @brief - * This interface takes the payload and returns the prepared message which can be send multiple times. - * - * @param session Peer node to which the payload to be sent - * @param payloadHeader The payloadHeader to be encoded into the packet - * @param message The payload to be sent - * @param preparedMessage The handle to hold the prepared message - */ - virtual CHIP_ERROR PrepareMessage(const SessionHandle & session, PayloadHeader & payloadHeader, - System::PacketBufferHandle && message, EncryptedPacketBufferHandle & preparedMessage) = 0; - virtual CHIP_ERROR SendPreparedMessage(const SessionHandle & session, - const EncryptedPacketBufferHandle & preparedMessage) const = 0; - - virtual CHIP_ERROR OnMessageReceived(uint32_t messageCounter, const PayloadHeader & payloadHeader, - const Transport::PeerAddress & peerAddress, MessageFlags msgFlags, - ReliableMessageContext * reliableMessageContext); + CHIP_ERROR OnMessageReceived(uint32_t messageCounter, const PayloadHeader & payloadHeader, + const Transport::PeerAddress & peerAddress, MessageFlags msgFlags, + ReliableMessageContext * reliableMessageContext); protected: virtual bool MessagePermitted(uint16_t protocol, uint8_t type) = 0; diff --git a/src/messaging/ExchangeMgr.cpp b/src/messaging/ExchangeMgr.cpp index f81d1054f43b21..41963fe6c1512d 100644 --- a/src/messaging/ExchangeMgr.cpp +++ b/src/messaging/ExchangeMgr.cpp @@ -87,7 +87,6 @@ CHIP_ERROR ExchangeManager::Init(SessionManager * sessionManager) sessionManager->SetMessageDelegate(this); mReliableMessageMgr.Init(sessionManager->SystemLayer()); - ReturnErrorOnFailure(mDefaultExchangeDispatch.Init(mSessionManager)); mState = State::kState_Initialized; diff --git a/src/messaging/ExchangeMgr.h b/src/messaging/ExchangeMgr.h index 4eedcf4c2fc26c..5cd2939e7d7b24 100644 --- a/src/messaging/ExchangeMgr.h +++ b/src/messaging/ExchangeMgr.h @@ -232,8 +232,6 @@ class DLL_EXPORT ExchangeManager : public SessionMessageDelegate, public Session SessionManager * mSessionManager; ReliableMessageMgr mReliableMessageMgr; - ApplicationExchangeDispatch mDefaultExchangeDispatch; - FabricIndex mFabricIndex = 0; BitMapObjectPool mContextPool; diff --git a/src/messaging/ReliableMessageMgr.cpp b/src/messaging/ReliableMessageMgr.cpp index 81f0007a6611d4..bc92ac837abd3e 100644 --- a/src/messaging/ReliableMessageMgr.cpp +++ b/src/messaging/ReliableMessageMgr.cpp @@ -213,8 +213,7 @@ bool ReliableMessageMgr::CheckAndRemRetransTable(ReliableMessageContext * rc, ui CHIP_ERROR ReliableMessageMgr::SendFromRetransTable(RetransTableEntry * entry) { - const ExchangeMessageDispatch * dispatcher = entry->ec->GetMessageDispatch(); - if (dispatcher == nullptr || !entry->ec->HasSessionHandle()) + if (!entry->ec->HasSessionHandle()) { // Using same error message for all errors to reduce code size. ChipLogError(ExchangeManager, @@ -226,7 +225,8 @@ CHIP_ERROR ReliableMessageMgr::SendFromRetransTable(RetransTableEntry * entry) return CHIP_ERROR_INCORRECT_STATE; } - CHIP_ERROR err = dispatcher->SendPreparedMessage(entry->ec->GetSessionHandle(), entry->retainedBuf); + auto * sessionManager = entry->ec->GetExchangeMgr()->GetSessionManager(); + CHIP_ERROR err = sessionManager->SendPreparedMessage(entry->ec->GetSessionHandle(), entry->retainedBuf); if (err == CHIP_NO_ERROR) { diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index 524a5f97ea0993..d78d38fe750521 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -156,10 +156,7 @@ class MockSessionEstablishmentDelegate : public ExchangeDelegate void OnResponseTimeout(ExchangeContext * ec) override {} - virtual ExchangeMessageDispatch * GetMessageDispatch(ReliableMessageMgr * rmMgr, SessionManager * sessionManager) override - { - return &mMessageDispatch; - } + virtual ExchangeMessageDispatch & GetMessageDispatch() override { return mMessageDispatch; } bool IsOnMessageReceivedCalled = false; MockSessionEstablishmentExchangeDispatch mMessageDispatch; @@ -327,9 +324,6 @@ void CheckFailedMessageRetainOnSend(nlTestSuite * inSuite, void * inContext) CHIP_ERROR err = CHIP_NO_ERROR; MockSessionEstablishmentDelegate mockSender; - err = mockSender.mMessageDispatch.Init(&ctx.GetSecureSessionManager()); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - ExchangeContext * exchange = ctx.NewExchangeToAlice(&mockSender); NL_TEST_ASSERT(inSuite, exchange != nullptr); @@ -375,19 +369,13 @@ void CheckUnencryptedMessageReceiveFailure(nlTestSuite * inSuite, void * inConte NL_TEST_ASSERT(inSuite, !buffer.IsNull()); MockSessionEstablishmentDelegate mockReceiver; - CHIP_ERROR err = mockReceiver.mMessageDispatch.Init(&ctx.GetSecureSessionManager()); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - - err = ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Echo::MsgType::EchoRequest, &mockReceiver); + CHIP_ERROR err = ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Echo::MsgType::EchoRequest, &mockReceiver); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); // Expect the received messages to be encrypted mockReceiver.mMessageDispatch.mRequireEncryption = true; MockSessionEstablishmentDelegate mockSender; - err = mockSender.mMessageDispatch.Init(&ctx.GetSecureSessionManager()); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - ExchangeContext * exchange = ctx.NewUnauthenticatedExchangeToAlice(&mockSender); NL_TEST_ASSERT(inSuite, exchange != nullptr); @@ -556,18 +544,12 @@ void CheckResendSessionEstablishmentMessageWithPeerExchange(nlTestSuite * inSuit NL_TEST_ASSERT(inSuite, !buffer.IsNull()); MockSessionEstablishmentDelegate mockReceiver; - err = mockReceiver.mMessageDispatch.Init(&ctx.GetSecureSessionManager()); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - err = ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Echo::MsgType::EchoRequest, &mockReceiver); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); mockReceiver.mTestSuite = inSuite; MockSessionEstablishmentDelegate mockSender; - err = mockSender.mMessageDispatch.Init(&ctx.GetSecureSessionManager()); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - ExchangeContext * exchange = ctx.NewUnauthenticatedExchangeToAlice(&mockSender); NL_TEST_ASSERT(inSuite, exchange != nullptr); diff --git a/src/protocols/secure_channel/CASEServer.cpp b/src/protocols/secure_channel/CASEServer.cpp index cf3cf5cdc0d555..fb524277d602ab 100644 --- a/src/protocols/secure_channel/CASEServer.cpp +++ b/src/protocols/secure_channel/CASEServer.cpp @@ -45,9 +45,6 @@ CHIP_ERROR CASEServer::ListenForSessionEstablishment(Messaging::ExchangeManager mIDAllocator = idAllocator; Cleanup(); - - ReturnErrorOnFailure(GetSession().MessageDispatch().Init(sessionManager)); - return CHIP_NO_ERROR; } diff --git a/src/protocols/secure_channel/CASEServer.h b/src/protocols/secure_channel/CASEServer.h index c2699b2a3f9f25..95fea0b43e9e2e 100644 --- a/src/protocols/secure_channel/CASEServer.h +++ b/src/protocols/secure_channel/CASEServer.h @@ -49,11 +49,7 @@ class CASEServer : public SessionEstablishmentDelegate, public Messaging::Exchan CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override; void OnResponseTimeout(Messaging::ExchangeContext * ec) override {} - Messaging::ExchangeMessageDispatch * GetMessageDispatch(Messaging::ReliableMessageMgr * reliableMessageManager, - SessionManager * sessionManager) override - { - return GetSession().GetMessageDispatch(reliableMessageManager, sessionManager); - } + Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return GetSession().GetMessageDispatch(); } virtual CASESession & GetSession() { return mPairingSession; } diff --git a/src/protocols/secure_channel/CASESession.h b/src/protocols/secure_channel/CASESession.h index 9d967ddf7074fe..b266158c011f4d 100644 --- a/src/protocols/secure_channel/CASESession.h +++ b/src/protocols/secure_channel/CASESession.h @@ -156,17 +156,11 @@ class DLL_EXPORT CASESession : public Messaging::ExchangeDelegate, public Pairin **/ CHIP_ERROR FromCachable(const CASESessionCachable & output); - SessionEstablishmentExchangeDispatch & MessageDispatch() { return mMessageDispatch; } - //// ExchangeDelegate Implementation //// CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override; void OnResponseTimeout(Messaging::ExchangeContext * ec) override; - Messaging::ExchangeMessageDispatch * GetMessageDispatch(Messaging::ReliableMessageMgr * rmMgr, - SessionManager * sessionManager) override - { - return &mMessageDispatch; - } + Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return SessionEstablishmentExchangeDispatch::Instance(); } FabricIndex GetFabricIndex() const { return mFabricInfo != nullptr ? mFabricInfo->GetFabricIndex() : kUndefinedFabricIndex; } @@ -249,7 +243,6 @@ class DLL_EXPORT CASESession : public Messaging::ExchangeDelegate, public Pairin uint8_t mIPK[kIPKSize]; Messaging::ExchangeContext * mExchangeCtxt = nullptr; - SessionEstablishmentExchangeDispatch mMessageDispatch; FabricTable * mFabricsTable = nullptr; FabricInfo * mFabricInfo = nullptr; diff --git a/src/protocols/secure_channel/PASESession.h b/src/protocols/secure_channel/PASESession.h index 48226c8245b092..2be0629dcc9035 100644 --- a/src/protocols/secure_channel/PASESession.h +++ b/src/protocols/secure_channel/PASESession.h @@ -201,8 +201,6 @@ class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public Pairin **/ void Clear(); - SessionEstablishmentExchangeDispatch & MessageDispatch() { return mMessageDispatch; } - //// ExchangeDelegate Implementation //// /** * @brief @@ -229,11 +227,7 @@ class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public Pairin */ void OnResponseTimeout(Messaging::ExchangeContext * ec) override; - Messaging::ExchangeMessageDispatch * GetMessageDispatch(Messaging::ReliableMessageMgr * rmMgr, - SessionManager * sessionManager) override - { - return &mMessageDispatch; - } + Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return SessionEstablishmentExchangeDispatch::Instance(); } private: enum Spake2pErrorType : uint8_t @@ -300,8 +294,6 @@ class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public Pairin Messaging::ExchangeContext * mExchangeCtxt = nullptr; - SessionEstablishmentExchangeDispatch mMessageDispatch; - Optional mLocalMRPConfig; struct Spake2pErrorMsg diff --git a/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.cpp b/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.cpp index a7ded771bc2b6f..240c3ada9dc087 100644 --- a/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.cpp +++ b/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.cpp @@ -28,27 +28,6 @@ namespace chip { using namespace Messaging; -CHIP_ERROR SessionEstablishmentExchangeDispatch::PrepareMessage(const SessionHandle & session, PayloadHeader & payloadHeader, - System::PacketBufferHandle && message, - EncryptedPacketBufferHandle & preparedMessage) -{ - return mSessionManager->PrepareMessage(session, payloadHeader, std::move(message), preparedMessage); -} - -CHIP_ERROR SessionEstablishmentExchangeDispatch::SendPreparedMessage(const SessionHandle & session, - const EncryptedPacketBufferHandle & preparedMessage) const -{ - return mSessionManager->SendPreparedMessage(session, preparedMessage); -} - -CHIP_ERROR SessionEstablishmentExchangeDispatch::OnMessageReceived(uint32_t messageCounter, const PayloadHeader & payloadHeader, - const Transport::PeerAddress & peerAddress, - Messaging::MessageFlags msgFlags, - ReliableMessageContext * reliableMessageContext) -{ - return ExchangeMessageDispatch::OnMessageReceived(messageCounter, payloadHeader, peerAddress, msgFlags, reliableMessageContext); -} - bool SessionEstablishmentExchangeDispatch::MessagePermitted(uint16_t protocol, uint8_t type) { switch (protocol) diff --git a/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.h b/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.h index 0c3344e7a3d840..60a3397033d716 100644 --- a/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.h +++ b/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.h @@ -25,40 +25,24 @@ #pragma once #include -#include namespace chip { class SessionEstablishmentExchangeDispatch : public Messaging::ExchangeMessageDispatch { public: - SessionEstablishmentExchangeDispatch() {} - - virtual ~SessionEstablishmentExchangeDispatch() {} - - CHIP_ERROR Init(SessionManager * sessionManager) + static ExchangeMessageDispatch & Instance() { - ReturnErrorCodeIf(sessionManager == nullptr, CHIP_ERROR_INVALID_ARGUMENT); - mSessionManager = sessionManager; - return ExchangeMessageDispatch::Init(); + static SessionEstablishmentExchangeDispatch instance; + return instance; } - CHIP_ERROR PrepareMessage(const SessionHandle & session, PayloadHeader & payloadHeader, System::PacketBufferHandle && message, - EncryptedPacketBufferHandle & out) override; - CHIP_ERROR SendPreparedMessage(const SessionHandle & session, - const EncryptedPacketBufferHandle & preparedMessage) const override; - - CHIP_ERROR OnMessageReceived(uint32_t messageCounter, const PayloadHeader & payloadHeader, - const Transport::PeerAddress & peerAddress, Messaging::MessageFlags msgFlags, - Messaging::ReliableMessageContext * reliableMessageContext) override; + SessionEstablishmentExchangeDispatch() {} + virtual ~SessionEstablishmentExchangeDispatch() {} protected: bool MessagePermitted(uint16_t protocol, uint8_t type) override; - bool IsEncryptionRequired() const override { return false; } - -private: - SessionManager * mSessionManager = nullptr; }; } // namespace chip diff --git a/src/protocols/secure_channel/tests/TestCASESession.cpp b/src/protocols/secure_channel/tests/TestCASESession.cpp index c832d020b0f954..31ac34baf785ef 100644 --- a/src/protocols/secure_channel/tests/TestCASESession.cpp +++ b/src/protocols/secure_channel/tests/TestCASESession.cpp @@ -173,7 +173,6 @@ void CASE_SecurePairingStartTest(nlTestSuite * inSuite, void * inContext) FabricInfo * fabric = gCommissionerFabrics.FindFabricWithIndex(gCommissionerFabricIndex); NL_TEST_ASSERT(inSuite, fabric != nullptr); - NL_TEST_ASSERT(inSuite, pairing.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); ExchangeContext * context = ctx.NewUnauthenticatedExchangeToBob(&pairing); NL_TEST_ASSERT(inSuite, @@ -201,7 +200,6 @@ void CASE_SecurePairingStartTest(nlTestSuite * inSuite, void * inContext) gLoopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST; CASESession pairing1; - NL_TEST_ASSERT(inSuite, pairing1.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); gLoopback.mSentMessageCount = 0; gLoopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST; @@ -227,8 +225,6 @@ void CASE_SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inConte CASESessionCachable serializableAccessory; gLoopback.mSentMessageCount = 0; - NL_TEST_ASSERT(inSuite, pairingCommissioner.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, pairingAccessory.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::CASE_Sigma1, @@ -365,8 +361,6 @@ void CASE_SecurePairingHandshakeServerTest(nlTestSuite * inSuite, void * inConte TestContext & ctx = *reinterpret_cast(inContext); gLoopback.mSentMessageCount = 0; - NL_TEST_ASSERT(inSuite, pairingCommissioner->MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, gPairingServer.GetSession().MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); SessionIDAllocator idAllocator; @@ -388,8 +382,7 @@ void CASE_SecurePairingHandshakeServerTest(nlTestSuite * inSuite, void * inConte NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 5); NL_TEST_ASSERT(inSuite, delegateCommissioner.mNumPairingComplete == 1); - auto * pairingCommissioner1 = chip::Platform::New(); - NL_TEST_ASSERT(inSuite, pairingCommissioner1->MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); + auto * pairingCommissioner1 = chip::Platform::New(); ExchangeContext * contextCommissioner1 = ctx.NewUnauthenticatedExchangeToBob(pairingCommissioner1); NL_TEST_ASSERT(inSuite, diff --git a/src/protocols/secure_channel/tests/TestPASESession.cpp b/src/protocols/secure_channel/tests/TestPASESession.cpp index 553fa514340d3e..3f50a98f2b5a95 100644 --- a/src/protocols/secure_channel/tests/TestPASESession.cpp +++ b/src/protocols/secure_channel/tests/TestPASESession.cpp @@ -126,7 +126,6 @@ void SecurePairingStartTest(nlTestSuite * inSuite, void * inContext) gLoopback.Reset(); - NL_TEST_ASSERT(inSuite, pairing.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); ExchangeContext * context = ctx.NewUnauthenticatedExchangeToBob(&pairing); NL_TEST_ASSERT(inSuite, @@ -151,7 +150,6 @@ void SecurePairingStartTest(nlTestSuite * inSuite, void * inContext) gLoopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST; PASESession pairing1; - NL_TEST_ASSERT(inSuite, pairing1.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); ExchangeContext * context1 = ctx.NewUnauthenticatedExchangeToBob(&pairing1); NL_TEST_ASSERT(inSuite, pairing1.Pair(Transport::PeerAddress(Transport::Type::kBle), 1234, 0, @@ -174,9 +172,6 @@ void SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inContext, P gLoopback.mSentMessageCount = 0; - NL_TEST_ASSERT(inSuite, pairingCommissioner.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, pairingAccessory.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); - ExchangeContext * contextCommissioner = ctx.NewUnauthenticatedExchangeToBob(&pairingCommissioner); if (gLoopback.mNumMessagesToDrop != 0) @@ -307,9 +302,6 @@ void SecurePairingFailedHandshake(nlTestSuite * inSuite, void * inContext) gLoopback.Reset(); gLoopback.mSentMessageCount = 0; - NL_TEST_ASSERT(inSuite, pairingCommissioner.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, pairingAccessory.MessageDispatch().Init(&ctx.GetSecureSessionManager()) == CHIP_NO_ERROR); - ExchangeContext * contextCommissioner = ctx.NewUnauthenticatedExchangeToBob(&pairingCommissioner); ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr();