1 /* 2 * Copyright 2022 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 #include <future> 18 #include <list> 19 #include <map> 20 #include <memory> 21 #include <queue> 22 #include <vector> 23 24 #include "hci/hci_layer.h" 25 26 namespace bluetooth { 27 namespace hci { 28 29 packet::PacketView<packet::kLittleEndian> GetPacketView( 30 std::unique_ptr<packet::BasePacketBuilder> packet); 31 32 std::unique_ptr<BasePacketBuilder> NextPayload(uint16_t handle); 33 34 class HciLayerFake : public HciLayer { 35 public: 36 void EnqueueCommand(std::unique_ptr<CommandBuilder> command, 37 common::ContextualOnceCallback<void(CommandStatusView)> on_status) override; 38 39 void EnqueueCommand( 40 std::unique_ptr<CommandBuilder> command, 41 common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override; 42 43 void EnqueueCommand(std::unique_ptr<CommandBuilder> command, 44 common::ContextualOnceCallback<void(CommandStatusOrCompleteView)> 45 on_status_or_complete) override; 46 47 CommandView GetCommand(); 48 49 CommandView GetCommand(OpCode op_code); 50 51 void AssertNoQueuedCommand(); 52 53 void RegisterEventHandler(EventCode event_code, 54 common::ContextualCallback<void(EventView)> event_handler) override; 55 56 void UnregisterEventHandler(EventCode event_code) override; 57 58 void RegisterLeEventHandler( 59 SubeventCode subevent_code, 60 common::ContextualCallback<void(LeMetaEventView)> event_handler) override; 61 62 void UnregisterLeEventHandler(SubeventCode subevent_code) override; 63 64 void RegisterVendorSpecificEventHandler( 65 VseSubeventCode subevent_code, 66 common::ContextualCallback<void(VendorSpecificEventView)> event_handler) override; 67 68 void UnregisterVendorSpecificEventHandler(VseSubeventCode subevent_code) override; 69 70 void IncomingEvent(std::unique_ptr<EventBuilder> event_builder); 71 72 void IncomingLeMetaEvent(std::unique_ptr<LeMetaEventBuilder> event_builder); 73 74 void CommandCompleteCallback(EventView event); 75 76 void CommandStatusCallback(EventView event); 77 78 void IncomingAclData(uint16_t handle); 79 80 void IncomingAclData(uint16_t handle, std::unique_ptr<AclBuilder> acl_builder); 81 82 void AssertNoOutgoingAclData(); 83 84 packet::PacketView<packet::kLittleEndian> OutgoingAclData(); 85 86 common::BidiQueueEnd<AclBuilder, AclView>* GetAclQueueEnd() override; 87 88 void Disconnect(uint16_t handle, ErrorCode reason) override; 89 90 protected: 91 void ListDependencies(ModuleList* list) const override; 92 void Start() override; 93 void Stop() override; 94 95 private: 96 void InitEmptyCommand(); 97 void do_disconnect(uint16_t handle, ErrorCode reason); 98 99 // Handler-only state. Mutexes are not needed when accessing these fields. 100 std::list<common::ContextualOnceCallback<void(CommandCompleteView)>> command_complete_callbacks; 101 std::list<common::ContextualOnceCallback<void(CommandStatusView)>> command_status_callbacks; 102 std::map<EventCode, common::ContextualCallback<void(EventView)>> registered_events_; 103 std::map<SubeventCode, common::ContextualCallback<void(LeMetaEventView)>> registered_le_events_; 104 std::map<VseSubeventCode, common::ContextualCallback<void(VendorSpecificEventView)>> 105 registered_vs_events_; 106 107 // thread-safe 108 common::BidiQueue<AclView, AclBuilder> acl_queue_{3 /* TODO: Set queue depth */}; 109 110 // Most operations must acquire this mutex before manipulating shared state. The ONLY exception 111 // is blocking on a promise, IF your thread is the only one mutating it. Note that SETTING a 112 // promise REQUIRES a lock, since another thread may replace the promise while you are doing so. 113 mutable std::mutex mutex_{}; 114 115 // Shared state between the test and stack threads 116 std::queue<std::unique_ptr<CommandBuilder>> command_queue_; 117 118 // We start with Consumed=Set, Command=Unset. 119 // When a command is enqueued, we set Command=set 120 // When a command is popped, we block until Command=Set, then (if the queue is now empty) we 121 // reset Command=Unset and set Consumed=Set. This way we emulate a blocking queue. 122 std::promise<void> command_promise_{}; // Set when at least one command is in the queue 123 std::future<void> command_future_ = 124 command_promise_.get_future(); // GetCommand() blocks until this is fulfilled 125 126 CommandView empty_command_view_ = CommandView::Create( 127 PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>())); 128 }; 129 130 } // namespace hci 131 } // namespace bluetooth 132