Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [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 | |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 19 | #include <hal/ffi.h> |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 20 | |
| 21 | #include <future> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "async_fd_watcher.h" |
| 25 | #include "h4_protocol.h" |
| 26 | #include "net_bluetooth_mgmt.h" |
| 27 | |
| 28 | namespace aidl::android::hardware::bluetooth::impl { |
| 29 | |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 30 | // This Bluetooth HAL implementation connects with a serial port at dev_path_. |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 31 | class BluetoothHci : public hal::IBluetoothHci { |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 32 | public: |
| 33 | BluetoothHci(const std::string& dev_path = "/dev/hvc5"); |
| 34 | |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 35 | void initialize( |
| 36 | const std::shared_ptr<hal::IBluetoothHciCallbacks>& cb) override; |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 37 | |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 38 | void sendHciCommand(const std::vector<uint8_t>& packet) override; |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 39 | |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 40 | void sendAclData(const std::vector<uint8_t>& packet) override; |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 41 | |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 42 | void sendScoData(const std::vector<uint8_t>& packet) override; |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 43 | |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 44 | void sendIsoData(const std::vector<uint8_t>& packet) override; |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 45 | |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 46 | void close() override; |
| 47 | |
| 48 | void clientDied() override; |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 49 | |
| 50 | static void OnPacketReady(); |
| 51 | |
| 52 | static BluetoothHci* get(); |
| 53 | |
| 54 | private: |
| 55 | int mFd{-1}; |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 56 | std::shared_ptr<hal::IBluetoothHciCallbacks> mCb = nullptr; |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 57 | |
| 58 | std::shared_ptr<::android::hardware::bluetooth::hci::H4Protocol> mH4; |
| 59 | |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 60 | std::string mDevPath; |
| 61 | |
| 62 | ::android::hardware::bluetooth::async::AsyncFdWatcher mFdWatcher; |
| 63 | |
| 64 | int getFdFromDevPath(); |
Antoine SOULIER | 35fef7c | 2025-02-26 23:01:21 +0000 | [diff] [blame] | 65 | void send(::android::hardware::bluetooth::hci::PacketType type, |
| 66 | const std::vector<uint8_t>& packet); |
Antoine SOULIER | f0d56e8 | 2025-02-26 17:35:45 +0000 | [diff] [blame] | 67 | std::unique_ptr<NetBluetoothMgmt> management_{}; |
| 68 | |
| 69 | // Send a reset command and discard all packets until a reset is received. |
| 70 | void reset(); |
| 71 | |
| 72 | // Don't close twice or open before close is complete |
| 73 | std::mutex mStateMutex; |
| 74 | enum class HalState { |
| 75 | READY, |
| 76 | INITIALIZING, |
| 77 | ONE_CLIENT, |
| 78 | CLOSING, |
| 79 | } mState{HalState::READY}; |
| 80 | }; |
| 81 | |
| 82 | } // namespace aidl::android::hardware::bluetooth::impl |