Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / inet / InetLayerBasis.cpp
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 #include "InetLayerBasis.h"
27
28 namespace chip {
29 namespace Inet {
30
31 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
32 /**
33  *  Sets the bit for the specified file descriptor in the given sets of file descriptors.
34  *
35  *  @param[in]    socket    The file descriptor for which the bit is being set.
36  *
37  *  @param[out]   nfds      A reference to the range of file descriptors in the set.
38  *
39  *  @param[in]    readfds   A pointer to the set of readable file descriptors.
40  *
41  *  @param[in]    writefds  A pointer to the set of writable file descriptors.
42  *
43  *  @param[in]    exceptfds  A pointer to the set of file descriptors with errors.
44  *
45  */
46 void SocketEvents::SetFDs(int socket, int & nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds)
47 {
48     if (socket != INET_INVALID_SOCKET_FD)
49     {
50         if (IsReadable())
51             FD_SET(socket, readfds);
52         if (IsWriteable())
53             FD_SET(socket, writefds);
54         if (IsError())
55             FD_SET(socket, exceptfds);
56         if (IsSet() && (socket + 1) > nfds)
57             nfds = socket + 1;
58     }
59 }
60
61 /**
62  *  Set the read, write or exception bit flags for the specified socket based on its status in
63  *  the corresponding file descriptor sets.
64  *
65  *  @param[in]    socket    The file descriptor for which the bit flags are being set.
66  *
67  *  @param[in]    readfds   A pointer to the set of readable file descriptors.
68  *
69  *  @param[in]    writefds  A pointer to the set of writable file descriptors.
70  *
71  *  @param[in]    exceptfds  A pointer to the set of file descriptors with errors.
72  *
73  */
74 SocketEvents SocketEvents::FromFDs(int socket, fd_set * readfds, fd_set * writefds, fd_set * exceptfds)
75 {
76     SocketEvents res;
77
78     if (socket != INET_INVALID_SOCKET_FD)
79     {
80         if (FD_ISSET(socket, readfds))
81             res.SetRead();
82         if (FD_ISSET(socket, writefds))
83             res.SetWrite();
84         if (FD_ISSET(socket, exceptfds))
85             res.SetError();
86     }
87
88     return res;
89 }
90 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
91
92 } // namespace Inet
93 } // namespace chip