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', 'i', 'c', 'e',
40 '-', 'b', 'e', 'a', 'c', 'o', 'n', 0x02 /* Length */, 0x01 /* TYPE_FLAG */,
41 0x4 /* BREDR_NOT_SUPPORTED */ | 0x2 /* GENERAL_DISCOVERABLE */
42 }),
43 scan_response_data_({0x05 /* Length */, 0x08 /* TYPE_NAME_SHORT */, 'b', 'e', 'a', 'c'}),
44 advertising_interval_(1280ms) {}
45
Beacon(const std::vector<std::string> & args)46 Beacon::Beacon(const std::vector<std::string>& args) : Beacon() {
47 if (args.size() >= 2) {
48 Address::FromString(args[1], address_);
49 }
50
51 if (args.size() >= 3) {
52 advertising_interval_ = std::chrono::milliseconds(std::stoi(args[2]));
53 }
54 }
55
Tick()56 void Beacon::Tick() {
57 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
58 if ((now - advertising_last_) >= advertising_interval_) {
59 advertising_last_ = now;
60 SendLinkLayerPacket(LeLegacyAdvertisingPduBuilder::Create(
61 address_, Address::kEmpty, AddressType::PUBLIC, AddressType::PUBLIC,
62 advertising_type_,
63 std::vector(advertising_data_.begin(), advertising_data_.end())),
64 Phy::Type::LOW_ENERGY);
65 }
66 }
67
ReceiveLinkLayerPacket(LinkLayerPacketView packet,Phy::Type,int8_t)68 void Beacon::ReceiveLinkLayerPacket(LinkLayerPacketView packet, Phy::Type /*type*/,
69 int8_t /*rssi*/) {
70 if (packet.GetDestinationAddress() == address_ && packet.GetType() == PacketType::LE_SCAN &&
71 (advertising_type_ == LegacyAdvertisingType::ADV_IND ||
72 advertising_type_ == LegacyAdvertisingType::ADV_SCAN_IND)) {
73 SendLinkLayerPacket(
74 LeScanResponseBuilder::Create(
75 address_, packet.GetSourceAddress(), AddressType::PUBLIC,
76 std::vector(scan_response_data_.begin(), scan_response_data_.end())),
77 Phy::Type::LOW_ENERGY);
78 }
79 }
80
81 } // namespace rootcanal
82