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