Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / transport / raw / Base.h
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 <core/CHIPError.h>
27 #include <inet/IPAddress.h>
28 #include <inet/UDPEndPoint.h>
29 #include <system/SystemPacketBuffer.h>
30 #include <transport/raw/MessageHeader.h>
31 #include <transport/raw/PeerAddress.h>
32
33 namespace chip {
34 namespace Transport {
35
36 class RawTransportDelegate
37 {
38 public:
39     virtual ~RawTransportDelegate() {}
40     virtual void HandleMessageReceived(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
41                                        System::PacketBufferHandle msg) = 0;
42 };
43
44 /**
45  * Transport class base, defining common methods among transports (message
46  * packing by encoding and decoding headers) and generic message transport
47  * methods.
48  */
49 class Base
50 {
51 public:
52     virtual ~Base() {}
53
54     /**
55      * Sets the delegate of the transport
56      *
57      * @param[in] delegate  The argument to pass in to the handler function
58      *
59      */
60     void SetDelegate(RawTransportDelegate * delegate) { mDelegate = delegate; }
61
62     /**
63      * @brief Send a message to the specified target.
64      *
65      * On connection-oriented transports, sending a message implies connecting to the target first.
66      */
67     virtual CHIP_ERROR SendMessage(const PacketHeader & header, const PeerAddress & address, System::PacketBufferHandle msgBuf) = 0;
68
69     /**
70      * Determine if this transport can SendMessage to the specified peer address.
71      *
72      * Generally it is expected that a transport can send to any peer from which it receives a message.
73      */
74     virtual bool CanSendToPeer(const PeerAddress & address) = 0;
75
76     /**
77      * Handle disconnection from the specified peer if currently connected to it.
78      */
79     virtual void Disconnect(const PeerAddress & address) {}
80
81     /**
82      * Close the open endpoint without destroying the object
83      */
84     virtual void Close(){};
85
86 protected:
87     /**
88      * Method used by subclasses to notify that a packet has been received after
89      * any associated headers have been decoded.
90      */
91     void HandleMessageReceived(const PacketHeader & header, const PeerAddress & source, System::PacketBufferHandle && buffer)
92     {
93         mDelegate->HandleMessageReceived(header, source, std::move(buffer));
94     }
95
96     RawTransportDelegate * mDelegate;
97 };
98
99 } // namespace Transport
100 } // namespace chip