1 /*
2 * Copyright 2018 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 "model/devices/sniffer.h"
18
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22
23 #include "hci/address.h"
24 #include "log.h"
25 #include "model/setup/device_boutique.h"
26 #include "packets/link_layer_packets.h"
27 #include "phy.h"
28
29 namespace rootcanal {
30
31 bool Sniffer::registered_ = DeviceBoutique::Register("sniffer", &Sniffer::Create);
32
Sniffer(const std::vector<std::string> & args)33 Sniffer::Sniffer(const std::vector<std::string>& args) {
34 if (args.size() >= 2) {
35 Address::FromString(args[1], address_);
36 }
37 }
38
ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView packet,Phy::Type,int8_t)39 void Sniffer::ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView packet, Phy::Type /*type*/,
40 int8_t /*rssi*/) {
41 Address source = packet.GetSourceAddress();
42 Address dest = packet.GetDestinationAddress();
43 model::packets::PacketType packet_type = packet.GetType();
44
45 bool match_source = address_ == source;
46 bool match_dest = address_ == dest;
47 if (!match_source && !match_dest) {
48 return;
49 }
50
51 INFO("{} {} -> {} (Type {})", (match_source ? (match_dest ? "<->" : "<--") : "-->"), source, dest,
52 model::packets::PacketTypeText(packet_type));
53 }
54
55 } // namespace rootcanal
56