Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / inet / InetLayerBasis.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2014-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 reference counting
22  *      objects by the Inet layer as well as a class for representing
23  *      the pending or resulting I/O events on a socket.
24  */
25
26 #pragma once
27
28 #include <inet/InetConfig.h>
29
30 #include <support/DLLUtil.h>
31 #include <system/SystemObject.h>
32
33 #include <stdint.h>
34 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
35 #include <sys/select.h>
36 #endif
37
38 namespace chip {
39 namespace Inet {
40
41 //--- Forward declaration of InetLayer singleton class
42 class InetLayer;
43
44 /**
45  *  @class InetLayerBasis
46  *
47  *  @brief
48  *    This is the basis class of reference-counted objects managed by an
49  *    InetLayer object.
50  *
51  */
52 class InetLayerBasis : public chip::System::Object
53 {
54 public:
55     InetLayer & Layer() const;
56     bool IsCreatedByInetLayer(const InetLayer & aInetLayer) const;
57
58 protected:
59     void InitInetLayerBasis(InetLayer & aInetLayer, void * aAppState = nullptr);
60
61 private:
62     InetLayer * mInetLayer; /**< Pointer to the InetLayer object that owns this object. */
63 };
64
65 /**
66  *  Returns a reference to the Inet layer object that owns this basis object.
67  */
68 inline InetLayer & InetLayerBasis::Layer() const
69 {
70     return *mInetLayer;
71 }
72
73 /**
74  *  Returns \c true if the basis object was obtained by the specified INET layer instance.
75  *
76  *  @param[in]  aInetLayer    An instance of the INET layer.
77  *
78  *  @return     \c true if owned by \c aInetLayer, otherwise \c false.
79  *
80  *  @note
81  *      Does not check whether the object is actually obtained by the system layer instance associated with the INET layer
82  *      instance. It merely tests whether \c aInetLayer is the INET layer instance that was provided to \c InitInetLayerBasis.
83  */
84 inline bool InetLayerBasis::IsCreatedByInetLayer(const InetLayer & aInetLayer) const
85 {
86     return mInetLayer == &aInetLayer;
87 }
88
89 inline void InetLayerBasis::InitInetLayerBasis(InetLayer & aInetLayer, void * aAppState)
90 {
91     AppState   = aAppState;
92     mInetLayer = &aInetLayer;
93 }
94
95 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
96
97 /**
98  *  @class SocketEvents
99  *
100  *  @brief
101  *    Represent a set of I/O events requested/pending on a socket.
102  *
103  */
104 class SocketEvents
105 {
106 public:
107     enum
108     {
109         kRead  = 0x01, /**< Bit flag indicating if there is a read event on a socket. */
110         kWrite = 0x02, /**< Bit flag indicating if there is a write event on a socket. */
111         kError = 0x04, /**< Bit flag indicating if there is an error event on a socket. */
112     };
113
114     int Value; /**< Contains the bit flags for the socket event. */
115
116     /**
117      *  Constructor for the SocketEvents class.
118      *
119      */
120     SocketEvents() { Value = 0; }
121
122     /**
123      *  Copy constructor for the SocketEvents class.
124      *
125      */
126     SocketEvents(const SocketEvents & other) { Value = other.Value; }
127
128     /**
129      *  Copy assignment operator for the SocketEvents class.
130      *
131      */
132     SocketEvents & operator=(const SocketEvents & other) = default;
133
134     /**
135      *  Check if any of the bit flags for the socket events are set.
136      *
137      *  @return true if set, otherwise false.
138      *
139      */
140     bool IsSet() const { return Value != 0; }
141
142     /**
143      *  Check if the bit flags indicate that the socket is readable.
144      *
145      *  @return true if socket is readable, otherwise false.
146      *
147      */
148     bool IsReadable() const { return (Value & kRead) != 0; }
149
150     /**
151      *  Check if the bit flags indicate that the socket is writable.
152      *
153      *  @return true if socket is writable, otherwise false.
154      *
155      */
156     bool IsWriteable() const { return (Value & kWrite) != 0; }
157
158     /**
159      *  Check if the bit flags indicate that the socket has an error.
160      *
161      *  @return true if socket has an error, otherwise false.
162      *
163      */
164     bool IsError() const { return (Value & kError) != 0; }
165
166     /**
167      *  Set the read bit flag for the socket.
168      *
169      */
170     void SetRead() { Value |= kRead; }
171
172     /**
173      *  Set the write bit flag for the socket.
174      *
175      */
176     void SetWrite() { Value |= kWrite; }
177
178     /**
179      *  Set the error bit flag for the socket.
180      *
181      */
182     void SetError() { Value |= kError; }
183
184     /**
185      *  Clear the bit flags for the socket.
186      *
187      */
188     void Clear() { Value = 0; }
189
190     /**
191      *  Clear the read bit flag for the socket.
192      *
193      */
194     void ClearRead() { Value &= ~kRead; }
195
196     /**
197      *  Clear the write bit flag for the socket.
198      *
199      */
200     void ClearWrite() { Value &= ~kWrite; }
201
202     /**
203      *  Clear the error bit flag for the socket.
204      *
205      */
206     void ClearError() { Value &= ~kError; }
207
208     void SetFDs(int socket, int & nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds);
209     static SocketEvents FromFDs(int socket, fd_set * readfds, fd_set * writefds, fd_set * exceptfds);
210 };
211
212 /**
213  *  @def INET_INVALID_SOCKET_FD
214  *
215  *  @brief
216  *    This is the invalid socket file descriptor identifier.
217  */
218 #define INET_INVALID_SOCKET_FD (-1)
219
220 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
221
222 } // namespace Inet
223 } // namespace chip