Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / SystemWakeEvent.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  *    @file
20  *      This file declares the abstraction of one-directional, anonymous
21  *      data stream built on top of two file descriptors.
22  */
23
24 #include <system/SystemWakeEvent.h>
25
26 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
27
28 // Include additional CHIP headers
29 #include <support/CodeUtils.h>
30
31 // Include system and language headers
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #if !CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE
37 #include <sys/eventfd.h>
38 #endif
39
40 namespace chip {
41 namespace System {
42
43 #if CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE
44
45 namespace {
46 inline int SetNonBlockingMode(int fd)
47 {
48     int flags = ::fcntl(fd, F_GETFL, 0);
49     return ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
50 }
51 } // anonymous namespace
52
53 Error SystemWakeEvent::Open()
54 {
55     mFDs[FD_READ] = mFDs[FD_WRITE] = -1;
56
57     if (::pipe(mFDs) < 0)
58         return chip::System::MapErrorPOSIX(errno);
59
60     if (SetNonBlockingMode(mFDs[FD_READ]) < 0)
61         return chip::System::MapErrorPOSIX(errno);
62
63     if (SetNonBlockingMode(mFDs[FD_WRITE]) < 0)
64         return chip::System::MapErrorPOSIX(errno);
65
66     return CHIP_SYSTEM_NO_ERROR;
67 }
68
69 Error SystemWakeEvent::Close()
70 {
71     int res = 0;
72
73     res |= ::close(mFDs[FD_WRITE]);
74     res |= ::close(mFDs[FD_READ]);
75     mFDs[FD_READ] = mFDs[FD_WRITE] = -1;
76
77     if (res < 0)
78     {
79         return chip::System::MapErrorPOSIX(errno);
80     }
81
82     return CHIP_SYSTEM_NO_ERROR;
83 }
84
85 Error SystemWakeEvent::Confirm()
86 {
87     uint8_t buffer[128];
88     ssize_t res;
89
90     do
91     {
92         res = ::read(mFDs[FD_READ], buffer, sizeof(buffer));
93         if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
94         {
95             return chip::System::MapErrorPOSIX(errno);
96         }
97     } while (res == sizeof(buffer));
98
99     return CHIP_SYSTEM_NO_ERROR;
100 }
101
102 Error SystemWakeEvent::Notify()
103 {
104     char byte = 1;
105
106     if (::write(mFDs[FD_WRITE], &byte, 1) < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
107     {
108         return chip::System::MapErrorPOSIX(errno);
109     }
110
111     return CHIP_SYSTEM_NO_ERROR;
112 }
113
114 #else // CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE
115
116 Error SystemWakeEvent::Open()
117 {
118     mFD = ::eventfd(0, 0);
119
120     if (mFD == -1)
121     {
122         return chip::System::MapErrorPOSIX(errno);
123     }
124
125     return CHIP_SYSTEM_NO_ERROR;
126 }
127
128 Error SystemWakeEvent::Close()
129 {
130     int res = ::close(mFD);
131     mFD     = -1;
132
133     if (res < 0)
134     {
135         return chip::System::MapErrorPOSIX(errno);
136     }
137
138     return CHIP_SYSTEM_NO_ERROR;
139 }
140
141 Error SystemWakeEvent::Confirm()
142 {
143     uint64_t value;
144
145     if (::read(mFD, &value, sizeof(value)) < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
146     {
147         return chip::System::MapErrorPOSIX(errno);
148     }
149
150     return CHIP_SYSTEM_NO_ERROR;
151 }
152
153 Error SystemWakeEvent::Notify()
154 {
155     uint64_t value = 1;
156
157     if (::write(mFD, &value, sizeof(value)) < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
158     {
159         return chip::System::MapErrorPOSIX(errno);
160     }
161
162     return CHIP_SYSTEM_NO_ERROR;
163 }
164
165 #endif // CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE
166
167 } // namespace System
168 } // namespace chip
169
170 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS