3 * Copyright (c) 2021 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * This file defines the internal classes used by CHIP Channel.
27 #include <lib/core/ReferenceCounted.h>
28 #include <lib/mdns/platform/Mdns.h>
29 #include <messaging/Channel.h>
30 #include <transport/CASESession.h>
31 #include <transport/PeerConnectionState.h>
32 #include <transport/SecureSessionMgr.h>
37 class ExchangeManager;
40 class ChannelContextDeletor
43 static void Release(ChannelContext * context);
48 * The object of the class holds all state of a channel. It is a state machine, with following states:
50 * N: None, the initial state
52 * R: Ready, the channel is ready to use
53 * C: Closed, the channel is closed
54 * F: Failed, the channel is failed
56 * +---+ +---+ +---+ +---+
57 * | N |-->| P |-->| R |-->| C |
58 * +---+ +---+ +---+ +---+
64 * Note: The state never goes back, when a channel is failed, it can't be reset or fixed. The application must create a
65 * new channel to replace the failed channel
67 * Preparing Substates:
68 * A: AddressResolving, use mDNS to resolve the node address
69 * C: CasePairing, do SIGMA key exchange
70 * CD: CasePairingDone, wait for OnNewConnection from SecureSessionManager
72 * /---\ +---+ +---+ +----+ /---\
73 * | |-->| A |-->| C |-->| CD |-->| O |
74 * \---/ +---+ +---+ +----+ \---/
76 class ChannelContext : public ReferenceCounted<ChannelContext, ChannelContextDeletor>, public SessionEstablishmentDelegate
79 ChannelContext(ExchangeManager * exchangeManager) : mState(ChannelState::kNone), mExchangeManager(exchangeManager) {}
81 void Start(const ChannelBuilder & builder);
85 * Create a new exchange on the channel.
87 * @pre GetState() == ChannelState::kReady
89 ExchangeContext * NewExchange(ExchangeDelegate * delegate);
91 ChannelState GetState() const { return mState; }
93 bool MatchNodeId(NodeId nodeId);
94 bool MatchTransport(Transport::Type transport);
95 bool MatchTransportPreference(ChannelBuilder::TransportPreference transport);
96 bool MatchCaseParameters();
100 bool MatchesBuilder(const ChannelBuilder & builder);
101 bool MatchesSession(SecureSessionHandle session, SecureSessionMgr * ssm);
103 // events of ResolveDelegate, propagated from ExchangeManager
104 void HandleNodeIdResolve(CHIP_ERROR error, uint64_t nodeId, const Mdns::MdnsService & address);
106 // events of SecureSessionManager, propagated from ExchangeManager
107 void OnNewConnection(SecureSessionHandle session);
108 void OnConnectionExpired(SecureSessionHandle session);
111 CHIP_ERROR HandlePairingMessage(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
112 System::PacketBufferHandle && msg);
113 CHIP_ERROR SendSessionEstablishmentMessage(const PacketHeader & header, const Transport::PeerAddress & peerAddress,
114 System::PacketBufferHandle msgIn) override;
115 void OnSessionEstablishmentError(CHIP_ERROR error) override;
116 void OnSessionEstablished() override;
119 friend class ChannelContextDeletor;
120 friend class ChannelHandle;
123 ExchangeManager * mExchangeManager;
125 enum class PrepareState
136 // mPreparing is pretty big, consider move it outside
140 Inet::IPAddressType mAddressType;
141 Inet::IPAddress mAddress;
142 CASESession * mCasePairingSession;
143 ChannelBuilder mBuilder;
148 SecureSessionHandle mSession;
152 // State machine functions
153 void EnterPreparingState(const ChannelBuilder & builder);
154 void ExitPreparingState();
156 void EnterReadyState(SecureSessionHandle session);
157 void ExitReadyState();
159 void EnterFailedState(CHIP_ERROR error);
160 void EnterClosedState();
162 // Preparing sub-states
163 void EnterAddressResolve();
164 static void AddressResolveTimeout(System::Layer * aLayer, void * aAppState, System::Error aError);
165 void AddressResolveTimeout();
166 void ExitAddressResolve() {}
168 void EnterCasePairingState();
169 void ExitCasePairingState();
172 class ChannelContextHandleAssociation
175 ChannelContextHandleAssociation(ChannelContext * channelContext, ChannelDelegate * channelDelegate) :
176 mChannelContext(channelContext), mChannelDelegate(channelDelegate)
178 mChannelContext->Retain();
180 ~ChannelContextHandleAssociation() { mChannelContext->Release(); }
183 friend class ExchangeManager;
184 friend class ChannelHandle;
185 ChannelContext * mChannelContext;
186 ChannelDelegate * mChannelDelegate;
189 } // namespace Messaging