• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 "hci_sniffer.h"
18 
19 #include "hci/pcap_filter.h"
20 #include "pcap.h"
21 
22 namespace rootcanal {
23 
HciSniffer(std::shared_ptr<HciTransport> transport,std::shared_ptr<std::ostream> outputStream,std::shared_ptr<PcapFilter> filter)24 HciSniffer::HciSniffer(std::shared_ptr<HciTransport> transport,
25                        std::shared_ptr<std::ostream> outputStream,
26                        std::shared_ptr<PcapFilter> filter)
27     : transport_(transport), filter_(filter) {
28   SetOutputStream(outputStream);
29 }
30 
SetPcapFilter(std::shared_ptr<PcapFilter> filter)31 void HciSniffer::SetPcapFilter(std::shared_ptr<PcapFilter> filter) {
32   filter_ = filter;
33 }
34 
SetOutputStream(std::shared_ptr<std::ostream> outputStream)35 void HciSniffer::SetOutputStream(std::shared_ptr<std::ostream> outputStream) {
36   output_ = outputStream;
37   if (output_) {
38     uint32_t linktype = 201;  // http://www.tcpdump.org/linktypes.html
39                               // LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR
40 
41     pcap::WriteHeader(*output_, linktype);
42   }
43 }
44 
AppendRecord(PacketDirection packet_direction,PacketType packet_type,const std::vector<uint8_t> & packet)45 void HciSniffer::AppendRecord(PacketDirection packet_direction,
46                               PacketType packet_type,
47                               const std::vector<uint8_t>& packet) {
48   if (output_ == nullptr) {
49     return;
50   }
51 
52   pcap::WriteRecordHeader(*output_, 4 + 1 + packet.size());
53 
54   // http://www.tcpdump.org/linktypes.html LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR
55   // Note: the description given for the direction bit by tcpdump
56   // is in opposition with the implementation in wireshark.
57   // The values match wireshark's implementation here.
58   char direction[4] = {0, 0, 0, static_cast<char>(packet_direction)};
59   uint8_t idc = static_cast<uint8_t>(packet_type);
60   output_->write(direction, sizeof(direction));
61   output_->write((char*)&idc, 1);
62 
63   // Apply the PCAP filter when provided.
64   if (filter_ != nullptr) {
65     std::vector<uint8_t> filtered_packet =
66         filter_->FilterHciPacket(packet, idc);
67     output_->write((char*)filtered_packet.data(), filtered_packet.size());
68   } else {
69     output_->write((char*)packet.data(), packet.size());
70   }
71 
72   // Flush packet.
73   output_->flush();
74 }
75 
RegisterCallbacks(PacketCallback command_callback,PacketCallback acl_callback,PacketCallback sco_callback,PacketCallback iso_callback,CloseCallback close_callback)76 void HciSniffer::RegisterCallbacks(PacketCallback command_callback,
77                                    PacketCallback acl_callback,
78                                    PacketCallback sco_callback,
79                                    PacketCallback iso_callback,
80                                    CloseCallback close_callback) {
81   transport_->RegisterCallbacks(
82       [this,
83        command_callback](const std::shared_ptr<std::vector<uint8_t>> command) {
84         AppendRecord(PacketDirection::HOST_TO_CONTROLLER, PacketType::COMMAND,
85                      *command);
86         command_callback(command);
87       },
88       [this, acl_callback](const std::shared_ptr<std::vector<uint8_t>> acl) {
89         AppendRecord(PacketDirection::HOST_TO_CONTROLLER, PacketType::ACL,
90                      *acl);
91         acl_callback(acl);
92       },
93       [this, sco_callback](const std::shared_ptr<std::vector<uint8_t>> sco) {
94         AppendRecord(PacketDirection::HOST_TO_CONTROLLER, PacketType::SCO,
95                      *sco);
96         sco_callback(sco);
97       },
98       [this, iso_callback](const std::shared_ptr<std::vector<uint8_t>> iso) {
99         AppendRecord(PacketDirection::HOST_TO_CONTROLLER, PacketType::ISO,
100                      *iso);
101         iso_callback(iso);
102       },
103       close_callback);
104 }
105 
Tick()106 void HciSniffer::Tick() { transport_->Tick(); }
107 
Close()108 void HciSniffer::Close() {
109   transport_->Close();
110   if (output_ != nullptr) {
111     output_->flush();
112   }
113 }
114 
SendEvent(const std::vector<uint8_t> & packet)115 void HciSniffer::SendEvent(const std::vector<uint8_t>& packet) {
116   AppendRecord(PacketDirection::CONTROLLER_TO_HOST, PacketType::EVENT, packet);
117   transport_->SendEvent(packet);
118 }
119 
SendAcl(const std::vector<uint8_t> & packet)120 void HciSniffer::SendAcl(const std::vector<uint8_t>& packet) {
121   AppendRecord(PacketDirection::CONTROLLER_TO_HOST, PacketType::ACL, packet);
122   transport_->SendAcl(packet);
123 }
124 
SendSco(const std::vector<uint8_t> & packet)125 void HciSniffer::SendSco(const std::vector<uint8_t>& packet) {
126   AppendRecord(PacketDirection::CONTROLLER_TO_HOST, PacketType::SCO, packet);
127   transport_->SendSco(packet);
128 }
129 
SendIso(const std::vector<uint8_t> & packet)130 void HciSniffer::SendIso(const std::vector<uint8_t>& packet) {
131   AppendRecord(PacketDirection::CONTROLLER_TO_HOST, PacketType::ISO, packet);
132   transport_->SendIso(packet);
133 }
134 }  // namespace rootcanal
135