blob: 5c31468323c445a2d89ccc38f191ffc29fbf62b3 [file] [log] [blame]
Antoine SOULIERf0d56e82025-02-26 17:35:45 +00001/*
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 SOULIER35fef7c2025-02-26 23:01:21 +000019#include <hal/ffi.h>
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000020
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
28namespace aidl::android::hardware::bluetooth::impl {
29
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000030// This Bluetooth HAL implementation connects with a serial port at dev_path_.
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000031class BluetoothHci : public hal::IBluetoothHci {
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000032 public:
33 BluetoothHci(const std::string& dev_path = "/dev/hvc5");
34
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000035 void initialize(
36 const std::shared_ptr<hal::IBluetoothHciCallbacks>& cb) override;
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000037
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000038 void sendHciCommand(const std::vector<uint8_t>& packet) override;
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000039
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000040 void sendAclData(const std::vector<uint8_t>& packet) override;
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000041
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000042 void sendScoData(const std::vector<uint8_t>& packet) override;
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000043
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000044 void sendIsoData(const std::vector<uint8_t>& packet) override;
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000045
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000046 void close() override;
47
48 void clientDied() override;
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000049
50 static void OnPacketReady();
51
52 static BluetoothHci* get();
53
54 private:
55 int mFd{-1};
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000056 std::shared_ptr<hal::IBluetoothHciCallbacks> mCb = nullptr;
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000057
58 std::shared_ptr<::android::hardware::bluetooth::hci::H4Protocol> mH4;
59
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000060 std::string mDevPath;
61
62 ::android::hardware::bluetooth::async::AsyncFdWatcher mFdWatcher;
63
64 int getFdFromDevPath();
Antoine SOULIER35fef7c2025-02-26 23:01:21 +000065 void send(::android::hardware::bluetooth::hci::PacketType type,
66 const std::vector<uint8_t>& packet);
Antoine SOULIERf0d56e82025-02-26 17:35:45 +000067 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