blob: bc0bc928d24176bc253310bda0656f5208be93af [file] [log] [blame]
Andre Eisenbach89ba5282016-10-13 15:45:02 -07001//
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 Cuadra8b3f3152017-04-20 16:02:48 -070017#define LOG_TAG "android.hardware.bluetooth@1.0-impl"
18
Andre Eisenbach89ba5282016-10-13 15:45:02 -070019#include "async_fd_watcher.h"
20
21#include <algorithm>
22#include <atomic>
23#include <condition_variable>
Myles Watsonf3a3cb72017-03-02 09:26:53 -080024#include <map>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070025#include <mutex>
26#include <thread>
Philip Cuadra8b3f3152017-04-20 16:02:48 -070027#include <utils/Log.h>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070028#include <vector>
29#include "fcntl.h"
30#include "sys/select.h"
31#include "unistd.h"
32
Myles Watsonf3a3cb72017-03-02 09:26:53 -080033static const int INVALID_FD = -1;
34
Philip Cuadra8b3f3152017-04-20 16:02:48 -070035static const int BT_RT_PRIORITY = 1;
36
Andre Eisenbach89ba5282016-10-13 15:45:02 -070037namespace android {
38namespace hardware {
39namespace bluetooth {
Myles Watsonbe6176d2017-02-21 13:27:01 -080040namespace async {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070041
42int 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 Watsonf3a3cb72017-03-02 09:26:53 -080047 watched_fds_[file_descriptor] = on_read_fd_ready_callback;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070048 }
49
50 // Start the thread if not started yet
Myles Watson5ea487b2017-02-21 16:53:34 -080051 return tryStartThread();
Andre Eisenbach89ba5282016-10-13 15:45:02 -070052}
53
Myles Watson7d42dca2017-01-24 16:51:39 -080054int 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 Watsonf3a3cb72017-03-02 09:26:53 -080068void AsyncFdWatcher::StopWatchingFileDescriptors() { stopThread(); }
Andre Eisenbach89ba5282016-10-13 15:45:02 -070069
70AsyncFdWatcher::~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
74int 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
90int 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 Watsonf3a3cb72017-03-02 09:26:53 -0800100 watched_fds_.clear();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700101 }
102
Myles Watson7d42dca2017-01-24 16:51:39 -0800103 {
104 std::unique_lock<std::mutex> guard(timeout_mutex_);
105 timeout_cb_ = nullptr;
106 }
107
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700108 return 0;
109}
110
111int 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
119void AsyncFdWatcher::ThreadRoutine() {
Myles Watson8c71c852017-05-26 10:09:07 -0700120 // 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 Cuadra8b3f3152017-04-20 16:02:48 -0700126 }
127
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700128 while (running_) {
129 fd_set read_fds;
130 FD_ZERO(&read_fds);
131 FD_SET(notification_listen_fd_, &read_fds);
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800132 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 Eisenbach89ba5282016-10-13 15:45:02 -0700137
Myles Watson7d42dca2017-01-24 16:51:39 -0800138 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 Eisenbach89ba5282016-10-13 15:45:02 -0700145
Myles Watson7d42dca2017-01-24 16:51:39 -0800146 // Wait until there is data available to read on some FD.
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800147 int nfds = std::max(notification_listen_fd_, max_read_fd);
Myles Watson7d42dca2017-01-24 16:51:39 -0800148 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 Watsoneba13122017-02-02 10:47:36 -0800155 // 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 Watson7d42dca2017-01-24 16:51:39 -0800164 continue;
165 }
166
167 // Read data from the notification FD.
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700168 if (FD_ISSET(notification_listen_fd_, &read_fds)) {
169 char buffer[] = {0};
170 TEMP_FAILURE_RETRY(read(notification_listen_fd_, buffer, 1));
Myles Watson7d42dca2017-01-24 16:51:39 -0800171 continue;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700172 }
173
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800174 // Invoke the data ready callbacks if appropriate.
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800175 {
Myles Watson9ef1f712017-03-09 10:39:31 -0800176 // Hold the mutex to make sure that the callbacks are still valid.
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700177 std::unique_lock<std::mutex> guard(internal_mutex_);
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800178 for (auto& it : watched_fds_) {
179 if (FD_ISSET(it.first, &read_fds)) {
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800180 it.second(it.first);
Myles Watson9ef1f712017-03-09 10:39:31 -0800181 }
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800182 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700183 }
184 }
185}
186
Myles Watsonbe6176d2017-02-21 13:27:01 -0800187} // namespace async
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700188} // namespace bluetooth
189} // namespace hardware
190} // namespace android