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 20 : void Free()
64 : {
65 20 : mEndPoint->Free();
66 20 : mPeerAddr = PeerAddress::Uninitialized();
67 20 : mEndPoint = nullptr;
68 20 : mReceived = nullptr;
69 20 : mAppState = nullptr;
70 20 : }
71 :
72 82 : bool InUse() const { return mEndPoint != nullptr; }
73 :
74 159 : bool IsConnected() const { return (mEndPoint != nullptr && mConnectionState == TCPState::kConnected); }
75 :
76 0 : bool IsConnecting() const { return (mEndPoint != nullptr && mConnectionState == TCPState::kConnecting); }
77 :
78 : // Associated endpoint.
79 : Inet::TCPEndPoint * mEndPoint;
80 :
81 : // Peer Node Address
82 : PeerAddress mPeerAddr;
83 :
84 : // Buffers received but not yet consumed.
85 : System::PacketBufferHandle mReceived;
86 :
87 : // Current state of the connection
88 : TCPState mConnectionState;
89 :
90 : // A pointer to an application-specific state object. It should
91 : // represent an object that is at a layer above the SessionManager. The
92 : // SessionManager would accept this object at the time of connecting to
93 : // the peer, and percolate it down to the TransportManager that then,
94 : // should store this state in the corresponding connection object that
95 : // is created.
96 : // At various connection events, this state is passed back to the
97 : // corresponding application.
98 : AppTCPConnectionCallbackCtxt * mAppState = nullptr;
99 :
100 : // KeepAlive interval in seconds
101 : uint16_t mTCPKeepAliveIntervalSecs = CHIP_CONFIG_TCP_KEEPALIVE_INTERVAL_SECS;
102 : uint16_t mTCPMaxNumKeepAliveProbes = CHIP_CONFIG_MAX_TCP_KEEPALIVE_PROBES;
103 : };
104 :
105 : // Functors for callbacks into higher layers
106 : using OnTCPConnectionReceivedCallback = void (*)(ActiveTCPConnectionState * conn);
107 :
108 : using OnTCPConnectionCompleteCallback = void (*)(ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
109 :
110 : using OnTCPConnectionClosedCallback = void (*)(ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
111 :
112 : /*
113 : * Application callback state that is passed down at connection establishment
114 : * stage.
115 : * */
116 : struct AppTCPConnectionCallbackCtxt
117 : {
118 : void * appContext = nullptr; // A pointer to an application context object.
119 : OnTCPConnectionReceivedCallback connReceivedCb = nullptr;
120 : OnTCPConnectionCompleteCallback connCompleteCb = nullptr;
121 : OnTCPConnectionClosedCallback connClosedCb = nullptr;
122 : };
123 :
124 : } // namespace Transport
125 : } // namespace chip
|