• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "beacon"
18 
19 #include "beacon.h"
20 
21 #include "le_advertisement.h"
22 #include "model/setup/device_boutique.h"
23 #include "osi/include/log.h"
24 
25 using std::vector;
26 
27 namespace test_vendor_lib {
28 
29 bool Beacon::registered_ = DeviceBoutique::Register(LOG_TAG, &Beacon::Create);
30 
Beacon()31 Beacon::Beacon() {
32   advertising_interval_ms_ = std::chrono::milliseconds(1280);
33   properties_.SetLeAdvertisementType(BTM_BLE_NON_CONNECT_EVT);
34   properties_.SetLeAdvertisement({0x0F,  // Length
35                                   BTM_BLE_AD_TYPE_NAME_CMPL, 'g', 'D', 'e', 'v', 'i', 'c', 'e', '-', 'b', 'e', 'a', 'c',
36                                   'o', 'n',
37                                   0x02,  // Length
38                                   BTM_BLE_AD_TYPE_FLAG, BTM_BLE_BREDR_NOT_SPT | BTM_BLE_GEN_DISC_FLAG});
39 
40   properties_.SetLeScanResponse({0x05,  // Length
41                                  BTM_BLE_AD_TYPE_NAME_SHORT, 'b', 'e', 'a', 'c'});
42 }
43 
GetTypeString() const44 std::string Beacon::GetTypeString() const {
45   return "beacon";
46 }
47 
ToString() const48 std::string Beacon::ToString() const {
49   std::string dev = GetTypeString() + "@" + properties_.GetLeAddress().ToString();
50 
51   return dev;
52 }
53 
Initialize(const vector<std::string> & args)54 void Beacon::Initialize(const vector<std::string>& args) {
55   if (args.size() < 2) return;
56 
57   Address addr;
58   if (Address::FromString(args[1], addr)) properties_.SetLeAddress(addr);
59 
60   if (args.size() < 3) return;
61 
62   SetAdvertisementInterval(std::chrono::milliseconds(std::stoi(args[2])));
63 }
64 
TimerTick()65 void Beacon::TimerTick() {
66   if (IsAdvertisementAvailable(std::chrono::milliseconds(5000))) {
67     std::unique_ptr<packets::LeAdvertisementBuilder> ad = packets::LeAdvertisementBuilder::Create(
68         LeAdvertisement::AddressType::PUBLIC,
69         static_cast<LeAdvertisement::AdvertisementType>(properties_.GetLeAdvertisementType()),
70         properties_.GetLeAdvertisement());
71     std::shared_ptr<packets::LinkLayerPacketBuilder> to_send =
72         packets::LinkLayerPacketBuilder::WrapLeAdvertisement(std::move(ad), properties_.GetLeAddress());
73     std::vector<std::shared_ptr<PhyLayer>> le_phys = phy_layers_[Phy::Type::LOW_ENERGY];
74     for (std::shared_ptr<PhyLayer> phy : le_phys) {
75       phy->Send(to_send);
76     }
77   }
78 }
79 
IncomingPacket(packets::LinkLayerPacketView packet)80 void Beacon::IncomingPacket(packets::LinkLayerPacketView packet) {
81   if (packet.GetDestinationAddress() == properties_.GetLeAddress() && packet.GetType() == Link::PacketType::LE_SCAN) {
82     std::unique_ptr<packets::LeAdvertisementBuilder> scan_response = packets::LeAdvertisementBuilder::Create(
83         LeAdvertisement::AddressType::PUBLIC, LeAdvertisement::AdvertisementType::SCAN_RESPONSE,
84         properties_.GetLeScanResponse());
85     std::shared_ptr<packets::LinkLayerPacketBuilder> to_send = packets::LinkLayerPacketBuilder::WrapLeScanResponse(
86         std::move(scan_response), properties_.GetLeAddress(), packet.GetSourceAddress());
87     std::vector<std::shared_ptr<PhyLayer>> le_phys = phy_layers_[Phy::Type::LOW_ENERGY];
88     for (auto phy : le_phys) {
89       phy->Send(to_send);
90     }
91   }
92 }
93 
94 }  // namespace test_vendor_lib
95