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) { filter_ = filter; }
32 
SetOutputStream(std::shared_ptr<std::ostream> outputStream)33 void HciSniffer::SetOutputStream(std::shared_ptr<std::ostream> outputStream) {
34   output_ = outputStream;
35   if (output_) {
36     uint32_t linktype = 201;  // http://www.tcpdump.org/linktypes.html
37                               // LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR
38 
39     pcap::WriteHeader(*output_, linktype);
40   }
41 }
42 
AppendRecord(PacketDirection packet_direction,PacketType packet_type,const std::vector<uint8_t> & packet)43 void HciSniffer::AppendRecord(PacketDirection packet_direction, PacketType packet_type,
44                               const std::vector<uint8_t>& packet) {
45   if (output_ == nullptr) {
46     return;
47   }
48 
49   pcap::WriteRecordHeader(*output_, 4 + 1 + packet.size());
50 
51   // http://www.tcpdump.org/linktypes.html LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR
52   // Note: the description given for the direction bit by tcpdump
53   // is in opposition with the implementation in wireshark.
54   // The values match wireshark's implementation here.
55   char direction[4] = {0, 0, 0, static_cast<char>(packet_direction)};
56   uint8_t idc = static_cast<uint8_t>(packet_type);
57   output_->write(direction, sizeof(direction));
58   output_->write((char*)&idc, 1);
59 
60   // Apply the PCAP filter when provided.
61   if (filter_ != nullptr) {
62     std::vector<uint8_t> filtered_packet = filter_->FilterHciPacket(packet, idc);
63     output_->write((char*)filtered_packet.data(), filtered_packet.size());
64   } else {
65     output_->write((char*)packet.data(), packet.size());
66   }
67 
68   // Flush packet.
69   output_->flush();
70 }
71 
RegisterCallbacks(PacketCallback packet_callback,CloseCallback close_callback)72 void HciSniffer::RegisterCallbacks(PacketCallback packet_callback, CloseCallback close_callback) {
73   transport_->RegisterCallbacks(
74           [this, packet_callback](PacketType packet_type,
75                                   const std::shared_ptr<std::vector<uint8_t>> packet) {
76             AppendRecord(PacketDirection::HOST_TO_CONTROLLER, packet_type, *packet);
77             packet_callback(packet_type, packet);
78           },
79           close_callback);
80 }
81 
Tick()82 void HciSniffer::Tick() { transport_->Tick(); }
83 
Close()84 void HciSniffer::Close() {
85   transport_->Close();
86   if (output_ != nullptr) {
87     output_->flush();
88   }
89 }
90 
Send(PacketType packet_type,const std::vector<uint8_t> & packet)91 void HciSniffer::Send(PacketType packet_type, const std::vector<uint8_t>& packet) {
92   AppendRecord(PacketDirection::CONTROLLER_TO_HOST, packet_type, packet);
93   transport_->Send(packet_type, packet);
94 }
95 
96 }  // namespace rootcanal
97