1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "async_fd_watcher.h"
18
19 #include <string.h>
20
21 #include <algorithm>
22 #include <atomic>
23 #include <condition_variable>
24 #include <map>
25 #include <mutex>
26 #include <thread>
27 #include <vector>
28
29 #include "fcntl.h"
30 #include "log/log.h"
31 #include "sys/select.h"
32 #include "unistd.h"
33
34 static const int INVALID_FD = -1;
35
36 namespace android::hardware::bluetooth::async {
37
WatchFdForNonBlockingReads(int file_descriptor,const ReadCallback & on_read_fd_ready_callback)38 int AsyncFdWatcher::WatchFdForNonBlockingReads(
39 int file_descriptor, const ReadCallback& on_read_fd_ready_callback) {
40 // Add file descriptor and callback
41 {
42 std::unique_lock<std::mutex> guard(internal_mutex_);
43 watched_fds_[file_descriptor] = on_read_fd_ready_callback;
44 }
45
46 // Start the thread if not started yet
47 return tryStartThread();
48 }
49
ConfigureTimeout(const std::chrono::milliseconds timeout,const TimeoutCallback & on_timeout_callback)50 int AsyncFdWatcher::ConfigureTimeout(
51 const std::chrono::milliseconds timeout,
52 const TimeoutCallback& on_timeout_callback) {
53 // Add timeout and callback
54 {
55 std::unique_lock<std::mutex> guard(timeout_mutex_);
56 timeout_cb_ = on_timeout_callback;
57 timeout_ms_ = timeout;
58 }
59
60 notifyThread();
61 return 0;
62 }
63
StopWatchingFileDescriptors()64 void AsyncFdWatcher::StopWatchingFileDescriptors() { stopThread(); }
65
~AsyncFdWatcher()66 AsyncFdWatcher::~AsyncFdWatcher() {}
67
68 // Make sure to call this with at least one file descriptor ready to be
69 // watched upon or the thread routine will return immediately
tryStartThread()70 int AsyncFdWatcher::tryStartThread() {
71 if (std::atomic_exchange(&running_, true)) return 0;
72
73 // Set up the communication channel
74 int pipe_fds[2];
75 if (pipe2(pipe_fds, O_NONBLOCK)) return -1;
76
77 notification_listen_fd_ = pipe_fds[0];
78 notification_write_fd_ = pipe_fds[1];
79
80 thread_ = std::thread([this]() { ThreadRoutine(); });
81 if (!thread_.joinable()) return -1;
82
83 return 0;
84 }
85
stopThread()86 int AsyncFdWatcher::stopThread() {
87 if (!std::atomic_exchange(&running_, false)) return 0;
88
89 notifyThread();
90 if (std::this_thread::get_id() != thread_.get_id()) {
91 thread_.join();
92 }
93
94 {
95 std::unique_lock<std::mutex> guard(internal_mutex_);
96 watched_fds_.clear();
97 }
98
99 {
100 std::unique_lock<std::mutex> guard(timeout_mutex_);
101 timeout_cb_ = nullptr;
102 }
103
104 close(notification_listen_fd_);
105 close(notification_write_fd_);
106
107 return 0;
108 }
109
notifyThread()110 int AsyncFdWatcher::notifyThread() {
111 uint8_t buffer[] = {0};
112 if (TEMP_FAILURE_RETRY(write(notification_write_fd_, &buffer, 1)) < 0) {
113 return -1;
114 }
115 return 0;
116 }
117
ThreadRoutine()118 void AsyncFdWatcher::ThreadRoutine() {
119 while (running_) {
120 fd_set read_fds;
121 FD_ZERO(&read_fds);
122 FD_SET(notification_listen_fd_, &read_fds);
123 int max_read_fd = INVALID_FD;
124 for (auto& it : watched_fds_) {
125 FD_SET(it.first, &read_fds);
126 max_read_fd = std::max(max_read_fd, it.first);
127 }
128
129 struct timeval timeout;
130 struct timeval* timeout_ptr = NULL;
131 if (timeout_ms_ > std::chrono::milliseconds(0)) {
132 timeout.tv_sec = timeout_ms_.count() / 1000;
133 timeout.tv_usec = (timeout_ms_.count() % 1000) * 1000;
134 timeout_ptr = &timeout;
135 }
136
137 // Wait until there is data available to read on some FD.
138 int nfds = std::max(notification_listen_fd_, max_read_fd);
139 int retval = select(nfds + 1, &read_fds, NULL, NULL, timeout_ptr);
140
141 // There was some error.
142 if (retval < 0) continue;
143
144 // Timeout.
145 if (retval == 0) {
146 // Allow the timeout callback to modify the timeout.
147 TimeoutCallback saved_cb;
148 {
149 std::unique_lock<std::mutex> guard(timeout_mutex_);
150 if (timeout_ms_ > std::chrono::milliseconds(0)) saved_cb = timeout_cb_;
151 }
152 if (saved_cb != nullptr) saved_cb();
153 continue;
154 }
155
156 // Read data from the notification FD.
157 if (FD_ISSET(notification_listen_fd_, &read_fds)) {
158 char buffer[] = {0};
159 TEMP_FAILURE_RETRY(read(notification_listen_fd_, buffer, 1));
160 continue;
161 }
162
163 // Invoke the data ready callbacks if appropriate.
164 {
165 // Hold the mutex to make sure that the callbacks are still valid.
166 std::unique_lock<std::mutex> guard(internal_mutex_);
167 for (auto& it : watched_fds_) {
168 if (FD_ISSET(it.first, &read_fds)) {
169 it.second(it.first);
170 }
171 }
172 }
173 }
174 }
175
176 } // namespace android::hardware::bluetooth::async
177