3 * Copyright (c) 2020 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * This file declares the abstraction of one-directional, anonymous
21 * data stream built on top of two file descriptors.
24 #include <system/SystemWakeEvent.h>
26 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
28 // Include additional CHIP headers
29 #include <support/CodeUtils.h>
31 // Include system and language headers
36 #if !CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE
37 #include <sys/eventfd.h>
43 #if CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE
46 inline int SetNonBlockingMode(int fd)
48 int flags = ::fcntl(fd, F_GETFL, 0);
49 return ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
51 } // anonymous namespace
53 Error SystemWakeEvent::Open()
55 mFDs[FD_READ] = mFDs[FD_WRITE] = -1;
58 return chip::System::MapErrorPOSIX(errno);
60 if (SetNonBlockingMode(mFDs[FD_READ]) < 0)
61 return chip::System::MapErrorPOSIX(errno);
63 if (SetNonBlockingMode(mFDs[FD_WRITE]) < 0)
64 return chip::System::MapErrorPOSIX(errno);
66 return CHIP_SYSTEM_NO_ERROR;
69 Error SystemWakeEvent::Close()
73 res |= ::close(mFDs[FD_WRITE]);
74 res |= ::close(mFDs[FD_READ]);
75 mFDs[FD_READ] = mFDs[FD_WRITE] = -1;
79 return chip::System::MapErrorPOSIX(errno);
82 return CHIP_SYSTEM_NO_ERROR;
85 Error SystemWakeEvent::Confirm()
92 res = ::read(mFDs[FD_READ], buffer, sizeof(buffer));
93 if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
95 return chip::System::MapErrorPOSIX(errno);
97 } while (res == sizeof(buffer));
99 return CHIP_SYSTEM_NO_ERROR;
102 Error SystemWakeEvent::Notify()
106 if (::write(mFDs[FD_WRITE], &byte, 1) < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
108 return chip::System::MapErrorPOSIX(errno);
111 return CHIP_SYSTEM_NO_ERROR;
114 #else // CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE
116 Error SystemWakeEvent::Open()
118 mFD = ::eventfd(0, 0);
122 return chip::System::MapErrorPOSIX(errno);
125 return CHIP_SYSTEM_NO_ERROR;
128 Error SystemWakeEvent::Close()
130 int res = ::close(mFD);
135 return chip::System::MapErrorPOSIX(errno);
138 return CHIP_SYSTEM_NO_ERROR;
141 Error SystemWakeEvent::Confirm()
145 if (::read(mFD, &value, sizeof(value)) < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
147 return chip::System::MapErrorPOSIX(errno);
150 return CHIP_SYSTEM_NO_ERROR;
153 Error SystemWakeEvent::Notify()
157 if (::write(mFD, &value, sizeof(value)) < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
159 return chip::System::MapErrorPOSIX(errno);
162 return CHIP_SYSTEM_NO_ERROR;
165 #endif // CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE
167 } // namespace System
170 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS