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_ =
32 DeviceBoutique::Register("sniffer", &Sniffer::Create);
33
Sniffer(const std::vector<std::string> & args)34 Sniffer::Sniffer(const std::vector<std::string>& args) {
35 if (args.size() >= 2) {
36 Address::FromString(args[1], address_);
37 }
38 }
39
ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView packet,Phy::Type,int8_t)40 void Sniffer::ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView packet,
41 Phy::Type /*type*/, int8_t /*rssi*/) {
42 Address source = packet.GetSourceAddress();
43 Address dest = packet.GetDestinationAddress();
44 model::packets::PacketType packet_type = packet.GetType();
45
46 bool match_source = address_ == source;
47 bool match_dest = address_ == dest;
48 if (!match_source && !match_dest) {
49 return;
50 }
51
52 INFO("{} {} -> {} (Type {})",
53 (match_source ? (match_dest ? "<->" : "<--") : "-->"), source, dest,
54 model::packets::PacketTypeText(packet_type));
55 }
56
57 } // namespace rootcanal
58