• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "android.hardware.bluetooth@1.0-impl"
18 
19 #include "async_fd_watcher.h"
20 
21 #include <algorithm>
22 #include <atomic>
23 #include <condition_variable>
24 #include <map>
25 #include <mutex>
26 #include <thread>
27 #include <log/log.h>
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   return 0;
109 }
110 
notifyThread()111 int AsyncFdWatcher::notifyThread() {
112   uint8_t buffer[] = {0};
113   if (TEMP_FAILURE_RETRY(write(notification_write_fd_, &buffer, 1)) < 0) {
114     return -1;
115   }
116   return 0;
117 }
118 
ThreadRoutine()119 void AsyncFdWatcher::ThreadRoutine() {
120   // Make watching thread RT.
121   struct sched_param rt_params;
122   rt_params.sched_priority = BT_RT_PRIORITY;
123   if (sched_setscheduler(gettid(), SCHED_FIFO, &rt_params)) {
124     ALOGE("%s unable to set SCHED_FIFO for pid %d, tid %d, error %s", __func__,
125           getpid(), gettid(), strerror(errno));
126   }
127 
128   while (running_) {
129     fd_set read_fds;
130     FD_ZERO(&read_fds);
131     FD_SET(notification_listen_fd_, &read_fds);
132     int max_read_fd = INVALID_FD;
133     for (auto& it : watched_fds_) {
134       FD_SET(it.first, &read_fds);
135       max_read_fd = std::max(max_read_fd, it.first);
136     }
137 
138     struct timeval timeout;
139     struct timeval* timeout_ptr = NULL;
140     if (timeout_ms_ > std::chrono::milliseconds(0)) {
141       timeout.tv_sec = timeout_ms_.count() / 1000;
142       timeout.tv_usec = (timeout_ms_.count() % 1000) * 1000;
143       timeout_ptr = &timeout;
144     }
145 
146     // Wait until there is data available to read on some FD.
147     int nfds = std::max(notification_listen_fd_, max_read_fd);
148     int retval = select(nfds + 1, &read_fds, NULL, NULL, timeout_ptr);
149 
150     // There was some error.
151     if (retval < 0) continue;
152 
153     // Timeout.
154     if (retval == 0) {
155       // Allow the timeout callback to modify the timeout.
156       TimeoutCallback saved_cb;
157       {
158         std::unique_lock<std::mutex> guard(timeout_mutex_);
159         if (timeout_ms_ > std::chrono::milliseconds(0))
160           saved_cb = timeout_cb_;
161       }
162       if (saved_cb != nullptr)
163         saved_cb();
164       continue;
165     }
166 
167     // Read data from the notification FD.
168     if (FD_ISSET(notification_listen_fd_, &read_fds)) {
169       char buffer[] = {0};
170       TEMP_FAILURE_RETRY(read(notification_listen_fd_, buffer, 1));
171       continue;
172     }
173 
174     // Invoke the data ready callbacks if appropriate.
175     {
176       // Hold the mutex to make sure that the callbacks are still valid.
177       std::unique_lock<std::mutex> guard(internal_mutex_);
178       for (auto& it : watched_fds_) {
179         if (FD_ISSET(it.first, &read_fds)) {
180         it.second(it.first);
181         }
182       }
183     }
184   }
185 }
186 
187 } // namespace async
188 } // namespace bluetooth
189 } // namespace hardware
190 } // namespace android
191