• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hci_packet.pb.h"
22 #include "model/hci/hci_transport.h"    // for HciTransport
23 #include "model/setup/async_manager.h"  // for AsyncManager
24 #include "model/setup/phy_device.h"     // for Identifier
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(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   /**
46    * @brief Constructor for HciPacketTransport class.
47    *
48    * Moves HCI packets between packet_hub and rootcanal HciTransport
49    */
50   void Connect(rootcanal::PhyDevice::Identifier device_id);
51 
52   void SendEvent(const std::vector<uint8_t> &packet) override;
53 
54   void SendAcl(const std::vector<uint8_t> &packet) override;
55 
56   void SendSco(const std::vector<uint8_t> &packet) override;
57 
58   void SendIso(const std::vector<uint8_t> &packet) override;
59 
60   void RegisterCallbacks(PacketCallback command_callback,
61                          PacketCallback acl_callback,
62                          PacketCallback sco_callback,
63                          PacketCallback iso_callback,
64                          CloseCallback close_callback) override;
65 
66   void Tick() override;
67 
68   void Close() override;
69 
70   void Request(packet::HCIPacket_PacketType packet_type,
71                const std::shared_ptr<std::vector<uint8_t>> &packet);
72 
73  private:
74   void Response(packet::HCIPacket_PacketType packet_type,
75                 const std::vector<uint8_t> &packet);
76   rootcanal::PacketCallback PacketTypeCallback(
77       packet::HCIPacket_PacketType packet_type);
78 
79   rootcanal::PacketCallback mAclCallback;
80   rootcanal::PacketCallback mCommandCallback;
81   rootcanal::PacketCallback mScoCallback;
82   rootcanal::PacketCallback mIsoCallback;
83   rootcanal::CloseCallback mCloseCallback;
84   // Device ID is the same as Chip Id externally.
85   std::optional<rootcanal::PhyDevice::Identifier> mDeviceId;
86   std::shared_ptr<rootcanal::AsyncManager> mAsyncManager;
87 };
88 
89 }  // namespace hci
90 }  // namespace netsim
91