1 /*
2  * Copyright 2024 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 <memory>
20 #include <utility>
21 
22 #include "common/bidi_queue.h"
23 #include "common/contextual_callback.h"
24 #include "hci/acl_connection_interface.h"
25 #include "hci/address.h"
26 #include "hci/class_of_device.h"
27 #include "hci/distance_measurement_interface.h"
28 #include "hci/hci_packets.h"
29 #include "hci/inquiry_interface.h"
30 #include "hci/le_acl_connection_interface.h"
31 #include "hci/le_advertising_interface.h"
32 #include "hci/le_iso_interface.h"
33 #include "hci/le_scanning_interface.h"
34 #include "hci/le_security_interface.h"
35 #include "hci/security_interface.h"
36 
37 namespace bluetooth {
38 namespace hci {
39 
40 class HciInterface : public CommandInterface<CommandBuilder> {
41 public:
42   HciInterface() = default;
43   virtual ~HciInterface() = default;
44 
45   void EnqueueCommand(
46           std::unique_ptr<CommandBuilder> command,
47           common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override = 0;
48 
49   void EnqueueCommand(std::unique_ptr<CommandBuilder> command,
50                       common::ContextualOnceCallback<void(CommandStatusView)> on_status) override =
51           0;
52 
53   void EnqueueCommand(std::unique_ptr<CommandBuilder> command,
54                       common::ContextualOnceCallback<void(CommandStatusOrCompleteView)>
55                               on_status_or_complete) override = 0;
56 
57   virtual common::BidiQueueEnd<AclBuilder, AclView>* GetAclQueueEnd() = 0;
58 
59   virtual common::BidiQueueEnd<ScoBuilder, ScoView>* GetScoQueueEnd() = 0;
60 
61   virtual common::BidiQueueEnd<IsoBuilder, IsoView>* GetIsoQueueEnd() = 0;
62 
63   virtual void RegisterEventHandler(EventCode event_code,
64                                     common::ContextualCallback<void(EventView)> event_handler) = 0;
65 
66   virtual void UnregisterEventHandler(EventCode event_code) = 0;
67 
68   virtual void RegisterDefaultVendorSpecificEventHandler(
69           common::ContextualCallback<void(VendorSpecificEventView)> handler) = 0;
70 
71   virtual void UnregisterDefaultVendorSpecificEventHandler() = 0;
72 
73   virtual void RegisterLeEventHandler(
74           SubeventCode subevent_code,
75           common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
76 
77   virtual void UnregisterLeEventHandler(SubeventCode subevent_code) = 0;
78 
79   virtual void RegisterVendorSpecificEventHandler(
80           VseSubeventCode subevent_code,
81           common::ContextualCallback<void(VendorSpecificEventView)> event_handler) = 0;
82 
83   virtual void UnregisterVendorSpecificEventHandler(VseSubeventCode subevent_code) = 0;
84 
85   virtual void RegisterForDisconnects(
86           common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect) = 0;
87 
88   virtual SecurityInterface* GetSecurityInterface(
89           common::ContextualCallback<void(EventView)> event_handler) = 0;
90 
91   virtual LeSecurityInterface* GetLeSecurityInterface(
92           common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
93 
94   virtual AclConnectionInterface* GetAclConnectionInterface(
95           common::ContextualCallback<void(EventView)> event_handler,
96           common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
97           common::ContextualCallback<void(Address, ClassOfDevice)> on_connection_request,
98           common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
99                   on_read_remote_version_complete) = 0;
100   virtual void PutAclConnectionInterface() = 0;
101 
102   virtual LeAclConnectionInterface* GetLeAclConnectionInterface(
103           common::ContextualCallback<void(LeMetaEventView)> event_handler,
104           common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
105           common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
106                   on_read_remote_version_complete) = 0;
107   virtual void PutLeAclConnectionInterface() = 0;
108 
109   virtual LeAdvertisingInterface* GetLeAdvertisingInterface(
110           common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
111 
112   virtual LeScanningInterface* GetLeScanningInterface(
113           common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
114 
115   virtual void RegisterForScoConnectionRequests(
116           common::ContextualCallback<void(Address, ClassOfDevice, ConnectionRequestLinkType)>
117                   on_sco_connection_request) = 0;
118 
119   virtual LeIsoInterface* GetLeIsoInterface(
120           common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
121 
122   virtual DistanceMeasurementInterface* GetDistanceMeasurementInterface(
123           common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
124 
125   virtual std::unique_ptr<InquiryInterface> GetInquiryInterface(
126           common::ContextualCallback<void(EventView)> event_handler) = 0;
127 
128 protected:
129   template <typename T>
130   class CommandInterfaceImpl : public CommandInterface<T> {
131   public:
CommandInterfaceImpl(HciInterface * hci,common::OnceCallback<void ()> cleanup)132     explicit CommandInterfaceImpl(HciInterface* hci, common::OnceCallback<void()> cleanup)
133         : hci_(hci), cleanup_(std::move(cleanup)) {}
CommandInterfaceImpl(HciInterface * hci)134     explicit CommandInterfaceImpl(HciInterface* hci) : hci_(hci) {
135       cleanup_ = common::BindOnce([]() {});
136     }
~CommandInterfaceImpl()137     ~CommandInterfaceImpl() { std::move(cleanup_).Run(); }
138 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandCompleteView)> on_complete)139     void EnqueueCommand(
140             std::unique_ptr<T> command,
141             common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
142       hci_->EnqueueCommand(std::move(command), std::move(on_complete));
143     }
144 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandStatusView)> on_status)145     void EnqueueCommand(
146             std::unique_ptr<T> command,
147             common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
148       hci_->EnqueueCommand(std::move(command), std::move(on_status));
149     }
150 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandStatusOrCompleteView)> on_status_or_complete)151     void EnqueueCommand(std::unique_ptr<T> command,
152                         common::ContextualOnceCallback<void(CommandStatusOrCompleteView)>
153                                 on_status_or_complete) override {
154       hci_->EnqueueCommand(std::move(command), std::move(on_status_or_complete));
155     }
156 
157     HciInterface* hci_;
158     common::OnceCallback<void()> cleanup_;
159   };
160 };
161 
162 }  // namespace hci
163 }  // namespace bluetooth
164