• 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 #include "packet_hub/packet_hub.h"
18 
19 #include "backend/backend_packet_hub.h"
20 #include "common.pb.h"
21 #include "hci/hci_packet_hub.h"
22 #include "hci_packet.pb.h"
23 #include "netsim-cxx/src/lib.rs.h"
24 #include "wifi/wifi_packet_hub.h"
25 
26 namespace netsim {
27 namespace packet_hub {
28 
29 using netsim::common::ChipKind;
30 
31 // forward from transport to facade via packet_hub
HandleRequest(ChipKind kind,uint32_t facade_id,const std::vector<uint8_t> & packet,packet::HCIPacket_PacketType packet_type)32 void HandleRequest(ChipKind kind, uint32_t facade_id,
33                    const std::vector<uint8_t> &packet,
34                    packet::HCIPacket_PacketType packet_type) {
35   // Copied
36   auto shared_packet = std::make_shared<std::vector<uint8_t>>(packet);
37   if (kind == ChipKind::BLUETOOTH) {
38     netsim::hci::handle_bt_request(facade_id, packet_type, shared_packet);
39   } else if (kind == ChipKind::WIFI) {
40     netsim::wifi::HandleWifiRequest(facade_id, shared_packet);
41   }
42   netsim::pcap::HandleRequest(kind, facade_id, packet, packet_type);
43 }
44 
HandleRequestCxx(uint32_t kind,uint32_t facade_id,const rust::Vec<uint8_t> & packet,uint8_t packet_type)45 void HandleRequestCxx(uint32_t kind, uint32_t facade_id,
46                       const rust::Vec<uint8_t> &packet, uint8_t packet_type) {
47   std::vector<uint8_t> buffer(packet.begin(), packet.end());
48   HandleRequest(static_cast<ChipKind>(kind), facade_id, buffer,
49                 static_cast<packet::HCIPacket_PacketType>(packet_type));
50 }
51 
52 // forward from facade to transport via packet_hub
HandleBtResponse(uint32_t facade_id,packet::HCIPacket_PacketType packet_type,const std::shared_ptr<std::vector<uint8_t>> & packet)53 void HandleBtResponse(uint32_t facade_id,
54                       packet::HCIPacket_PacketType packet_type,
55                       const std::shared_ptr<std::vector<uint8_t>> &packet) {
56   netsim::backend::HandleResponse(ChipKind::BLUETOOTH, facade_id, *packet,
57                                   packet_type);
58   netsim::fd::HandleResponse(ChipKind::BLUETOOTH, facade_id, *packet,
59                              packet_type);
60   netsim::pcap::HandleResponse(ChipKind::BLUETOOTH, facade_id, *packet,
61                                packet_type);
62 }
63 
64 // forward from facade to transport via packet_hub
HandleWifiResponse(uint32_t facade_id,const std::shared_ptr<std::vector<uint8_t>> & packet)65 void HandleWifiResponse(uint32_t facade_id,
66                         const std::shared_ptr<std::vector<uint8_t>> &packet) {
67   netsim::backend::HandleResponse(ChipKind::WIFI, facade_id, *packet,
68                                   packet::HCIPacket::HCI_PACKET_UNSPECIFIED);
69   netsim::fd::HandleResponse(ChipKind::WIFI, facade_id, *packet,
70                              packet::HCIPacket::HCI_PACKET_UNSPECIFIED);
71   netsim::pcap::HandleResponse(ChipKind::WIFI, facade_id, *packet,
72                                packet::HCIPacket::HCI_PACKET_UNSPECIFIED);
73 }
74 
75 }  // namespace packet_hub
76 }  // namespace netsim
77