OSDN Git Service

RootCanal: New Directory Structure
[android-x86/system-bt.git] / vendor_libs / test_vendor_lib / model / devices / loopback.cc
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 "loopback"
18
19 #include "loopback.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 Loopback::registered_ = DeviceBoutique::Register(LOG_TAG, &Loopback::Create);
30
31 Loopback::Loopback() {
32   advertising_interval_ms_ = std::chrono::milliseconds(1280);
33   properties_.SetLeAdvertisementType(BTM_BLE_NON_CONNECT_EVT);
34   properties_.SetLeAdvertisement({0x11,  // Length
35                                   BTM_BLE_AD_TYPE_NAME_CMPL,
36                                   'g',
37                                   'D',
38                                   'e',
39                                   'v',
40                                   'i',
41                                   'c',
42                                   'e',
43                                   '-',
44                                   'l',
45                                   'o',
46                                   'o',
47                                   'p',
48                                   'b',
49                                   'a',
50                                   'c',
51                                   'k',
52                                   0x02,  // Length
53                                   BTM_BLE_AD_TYPE_FLAG,
54                                   BTM_BLE_BREDR_NOT_SPT | BTM_BLE_GEN_DISC_FLAG});
55
56   properties_.SetLeScanResponse({0x05,  // Length
57                                  BTM_BLE_AD_TYPE_NAME_SHORT, 'l', 'o', 'o', 'p'});
58 }
59
60 std::string Loopback::GetTypeString() const {
61   return "loopback";
62 }
63
64 std::string Loopback::ToString() const {
65   std::string dev = GetTypeString() + "@" + properties_.GetLeAddress().ToString();
66
67   return dev;
68 }
69
70 void Loopback::Initialize(const vector<std::string>& args) {
71   if (args.size() < 2) return;
72
73   Address addr;
74   if (Address::FromString(args[1], addr)) properties_.SetLeAddress(addr);
75
76   if (args.size() < 3) return;
77
78   SetAdvertisementInterval(std::chrono::milliseconds(std::stoi(args[2])));
79 }
80
81 void Loopback::TimerTick() {}
82
83 void Loopback::IncomingPacket(packets::LinkLayerPacketView packet) {
84   LOG_INFO(LOG_TAG, "Got a packet of type %d", static_cast<int>(packet.GetType()));
85   if (packet.GetDestinationAddress() == properties_.GetLeAddress() && packet.GetType() == Link::PacketType::LE_SCAN) {
86     LOG_INFO(LOG_TAG, "Got a scan");
87     std::unique_ptr<packets::LeAdvertisementBuilder> scan_response = packets::LeAdvertisementBuilder::Create(
88         LeAdvertisement::AddressType::PUBLIC, LeAdvertisement::AdvertisementType::SCAN_RESPONSE,
89         properties_.GetLeScanResponse());
90     std::shared_ptr<packets::LinkLayerPacketBuilder> to_send = packets::LinkLayerPacketBuilder::WrapLeScanResponse(
91         std::move(scan_response), properties_.GetLeAddress(), packet.GetSourceAddress());
92     std::vector<std::shared_ptr<PhyLayer>> le_phys = phy_layers_[Phy::Type::LOW_ENERGY];
93     for (auto phy : le_phys) {
94       LOG_INFO(LOG_TAG, "Sending a Scan Response on a Phy");
95       phy->Send(to_send);
96     }
97   }
98 }
99
100 }  // namespace test_vendor_lib