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