Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : *
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
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
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.
16 : */
17 :
18 : /**
19 : * @file
20 : * Defines base properties and constants valid across all transport
21 : * classes (UDP, TCP, BLE, ....)
22 : */
23 :
24 : #pragma once
25 :
26 : #include <inet/IPAddress.h>
27 : #include <inet/TCPEndPoint.h>
28 : #include <inet/UDPEndPoint.h>
29 : #include <lib/core/CHIPError.h>
30 : #include <system/SystemPacketBuffer.h>
31 : #include <transport/raw/MessageHeader.h>
32 : #include <transport/raw/PeerAddress.h>
33 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
34 : #include <transport/raw/ActiveTCPConnectionState.h>
35 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
36 :
37 : namespace chip {
38 : namespace Transport {
39 :
40 : struct MessageTransportContext
41 : {
42 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
43 : ActiveTCPConnectionState * conn = nullptr;
44 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
45 : };
46 :
47 : class RawTransportDelegate
48 : {
49 : public:
50 1 : virtual ~RawTransportDelegate() {}
51 : virtual void HandleMessageReceived(const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msg,
52 : MessageTransportContext * ctxt = nullptr) = 0;
53 :
54 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
55 0 : virtual void HandleConnectionReceived(ActiveTCPConnectionState * conn){};
56 0 : virtual void HandleConnectionAttemptComplete(ActiveTCPConnectionState * conn, CHIP_ERROR conErr){};
57 0 : virtual void HandleConnectionClosed(ActiveTCPConnectionState * conn, CHIP_ERROR conErr){};
58 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
59 : };
60 :
61 : /**
62 : * Transport class base, defining common methods among transports (message
63 : * packing by encoding and decoding headers) and generic message transport
64 : * methods.
65 : */
66 : class Base
67 : {
68 : public:
69 21 : virtual ~Base() {}
70 :
71 : /**
72 : * Sets the delegate of the transport
73 : *
74 : * @param[in] delegate The argument to pass in to the handler function
75 : *
76 : */
77 17 : void SetDelegate(RawTransportDelegate * delegate) { mDelegate = delegate; }
78 :
79 : /**
80 : * @brief Send a message to the specified target.
81 : *
82 : * On connection-oriented transports, sending a message implies connecting to the target first.
83 : */
84 : virtual CHIP_ERROR SendMessage(const PeerAddress & address, System::PacketBufferHandle && msgBuf) = 0;
85 :
86 : /**
87 : * Determine if this transport can SendMessage to the specified peer address.
88 : *
89 : * Generally it is expected that a transport can send to any peer from which it receives a message.
90 : */
91 : virtual bool CanSendToPeer(const PeerAddress & address) = 0;
92 :
93 : /**
94 : * Determine if this transport can Listen to IPV6 Multicast.
95 : */
96 0 : virtual bool CanListenMulticast() { return false; }
97 :
98 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
99 : /**
100 : * Connect to the specified peer.
101 : */
102 0 : virtual CHIP_ERROR TCPConnect(const PeerAddress & address, Transport::AppTCPConnectionCallbackCtxt * appState,
103 : Transport::ActiveTCPConnectionState ** peerConnState)
104 : {
105 0 : return CHIP_NO_ERROR;
106 : }
107 :
108 : /**
109 : * Handle disconnection from the specified peer if currently connected to it.
110 : */
111 0 : virtual void TCPDisconnect(const PeerAddress & address) {}
112 :
113 : /**
114 : * Disconnect on the active connection that is passed in.
115 : */
116 0 : virtual void TCPDisconnect(Transport::ActiveTCPConnectionState * conn, bool shouldAbort = 0) {}
117 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
118 :
119 : /**
120 : * Enable Listening for multicast messages ( IPV6 UDP only)
121 : */
122 0 : virtual CHIP_ERROR MulticastGroupJoinLeave(const Transport::PeerAddress & address, bool join) { return CHIP_ERROR_INTERNAL; }
123 :
124 : /**
125 : * Close the open endpoint without destroying the object
126 : */
127 1 : virtual void Close() {}
128 :
129 : protected:
130 : /**
131 : * Method used by subclasses to notify that a packet has been received after
132 : * any associated headers have been decoded.
133 : */
134 14 : void HandleMessageReceived(const PeerAddress & source, System::PacketBufferHandle && buffer,
135 : MessageTransportContext * ctxt = nullptr)
136 : {
137 14 : mDelegate->HandleMessageReceived(source, std::move(buffer), ctxt);
138 14 : }
139 :
140 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
141 : // Handle an incoming connection request from a peer.
142 10 : void HandleConnectionReceived(ActiveTCPConnectionState * conn) { mDelegate->HandleConnectionReceived(conn); }
143 :
144 : // Callback during connection establishment to notify of success or any
145 : // error.
146 10 : void HandleConnectionAttemptComplete(ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
147 : {
148 10 : mDelegate->HandleConnectionAttemptComplete(conn, conErr);
149 10 : }
150 :
151 : // Callback to notify the higher layer of an unexpected connection closure.
152 10 : void HandleConnectionClosed(ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
153 : {
154 10 : mDelegate->HandleConnectionClosed(conn, conErr);
155 10 : }
156 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
157 :
158 : RawTransportDelegate * mDelegate = nullptr;
159 : };
160 :
161 : } // namespace Transport
162 : } // namespace chip
|