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 #pragma once
18 
19 #include <bluetooth/log.h>
20 
21 #include <future>
22 #include <list>
23 #include <optional>
24 
25 #include "common/blocking_queue.h"
26 #include "hal/hci_hal.h"
27 #include "hci/hci_packets.h"
28 #include "packet/packet_view.h"
29 
30 namespace bluetooth {
31 namespace hal {
32 
33 class TestHciHal : public hal::HciHal {
34 public:
TestHciHal()35   TestHciHal() : hal::HciHal() {}
36 
~TestHciHal()37   ~TestHciHal() {
38     if (callbacks != nullptr) {
39       log::fatal("unregisterIncomingPacketCallback() must be called");
40     }
41   }
42 
registerIncomingPacketCallback(hal::HciHalCallbacks * callback)43   void registerIncomingPacketCallback(hal::HciHalCallbacks* callback) override {
44     callbacks = callback;
45   }
46 
unregisterIncomingPacketCallback()47   void unregisterIncomingPacketCallback() override { callbacks = nullptr; }
48 
49   void sendHciCommand(hal::HciPacket command) override;
50 
51   void sendAclData(hal::HciPacket data) override;
52 
53   void sendScoData(hal::HciPacket data) override;
54 
55   void sendIsoData(hal::HciPacket data) override;
56 
57   hal::HciHalCallbacks* callbacks = nullptr;
58 
59   packet::PacketView<packet::kLittleEndian> GetPacketView(hal::HciPacket data);
60 
61   std::optional<hci::CommandView> GetSentCommand(
62           std::chrono::milliseconds timeout = std::chrono::seconds(1));
63 
64   std::optional<hci::AclView> GetSentAcl(
65           std::chrono::milliseconds timeout = std::chrono::seconds(1));
66 
67   std::optional<hci::ScoView> GetSentSco(
68           std::chrono::milliseconds timeout = std::chrono::seconds(1));
69 
70   std::optional<hci::IsoView> GetSentIso(
71           std::chrono::milliseconds timeout = std::chrono::seconds(1));
72 
73   void InjectEvent(std::unique_ptr<packet::BasePacketBuilder> event);
74 
Start()75   void Start() {}
76 
Stop()77   void Stop() {}
78 
ListDependencies(ModuleList *)79   void ListDependencies(ModuleList* /* list */) const {}
80 
ToString()81   std::string ToString() const override { return std::string("TestHciHal"); }
82 
83   static const ModuleFactory Factory;
84 
85 private:
86   common::BlockingQueue<hal::HciPacket> outgoing_commands_;
87   common::BlockingQueue<hal::HciPacket> outgoing_acl_;
88   common::BlockingQueue<hal::HciPacket> outgoing_sco_;
89   common::BlockingQueue<hal::HciPacket> outgoing_iso_;
90 };
91 
92 }  // namespace hal
93 }  // namespace bluetooth
94