1 /*
2  * Copyright 2023 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 "hal/hci_hal_fake.h"
18 
19 namespace bluetooth {
20 namespace hal {
21 
sendHciCommand(hal::HciPacket command)22 void TestHciHal::sendHciCommand(hal::HciPacket command) {
23   outgoing_commands_.push(std::move(command));
24 }
25 
sendAclData(hal::HciPacket data)26 void TestHciHal::sendAclData(hal::HciPacket data) {
27   outgoing_acl_.push(std::move(data));
28 }
29 
sendScoData(hal::HciPacket data)30 void TestHciHal::sendScoData(hal::HciPacket data) {
31   outgoing_sco_.push(std::move(data));
32 }
33 
sendIsoData(hal::HciPacket data)34 void TestHciHal::sendIsoData(hal::HciPacket data) {
35   outgoing_iso_.push(std::move(data));
36 }
37 
GetPacketView(hal::HciPacket data)38 packet::PacketView<packet::kLittleEndian> TestHciHal::GetPacketView(hal::HciPacket data) {
39   auto shared = std::make_shared<std::vector<uint8_t>>(data);
40   return packet::PacketView<packet::kLittleEndian>(shared);
41 }
42 
GetSentCommand(std::chrono::milliseconds timeout)43 std::optional<hci::CommandView> TestHciHal::GetSentCommand(std::chrono::milliseconds timeout) {
44   if (!outgoing_commands_.wait_to_take(timeout)) {
45     // Timed out
46     return {};
47   }
48   auto command = hci::CommandView::Create(GetPacketView(std::move(outgoing_commands_.take())));
49   log::assert_that(command.IsValid(), "assert failed: command.IsValid()");
50   return command;
51 }
52 
GetSentAcl(std::chrono::milliseconds timeout)53 std::optional<hci::AclView> TestHciHal::GetSentAcl(std::chrono::milliseconds timeout) {
54   if (!outgoing_acl_.wait_to_take(timeout)) {
55     // Timed out
56     return {};
57   }
58   auto acl = hci::AclView::Create(GetPacketView(std::move(outgoing_acl_.take())));
59   log::assert_that(acl.IsValid(), "assert failed: acl.IsValid()");
60   return acl;
61 }
62 
GetSentSco(std::chrono::milliseconds timeout)63 std::optional<hci::ScoView> TestHciHal::GetSentSco(std::chrono::milliseconds timeout) {
64   if (!outgoing_commands_.wait_to_take(timeout)) {
65     // Timed out
66     return {};
67   }
68   auto sco = hci::ScoView::Create(GetPacketView(std::move(outgoing_sco_.take())));
69   log::assert_that(sco.IsValid(), "assert failed: sco.IsValid()");
70   return sco;
71 }
72 
GetSentIso(std::chrono::milliseconds timeout)73 std::optional<hci::IsoView> TestHciHal::GetSentIso(std::chrono::milliseconds timeout) {
74   if (!outgoing_commands_.wait_to_take(timeout)) {
75     // Timed out
76     return {};
77   }
78   log::assert_that(
79       outgoing_iso_.wait_to_take(timeout), "assert failed: outgoing_iso_.wait_to_take(timeout)");
80   auto iso = hci::IsoView::Create(GetPacketView(std::move(outgoing_iso_.take())));
81   log::assert_that(iso.IsValid(), "assert failed: iso.IsValid()");
82   return iso;
83 }
84 
InjectEvent(std::unique_ptr<packet::BasePacketBuilder> event)85 void TestHciHal::InjectEvent(std::unique_ptr<packet::BasePacketBuilder> event) {
86   log::assert_that(callbacks != nullptr, "assert failed: callbacks != nullptr");
87   auto view = std::vector<uint8_t>();
88   packet::BitInserter bi{view};
89   event->Serialize(bi);
90   callbacks->hciEventReceived(view);
91 }
92 
__anon5b7aaf1e0102() 93 const ModuleFactory TestHciHal::Factory = ModuleFactory([]() { return new TestHciHal(); });
94 }  // namespace hal
95 }  // namespace bluetooth
96