1 /*
2  * Copyright 2016 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/beacon.h"
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "hci/address.h"
26 #include "model/setup/device_boutique.h"
27 #include "packets/link_layer_packets.h"
28 #include "phy.h"
29 
30 namespace rootcanal {
31 using namespace model::packets;
32 using namespace std::chrono_literals;
33 
34 bool Beacon::registered_ = DeviceBoutique::Register("beacon", &Beacon::Create);
35 
Beacon()36 Beacon::Beacon()
37     : advertising_type_(LegacyAdvertisingType::ADV_NONCONN_IND),
38       advertising_data_({
39           0x0F /* Length */, 0x09 /* TYPE_NAME_COMPLETE */, 'g', 'D', 'e', 'v',
40           'i', 'c', 'e', '-', 'b', 'e', 'a', 'c', 'o', 'n', 0x02 /* Length */,
41           0x01 /* TYPE_FLAG */,
42           0x4 /* BREDR_NOT_SUPPORTED */ | 0x2 /* GENERAL_DISCOVERABLE */
43       }),
44       scan_response_data_(
45           {0x05 /* Length */, 0x08 /* TYPE_NAME_SHORT */, 'b', 'e', 'a', 'c'}),
46       advertising_interval_(1280ms) {}
47 
Beacon(const std::vector<std::string> & args)48 Beacon::Beacon(const std::vector<std::string>& args) : Beacon() {
49   if (args.size() >= 2) {
50     Address::FromString(args[1], address_);
51   }
52 
53   if (args.size() >= 3) {
54     advertising_interval_ = std::chrono::milliseconds(std::stoi(args[2]));
55   }
56 }
57 
Tick()58 void Beacon::Tick() {
59   std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
60   if ((now - advertising_last_) >= advertising_interval_) {
61     advertising_last_ = now;
62     SendLinkLayerPacket(
63         std::move(LeLegacyAdvertisingPduBuilder::Create(
64             address_, Address::kEmpty, AddressType::PUBLIC, AddressType::PUBLIC,
65             advertising_type_,
66             std::vector(advertising_data_.begin(), advertising_data_.end()))),
67         Phy::Type::LOW_ENERGY);
68   }
69 }
70 
ReceiveLinkLayerPacket(LinkLayerPacketView packet,Phy::Type,int8_t)71 void Beacon::ReceiveLinkLayerPacket(LinkLayerPacketView packet,
72                                     Phy::Type /*type*/, int8_t /*rssi*/) {
73   if (packet.GetDestinationAddress() == address_ &&
74       packet.GetType() == PacketType::LE_SCAN &&
75       (advertising_type_ == LegacyAdvertisingType::ADV_IND ||
76        advertising_type_ == LegacyAdvertisingType::ADV_SCAN_IND)) {
77     SendLinkLayerPacket(
78         std::move(LeScanResponseBuilder::Create(
79             address_, packet.GetSourceAddress(), AddressType::PUBLIC,
80             std::vector(scan_response_data_.begin(),
81                         scan_response_data_.end()))),
82         Phy::Type::LOW_ENERGY);
83   }
84 }
85 
86 }  // namespace rootcanal
87