Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 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 | |
Philip Cuadra | 8b3f315 | 2017-04-20 16:02:48 -0700 | [diff] [blame] | 17 | #define LOG_TAG "android.hardware.bluetooth@1.0-impl" |
| 18 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 19 | #include "async_fd_watcher.h" |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <atomic> |
| 23 | #include <condition_variable> |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 24 | #include <map> |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 25 | #include <mutex> |
| 26 | #include <thread> |
Philip Cuadra | 8b3f315 | 2017-04-20 16:02:48 -0700 | [diff] [blame] | 27 | #include <utils/Log.h> |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | #include "fcntl.h" |
| 30 | #include "sys/select.h" |
| 31 | #include "unistd.h" |
| 32 | |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 33 | static const int INVALID_FD = -1; |
| 34 | |
Philip Cuadra | 8b3f315 | 2017-04-20 16:02:48 -0700 | [diff] [blame] | 35 | static const int BT_RT_PRIORITY = 1; |
| 36 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace hardware { |
| 39 | namespace bluetooth { |
Myles Watson | be6176d | 2017-02-21 13:27:01 -0800 | [diff] [blame] | 40 | namespace async { |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 41 | |
| 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_); |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 47 | watched_fds_[file_descriptor] = on_read_fd_ready_callback; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Start the thread if not started yet |
Myles Watson | 5ea487b | 2017-02-21 16:53:34 -0800 | [diff] [blame] | 51 | return tryStartThread(); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Myles Watson | 7d42dca | 2017-01-24 16:51:39 -0800 | [diff] [blame] | 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 | |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 68 | void AsyncFdWatcher::StopWatchingFileDescriptors() { stopThread(); } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 69 | |
| 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 |
| 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 | |
| 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_); |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 100 | watched_fds_.clear(); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Myles Watson | 7d42dca | 2017-01-24 16:51:39 -0800 | [diff] [blame] | 103 | { |
| 104 | std::unique_lock<std::mutex> guard(timeout_mutex_); |
| 105 | timeout_cb_ = nullptr; |
| 106 | } |
| 107 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 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 | |
| 119 | void AsyncFdWatcher::ThreadRoutine() { |
Myles Watson | 8c71c85 | 2017-05-26 10:09:07 -0700 | [diff] [blame] | 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)); |
Philip Cuadra | 8b3f315 | 2017-04-20 16:02:48 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 128 | while (running_) { |
| 129 | fd_set read_fds; |
| 130 | FD_ZERO(&read_fds); |
| 131 | FD_SET(notification_listen_fd_, &read_fds); |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 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 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 137 | |
Myles Watson | 7d42dca | 2017-01-24 16:51:39 -0800 | [diff] [blame] | 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 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 145 | |
Myles Watson | 7d42dca | 2017-01-24 16:51:39 -0800 | [diff] [blame] | 146 | // Wait until there is data available to read on some FD. |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 147 | int nfds = std::max(notification_listen_fd_, max_read_fd); |
Myles Watson | 7d42dca | 2017-01-24 16:51:39 -0800 | [diff] [blame] | 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) { |
Myles Watson | eba1312 | 2017-02-02 10:47:36 -0800 | [diff] [blame] | 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(); |
Myles Watson | 7d42dca | 2017-01-24 16:51:39 -0800 | [diff] [blame] | 164 | continue; |
| 165 | } |
| 166 | |
| 167 | // Read data from the notification FD. |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 168 | if (FD_ISSET(notification_listen_fd_, &read_fds)) { |
| 169 | char buffer[] = {0}; |
| 170 | TEMP_FAILURE_RETRY(read(notification_listen_fd_, buffer, 1)); |
Myles Watson | 7d42dca | 2017-01-24 16:51:39 -0800 | [diff] [blame] | 171 | continue; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 174 | // Invoke the data ready callbacks if appropriate. |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 175 | { |
Myles Watson | 9ef1f71 | 2017-03-09 10:39:31 -0800 | [diff] [blame] | 176 | // Hold the mutex to make sure that the callbacks are still valid. |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 177 | std::unique_lock<std::mutex> guard(internal_mutex_); |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 178 | for (auto& it : watched_fds_) { |
| 179 | if (FD_ISSET(it.first, &read_fds)) { |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 180 | it.second(it.first); |
Myles Watson | 9ef1f71 | 2017-03-09 10:39:31 -0800 | [diff] [blame] | 181 | } |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame] | 182 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Myles Watson | be6176d | 2017-02-21 13:27:01 -0800 | [diff] [blame] | 187 | } // namespace async |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 188 | } // namespace bluetooth |
| 189 | } // namespace hardware |
| 190 | } // namespace android |