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 {
48     callbacks = nullptr;
49   }
50 
51   void sendHciCommand(hal::HciPacket command) override;
52 
53   void sendAclData(hal::HciPacket data) override;
54 
55   void sendScoData(hal::HciPacket data) override;
56 
57   void sendIsoData(hal::HciPacket data) override;
58 
59   hal::HciHalCallbacks* callbacks = nullptr;
60 
61   packet::PacketView<packet::kLittleEndian> GetPacketView(hal::HciPacket data);
62 
63   std::optional<hci::CommandView> GetSentCommand(
64       std::chrono::milliseconds timeout = std::chrono::seconds(1));
65 
66   std::optional<hci::AclView> GetSentAcl(
67       std::chrono::milliseconds timeout = std::chrono::seconds(1));
68 
69   std::optional<hci::ScoView> GetSentSco(
70       std::chrono::milliseconds timeout = std::chrono::seconds(1));
71 
72   std::optional<hci::IsoView> GetSentIso(
73       std::chrono::milliseconds timeout = std::chrono::seconds(1));
74 
75   void InjectEvent(std::unique_ptr<packet::BasePacketBuilder> event);
76 
Start()77   void Start() {}
78 
Stop()79   void Stop() {}
80 
ListDependencies(ModuleList *)81   void ListDependencies(ModuleList* /* list */) const {}
82 
ToString()83   std::string ToString() const override {
84     return std::string("TestHciHal");
85   }
86 
87   static const ModuleFactory Factory;
88 
89  private:
90   common::BlockingQueue<hal::HciPacket> outgoing_commands_;
91   common::BlockingQueue<hal::HciPacket> outgoing_acl_;
92   common::BlockingQueue<hal::HciPacket> outgoing_sco_;
93   common::BlockingQueue<hal::HciPacket> outgoing_iso_;
94 };
95 
96 }  // namespace hal
97 }  // namespace bluetooth
98