3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2014-2017 Nest Labs, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
28 #include <inet/InetConfig.h>
30 #include <support/DLLUtil.h>
31 #include <system/SystemObject.h>
34 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
35 #include <sys/select.h>
41 //--- Forward declaration of InetLayer singleton class
45 * @class InetLayerBasis
48 * This is the basis class of reference-counted objects managed by an
52 class InetLayerBasis : public chip::System::Object
55 InetLayer & Layer() const;
56 bool IsCreatedByInetLayer(const InetLayer & aInetLayer) const;
59 void InitInetLayerBasis(InetLayer & aInetLayer, void * aAppState = nullptr);
62 InetLayer * mInetLayer; /**< Pointer to the InetLayer object that owns this object. */
66 * Returns a reference to the Inet layer object that owns this basis object.
68 inline InetLayer & InetLayerBasis::Layer() const
74 * Returns \c true if the basis object was obtained by the specified INET layer instance.
76 * @param[in] aInetLayer An instance of the INET layer.
78 * @return \c true if owned by \c aInetLayer, otherwise \c false.
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.
84 inline bool InetLayerBasis::IsCreatedByInetLayer(const InetLayer & aInetLayer) const
86 return mInetLayer == &aInetLayer;
89 inline void InetLayerBasis::InitInetLayerBasis(InetLayer & aInetLayer, void * aAppState)
92 mInetLayer = &aInetLayer;
95 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
101 * Represent a set of I/O events requested/pending on a socket.
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. */
114 int Value; /**< Contains the bit flags for the socket event. */
117 * Constructor for the SocketEvents class.
120 SocketEvents() { Value = 0; }
123 * Copy constructor for the SocketEvents class.
126 SocketEvents(const SocketEvents & other) { Value = other.Value; }
129 * Copy assignment operator for the SocketEvents class.
132 SocketEvents & operator=(const SocketEvents & other) = default;
135 * Check if any of the bit flags for the socket events are set.
137 * @return true if set, otherwise false.
140 bool IsSet() const { return Value != 0; }
143 * Check if the bit flags indicate that the socket is readable.
145 * @return true if socket is readable, otherwise false.
148 bool IsReadable() const { return (Value & kRead) != 0; }
151 * Check if the bit flags indicate that the socket is writable.
153 * @return true if socket is writable, otherwise false.
156 bool IsWriteable() const { return (Value & kWrite) != 0; }
159 * Check if the bit flags indicate that the socket has an error.
161 * @return true if socket has an error, otherwise false.
164 bool IsError() const { return (Value & kError) != 0; }
167 * Set the read bit flag for the socket.
170 void SetRead() { Value |= kRead; }
173 * Set the write bit flag for the socket.
176 void SetWrite() { Value |= kWrite; }
179 * Set the error bit flag for the socket.
182 void SetError() { Value |= kError; }
185 * Clear the bit flags for the socket.
188 void Clear() { Value = 0; }
191 * Clear the read bit flag for the socket.
194 void ClearRead() { Value &= ~kRead; }
197 * Clear the write bit flag for the socket.
200 void ClearWrite() { Value &= ~kWrite; }
203 * Clear the error bit flag for the socket.
206 void ClearError() { Value &= ~kError; }
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);
213 * @def INET_INVALID_SOCKET_FD
216 * This is the invalid socket file descriptor identifier.
218 #define INET_INVALID_SOCKET_FD (-1)
220 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS