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