blob: 1e4da8c8cde4933d7c515673d056da61e373d294 [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
17#pragma once
18
19#include <mutex>
20#include <thread>
21
22namespace android {
23namespace hardware {
24namespace bluetooth {
25namespace V1_0 {
26namespace implementation {
27
28using ReadCallback = std::function<void(int)>;
29
30class AsyncFdWatcher {
31 public:
32 AsyncFdWatcher() = default;
33 ~AsyncFdWatcher();
34
35 int WatchFdForNonBlockingReads(int file_descriptor,
36 const ReadCallback& on_read_fd_ready_callback);
37 void StopWatchingFileDescriptor();
38
39 private:
40 AsyncFdWatcher(const AsyncFdWatcher&) = delete;
41 AsyncFdWatcher& operator=(const AsyncFdWatcher&) = delete;
42
43 int tryStartThread();
44 int stopThread();
45 int notifyThread();
46 void ThreadRoutine();
47
48 std::atomic_bool running_{false};
49 std::thread thread_;
50 std::mutex internal_mutex_;
51
52 int read_fd_;
53 int notification_listen_fd_;
54 int notification_write_fd_;
55 ReadCallback cb_;
56};
57
58
59} // namespace implementation
60} // namespace V1_0
61} // namespace bluetooth
62} // namespace hardware
63} // namespace android