/* * Copyright 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include "common/bidi_queue.h" #include "common/contextual_callback.h" #include "hci/acl_connection_interface.h" #include "hci/address.h" #include "hci/class_of_device.h" #include "hci/distance_measurement_interface.h" #include "hci/hci_interface.h" #include "hci/hci_packets.h" #include "hci/inquiry_interface.h" #include "hci/le_acl_connection_interface.h" #include "hci/le_advertising_interface.h" #include "hci/le_iso_interface.h" #include "hci/le_scanning_interface.h" #include "hci/le_security_interface.h" #include "hci/security_interface.h" #include "module.h" #include "os/handler.h" namespace bluetooth { namespace hci { class HciLayer : public Module, public HciInterface { // LINT.IfChange public: HciLayer(); HciLayer(const HciLayer&) = delete; HciLayer& operator=(const HciLayer&) = delete; virtual ~HciLayer(); void EnqueueCommand( std::unique_ptr command, common::ContextualOnceCallback on_complete) override; void EnqueueCommand(std::unique_ptr command, common::ContextualOnceCallback on_status) override; void EnqueueCommand(std::unique_ptr command, common::ContextualOnceCallback on_status_or_complete) override; virtual common::BidiQueueEnd* GetAclQueueEnd(); virtual common::BidiQueueEnd* GetScoQueueEnd(); virtual common::BidiQueueEnd* GetIsoQueueEnd(); virtual void RegisterEventHandler(EventCode event_code, common::ContextualCallback event_handler); virtual void UnregisterEventHandler(EventCode event_code); virtual void RegisterLeEventHandler( SubeventCode subevent_code, common::ContextualCallback event_handler); virtual void UnregisterLeEventHandler(SubeventCode subevent_code); virtual void RegisterVendorSpecificEventHandler( VseSubeventCode event, common::ContextualCallback handler); virtual void UnregisterVendorSpecificEventHandler(VseSubeventCode event); virtual void RegisterDefaultVendorSpecificEventHandler( common::ContextualCallback handler); virtual void UnregisterDefaultVendorSpecificEventHandler(); virtual void RegisterForDisconnects( common::ContextualCallback on_disconnect); virtual SecurityInterface* GetSecurityInterface( common::ContextualCallback event_handler); virtual LeSecurityInterface* GetLeSecurityInterface( common::ContextualCallback event_handler); virtual AclConnectionInterface* GetAclConnectionInterface( common::ContextualCallback event_handler, common::ContextualCallback on_disconnect, common::ContextualCallback on_connection_request, common::ContextualCallback on_read_remote_version_complete); virtual void PutAclConnectionInterface(); virtual LeAclConnectionInterface* GetLeAclConnectionInterface( common::ContextualCallback event_handler, common::ContextualCallback on_disconnect, common::ContextualCallback on_read_remote_version_complete); virtual void PutLeAclConnectionInterface(); virtual LeAdvertisingInterface* GetLeAdvertisingInterface( common::ContextualCallback event_handler); virtual LeScanningInterface* GetLeScanningInterface( common::ContextualCallback event_handler); virtual void RegisterForScoConnectionRequests( common::ContextualCallback on_sco_connection_request); virtual LeIsoInterface* GetLeIsoInterface( common::ContextualCallback event_handler); virtual DistanceMeasurementInterface* GetDistanceMeasurementInterface( common::ContextualCallback event_handler); std::unique_ptr GetInquiryInterface( common::ContextualCallback event_handler) override; std::string ToString() const override { return "Hci Layer"; } static constexpr std::chrono::milliseconds kHciTimeoutMs = std::chrono::milliseconds(2000); static constexpr std::chrono::milliseconds kHciTimeoutRestartMs = std::chrono::milliseconds(5000); static const ModuleFactory Factory; protected: // LINT.ThenChange(fuzz/fuzz_hci_layer.h) void ListDependencies(ModuleList* list) const override; void Start() override; void StartWithNoHalDependencies(os::Handler* handler); void Stop() override; virtual void Disconnect(uint16_t handle, ErrorCode reason); virtual void ReadRemoteVersion(hci::ErrorCode hci_status, uint16_t handle, uint8_t version, uint16_t manufacturer_name, uint16_t sub_version); std::list> disconnect_handlers_; std::list> read_remote_version_handlers_; private: struct impl; struct hal_callbacks; impl* impl_; hal_callbacks* hal_callbacks_; std::mutex callback_handlers_guard_; void on_connection_request(EventView event_view); void on_disconnection_complete(EventView event_view); void on_read_remote_version_complete(EventView event_view); common::ContextualCallback on_acl_connection_request_{}; common::ContextualCallback on_sco_connection_request_{}; // Interfaces CommandInterfaceImpl acl_connection_manager_interface_{this}; CommandInterfaceImpl le_acl_connection_manager_interface_{this}; CommandInterfaceImpl security_interface{this}; CommandInterfaceImpl le_security_interface{this}; CommandInterfaceImpl le_advertising_interface{this}; CommandInterfaceImpl le_scanning_interface{this}; CommandInterfaceImpl le_iso_interface{this}; CommandInterfaceImpl distance_measurement_interface{this}; }; } // namespace hci } // namespace bluetooth