blob: 6866b342d2b9e2edc7c455e2fe93cd9e78d2a3ed [file] [log] [blame]
Martin Brabham80854c22019-11-12 14:52:42 -08001/*
Martin Brabham605d6f12019-03-29 12:02:30 -07002 *
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 *
Martin Brabham80854c22019-11-12 14:52:42 -080017 */
Martin Brabham605d6f12019-03-29 12:02:30 -070018
19#include "common/bind.h"
20#include "hci/hci_layer.h"
21
22namespace bluetooth {
23namespace security {
24
25using common::OnceCallback;
26using hci::CommandCompleteView;
27using hci::CommandPacketBuilder;
28using hci::CommandStatusView;
29using hci::EventCode;
30using hci::EventPacketBuilder;
31using hci::EventPacketView;
32using hci::HciLayer;
33using os::Handler;
34
35namespace {
36
37PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
38 auto bytes = std::make_shared<std::vector<uint8_t>>();
39 BitInserter i(*bytes);
40 bytes->reserve(packet->size());
41 packet->Serialize(i);
42 return packet::PacketView<packet::kLittleEndian>(bytes);
43}
44
45class CommandQueueEntry {
46 public:
47 CommandQueueEntry(std::unique_ptr<CommandPacketBuilder> command_packet,
48 OnceCallback<void(CommandCompleteView)> on_complete_function, Handler* handler)
49 : command(std::move(command_packet)), waiting_for_status_(false), on_complete(std::move(on_complete_function)),
50 caller_handler(handler) {}
51
52 CommandQueueEntry(std::unique_ptr<CommandPacketBuilder> command_packet,
53 OnceCallback<void(CommandStatusView)> on_status_function, Handler* handler)
54 : command(std::move(command_packet)), waiting_for_status_(true), on_status(std::move(on_status_function)),
55 caller_handler(handler) {}
56
57 std::unique_ptr<CommandPacketBuilder> command;
58 bool waiting_for_status_;
59 OnceCallback<void(CommandStatusView)> on_status;
60 OnceCallback<void(CommandCompleteView)> on_complete;
61 Handler* caller_handler;
62};
63
64} // namespace
65
66class FakeHciLayer : public HciLayer {
67 public:
68 void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command, OnceCallback<void(CommandStatusView)> on_status,
69 Handler* handler) override {
70 auto command_queue_entry = std::make_unique<CommandQueueEntry>(std::move(command), std::move(on_status), handler);
71 command_queue_.push(std::move(command_queue_entry));
72 }
73
74 void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
75 OnceCallback<void(CommandCompleteView)> on_complete, Handler* handler) override {
76 auto command_queue_entry = std::make_unique<CommandQueueEntry>(std::move(command), std::move(on_complete), handler);
77 command_queue_.push(std::move(command_queue_entry));
78 }
79
80 std::unique_ptr<CommandQueueEntry> GetLastCommand() {
81 EXPECT_FALSE(command_queue_.empty());
82 auto last = std::move(command_queue_.front());
83 command_queue_.pop();
84 return last;
85 }
86
87 void RegisterEventHandler(EventCode event_code, common::Callback<void(EventPacketView)> event_handler,
88 Handler* handler) override {
89 registered_events_[event_code] = event_handler;
90 }
91
92 void UnregisterEventHandler(EventCode event_code) override {
93 registered_events_.erase(event_code);
94 }
95
96 void IncomingEvent(std::unique_ptr<EventPacketBuilder> event_builder) {
97 auto packet = GetPacketView(std::move(event_builder));
98 EventPacketView event = EventPacketView::Create(packet);
Martin Brabham9f4120f2019-11-07 12:12:17 -080099 ASSERT_TRUE(event.IsValid());
Martin Brabham605d6f12019-03-29 12:02:30 -0700100 EventCode event_code = event.GetEventCode();
Martin Brabham80854c22019-11-12 14:52:42 -0800101 ASSERT_TRUE(registered_events_.find(event_code) != registered_events_.end());
Martin Brabham605d6f12019-03-29 12:02:30 -0700102 registered_events_[event_code].Run(event);
103 }
104
105 void ListDependencies(ModuleList* list) override {}
106 void Start() override {}
107 void Stop() override {}
108
109 private:
110 std::map<EventCode, common::Callback<void(EventPacketView)>> registered_events_;
111 std::queue<std::unique_ptr<CommandQueueEntry>> command_queue_;
112};
113
114} // namespace security
115} // namespace bluetooth