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