1 /*
2  * Copyright 2019 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 <chrono>
20 #include <list>
21 #include <memory>
22 #include <string>
23 
24 #include "address.h"
25 #include "class_of_device.h"
26 #include "common/bidi_queue.h"
27 #include "common/contextual_callback.h"
28 #include "hci/acl_connection_interface.h"
29 #include "hci/distance_measurement_interface.h"
30 #include "hci/hci_interface.h"
31 #include "hci/hci_packets.h"
32 #include "hci/le_acl_connection_interface.h"
33 #include "hci/le_advertising_interface.h"
34 #include "hci/le_iso_interface.h"
35 #include "hci/le_scanning_interface.h"
36 #include "hci/le_security_interface.h"
37 #include "hci/security_interface.h"
38 #include "module.h"
39 #include "os/handler.h"
40 
41 namespace bluetooth {
42 namespace hci {
43 
44 class HciLayer : public Module, public HciInterface {
45   // LINT.IfChange
46  public:
47   HciLayer();
48   HciLayer(const HciLayer&) = delete;
49   HciLayer& operator=(const HciLayer&) = delete;
50 
51   virtual ~HciLayer();
52 
53   void EnqueueCommand(
54       std::unique_ptr<CommandBuilder> command,
55       common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override;
56 
57   void EnqueueCommand(
58       std::unique_ptr<CommandBuilder> command,
59       common::ContextualOnceCallback<void(CommandStatusView)> on_status) override;
60 
61   virtual common::BidiQueueEnd<AclBuilder, AclView>* GetAclQueueEnd();
62 
63   virtual common::BidiQueueEnd<ScoBuilder, ScoView>* GetScoQueueEnd();
64 
65   virtual common::BidiQueueEnd<IsoBuilder, IsoView>* GetIsoQueueEnd();
66 
67   virtual void RegisterEventHandler(EventCode event_code, common::ContextualCallback<void(EventView)> event_handler);
68 
69   virtual void UnregisterEventHandler(EventCode event_code);
70 
71   virtual void RegisterLeEventHandler(SubeventCode subevent_code,
72                                       common::ContextualCallback<void(LeMetaEventView)> event_handler);
73 
74   virtual void UnregisterLeEventHandler(SubeventCode subevent_code);
75 
76   virtual void RegisterVendorSpecificEventHandler(
77       VseSubeventCode event, common::ContextualCallback<void(VendorSpecificEventView)> handler);
78 
79   virtual void UnregisterVendorSpecificEventHandler(VseSubeventCode event);
80 
81   virtual void RegisterForDisconnects(
82       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect);
83 
84   virtual SecurityInterface* GetSecurityInterface(common::ContextualCallback<void(EventView)> event_handler);
85 
86   virtual LeSecurityInterface* GetLeSecurityInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler);
87 
88   virtual AclConnectionInterface* GetAclConnectionInterface(
89       common::ContextualCallback<void(EventView)> event_handler,
90       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
91       common::ContextualCallback<void(Address, ClassOfDevice)> on_connection_request,
92       common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
93           on_read_remote_version_complete);
94   virtual void PutAclConnectionInterface();
95 
96   virtual LeAclConnectionInterface* GetLeAclConnectionInterface(
97       common::ContextualCallback<void(LeMetaEventView)> event_handler,
98       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
99       common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
100           on_read_remote_version_complete);
101   virtual void PutLeAclConnectionInterface();
102 
103   virtual LeAdvertisingInterface* GetLeAdvertisingInterface(
104       common::ContextualCallback<void(LeMetaEventView)> event_handler);
105 
106   virtual LeScanningInterface* GetLeScanningInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler);
107 
108   virtual void RegisterForScoConnectionRequests(
109       common::ContextualCallback<void(Address, ClassOfDevice, ConnectionRequestLinkType)>
110           on_sco_connection_request);
111 
112   virtual LeIsoInterface* GetLeIsoInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler);
113 
114   virtual DistanceMeasurementInterface* GetDistanceMeasurementInterface(
115       common::ContextualCallback<void(LeMetaEventView)> event_handler);
116 
ToString()117   std::string ToString() const override {
118     return "Hci Layer";
119   }
120 
121   static constexpr std::chrono::milliseconds kHciTimeoutMs = std::chrono::milliseconds(2000);
122   static constexpr std::chrono::milliseconds kHciTimeoutRestartMs = std::chrono::milliseconds(5000);
123 
124   static const ModuleFactory Factory;
125 
126  protected:
127   // LINT.ThenChange(fuzz/fuzz_hci_layer.h)
128   void ListDependencies(ModuleList* list) const override;
129 
130   void Start() override;
131 
132   void StartWithNoHalDependencies(os::Handler* handler);
133 
134   void Stop() override;
135 
136   virtual void Disconnect(uint16_t handle, ErrorCode reason);
137   virtual void ReadRemoteVersion(
138       hci::ErrorCode hci_status,
139       uint16_t handle,
140       uint8_t version,
141       uint16_t manufacturer_name,
142       uint16_t sub_version);
143 
144   std::list<common::ContextualCallback<void(uint16_t, ErrorCode)>> disconnect_handlers_;
145   std::list<common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>>
146       read_remote_version_handlers_;
147 
148  private:
149   struct impl;
150   struct hal_callbacks;
151   impl* impl_;
152   hal_callbacks* hal_callbacks_;
153 
154   std::mutex callback_handlers_guard_;
155   void on_connection_request(EventView event_view);
156   void on_disconnection_complete(EventView event_view);
157   void on_read_remote_version_complete(EventView event_view);
158 
159   common::ContextualCallback<void(Address bd_addr, ClassOfDevice cod)> on_acl_connection_request_{};
160   common::ContextualCallback<void(
161       Address bd_addr, ClassOfDevice cod, ConnectionRequestLinkType link_type)>
162       on_sco_connection_request_{};
163 
164   // Interfaces
165   CommandInterfaceImpl<AclCommandBuilder> acl_connection_manager_interface_{*this};
166   CommandInterfaceImpl<AclCommandBuilder> le_acl_connection_manager_interface_{*this};
167   CommandInterfaceImpl<SecurityCommandBuilder> security_interface{*this};
168   CommandInterfaceImpl<LeSecurityCommandBuilder> le_security_interface{*this};
169   CommandInterfaceImpl<LeAdvertisingCommandBuilder> le_advertising_interface{*this};
170   CommandInterfaceImpl<LeScanningCommandBuilder> le_scanning_interface{*this};
171   CommandInterfaceImpl<LeIsoCommandBuilder> le_iso_interface{*this};
172   CommandInterfaceImpl<DistanceMeasurementCommandBuilder> distance_measurement_interface{*this};
173 };
174 }  // namespace hci
175 }  // namespace bluetooth
176