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 : ActiveTCPConnectionHolder conn;
44 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
45 : };
46 :
47 : class RawTransportDelegate
48 : {
49 : public:
50 74 : 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(ActiveTCPConnectionHolder & 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 184 : 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 118 : 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 : ActiveTCPConnectionHolder & peerConnState)
104 : {
105 0 : return CHIP_NO_ERROR;
106 : }
107 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
108 :
109 : /**
110 : * Enable Listening for multicast messages ( IPV6 UDP only)
111 : */
112 0 : virtual CHIP_ERROR MulticastGroupJoinLeave(const Transport::PeerAddress & address, bool join) { return CHIP_ERROR_INTERNAL; }
113 :
114 : /**
115 : * Close the open endpoint without destroying the object
116 : */
117 50 : virtual void Close() {}
118 :
119 : protected:
120 : /**
121 : * Method used by subclasses to notify that a packet has been received after
122 : * any associated headers have been decoded.
123 : */
124 14992 : void HandleMessageReceived(const PeerAddress & source, System::PacketBufferHandle && buffer,
125 : MessageTransportContext * ctxt = nullptr)
126 : {
127 14992 : mDelegate->HandleMessageReceived(source, std::move(buffer), ctxt);
128 14992 : }
129 :
130 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
131 : // Handle an incoming connection request from a peer.
132 12 : void HandleConnectionReceived(ActiveTCPConnectionState & conn) { mDelegate->HandleConnectionReceived(conn); }
133 :
134 : // Callback during connection establishment to notify of success or any
135 : // error.
136 14 : void HandleConnectionAttemptComplete(ActiveTCPConnectionHolder & conn, CHIP_ERROR conErr)
137 : {
138 14 : mDelegate->HandleConnectionAttemptComplete(conn, conErr);
139 14 : }
140 :
141 : // Callback to notify the higher layer of an unexpected connection closure.
142 5 : void HandleConnectionClosed(ActiveTCPConnectionState & conn, CHIP_ERROR conErr)
143 : {
144 5 : mDelegate->HandleConnectionClosed(conn, conErr);
145 5 : }
146 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
147 :
148 : RawTransportDelegate * mDelegate = nullptr;
149 : };
150 :
151 : } // namespace Transport
152 : } // namespace chip
|