/* * Copyright 2020 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 "hci/command_interface.h" #include "hci/hci_layer.h" #include "os/fuzz/dev_null_queue.h" #include "os/fuzz/fuzz_inject_queue.h" #include "os/log.h" #include #include "fuzz/helpers.h" namespace bluetooth { namespace hci { namespace fuzz { template class FuzzCommandInterface : public CommandInterface { public: void EnqueueCommand(std::unique_ptr command, common::ContextualOnceCallback on_complete) override {} void EnqueueCommand(std::unique_ptr command, common::ContextualOnceCallback on_status) override {} }; class FuzzHciLayer : public HciLayer { public: void TurnOnAutoReply(FuzzedDataProvider* fdp) { auto_reply_fdp = fdp; } void TurnOffAutoReply() { auto_reply_fdp = nullptr; } void EnqueueCommand( std::unique_ptr command, common::ContextualOnceCallback on_complete) override { on_command_complete_ = std::move(on_complete); if (auto_reply_fdp != nullptr) { injectCommandComplete(bluetooth::fuzz::GetArbitraryBytes(auto_reply_fdp)); } } void EnqueueCommand( std::unique_ptr command, common::ContextualOnceCallback on_status) override { on_command_status_ = std::move(on_status); if (auto_reply_fdp != nullptr) { injectCommandStatus(bluetooth::fuzz::GetArbitraryBytes(auto_reply_fdp)); } } common::BidiQueueEnd* GetAclQueueEnd() override { return acl_queue_.GetUpEnd(); } common::BidiQueueEnd* GetIsoQueueEnd() override { return iso_queue_.GetUpEnd(); } void RegisterEventHandler(hci::EventCode event, common::ContextualCallback handler) override { event_handlers_[event] = handler; } void UnregisterEventHandler(hci::EventCode event) override { auto it = event_handlers_.find(event); if (it != event_handlers_.end()) { event_handlers_.erase(it); } } void RegisterLeEventHandler(hci::SubeventCode event, common::ContextualCallback handler) override { le_event_handlers_[event] = handler; } void UnregisterLeEventHandler(hci::SubeventCode event) override { auto it = le_event_handlers_.find(event); if (it != le_event_handlers_.end()) { le_event_handlers_.erase(it); } } hci::SecurityInterface* GetSecurityInterface(common::ContextualCallback event_handler) override; hci::LeSecurityInterface* GetLeSecurityInterface( common::ContextualCallback event_handler) override; hci::AclConnectionInterface* GetAclConnectionInterface( common::ContextualCallback event_handler, common::ContextualCallback on_disconnect, common::ContextualCallback on_read_remote_version) override; hci::LeAclConnectionInterface* GetLeAclConnectionInterface( common::ContextualCallback event_handler, common::ContextualCallback on_disconnect, common::ContextualCallback on_read_remote_version) override; hci::LeAdvertisingInterface* GetLeAdvertisingInterface( common::ContextualCallback event_handler) override; hci::LeScanningInterface* GetLeScanningInterface( common::ContextualCallback event_handler) override; hci::LeIsoInterface* GetLeIsoInterface(common::ContextualCallback event_handler) override; void injectArbitrary(FuzzedDataProvider& fdp); std::string ToString() const override { return "FuzzHciLayer"; } static const ModuleFactory Factory; protected: void ListDependencies(ModuleList* list) override {} void Start() override; void Stop() override; private: void injectAclData(std::vector data); void injectCommandComplete(std::vector data); void injectCommandStatus(std::vector data); void injectEvent(FuzzedDataProvider& fdp); void injectLeEvent(FuzzedDataProvider& fdp); void injectSecurityEvent(std::vector data); void injectLeSecurityEvent(std::vector data); void injectAclEvent(std::vector data); void injectAclDisconnect(FuzzedDataProvider& fdp); void injectLeAclEvent(std::vector data); void injectLeAclDisconnect(FuzzedDataProvider& fdp); void injectLeAdvertisingEvent(std::vector data); void injectLeScanningEvent(std::vector data); void injectLeIsoEvent(std::vector data); FuzzedDataProvider* auto_reply_fdp; common::BidiQueue acl_queue_{3}; common::BidiQueue iso_queue_{3}; os::fuzz::DevNullQueue* acl_dev_null_; os::fuzz::FuzzInjectQueue* acl_inject_; FuzzCommandInterface acl_connection_interface_{}; FuzzCommandInterface le_acl_connection_interface_{}; FuzzCommandInterface security_interface_{}; FuzzCommandInterface le_security_interface_{}; FuzzCommandInterface le_advertising_interface_{}; FuzzCommandInterface le_scanning_interface_{}; FuzzCommandInterface le_iso_interface_{}; common::ContextualOnceCallback on_command_complete_; common::ContextualOnceCallback on_command_status_; std::map> event_handlers_; std::map> le_event_handlers_; common::ContextualCallback security_event_handler_; common::ContextualCallback le_security_event_handler_; common::ContextualCallback acl_event_handler_; common::ContextualCallback acl_on_disconnect_; common::ContextualCallback le_acl_event_handler_; common::ContextualCallback le_acl_on_disconnect_; common::ContextualCallback le_advertising_event_handler_; common::ContextualCallback le_scanning_event_handler_; common::ContextualCallback le_iso_event_handler_; }; } // namespace fuzz } // namespace hci } // namespace bluetooth