1 /*
2  * Copyright 2022 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 <memory>
20 
21 #include "model/hci/hci_transport.h"    // for HciTransport
22 #include "model/setup/async_manager.h"  // for AsyncManager
23 #include "model/setup/phy_device.h"     // for Identifier
24 #include "netsim/hci_packet.pb.h"
25 
26 namespace netsim {
27 namespace hci {
28 using rootcanal::CloseCallback;
29 using rootcanal::PacketCallback;
30 
31 /**
32  * @class HciPacketTransport
33  *
34  * Connects Rootcanal's HciTransport to the packet_hub.
35  *
36  */
37 class HciPacketTransport : public rootcanal::HciTransport {
38  public:
39   HciPacketTransport(uint32_t, std::shared_ptr<rootcanal::AsyncManager>);
40   ~HciPacketTransport() = default;
41 
42   static void Add(rootcanal::PhyDevice::Identifier id,
43                   const std::shared_ptr<HciPacketTransport> &transport);
44 
45   static void Remove(rootcanal::PhyDevice::Identifier id);
46 
47   /**
48    * @brief Constructor for HciPacketTransport class.
49    *
50    * Moves HCI packets between packet_hub and rootcanal HciTransport
51    */
52   void Connect(rootcanal::PhyDevice::Identifier rootcanal_id);
53 
54   void Send(rootcanal::PacketType packet_type,
55             const std::vector<uint8_t> &packet) override;
56 
57   void RegisterCallbacks(PacketCallback packet_callback,
58                          CloseCallback close_callback) override;
59 
60   void Tick() override;
61 
62   void Close() override;
63 
64   void Request(packet::HCIPacket_PacketType packet_type,
65                const std::shared_ptr<std::vector<uint8_t>> &packet);
66 
67  private:
68   rootcanal::PacketCallback mPacketCallback;
69   rootcanal::CloseCallback mCloseCallback;
70   // Device ID is the same as Chip Id externally.
71   std::optional<rootcanal::PhyDevice::Identifier> rootcanalId;
72   uint32_t netsimChipId;
73   std::shared_ptr<rootcanal::AsyncManager> mAsyncManager;
74 };
75 
76 }  // namespace hci
77 }  // namespace netsim
78