From 11545327dabdbbe7b5cb131ec8401042b6180162 Mon Sep 17 00:00:00 2001 From: Zang MingJie Date: Thu, 9 Dec 2021 21:57:33 +0800 Subject: [PATCH] Remove src/channel (#12780) --- BUILD.gn | 1 - src/channel/BUILD.gn | 38 --- src/channel/Channel.cpp | 49 --- src/channel/Channel.h | 201 ------------- src/channel/ChannelContext.cpp | 376 ------------------------ src/channel/ChannelContext.h | 195 ------------ src/channel/Manager.cpp | 56 ---- src/channel/Manager.h | 83 ------ src/channel/tests/BUILD.gn | 42 --- src/channel/tests/TestChannel.cpp | 195 ------------ src/channel/tests/TestChannelDriver.cpp | 35 --- 11 files changed, 1271 deletions(-) delete mode 100644 src/channel/BUILD.gn delete mode 100644 src/channel/Channel.cpp delete mode 100644 src/channel/Channel.h delete mode 100644 src/channel/ChannelContext.cpp delete mode 100644 src/channel/ChannelContext.h delete mode 100644 src/channel/Manager.cpp delete mode 100644 src/channel/Manager.h delete mode 100644 src/channel/tests/BUILD.gn delete mode 100644 src/channel/tests/TestChannel.cpp delete mode 100644 src/channel/tests/TestChannelDriver.cpp diff --git a/BUILD.gn b/BUILD.gn index fb27e5768c16c5..d3a9503530e5a0 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -65,7 +65,6 @@ if (current_toolchain != "${dir_pw_toolchain}/default:default") { deps = [ "${chip_root}/src/app", "${chip_root}/src/ble", - "${chip_root}/src/channel", "${chip_root}/src/controller", "${chip_root}/src/credentials", "${chip_root}/src/crypto", diff --git a/src/channel/BUILD.gn b/src/channel/BUILD.gn deleted file mode 100644 index 84ece73135c323..00000000000000 --- a/src/channel/BUILD.gn +++ /dev/null @@ -1,38 +0,0 @@ -# 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. - -import("//build_overrides/chip.gni") - -static_library("channel") { - output_name = "libChannelAPI" - - sources = [ - "Channel.cpp", - "Channel.h", - "ChannelContext.cpp", - "ChannelContext.h", - "Manager.cpp", - "Manager.h", - ] - - cflags = [ "-Wconversion" ] - - public_deps = [ - "${chip_root}/src/lib/core", - "${chip_root}/src/lib/support", - "${chip_root}/src/messaging", - "${chip_root}/src/platform", - "${chip_root}/src/protocols/secure_channel", - ] -} diff --git a/src/channel/Channel.cpp b/src/channel/Channel.cpp deleted file mode 100644 index 5995d686756230..00000000000000 --- a/src/channel/Channel.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * - * 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. - */ - -#include -#include -#include -#include - -namespace chip { -namespace Messaging { - -ChannelState ChannelHandle::GetState() const -{ - if (mAssociation == nullptr) - return ChannelState::kNone; - return mAssociation->mChannelContext->GetState(); -} - -ExchangeContext * ChannelHandle::NewExchange(ExchangeDelegate * delegate) -{ - VerifyOrDie(mAssociation != nullptr); - return mAssociation->mChannelContext->NewExchange(delegate); -} - -void ChannelHandle::Release() -{ - if (mAssociation == nullptr) - return; - - mAssociation->mChannelContext->mChannelManager->ReleaseChannelHandle(mAssociation); - mAssociation = nullptr; -} - -} // namespace Messaging -} // namespace chip diff --git a/src/channel/Channel.h b/src/channel/Channel.h deleted file mode 100644 index 310fd4aa998f5e..00000000000000 --- a/src/channel/Channel.h +++ /dev/null @@ -1,201 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file defines the API classes for to CHIP Channel. - * - * Channel is a object to abstract all low layer dependencies of an - * exchange, including secure session, transport connection and network - * status. - * - * Channel is not connection. Channel can abstract both connection - * oriented and connectionless transports, It contains information to send - * and receive messages via exchanges. For example, when using - * connectionless transport, channel will contain peer address and session - * key; when using connection oriented transport, channel will contain - * connection handle and session key. - * - * Channel is not session. Session do persistent through cold reboot, but - * channel doesn't. Applications must re-establish channels after a cold - * reboot. - * - * Because channel is a local concept, peer device is not able aware of - * channel establishment events. Instead, peer device is able to aware - * session establishment events, connection establishment events for - * connection oriented transport and message received events for - * connectionless transport. - */ - -#pragma once - -#include -#include - -namespace chip { -namespace Messaging { - -/** - * @brief - * The ChannelBuilder object provides information to build a Channel. - * - * ChannelBuilder can be used by application to provide information to - * request a channel to a peer. When a channel is requested via - * ExchangeManager::EstablishChannel, a ChannelHandle will be return to - * represent the state of the channel - * - * The ChannelBuilder object should be short-live and it is only used within - * ExchangeManager::EstablishChannel call, after the call its state will be - * copied into an internal object represented by ChannelHandle, then the - * ChannelBuilder can be safely released. - */ -class ChannelBuilder -{ -public: - enum class TransportPreference - { - kConnectionless, - kPreferConnectionOriented, // will fallback to connectionless if TCP is not supported - kConnectionOriented, // will fail if TCP is not supported - - kDefault = kConnectionless, - }; - - ChannelBuilder & SetPeerNodeId(NodeId peerNodeId) - { - mPeerNodeId = peerNodeId; - return *this; - } - NodeId GetPeerNodeId() const { return mPeerNodeId; } - - ChannelBuilder & SetTransportPreference(TransportPreference preference) - { - mTransportPreference = preference; - return *this; - } - TransportPreference GetTransportPreference() const { return mTransportPreference; } - - uint16_t GetPeerSessionId() const { return mCaseParameters.mPeerSessionId; } - ChannelBuilder & SetPeerSessionId(uint16_t sessionId) - { - mCaseParameters.mPeerSessionId = sessionId; - return *this; - } - - Optional GetForcePeerAddress() const { return mForcePeerAddr; } - ChannelBuilder & SetForcePeerAddress(Inet::IPAddress peerAddr) - { - mForcePeerAddr.SetValue(peerAddr); - return *this; - } - -private: - NodeId mPeerNodeId = kUndefinedNodeId; - TransportPreference mTransportPreference = TransportPreference::kDefault; - struct - { - uint16_t mPeerSessionId; - } mCaseParameters; - - Optional mForcePeerAddr; -}; - -class ExchangeContext; -class ExchangeDelegate; - -enum class ChannelState -{ - kNone, - kPreparing, - kReady, - kClosed, - kFailed, -}; - -class ChannelContextHandleAssociation; - -/** - * @brief - * ChannelHandle is a reference to a channel. An active ChannelHandle will - * keep the channel available and ready for use, such that a message can be - * sent immediately to the peer. - * - * The ChannelHandle controls the lifespan of the channel. When the handle - * is released, the channel will be flagged as pending close, and if there - * is no active exchange which is using the channel, the channel will be - * closed. - * - * The ChannelHandle will track channel status, and notify applications - * when the channel state changes via ChannelDelegate. - */ -class ChannelHandle -{ -public: - explicit ChannelHandle(ChannelContextHandleAssociation * association = nullptr) : mAssociation(association) {} - ~ChannelHandle() { Release(); } - - // non copyable - ChannelHandle(const ChannelHandle &) = delete; - ChannelHandle & operator=(const ChannelHandle &) = delete; - - // movable - ChannelHandle(ChannelHandle && that) - { - Release(); - this->mAssociation = that.mAssociation; - that.mAssociation = nullptr; - } - ChannelHandle & operator=(ChannelHandle && that) - { - Release(); - this->mAssociation = that.mAssociation; - that.mAssociation = nullptr; - return *this; - } - - ChannelState GetState() const; - - /* - * @brief - * Create a new exchange on the channel. - * - * @pre GetState() == ChannelState::kReady - */ - ExchangeContext * NewExchange(ExchangeDelegate * delegate); - - void Release(); - -private: - ChannelContextHandleAssociation * mAssociation; -}; - -/** - * @brief - * Callback receiver interface of channels - */ -class ChannelDelegate -{ -public: - virtual ~ChannelDelegate() {} - - virtual void OnEstablished() = 0; - virtual void OnClosed() = 0; - virtual void OnFail(CHIP_ERROR err) = 0; -}; - -} // namespace Messaging -} // namespace chip diff --git a/src/channel/ChannelContext.cpp b/src/channel/ChannelContext.cpp deleted file mode 100644 index 8455dfef95841f..00000000000000 --- a/src/channel/ChannelContext.cpp +++ /dev/null @@ -1,376 +0,0 @@ -/* - * - * 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. - */ - -#include -#include -#include -#include - -namespace chip { -namespace Messaging { - -void ChannelContextDeletor::Release(ChannelContext * context) -{ - context->mChannelManager->ReleaseChannelContext(context); -} - -void ChannelContext::Start(const ChannelBuilder & builder) -{ - if (mState != ChannelState::kNone) - return; - EnterPreparingState(builder); -} - -ExchangeContext * ChannelContext::NewExchange(ExchangeDelegate * delegate) -{ - VerifyOrDie(GetState() == ChannelState::kReady); - return mExchangeManager->NewContext(GetReadyVars().mSession, delegate); -} - -bool ChannelContext::MatchNodeId(NodeId nodeId) -{ - switch (mState) - { - case ChannelState::kPreparing: - return nodeId == GetPrepareVars().mBuilder.GetPeerNodeId(); - case ChannelState::kReady: { - auto state = mExchangeManager->GetSessionManager()->GetSecureSession(GetReadyVars().mSession); - if (state == nullptr) - return false; - return nodeId == state->GetPeerNodeId(); - } - default: - return false; - } -} - -bool ChannelContext::MatchTransport(Transport::Type transport) -{ - switch (mState) - { - case ChannelState::kPreparing: - switch (GetPrepareVars().mBuilder.GetTransportPreference()) - { - case ChannelBuilder::TransportPreference::kPreferConnectionOriented: - case ChannelBuilder::TransportPreference::kConnectionOriented: - return transport == Transport::Type::kTcp; - case ChannelBuilder::TransportPreference::kConnectionless: - return transport == Transport::Type::kUdp; - } - return false; - case ChannelState::kReady: { - auto state = mExchangeManager->GetSessionManager()->GetSecureSession(GetReadyVars().mSession); - if (state == nullptr) - return false; - return transport == state->GetPeerAddress().GetTransportType(); - } - default: - return false; - } -} - -bool ChannelContext::MatchTransportPreference(ChannelBuilder::TransportPreference transport) -{ - switch (transport) - { - case ChannelBuilder::TransportPreference::kPreferConnectionOriented: - case ChannelBuilder::TransportPreference::kConnectionOriented: - return MatchTransport(Transport::Type::kTcp); - case ChannelBuilder::TransportPreference::kConnectionless: - return MatchTransport(Transport::Type::kUdp); - } - return false; -} - -bool ChannelContext::MatchCaseParameters() -{ - // TODO: not supported yet, should compare CASE parameters here, and return false if doesn't match - return true; -} - -bool ChannelContext::MatchesBuilder(const ChannelBuilder & builder) -{ - // Channel is identified by {node id, {case key id}, network interface, tcp/udp} - // PASE is not supported yet - // Network interface is not supported yet - // Channel is reused if builder parameters are matching an existing channel - if (!MatchNodeId(builder.GetPeerNodeId())) - return false; - if (!MatchCaseParameters()) - return false; - - return MatchTransportPreference(builder.GetTransportPreference()); -} - -bool ChannelContext::IsCasePairing() -{ - return mState == ChannelState::kPreparing && GetPrepareVars().mState == PrepareState::kCasePairing; -} - -bool ChannelContext::MatchesSession(SessionHandle session, SessionManager * sessionManager) -{ - switch (mState) - { - case ChannelState::kPreparing: { - switch (GetPrepareVars().mState) - { - case PrepareState::kCasePairing: { - auto state = sessionManager->GetSecureSession(session); - return (state->GetPeerNodeId() == GetPrepareVars().mBuilder.GetPeerNodeId() && - state->GetPeerSessionId() == GetPrepareVars().mBuilder.GetPeerSessionId()); - } - default: - return false; - } - } - case ChannelState::kReady: - return GetReadyVars().mSession == session; - default: - return false; - } -} - -void ChannelContext::EnterPreparingState(const ChannelBuilder & builder) -{ - mState = ChannelState::kPreparing; - - mStateVars.Set(); - GetPrepareVars().mBuilder = builder; - - EnterAddressResolve(); -} - -void ChannelContext::ExitPreparingState() {} - -// Address resolve -void ChannelContext::EnterAddressResolve() -{ - GetPrepareVars().mState = PrepareState::kAddressResolving; - - // Skip address resolve if the address is provided - { - auto addr = GetPrepareVars().mBuilder.GetForcePeerAddress(); - if (addr.HasValue()) - { - GetPrepareVars().mAddress = addr.Value(); - ExitAddressResolve(); - // Only CASE session is supported - EnterCasePairingState(); - return; - } - } - - // TODO: call mDNS Scanner::SubscribeNode after PR #4459 is ready - // Scanner::RegisterScannerDelegate(this) - // Scanner::SubscribeNode(GetPrepareVars().mBuilder.GetPeerNodeId()) - - // The HandleNodeIdResolve may already have been called, recheck the state here before set up the timer - if (mState == ChannelState::kPreparing && GetPrepareVars().mState == PrepareState::kAddressResolving) - { - System::Layer * layer = mExchangeManager->GetSessionManager()->SystemLayer(); - layer->StartTimer(System::Clock::Milliseconds32(CHIP_CONFIG_NODE_ADDRESS_RESOLVE_TIMEOUT_MSECS), AddressResolveTimeout, - this); - Retain(); // Keep the pointer in the timer - } -} - -void ChannelContext::AddressResolveTimeout(System::Layer * aLayer, void * aAppState) -{ - ChannelContext * me = static_cast(aAppState); - me->AddressResolveTimeout(); - me->Release(); -} - -void ChannelContext::AddressResolveTimeout() -{ - if (mState != ChannelState::kPreparing) - return; - if (GetPrepareVars().mState != PrepareState::kAddressResolving) - return; - - ExitAddressResolve(); - ExitPreparingState(); - EnterFailedState(CHIP_ERROR_PEER_NODE_NOT_FOUND); -} - -void ChannelContext::HandleNodeIdResolve(CHIP_ERROR error, uint64_t nodeId, const Dnssd::DnssdService & address) -{ - switch (mState) - { - case ChannelState::kReady: { - if (error != CHIP_NO_ERROR) - { - // Ignore mDNS fail in ready state - return; - } - - // TODO: adjust peer address, secure session manager is not able to change peer address. - return; - } - case ChannelState::kPreparing: { - switch (GetPrepareVars().mState) - { - case PrepareState::kAddressResolving: { - if (error != CHIP_NO_ERROR) - { - ExitAddressResolve(); - ExitPreparingState(); - EnterFailedState(error); - return; - } - - if (!address.mAddress.HasValue()) - return; - GetPrepareVars().mAddressType = address.mAddressType; - GetPrepareVars().mAddress = address.mAddress.Value(); - ExitAddressResolve(); - EnterCasePairingState(); - return; - } - case PrepareState::kCasePairing: - case PrepareState::kCasePairingDone: - return; - } - return; - } - case ChannelState::kNone: - case ChannelState::kClosed: - case ChannelState::kFailed: - return; - } -} - -void ChannelContext::EnterCasePairingState() -{ - auto & prepare = GetPrepareVars(); - prepare.mCasePairingSession = Platform::New(); - - // TODO: currently only supports IP/UDP paring - Transport::PeerAddress addr; - addr.SetTransportType(Transport::Type::kUdp).SetIPAddress(prepare.mAddress); - - auto session = mExchangeManager->GetSessionManager()->CreateUnauthenticatedSession(addr, gDefaultMRPConfig); - if (!session.HasValue()) - { - ExitCasePairingState(); - ExitPreparingState(); - EnterFailedState(CHIP_ERROR_NO_MEMORY); - return; - } - - ExchangeContext * ctxt = mExchangeManager->NewContext(session.Value(), prepare.mCasePairingSession); - VerifyOrReturn(ctxt != nullptr); - - FabricInfo * fabric = mFabricsTable->FindFabricWithIndex(mFabricIndex); - VerifyOrReturn(fabric != nullptr); - CHIP_ERROR err = prepare.mCasePairingSession->EstablishSession(addr, fabric, prepare.mBuilder.GetPeerNodeId(), - mExchangeManager->GetNextKeyId(), ctxt, this); - if (err != CHIP_NO_ERROR) - { - ExitCasePairingState(); - ExitPreparingState(); - EnterFailedState(err); - } -} - -void ChannelContext::ExitCasePairingState() -{ - Platform::Delete(GetPrepareVars().mCasePairingSession); -} - -void ChannelContext::OnSessionEstablishmentError(CHIP_ERROR error) -{ - if (mState != ChannelState::kPreparing) - return; - switch (GetPrepareVars().mState) - { - case PrepareState::kCasePairing: - ExitCasePairingState(); - ExitPreparingState(); - EnterFailedState(error); - return; - default: - return; - } -} - -void ChannelContext::OnSessionEstablished() -{ - if (mState != ChannelState::kPreparing) - return; - switch (GetPrepareVars().mState) - { - case PrepareState::kCasePairing: - ExitCasePairingState(); - GetPrepareVars().mState = PrepareState::kCasePairingDone; - // TODO: current CASE paring session API doesn't show how to derive a secure session - return; - default: - return; - } -} - -void ChannelContext::OnNewConnection(SessionHandle session) -{ - if (mState != ChannelState::kPreparing) - return; - if (GetPrepareVars().mState != PrepareState::kCasePairingDone) - return; - - ExitPreparingState(); - EnterReadyState(session); -} - -void ChannelContext::EnterReadyState(SessionHandle session) -{ - mState = ChannelState::kReady; - mStateVars.Set(session); - mChannelManager->NotifyChannelEvent(this, [](ChannelDelegate * delegate) { delegate->OnEstablished(); }); -} - -void ChannelContext::OnConnectionExpired(SessionHandle session) -{ - if (mState != ChannelState::kReady) - return; - - ExitReadyState(); - EnterClosedState(); -} - -void ChannelContext::ExitReadyState() -{ - // TODO: close sesure session - // Currently SecureSessionManager doesn't provide an interface to close a session - - // TODO: call mDNS Scanner::UnubscribeNode after PR #4459 is ready - // Scanner::UnsubscribeNode(GetPrepareVars().mBuilder.GetPeerNodeId()) -} - -void ChannelContext::EnterFailedState(CHIP_ERROR error) -{ - mState = ChannelState::kFailed; - mChannelManager->NotifyChannelEvent(this, [error](ChannelDelegate * delegate) { delegate->OnFail(error); }); -} - -void ChannelContext::EnterClosedState() -{ - mState = ChannelState::kClosed; - mChannelManager->NotifyChannelEvent(this, [](ChannelDelegate * delegate) { delegate->OnClosed(); }); -} - -} // namespace Messaging -} // namespace chip diff --git a/src/channel/ChannelContext.h b/src/channel/ChannelContext.h deleted file mode 100644 index d98ae6fb68edf1..00000000000000 --- a/src/channel/ChannelContext.h +++ /dev/null @@ -1,195 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file defines the internal classes used by CHIP Channel. - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace chip { -namespace Messaging { - -class ExchangeManager; -class ChannelContext; -class ChannelManager; - -class ChannelContextDeletor -{ -public: - static void Release(ChannelContext * context); -}; - -/** - * @brief - * The object of the class holds all state of a channel. It is a state machine, with following states: - * - * N: None, the initial state - * P: Preparing - * R: Ready, the channel is ready to use - * C: Closed, the channel is closed - * F: Failed, the channel is failed - * - * +---+ +---+ +---+ +---+ - * | N |-->| P |-->| R |-->| C | - * +---+ +---+ +---+ +---+ - * | | - * | | +---+ - * +-------+---->| F | - * +---+ - * - * Note: The state never goes back, when a channel is failed, it can't be reset or fixed. The application must create a - * new channel to replace the failed channel - * - * Preparing Substates: - * A: AddressResolving, use mDNS to resolve the node address - * C: CasePairing, do SIGMA key exchange - * CD: CasePairingDone, wait for OnNewConnection from SecureSessionManager - * - * /---\ +---+ +---+ +----+ /---\ - * | |-->| A |-->| C |-->| CD |-->| O | - * \---/ +---+ +---+ +----+ \---/ - */ -class ChannelContext : public ReferenceCounted, public SessionEstablishmentDelegate -{ -public: - ChannelContext(ExchangeManager * exchangeManager, ChannelManager * channelManager) : - mState(ChannelState::kNone), mExchangeManager(exchangeManager), mChannelManager(channelManager), mFabricsTable(nullptr), - mFabricIndex(kUndefinedFabricIndex) - {} - - void Start(const ChannelBuilder & builder); - - /* - * @brief - * Create a new exchange on the channel. - * - * @pre GetState() == ChannelState::kReady - */ - ExchangeContext * NewExchange(ExchangeDelegate * delegate); - - ChannelState GetState() const { return mState; } - - bool MatchNodeId(NodeId nodeId); - bool MatchTransport(Transport::Type transport); - bool MatchTransportPreference(ChannelBuilder::TransportPreference transport); - bool MatchCaseParameters(); - - bool IsCasePairing(); - - bool MatchesBuilder(const ChannelBuilder & builder); - bool MatchesSession(SessionHandle session, SessionManager * sessionManager); - - // events of ResolveDelegate, propagated from ExchangeManager - void HandleNodeIdResolve(CHIP_ERROR error, uint64_t nodeId, const Dnssd::DnssdService & address); - - // events of SecureSessionManager, propagated from ExchangeManager - void OnNewConnection(SessionHandle session); - void OnConnectionExpired(SessionHandle session); - - // Pairing callbacks - void OnSessionEstablishmentError(CHIP_ERROR error) override; - void OnSessionEstablished() override; - -private: - friend class ChannelContextDeletor; - friend class ChannelHandle; - - ChannelState mState; - ExchangeManager * mExchangeManager; - ChannelManager * mChannelManager; - FabricTable * mFabricsTable; - FabricIndex mFabricIndex; - - enum class PrepareState - { - kAddressResolving, - kCasePairing, - kCasePairingDone, - }; - - // mPreparing is pretty big, consider move it outside - struct PrepareVars - { - PrepareState mState; - Inet::IPAddressType mAddressType; - Inet::IPAddress mAddress; - CASESession * mCasePairingSession; - ChannelBuilder mBuilder; - }; - - struct ReadyVars - { - ReadyVars(SessionHandle session) : mSession(session) {} - const SessionHandle mSession; - }; - - Variant mStateVars; - - PrepareVars & GetPrepareVars() { return mStateVars.Get(); } - ReadyVars & GetReadyVars() { return mStateVars.Get(); } - - // State machine functions - void EnterPreparingState(const ChannelBuilder & builder); - void ExitPreparingState(); - - void EnterReadyState(SessionHandle session); - void ExitReadyState(); - - void EnterFailedState(CHIP_ERROR error); - void EnterClosedState(); - - // Preparing sub-states - void EnterAddressResolve(); - static void AddressResolveTimeout(System::Layer * aLayer, void * aAppState); - void AddressResolveTimeout(); - void ExitAddressResolve() {} - - void EnterCasePairingState(); - void ExitCasePairingState(); -}; - -class ChannelContextHandleAssociation -{ -public: - ChannelContextHandleAssociation(ChannelContext * channelContext, ChannelDelegate * channelDelegate) : - mChannelContext(channelContext), mChannelDelegate(channelDelegate) - { - mChannelContext->Retain(); - } - ~ChannelContextHandleAssociation() { mChannelContext->Release(); } - -private: - friend class ChannelManager; - friend class ChannelHandle; - ChannelContext * mChannelContext; - ChannelDelegate * mChannelDelegate; -}; - -} // namespace Messaging -} // namespace chip diff --git a/src/channel/Manager.cpp b/src/channel/Manager.cpp deleted file mode 100644 index 8cba97bd83b405..00000000000000 --- a/src/channel/Manager.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * - * 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. - */ - -#include - -namespace chip { -namespace Messaging { - -ChannelHandle ChannelManager::EstablishChannel(const ChannelBuilder & builder, ChannelDelegate * delegate) -{ - ChannelContext * channelContext = nullptr; - - // Find an existing Channel matching the builder - mChannelContexts.ForEachActiveObject([&](ChannelContext * context) { - if (context->MatchesBuilder(builder)) - { - channelContext = context; - return Loop::Break; - } - return Loop::Continue; - }); - - if (channelContext == nullptr) - { - // create a new channel if not found - channelContext = mChannelContexts.CreateObject(mExchangeManager, this); - if (channelContext == nullptr) - return ChannelHandle{ nullptr }; - channelContext->Start(builder); - } - else - { - channelContext->Retain(); - } - - ChannelContextHandleAssociation * association = mChannelHandles.CreateObject(channelContext, delegate); - channelContext->Release(); - return ChannelHandle{ association }; -} - -} // namespace Messaging -} // namespace chip diff --git a/src/channel/Manager.h b/src/channel/Manager.h deleted file mode 100644 index d44e6df872d93a..00000000000000 --- a/src/channel/Manager.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * - * 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. - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include - -namespace chip { -namespace Messaging { - -class ChannelContext; - -/** - * @brief - * This class is used to manage Channel Contexts with other CHIP nodes. - */ -class DLL_EXPORT ChannelManager : public SessionCreationDelegate -{ -public: - ChannelManager(ExchangeManager * exchangeManager) : mExchangeManager(exchangeManager) - { - exchangeManager->GetSessionManager()->RegisterCreationDelegate(*this); - } - ChannelManager(const ChannelManager &) = delete; - ChannelManager operator=(const ChannelManager &) = delete; - - ChannelHandle EstablishChannel(const ChannelBuilder & builder, ChannelDelegate * delegate); - - // Internal APIs used for channel - void ReleaseChannelContext(ChannelContext * channel) { mChannelContexts.ReleaseObject(channel); } - - void ReleaseChannelHandle(ChannelContextHandleAssociation * association) { mChannelHandles.ReleaseObject(association); } - - template - void NotifyChannelEvent(ChannelContext * channel, Event event) - { - mChannelHandles.ForEachActiveObject([&](ChannelContextHandleAssociation * association) { - if (association->mChannelContext == channel) - event(association->mChannelDelegate); - return Loop::Continue; - }); - } - - void OnNewSession(SessionHandle session) override - { - mChannelContexts.ForEachActiveObject([&](ChannelContext * context) { - if (context->MatchesSession(session, mExchangeManager->GetSessionManager())) - { - context->OnNewConnection(session); - return Loop::Break; - } - return Loop::Continue; - }); - } - -private: - BitMapObjectPool mChannelContexts; - BitMapObjectPool mChannelHandles; - ExchangeManager * mExchangeManager; -}; - -} // namespace Messaging -} // namespace chip diff --git a/src/channel/tests/BUILD.gn b/src/channel/tests/BUILD.gn deleted file mode 100644 index 1c76824bae93cf..00000000000000 --- a/src/channel/tests/BUILD.gn +++ /dev/null @@ -1,42 +0,0 @@ -# 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. - -import("//build_overrides/build.gni") -import("//build_overrides/chip.gni") -import("//build_overrides/nlio.gni") -import("//build_overrides/nlunit_test.gni") - -import("${chip_root}/build/chip/chip_test_suite.gni") - -chip_test_suite("tests") { - output_name = "libChannelAPITests" - - sources = [ "TestChannel.cpp" ] - - cflags = [ "-Wconversion" ] - - public_deps = [ - "${chip_root}/src/inet/tests:helpers", - "${chip_root}/src/lib/core", - "${chip_root}/src/lib/support", - "${chip_root}/src/messaging", - "${chip_root}/src/protocols", - "${chip_root}/src/transport", - "${chip_root}/src/transport/raw/tests:helpers", - "${nlio_root}:nlio", - "${nlunit_test_root}:nlunit-test", - ] - - tests = [ "TestChannel" ] -} diff --git a/src/channel/tests/TestChannel.cpp b/src/channel/tests/TestChannel.cpp deleted file mode 100644 index d3a8df5af96d9e..00000000000000 --- a/src/channel/tests/TestChannel.cpp +++ /dev/null @@ -1,195 +0,0 @@ -/* - * - * Copyright (c) 2021 Project CHIP Authors - * All rights reserved. - * - * 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. - */ - -/** - * @file - * This file implements unit tests for the ExchangeManager implementation. - */ - -#include "TestMessagingLayer.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -namespace { - -using namespace chip; -using namespace chip::Inet; -using namespace chip::Transport; -using namespace chip::Messaging; - -using TestContext = chip::Test::MessagingContext; - -TestContext sContext; - -TransportMgr gTransportMgr; -chip::Test::IOContext gIOContext; - -class MockAppDelegate : public ExchangeDelegate -{ -public: - CHIP_ERROR OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, - System::PacketBufferHandle && buffer) override - { - IsOnMessageReceivedCalled = true; - return CHIP_NO_ERROR; - } - - void OnResponseTimeout(ExchangeContext * ec) override {} - - bool IsOnMessageReceivedCalled = false; -}; - -class MockChannelDelegate : public ChannelDelegate -{ -public: - ~MockChannelDelegate() override {} - - void OnEstablished() override {} - void OnClosed() override {} - void OnFail(CHIP_ERROR err) override {} -}; - -void CheckExchangeChannels(nlTestSuite * inSuite, void * inContext) -{ - TestContext & ctx = *reinterpret_cast(inContext); - - // create unsolicited exchange - MockAppDelegate mockUnsolicitedAppDelegate; - CHIP_ERROR err = ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Protocols::Id(VendorId::Common, 0x0001), - 0x0001, &mockUnsolicitedAppDelegate); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - - // create the channel - ChannelBuilder channelBuilder; - channelBuilder.SetPeerNodeId(ctx.GetDestinationNodeId()) - .SetForcePeerAddress(ctx.GetAddress()) - .SetOperationalCredentialSet(&ctx.GetOperationalCredentialSet()); - MockChannelDelegate channelDelegate; - auto channelHandle = ctx.GetExchangeManager().EstablishChannel(channelBuilder, &channelDelegate); - return; - -#if 0 - // TODO: complete test when CASESession is completed - // wait for channel establishment - ctx.DriveIOUntil(1000, [&] { return channelHandle.GetState() == ChannelState::kReady; }); - NL_TEST_ASSERT(inSuite, channelHandle.GetState() == ChannelState::kReady); - - MockAppDelegate mockAppDelegate; - ExchangeContext * ec1 = channelHandle.NewExchange(&mockAppDelegate); - - // send a malicious packet - ec1->SendMessage(0x0001, 0x0002, System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize)); - NL_TEST_ASSERT(inSuite, !mockUnsolicitedAppDelegate.IsOnMessageReceivedCalled); - - // Need to sort out what this test should really be testing and how; sending - // two messages in a row on an exchange is not something that really - // happens. - - // send a good packet - ec1->SendMessage(0x0001, 0x0001, System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize)); - NL_TEST_ASSERT(inSuite, mockUnsolicitedAppDelegate.IsOnMessageReceivedCalled); - - channelHandle.Release(); -#endif -} - -// Test Suite - -/** - * Test Suite that lists all the test functions. - */ -// clang-format off -const nlTest sTests[] = -{ - NL_TEST_DEF("Test Channel/Exchange", CheckExchangeChannels), - - NL_TEST_SENTINEL() -}; -// clang-format on - -int Initialize(void * aContext); -int Finalize(void * aContext); - -// clang-format off -nlTestSuite sSuite = -{ - "Test-CHIP-ExchangeManager", - &sTests[0], - Initialize, - Finalize -}; -// clang-format on - -/** - * Initialize the test suite. - */ -int Initialize(void * aContext) -{ - // Initialize System memory and resources - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gTransportMgr.Init("LOOPBACK") == CHIP_NO_ERROR, FAILURE); - - auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportMgr, &gIOContext) == CHIP_NO_ERROR, FAILURE); - - return CHIP_NO_ERROR; -} - -/** - * Finalize the test suite. - */ -int Finalize(void * aContext) -{ - CHIP_ERROR err = reinterpret_cast(aContext)->Shutdown(); - gIOContext.Shutdown(); - chip::Platform::MemoryShutdown(); - return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; -} - -} // namespace - -/** - * Main - */ -int TestChannel() -{ - // Run test suit against one context - nlTestRunner(&sSuite, &sContext); - - return (nlTestRunnerStats(&sSuite)); -} - -CHIP_REGISTER_TEST_SUITE(TestChannel) diff --git a/src/channel/tests/TestChannelDriver.cpp b/src/channel/tests/TestChannelDriver.cpp deleted file mode 100644 index 24eb3cb7f9e902..00000000000000 --- a/src/channel/tests/TestChannelDriver.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file implements a standalone/native program executable - * test driver for the CHIP core library CHIP ExchangeManager tests. - * - */ - -#include - -int TestChannel(); - -int main() -{ - // Generate machine-readable, comma-separated value (CSV) output. - nlTestSetOutputStyle(OUTPUT_CSV); - - return (TestChannel()); -}