Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 21 | |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 22 | #include <future> |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
| 25 | #include "async_fd_watcher.h" |
| 26 | #include "h4_protocol.h" |
Myles Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 27 | #include "net_bluetooth_mgmt.h" |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 28 | |
| 29 | namespace aidl::android::hardware::bluetooth::impl { |
| 30 | |
| 31 | class BluetoothDeathRecipient; |
| 32 | |
| 33 | // This Bluetooth HAL implementation connects with a serial port at dev_path_. |
| 34 | class 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 Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 68 | int getFdFromDevPath(); |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 69 | void send(::android::hardware::bluetooth::hci::PacketType type, |
| 70 | const std::vector<uint8_t>& packet); |
Myles Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 71 | std::unique_ptr<NetBluetoothMgmt> management_{}; |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 72 | |
| 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 Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace aidl::android::hardware::bluetooth::impl |