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> 21 22 #include <future> 23 #include <string> 24 25 #include "async_fd_watcher.h" 26 #include "h4_protocol.h" 27 #include "net_bluetooth_mgmt.h" 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 68 int getFdFromDevPath(); 69 [[nodiscard]] ndk::ScopedAStatus send( 70 ::android::hardware::bluetooth::hci::PacketType type, 71 const std::vector<uint8_t>& packet); 72 std::unique_ptr<NetBluetoothMgmt> management_{}; 73 74 // Send a reset command and discard all packets until a reset is received. 75 void reset(); 76 77 // Don't close twice or open before close is complete 78 std::mutex mStateMutex; 79 enum class HalState { 80 READY, 81 INITIALIZING, 82 ONE_CLIENT, 83 CLOSING, 84 } mState{HalState::READY}; 85 }; 86 87 } // namespace aidl::android::hardware::bluetooth::impl 88