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 packet_callback,CloseCallback close_callback)76 void HciSniffer::RegisterCallbacks(PacketCallback packet_callback,
77                                    CloseCallback close_callback) {
78   transport_->RegisterCallbacks(
79       [this, packet_callback](
80           PacketType packet_type,
81           const std::shared_ptr<std::vector<uint8_t>> packet) {
82         AppendRecord(PacketDirection::HOST_TO_CONTROLLER, packet_type, *packet);
83         packet_callback(packet_type, packet);
84       },
85       close_callback);
86 }
87 
Tick()88 void HciSniffer::Tick() { transport_->Tick(); }
89 
Close()90 void HciSniffer::Close() {
91   transport_->Close();
92   if (output_ != nullptr) {
93     output_->flush();
94   }
95 }
96 
Send(PacketType packet_type,const std::vector<uint8_t> & packet)97 void HciSniffer::Send(PacketType packet_type,
98                       const std::vector<uint8_t>& packet) {
99   AppendRecord(PacketDirection::CONTROLLER_TO_HOST, packet_type, packet);
100   transport_->Send(packet_type, packet);
101 }
102 
103 }  // namespace rootcanal
104