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/le_acl_connection_interface.h"
30 #include "hci/le_advertising_interface.h"
31 #include "hci/le_iso_interface.h"
32 #include "hci/le_scanning_interface.h"
33 #include "hci/le_security_interface.h"
34 #include "hci/security_interface.h"
35 
36 namespace bluetooth {
37 namespace hci {
38 
39 class HciInterface : public CommandInterface<CommandBuilder> {
40  public:
41   HciInterface() = default;
42   virtual ~HciInterface() = default;
43 
44   virtual void EnqueueCommand(
45       std::unique_ptr<CommandBuilder> command,
46       common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override = 0;
47 
48   virtual void EnqueueCommand(
49       std::unique_ptr<CommandBuilder> command,
50       common::ContextualOnceCallback<void(CommandStatusView)> on_status) override = 0;
51 
52   virtual common::BidiQueueEnd<AclBuilder, AclView>* GetAclQueueEnd() = 0;
53 
54   virtual common::BidiQueueEnd<ScoBuilder, ScoView>* GetScoQueueEnd() = 0;
55 
56   virtual common::BidiQueueEnd<IsoBuilder, IsoView>* GetIsoQueueEnd() = 0;
57 
58   virtual void RegisterEventHandler(
59       EventCode event_code, common::ContextualCallback<void(EventView)> event_handler) = 0;
60 
61   virtual void UnregisterEventHandler(EventCode event_code) = 0;
62 
63   virtual void RegisterLeEventHandler(
64       SubeventCode subevent_code,
65       common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
66 
67   virtual void UnregisterLeEventHandler(SubeventCode subevent_code) = 0;
68 
69   virtual void RegisterVendorSpecificEventHandler(
70       VseSubeventCode subevent_code,
71       common::ContextualCallback<void(VendorSpecificEventView)> event_handler) = 0;
72 
73   virtual void UnregisterVendorSpecificEventHandler(VseSubeventCode subevent_code) = 0;
74 
75   virtual void RegisterForDisconnects(
76       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect) = 0;
77 
78   virtual SecurityInterface* GetSecurityInterface(
79       common::ContextualCallback<void(EventView)> event_handler) = 0;
80 
81   virtual LeSecurityInterface* GetLeSecurityInterface(
82       common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
83 
84   virtual AclConnectionInterface* GetAclConnectionInterface(
85       common::ContextualCallback<void(EventView)> event_handler,
86       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
87       common::ContextualCallback<void(Address, ClassOfDevice)> on_connection_request,
88       common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
89           on_read_remote_version_complete) = 0;
90   virtual void PutAclConnectionInterface() = 0;
91 
92   virtual LeAclConnectionInterface* GetLeAclConnectionInterface(
93       common::ContextualCallback<void(LeMetaEventView)> event_handler,
94       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
95       common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
96           on_read_remote_version_complete) = 0;
97   virtual void PutLeAclConnectionInterface() = 0;
98 
99   virtual LeAdvertisingInterface* GetLeAdvertisingInterface(
100       common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
101 
102   virtual LeScanningInterface* GetLeScanningInterface(
103       common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
104 
105   virtual void RegisterForScoConnectionRequests(
106       common::ContextualCallback<void(Address, ClassOfDevice, ConnectionRequestLinkType)>
107           on_sco_connection_request) = 0;
108 
109   virtual LeIsoInterface* GetLeIsoInterface(
110       common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
111 
112   virtual DistanceMeasurementInterface* GetDistanceMeasurementInterface(
113       common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;
114 
115  protected:
116   template <typename T>
117   class CommandInterfaceImpl : public CommandInterface<T> {
118    public:
CommandInterfaceImpl(HciInterface & hci)119     explicit CommandInterfaceImpl(HciInterface& hci) : hci_(hci) {}
120     virtual ~CommandInterfaceImpl() = default;
121 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandCompleteView)> on_complete)122     void EnqueueCommand(
123         std::unique_ptr<T> command,
124         common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
125       hci_.EnqueueCommand(std::move(command), std::move(on_complete));
126     }
127 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandStatusView)> on_status)128     void EnqueueCommand(
129         std::unique_ptr<T> command,
130         common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
131       hci_.EnqueueCommand(std::move(command), std::move(on_status));
132     }
133     HciInterface& hci_;
134   };
135 };
136 
137 }  // namespace hci
138 }  // namespace bluetooth
139