Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2023 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * This file defines the CHIP Active Connection object that maintains TCP connections.
22 : */
23 :
24 : #pragma once
25 :
26 : #include <inet/IPAddress.h>
27 : #include <inet/InetInterface.h>
28 : #include <inet/TCPEndPoint.h>
29 : #include <lib/core/CHIPCore.h>
30 : #include <transport/raw/PeerAddress.h>
31 : #include <transport/raw/TCPConfig.h>
32 :
33 : namespace chip {
34 : namespace Transport {
35 :
36 : /**
37 : * The State of the TCP connection
38 : */
39 : enum class TCPState
40 : {
41 : kNotReady = 0, /**< State before initialization. */
42 : kInitialized = 1, /**< State after class is listening and ready. */
43 : kConnecting = 3, /**< Connection with peer has been initiated. */
44 : kConnected = 4, /**< Connected with peer and ready for Send/Receive. */
45 : kClosed = 5, /**< Connection is closed. */
46 : };
47 :
48 : struct AppTCPConnectionCallbackCtxt;
49 : /**
50 : * State for each active TCP connection
51 : */
52 : struct ActiveTCPConnectionState
53 : {
54 :
55 4 : void Init(Inet::TCPEndPoint * endPoint, const PeerAddress & peerAddr)
56 : {
57 4 : mEndPoint = endPoint;
58 4 : mPeerAddr = peerAddr;
59 4 : mReceived = nullptr;
60 4 : mAppState = nullptr;
61 4 : }
62 :
63 22 : void Free()
64 : {
65 22 : if (mEndPoint)
66 : {
67 22 : mEndPoint->Free();
68 : }
69 22 : mPeerAddr = PeerAddress::Uninitialized();
70 22 : mEndPoint = nullptr;
71 22 : mReceived = nullptr;
72 22 : mAppState = nullptr;
73 22 : }
74 :
75 105 : bool InUse() const { return mEndPoint != nullptr; }
76 :
77 163 : bool IsConnected() const { return (mEndPoint != nullptr && mConnectionState == TCPState::kConnected); }
78 :
79 0 : bool IsConnecting() const { return (mEndPoint != nullptr && mConnectionState == TCPState::kConnecting); }
80 :
81 : // Associated endpoint.
82 : Inet::TCPEndPoint * mEndPoint;
83 :
84 : // Peer Node Address
85 : PeerAddress mPeerAddr;
86 :
87 : // Buffers received but not yet consumed.
88 : System::PacketBufferHandle mReceived;
89 :
90 : // Current state of the connection
91 : TCPState mConnectionState;
92 :
93 : // A pointer to an application-specific state object. It should
94 : // represent an object that is at a layer above the SessionManager. The
95 : // SessionManager would accept this object at the time of connecting to
96 : // the peer, and percolate it down to the TransportManager that then,
97 : // should store this state in the corresponding connection object that
98 : // is created.
99 : // At various connection events, this state is passed back to the
100 : // corresponding application.
101 : AppTCPConnectionCallbackCtxt * mAppState = nullptr;
102 :
103 : // KeepAlive interval in seconds
104 : uint16_t mTCPKeepAliveIntervalSecs = CHIP_CONFIG_TCP_KEEPALIVE_INTERVAL_SECS;
105 : uint16_t mTCPMaxNumKeepAliveProbes = CHIP_CONFIG_MAX_TCP_KEEPALIVE_PROBES;
106 : };
107 :
108 : // Functors for callbacks into higher layers
109 : using OnTCPConnectionReceivedCallback = void (*)(ActiveTCPConnectionState * conn);
110 :
111 : using OnTCPConnectionCompleteCallback = void (*)(ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
112 :
113 : using OnTCPConnectionClosedCallback = void (*)(ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
114 :
115 : /*
116 : * Application callback state that is passed down at connection establishment
117 : * stage.
118 : * */
119 : struct AppTCPConnectionCallbackCtxt
120 : {
121 : void * appContext = nullptr; // A pointer to an application context object.
122 : OnTCPConnectionReceivedCallback connReceivedCb = nullptr;
123 : OnTCPConnectionCompleteCallback connCompleteCb = nullptr;
124 : OnTCPConnectionClosedCallback connClosedCb = nullptr;
125 : };
126 :
127 : } // namespace Transport
128 : } // namespace chip
|