blob: 18098ca12cd667e3972180d422fd735533b8e057 [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
Jack Hecaeab052018-10-23 18:13:51 -070021#include <log/log.h>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070022#include <algorithm>
23#include <atomic>
24#include <condition_variable>
Myles Watsonf3a3cb72017-03-02 09:26:53 -080025#include <map>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070026#include <mutex>
27#include <thread>
28#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
Peng Qi5c6da262017-08-22 17:38:34 +0800108 close(notification_listen_fd_);
109 close(notification_write_fd_);
110
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700111 return 0;
112}
113
114int 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
122void AsyncFdWatcher::ThreadRoutine() {
Myles Watson8c71c852017-05-26 10:09:07 -0700123 // 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));
Philip Cuadra8b3f3152017-04-20 16:02:48 -0700129 }
130
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700131 while (running_) {
132 fd_set read_fds;
133 FD_ZERO(&read_fds);
134 FD_SET(notification_listen_fd_, &read_fds);
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800135 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 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700140
Myles Watson7d42dca2017-01-24 16:51:39 -0800141 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 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700148
Myles Watson7d42dca2017-01-24 16:51:39 -0800149 // Wait until there is data available to read on some FD.
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800150 int nfds = std::max(notification_listen_fd_, max_read_fd);
Myles Watson7d42dca2017-01-24 16:51:39 -0800151 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) {
Myles Watsoneba13122017-02-02 10:47:36 -0800158 // Allow the timeout callback to modify the timeout.
159 TimeoutCallback saved_cb;
160 {
161 std::unique_lock<std::mutex> guard(timeout_mutex_);
Jack Hecaeab052018-10-23 18:13:51 -0700162 if (timeout_ms_ > std::chrono::milliseconds(0)) saved_cb = timeout_cb_;
Myles Watsoneba13122017-02-02 10:47:36 -0800163 }
Jack Hecaeab052018-10-23 18:13:51 -0700164 if (saved_cb != nullptr) saved_cb();
Myles Watson7d42dca2017-01-24 16:51:39 -0800165 continue;
166 }
167
168 // Read data from the notification FD.
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700169 if (FD_ISSET(notification_listen_fd_, &read_fds)) {
170 char buffer[] = {0};
171 TEMP_FAILURE_RETRY(read(notification_listen_fd_, buffer, 1));
Myles Watson7d42dca2017-01-24 16:51:39 -0800172 continue;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700173 }
174
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800175 // Invoke the data ready callbacks if appropriate.
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800176 {
Myles Watson9ef1f712017-03-09 10:39:31 -0800177 // Hold the mutex to make sure that the callbacks are still valid.
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700178 std::unique_lock<std::mutex> guard(internal_mutex_);
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800179 for (auto& it : watched_fds_) {
180 if (FD_ISSET(it.first, &read_fds)) {
Jack Hecaeab052018-10-23 18:13:51 -0700181 it.second(it.first);
Myles Watson9ef1f712017-03-09 10:39:31 -0800182 }
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800183 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700184 }
185 }
186}
187
Jack Hecaeab052018-10-23 18:13:51 -0700188} // namespace async
189} // namespace bluetooth
190} // namespace hardware
191} // namespace android