Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / inet / EndPointBasis.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2015-2017 Nest Labs, Inc.
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 contains the basis class for all the various transport
22  *      endpoint classes in the Inet layer, i.e. TCP, UDP, Raw and Tun.
23  */
24
25 #pragma once
26
27 #include <inet/InetConfig.h>
28
29 #include "inet/IANAConstants.h"
30 #include "inet/InetLayerBasis.h"
31 #include <inet/InetError.h>
32 #include <inet/InetInterface.h>
33 #include <inet/InetLayerEvents.h>
34
35 #include <support/DLLUtil.h>
36
37 #if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
38 #include <Network/Network.h>
39 #endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
40
41 //--- Declaration of LWIP protocol control buffer structure names
42 #if CHIP_SYSTEM_CONFIG_USE_LWIP
43 #if INET_CONFIG_ENABLE_RAW_ENDPOINT
44 struct raw_pcb;
45 #endif // INET_CONFIG_ENABLE_RAW_ENDPOINT
46 #if INET_CONFIG_ENABLE_UDP_ENDPOINT
47 struct udp_pcb;
48 #endif // INET_CONFIG_ENABLE_UDP_ENDPOINT
49 #if INET_CONFIG_ENABLE_TCP_ENDPOINT
50 struct tcp_pcb;
51 #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
52 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
53
54 namespace chip {
55 namespace Inet {
56
57 /**
58  * @class EndPointBasis
59  *
60  * @brief Basis of internet transport endpoint classes
61  */
62 class DLL_EXPORT EndPointBasis : public InetLayerBasis
63 {
64 public:
65     /** Common state codes */
66     enum
67     {
68         kBasisState_Closed = 0 /**< Encapsulated descriptor is not valid. */
69     };
70
71 #if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
72     /** Test whether endpoint is a Network.framework endpoint */
73     bool IsNetworkFrameworkEndPoint(void) const;
74 #endif
75
76 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
77     /** Test whether endpoint is a POSIX socket */
78     bool IsSocketsEndPoint() const;
79 #endif
80
81 #if CHIP_SYSTEM_CONFIG_USE_LWIP
82     /** Test whether endpoint is a LwIP protocol control buffer */
83     bool IsLWIPEndPoint(void) const;
84 #endif
85
86     /** Test whether endpoint has a valid descriptor. */
87     bool IsOpenEndPoint() const;
88
89 protected:
90 #if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
91     nw_parameters_t mParameters;
92     IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
93 #endif
94
95 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
96     int mSocket;             /**< Encapsulated socket descriptor. */
97     IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
98     SocketEvents mPendingIO; /**< Socket event masks */
99 #endif                       // CHIP_SYSTEM_CONFIG_USE_SOCKETS
100
101 #if CHIP_SYSTEM_CONFIG_USE_LWIP
102     /** Encapsulated LwIP protocol control block */
103     union
104     {
105         const void * mVoid; /**< An untyped protocol control buffer reference */
106 #if INET_CONFIG_ENABLE_RAW_ENDPOINT
107         raw_pcb * mRaw; /**< Raw network interface protocol control */
108 #endif                  // INET_CONFIG_ENABLE_RAW_ENDPOINT
109 #if INET_CONFIG_ENABLE_UDP_ENDPOINT
110         udp_pcb * mUDP; /**< User datagram protocol (UDP) control */
111 #endif                  // INET_CONFIG_ENABLE_UDP_ENDPOINT
112 #if INET_CONFIG_ENABLE_TCP_ENDPOINT
113         tcp_pcb * mTCP; /**< Transmission control protocol (TCP) control */
114 #endif                  // INET_CONFIG_ENABLE_TCP_ENDPOINT
115     };
116
117     enum
118     {
119         kLwIPEndPointType_Unknown = 0,
120
121         kLwIPEndPointType_Raw = 1,
122         kLwIPEndPointType_UDP = 2,
123         kLwIPEndPointType_UCP = 3,
124         kLwIPEndPointType_TCP = 4
125     };
126
127     uint8_t mLwIPEndPointType;
128
129     void DeferredFree(chip::System::Object::ReleaseDeferralErrorTactic aTactic);
130 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
131
132     void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr);
133 };
134
135 #if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
136 inline bool EndPointBasis::IsNetworkFrameworkEndPoint(void) const
137 {
138     return mParameters != NULL;
139 }
140 #endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
141
142 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
143 inline bool EndPointBasis::IsSocketsEndPoint() const
144 {
145     return mSocket >= 0;
146 }
147 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
148
149 #if CHIP_SYSTEM_CONFIG_USE_LWIP
150 inline bool EndPointBasis::IsLWIPEndPoint(void) const
151 {
152     return mVoid != NULL;
153 }
154 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
155
156 inline bool EndPointBasis::IsOpenEndPoint() const
157 {
158     bool lResult = false;
159
160 #if CHIP_SYSTEM_CONFIG_USE_LWIP
161     lResult = (lResult || IsLWIPEndPoint());
162 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
163
164 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
165     lResult = (lResult || IsSocketsEndPoint());
166 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
167
168 #if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
169     lResult = (lResult || IsNetworkFrameworkEndPoint());
170 #endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
171
172     return lResult;
173 }
174
175 #if CHIP_SYSTEM_CONFIG_USE_LWIP
176 inline void EndPointBasis::DeferredFree(chip::System::Object::ReleaseDeferralErrorTactic aTactic)
177 {
178     if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || IsLWIPEndPoint())
179     {
180         DeferredRelease(aTactic);
181     }
182     else
183     {
184         Release();
185     }
186 }
187 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
188
189 } // namespace Inet
190 } // namespace chip