2 * Copyright 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "loopback"
21 #include "le_advertisement.h"
22 #include "model/setup/device_boutique.h"
23 #include "osi/include/log.h"
27 namespace test_vendor_lib {
29 bool Loopback::registered_ = DeviceBoutique::Register(LOG_TAG, &Loopback::Create);
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,
54 BTM_BLE_BREDR_NOT_SPT | BTM_BLE_GEN_DISC_FLAG});
56 properties_.SetLeScanResponse({0x05, // Length
57 BTM_BLE_AD_TYPE_NAME_SHORT, 'l', 'o', 'o', 'p'});
60 std::string Loopback::GetTypeString() const {
64 std::string Loopback::ToString() const {
65 std::string dev = GetTypeString() + "@" + properties_.GetLeAddress().ToString();
70 void Loopback::Initialize(const vector<std::string>& args) {
71 if (args.size() < 2) return;
74 if (Address::FromString(args[1], addr)) properties_.SetLeAddress(addr);
76 if (args.size() < 3) return;
78 SetAdvertisementInterval(std::chrono::milliseconds(std::stoi(args[2])));
81 void Loopback::TimerTick() {}
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");
100 } // namespace test_vendor_lib