Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 1 | /* |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 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 | * |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 17 | */ |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 18 | |
| 19 | #include "common/bind.h" |
| 20 | #include "hci/hci_layer.h" |
| 21 | |
| 22 | namespace bluetooth { |
| 23 | namespace security { |
| 24 | |
| 25 | using common::OnceCallback; |
| 26 | using hci::CommandCompleteView; |
| 27 | using hci::CommandPacketBuilder; |
| 28 | using hci::CommandStatusView; |
| 29 | using hci::EventCode; |
| 30 | using hci::EventPacketBuilder; |
| 31 | using hci::EventPacketView; |
| 32 | using hci::HciLayer; |
| 33 | using os::Handler; |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | PacketView<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 | |
| 45 | class 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 | |
| 66 | class 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 Brabham | 9f4120f | 2019-11-07 12:12:17 -0800 | [diff] [blame] | 99 | ASSERT_TRUE(event.IsValid()); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 100 | EventCode event_code = event.GetEventCode(); |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 101 | ASSERT_TRUE(registered_events_.find(event_code) != registered_events_.end()); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 102 | 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 |