1*4d7e907cSAndroid Build Coastguard Worker /* 2*4d7e907cSAndroid Build Coastguard Worker * Copyright 2022 The Android Open Source Project 3*4d7e907cSAndroid Build Coastguard Worker * 4*4d7e907cSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*4d7e907cSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*4d7e907cSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*4d7e907cSAndroid Build Coastguard Worker * 8*4d7e907cSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*4d7e907cSAndroid Build Coastguard Worker * 10*4d7e907cSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*4d7e907cSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*4d7e907cSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*4d7e907cSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*4d7e907cSAndroid Build Coastguard Worker * limitations under the License. 15*4d7e907cSAndroid Build Coastguard Worker */ 16*4d7e907cSAndroid Build Coastguard Worker 17*4d7e907cSAndroid Build Coastguard Worker #pragma once 18*4d7e907cSAndroid Build Coastguard Worker 19*4d7e907cSAndroid Build Coastguard Worker #include <map> 20*4d7e907cSAndroid Build Coastguard Worker #include <mutex> 21*4d7e907cSAndroid Build Coastguard Worker #include <thread> 22*4d7e907cSAndroid Build Coastguard Worker 23*4d7e907cSAndroid Build Coastguard Worker namespace android::hardware::bluetooth::async { 24*4d7e907cSAndroid Build Coastguard Worker 25*4d7e907cSAndroid Build Coastguard Worker using ReadCallback = std::function<void(int)>; 26*4d7e907cSAndroid Build Coastguard Worker using TimeoutCallback = std::function<void(void)>; 27*4d7e907cSAndroid Build Coastguard Worker 28*4d7e907cSAndroid Build Coastguard Worker class AsyncFdWatcher { 29*4d7e907cSAndroid Build Coastguard Worker public: 30*4d7e907cSAndroid Build Coastguard Worker AsyncFdWatcher() = default; 31*4d7e907cSAndroid Build Coastguard Worker ~AsyncFdWatcher(); 32*4d7e907cSAndroid Build Coastguard Worker 33*4d7e907cSAndroid Build Coastguard Worker int WatchFdForNonBlockingReads(int file_descriptor, 34*4d7e907cSAndroid Build Coastguard Worker const ReadCallback& on_read_fd_ready_callback); 35*4d7e907cSAndroid Build Coastguard Worker int ConfigureTimeout(const std::chrono::milliseconds timeout, 36*4d7e907cSAndroid Build Coastguard Worker const TimeoutCallback& on_timeout_callback); 37*4d7e907cSAndroid Build Coastguard Worker void StopWatchingFileDescriptors(); 38*4d7e907cSAndroid Build Coastguard Worker 39*4d7e907cSAndroid Build Coastguard Worker private: 40*4d7e907cSAndroid Build Coastguard Worker AsyncFdWatcher(const AsyncFdWatcher&) = delete; 41*4d7e907cSAndroid Build Coastguard Worker AsyncFdWatcher& operator=(const AsyncFdWatcher&) = delete; 42*4d7e907cSAndroid Build Coastguard Worker 43*4d7e907cSAndroid Build Coastguard Worker int tryStartThread(); 44*4d7e907cSAndroid Build Coastguard Worker int stopThread(); 45*4d7e907cSAndroid Build Coastguard Worker int notifyThread(); 46*4d7e907cSAndroid Build Coastguard Worker void ThreadRoutine(); 47*4d7e907cSAndroid Build Coastguard Worker 48*4d7e907cSAndroid Build Coastguard Worker std::atomic_bool running_{false}; 49*4d7e907cSAndroid Build Coastguard Worker std::thread thread_; 50*4d7e907cSAndroid Build Coastguard Worker std::mutex internal_mutex_; 51*4d7e907cSAndroid Build Coastguard Worker std::mutex timeout_mutex_; 52*4d7e907cSAndroid Build Coastguard Worker 53*4d7e907cSAndroid Build Coastguard Worker std::map<int, ReadCallback> watched_fds_; 54*4d7e907cSAndroid Build Coastguard Worker int notification_listen_fd_; 55*4d7e907cSAndroid Build Coastguard Worker int notification_write_fd_; 56*4d7e907cSAndroid Build Coastguard Worker TimeoutCallback timeout_cb_; 57*4d7e907cSAndroid Build Coastguard Worker std::chrono::milliseconds timeout_ms_; 58*4d7e907cSAndroid Build Coastguard Worker }; 59*4d7e907cSAndroid Build Coastguard Worker 60*4d7e907cSAndroid Build Coastguard Worker } // namespace android::hardware::bluetooth::async 61