1 /*
2  *
3  *  Copyright 2019 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 
19 #include "common/bind.h"
20 #include "hci/hci_layer.h"
21 
22 namespace bluetooth {
23 namespace security {
24 
25 using common::ContextualOnceCallback;
26 using hci::CommandCompleteView;
27 using hci::CommandStatusView;
28 using hci::EventBuilder;
29 using hci::EventCode;
30 using hci::EventView;
31 using hci::HciLayer;
32 
33 namespace {
34 
GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet)35 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
36   auto bytes = std::make_shared<std::vector<uint8_t>>();
37   BitInserter i(*bytes);
38   bytes->reserve(packet->size());
39   packet->Serialize(i);
40   return packet::PacketView<packet::kLittleEndian>(bytes);
41 }
42 
43 class CommandQueueEntry {
44  public:
CommandQueueEntry(std::unique_ptr<hci::CommandBuilder> command_packet,ContextualOnceCallback<void (CommandCompleteView)> on_complete_function)45   CommandQueueEntry(
46       std::unique_ptr<hci::CommandBuilder> command_packet,
47       ContextualOnceCallback<void(CommandCompleteView)> on_complete_function)
48       : command(std::move(command_packet)), waiting_for_status_(false), on_complete(std::move(on_complete_function)) {}
49 
CommandQueueEntry(std::unique_ptr<hci::CommandBuilder> command_packet,ContextualOnceCallback<void (CommandStatusView)> on_status_function)50   CommandQueueEntry(
51       std::unique_ptr<hci::CommandBuilder> command_packet,
52       ContextualOnceCallback<void(CommandStatusView)> on_status_function)
53       : command(std::move(command_packet)), waiting_for_status_(true), on_status(std::move(on_status_function)) {}
54 
55   std::unique_ptr<hci::CommandBuilder> command;
56   bool waiting_for_status_;
57   ContextualOnceCallback<void(CommandStatusView)> on_status;
58   ContextualOnceCallback<void(CommandCompleteView)> on_complete;
59 };
60 
61 }  // namespace
62 
63 class FakeHciLayer : public HciLayer {
64  public:
EnqueueCommand(std::unique_ptr<hci::CommandBuilder> command,ContextualOnceCallback<void (CommandStatusView)> on_status)65   void EnqueueCommand(
66       std::unique_ptr<hci::CommandBuilder> command,
67       ContextualOnceCallback<void(CommandStatusView)> on_status) override {
68     auto command_queue_entry = std::make_unique<CommandQueueEntry>(std::move(command), std::move(on_status));
69     command_queue_.push(std::move(command_queue_entry));
70   }
71 
EnqueueCommand(std::unique_ptr<hci::CommandBuilder> command,ContextualOnceCallback<void (CommandCompleteView)> on_complete)72   void EnqueueCommand(
73       std::unique_ptr<hci::CommandBuilder> command,
74       ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
75     auto command_queue_entry = std::make_unique<CommandQueueEntry>(std::move(command), std::move(on_complete));
76     command_queue_.push(std::move(command_queue_entry));
77   }
78 
GetLastCommand()79   std::unique_ptr<CommandQueueEntry> GetLastCommand() {
80     EXPECT_FALSE(command_queue_.empty());
81     auto last = std::move(command_queue_.front());
82     command_queue_.pop();
83     return last;
84   }
85 
RegisterEventHandler(EventCode event_code,common::ContextualCallback<void (EventView)> event_handler)86   void RegisterEventHandler(EventCode event_code, common::ContextualCallback<void(EventView)> event_handler) override {
87     registered_events_[event_code] = event_handler;
88   }
89 
UnregisterEventHandler(EventCode event_code)90   void UnregisterEventHandler(EventCode event_code) override {
91     registered_events_.erase(event_code);
92   }
93 
IncomingEvent(std::unique_ptr<EventBuilder> event_builder)94   void IncomingEvent(std::unique_ptr<EventBuilder> event_builder) {
95     auto packet = GetPacketView(std::move(event_builder));
96     EventView event = EventView::Create(packet);
97     ASSERT_TRUE(event.IsValid());
98     EventCode event_code = event.GetEventCode();
99     ASSERT_TRUE(registered_events_.find(event_code) != registered_events_.end());
100     registered_events_[event_code].Invoke(event);
101   }
102 
ListDependencies(ModuleList * list)103   void ListDependencies(ModuleList* list) override {}
Start()104   void Start() override {}
Stop()105   void Stop() override {}
106 
107  private:
108   std::map<EventCode, common::ContextualCallback<void(EventView)>> registered_events_;
109   std::queue<std::unique_ptr<CommandQueueEntry>> command_queue_;
110 };
111 
112 }  // namespace security
113 }  // namespace bluetooth
114