• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <hal/ffi.h>
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 
30 // This Bluetooth HAL implementation connects with a serial port at dev_path_.
31 class BluetoothHci : public hal::IBluetoothHci {
32  public:
33   BluetoothHci(const std::string& dev_path = "/dev/hvc5");
34 
35   void initialize(
36       const std::shared_ptr<hal::IBluetoothHciCallbacks>& cb) override;
37 
38   void sendHciCommand(const std::vector<uint8_t>& packet) override;
39 
40   void sendAclData(const std::vector<uint8_t>& packet) override;
41 
42   void sendScoData(const std::vector<uint8_t>& packet) override;
43 
44   void sendIsoData(const std::vector<uint8_t>& packet) override;
45 
46   void close() override;
47 
48   void clientDied() override;
49 
50   static void OnPacketReady();
51 
52   static BluetoothHci* get();
53 
54  private:
55   int mFd{-1};
56   std::shared_ptr<hal::IBluetoothHciCallbacks> mCb = nullptr;
57 
58   std::shared_ptr<::android::hardware::bluetooth::hci::H4Protocol> mH4;
59 
60   std::string mDevPath;
61 
62   ::android::hardware::bluetooth::async::AsyncFdWatcher mFdWatcher;
63 
64   int getFdFromDevPath();
65   void send(::android::hardware::bluetooth::hci::PacketType type,
66             const std::vector<uint8_t>& packet);
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
83