blob: 85aafc819779241b87574446de07bb61b2aa69d8 [file] [log] [blame]
Myles Watson6d5e7722022-09-30 06:22:43 -07001/*
2 * Copyright 2022 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 <aidl/android/hardware/bluetooth/BnBluetoothHci.h>
20#include <aidl/android/hardware/bluetooth/IBluetoothHciCallbacks.h>
Myles Watson6d5e7722022-09-30 06:22:43 -070021
Myles Watson65b47f52023-01-26 12:59:06 -080022#include <future>
Myles Watson6d5e7722022-09-30 06:22:43 -070023#include <string>
24
25#include "async_fd_watcher.h"
26#include "h4_protocol.h"
Myles Watsonefa25d72022-10-03 16:27:32 -070027#include "net_bluetooth_mgmt.h"
Myles Watson6d5e7722022-09-30 06:22:43 -070028
29namespace aidl::android::hardware::bluetooth::impl {
30
31class BluetoothDeathRecipient;
32
33// This Bluetooth HAL implementation connects with a serial port at dev_path_.
34class BluetoothHci : public BnBluetoothHci {
35 public:
36 BluetoothHci(const std::string& dev_path = "/dev/hvc5");
37
38 ndk::ScopedAStatus initialize(
39 const std::shared_ptr<IBluetoothHciCallbacks>& cb) override;
40
41 ndk::ScopedAStatus sendHciCommand(
42 const std::vector<uint8_t>& packet) override;
43
44 ndk::ScopedAStatus sendAclData(const std::vector<uint8_t>& packet) override;
45
46 ndk::ScopedAStatus sendScoData(const std::vector<uint8_t>& packet) override;
47
48 ndk::ScopedAStatus sendIsoData(const std::vector<uint8_t>& packet) override;
49
50 ndk::ScopedAStatus close() override;
51
52 static void OnPacketReady();
53
54 static BluetoothHci* get();
55
56 private:
57 int mFd{-1};
58 std::shared_ptr<IBluetoothHciCallbacks> mCb = nullptr;
59
60 std::shared_ptr<::android::hardware::bluetooth::hci::H4Protocol> mH4;
61
62 std::shared_ptr<BluetoothDeathRecipient> mDeathRecipient;
63
64 std::string mDevPath;
65
66 ::android::hardware::bluetooth::async::AsyncFdWatcher mFdWatcher;
67
Myles Watsonefa25d72022-10-03 16:27:32 -070068 int getFdFromDevPath();
Myles Watson6d5e7722022-09-30 06:22:43 -070069 void send(::android::hardware::bluetooth::hci::PacketType type,
70 const std::vector<uint8_t>& packet);
Myles Watsonefa25d72022-10-03 16:27:32 -070071 std::unique_ptr<NetBluetoothMgmt> management_{};
Myles Watson65b47f52023-01-26 12:59:06 -080072
73 // Send a reset command and discard all packets until a reset is received.
74 void reset();
75
76 // Don't close twice or open before close is complete
77 std::mutex mStateMutex;
78 enum class HalState {
79 READY,
80 INITIALIZING,
81 ONE_CLIENT,
82 CLOSING,
83 } mState{HalState::READY};
Myles Watson6d5e7722022-09-30 06:22:43 -070084};
85
86} // namespace aidl::android::hardware::bluetooth::impl