Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / transport / raw / UDP.h
1 /*
2  *
3  *    Copyright (c) 2020 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 Connection object that maintains a UDP connection.
22  *      It binds to any avaiable local addr and port and begins listening.
23  *
24  */
25
26 #pragma once
27
28 #include <utility>
29
30 #include <core/CHIPCore.h>
31 #include <inet/IPAddress.h>
32 #include <inet/IPEndPointBasis.h>
33 #include <inet/InetInterface.h>
34 #include <transport/raw/Base.h>
35
36 namespace chip {
37 namespace Transport {
38
39 /** Defines listening parameters for setting up a UDP transport */
40 class UdpListenParameters
41 {
42 public:
43     explicit UdpListenParameters(Inet::InetLayer * layer) : mLayer(layer) {}
44     UdpListenParameters(const UdpListenParameters &) = default;
45     UdpListenParameters(UdpListenParameters &&)      = default;
46
47     Inet::InetLayer * GetInetLayer() { return mLayer; }
48
49     Inet::IPAddressType GetAddressType() const { return mAddressType; }
50     UdpListenParameters & SetAddressType(Inet::IPAddressType type)
51     {
52         mAddressType = type;
53
54         return *this;
55     }
56
57     uint16_t GetListenPort() const { return mListenPort; }
58     UdpListenParameters & SetListenPort(uint16_t port)
59     {
60         mListenPort = port;
61
62         return *this;
63     }
64
65     Inet::InterfaceId GetInterfaceId() const { return mInterfaceId; }
66     UdpListenParameters & SetInterfaceId(Inet::InterfaceId id)
67     {
68         mInterfaceId = id;
69
70         return *this;
71     }
72
73 private:
74     Inet::InetLayer * mLayer         = nullptr;                   ///< Associated inet layer
75     Inet::IPAddressType mAddressType = Inet::kIPAddressType_IPv6; ///< type of listening socket
76     uint16_t mListenPort             = CHIP_PORT;                 ///< UDP listen port
77     Inet::InterfaceId mInterfaceId   = INET_NULL_INTERFACEID;     ///< Interface to listen on
78 };
79
80 /** Implements a transport using UDP. */
81 class DLL_EXPORT UDP : public Base
82 {
83     /**
84      *  The State of the UDP connection
85      *
86      */
87     enum class State
88     {
89         kNotReady    = 0, /**< State before initialization. */
90         kInitialized = 1, /**< State after class is listening and ready. */
91     };
92
93 public:
94     ~UDP() override;
95
96     /**
97      * Initialize a UDP transport on a given port.
98      *
99      * @param params        UDP configuration parameters for this transport
100      *
101      * @details
102      *   Generally send and receive ports should be the same and equal to CHIP_PORT.
103      *   The class allows separate definitions to allow local execution of several
104      *   Nodes.
105      */
106     CHIP_ERROR Init(UdpListenParameters & params);
107
108     /**
109      * Close the open endpoint without destroying the object
110      */
111     void Close() override;
112
113     CHIP_ERROR SendMessage(const PacketHeader & header, const Transport::PeerAddress & address,
114                            System::PacketBufferHandle msgBuf) override;
115
116     bool CanSendToPeer(const Transport::PeerAddress & address) override
117     {
118         return (mState == State::kInitialized) && (address.GetTransportType() == Type::kUdp) &&
119             (address.GetIPAddress().Type() == mUDPEndpointType);
120     }
121
122 private:
123     // UDP message receive handler.
124     static void OnUdpReceive(Inet::IPEndPointBasis * endPoint, System::PacketBufferHandle buffer,
125                              const Inet::IPPacketInfo * pktInfo);
126
127     Inet::UDPEndPoint * mUDPEndPoint     = nullptr;                                     ///< UDP socket used by the transport
128     Inet::IPAddressType mUDPEndpointType = Inet::IPAddressType::kIPAddressType_Unknown; ///< Socket listening type
129     State mState                         = State::kNotReady;                            ///< State of the UDP transport
130 };
131
132 } // namespace Transport
133 } // namespace chip