1 //
2 // Copyright 2016 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 #define LOG_TAG "[email protected]"
18 
19 #include "async_fd_watcher.h"
20 
21 #include <log/log.h>
22 #include <algorithm>
23 #include <atomic>
24 #include <condition_variable>
25 #include <map>
26 #include <mutex>
27 #include <thread>
28 #include <vector>
29 #include "fcntl.h"
30 #include "sys/select.h"
31 #include "unistd.h"
32 
33 static const int INVALID_FD = -1;
34 
35 static const int BT_RT_PRIORITY = 1;
36 
37 namespace android {
38 namespace hardware {
39 namespace bluetooth {
40 namespace async {
41 
WatchFdForNonBlockingReads(int file_descriptor,const ReadCallback & on_read_fd_ready_callback)42 int AsyncFdWatcher::WatchFdForNonBlockingReads(
43     int file_descriptor, const ReadCallback& on_read_fd_ready_callback) {
44   // Add file descriptor and callback
45   {
46     std::unique_lock<std::mutex> guard(internal_mutex_);
47     watched_fds_[file_descriptor] = on_read_fd_ready_callback;
48   }
49 
50   // Start the thread if not started yet
51   return tryStartThread();
52 }
53 
ConfigureTimeout(const std::chrono::milliseconds timeout,const TimeoutCallback & on_timeout_callback)54 int AsyncFdWatcher::ConfigureTimeout(
55     const std::chrono::milliseconds timeout,
56     const TimeoutCallback& on_timeout_callback) {
57   // Add timeout and callback
58   {
59     std::unique_lock<std::mutex> guard(timeout_mutex_);
60     timeout_cb_ = on_timeout_callback;
61     timeout_ms_ = timeout;
62   }
63 
64   notifyThread();
65   return 0;
66 }
67 
StopWatchingFileDescriptors()68 void AsyncFdWatcher::StopWatchingFileDescriptors() { stopThread(); }
69 
~AsyncFdWatcher()70 AsyncFdWatcher::~AsyncFdWatcher() {}
71 
72 // Make sure to call this with at least one file descriptor ready to be
73 // watched upon or the thread routine will return immediately
tryStartThread()74 int AsyncFdWatcher::tryStartThread() {
75   if (std::atomic_exchange(&running_, true)) return 0;
76 
77   // Set up the communication channel
78   int pipe_fds[2];
79   if (pipe2(pipe_fds, O_NONBLOCK)) return -1;
80 
81   notification_listen_fd_ = pipe_fds[0];
82   notification_write_fd_ = pipe_fds[1];
83 
84   thread_ = std::thread([this]() { ThreadRoutine(); });
85   if (!thread_.joinable()) return -1;
86 
87   return 0;
88 }
89 
stopThread()90 int AsyncFdWatcher::stopThread() {
91   if (!std::atomic_exchange(&running_, false)) return 0;
92 
93   notifyThread();
94   if (std::this_thread::get_id() != thread_.get_id()) {
95     thread_.join();
96   }
97 
98   {
99     std::unique_lock<std::mutex> guard(internal_mutex_);
100     watched_fds_.clear();
101   }
102 
103   {
104     std::unique_lock<std::mutex> guard(timeout_mutex_);
105     timeout_cb_ = nullptr;
106   }
107 
108   close(notification_listen_fd_);
109   close(notification_write_fd_);
110 
111   return 0;
112 }
113 
notifyThread()114 int AsyncFdWatcher::notifyThread() {
115   uint8_t buffer[] = {0};
116   if (TEMP_FAILURE_RETRY(write(notification_write_fd_, &buffer, 1)) < 0) {
117     return -1;
118   }
119   return 0;
120 }
121 
ThreadRoutine()122 void AsyncFdWatcher::ThreadRoutine() {
123   // Make watching thread RT.
124   struct sched_param rt_params;
125   rt_params.sched_priority = BT_RT_PRIORITY;
126   if (sched_setscheduler(gettid(), SCHED_FIFO, &rt_params)) {
127     ALOGE("%s unable to set SCHED_FIFO for pid %d, tid %d, error %s", __func__,
128           getpid(), gettid(), strerror(errno));
129   }
130 
131   while (running_) {
132     fd_set read_fds;
133     FD_ZERO(&read_fds);
134     FD_SET(notification_listen_fd_, &read_fds);
135     int max_read_fd = INVALID_FD;
136     for (auto& it : watched_fds_) {
137       FD_SET(it.first, &read_fds);
138       max_read_fd = std::max(max_read_fd, it.first);
139     }
140 
141     struct timeval timeout;
142     struct timeval* timeout_ptr = NULL;
143     if (timeout_ms_ > std::chrono::milliseconds(0)) {
144       timeout.tv_sec = timeout_ms_.count() / 1000;
145       timeout.tv_usec = (timeout_ms_.count() % 1000) * 1000;
146       timeout_ptr = &timeout;
147     }
148 
149     // Wait until there is data available to read on some FD.
150     int nfds = std::max(notification_listen_fd_, max_read_fd);
151     int retval = select(nfds + 1, &read_fds, NULL, NULL, timeout_ptr);
152 
153     // There was some error.
154     if (retval < 0) continue;
155 
156     // Timeout.
157     if (retval == 0) {
158       // Allow the timeout callback to modify the timeout.
159       TimeoutCallback saved_cb;
160       {
161         std::unique_lock<std::mutex> guard(timeout_mutex_);
162         if (timeout_ms_ > std::chrono::milliseconds(0)) saved_cb = timeout_cb_;
163       }
164       if (saved_cb != nullptr) saved_cb();
165       continue;
166     }
167 
168     // Read data from the notification FD.
169     if (FD_ISSET(notification_listen_fd_, &read_fds)) {
170       char buffer[] = {0};
171       TEMP_FAILURE_RETRY(read(notification_listen_fd_, buffer, 1));
172       continue;
173     }
174 
175     // Invoke the data ready callbacks if appropriate.
176     {
177       // Hold the mutex to make sure that the callbacks are still valid.
178       std::unique_lock<std::mutex> guard(internal_mutex_);
179       for (auto& it : watched_fds_) {
180         if (FD_ISSET(it.first, &read_fds)) {
181           it.second(it.first);
182         }
183       }
184     }
185   }
186 }
187 
188 }  // namespace async
189 }  // namespace bluetooth
190 }  // namespace hardware
191 }  // namespace android
192