aboutsummaryrefslogtreecommitdiff
path: root/vendor_libs/test_vendor_lib/model/devices/loopback.cc
blob: 18af1c10e23ff8fca7b97fb7b7bb231464b92581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "loopback.h"

#include "le_advertisement.h"
#include "model/setup/device_boutique.h"
#include "os/log.h"

using std::vector;

namespace test_vendor_lib {

bool Loopback::registered_ = DeviceBoutique::Register("loopback", &Loopback::Create);

Loopback::Loopback() {
  advertising_interval_ms_ = std::chrono::milliseconds(1280);
  properties_.SetLeAdvertisementType(0x03);  // NON_CONNECT
  properties_.SetLeAdvertisement({
      0x11,  // Length
      0x09,  // NAME_CMPL
      'g',         'D', 'e', 'v', 'i', 'c', 'e', '-', 'l', 'o', 'o', 'p', 'b', 'a', 'c', 'k',
      0x02,         // Length
      0x01,         // TYPE_FLAG
      0x04 | 0x02,  // BREDR_NOT_SPT | GEN_DISC
  });

  properties_.SetLeScanResponse({0x05,  // Length
                                 0x08,  // NAME_SHORT
                                 'l', 'o', 'o', 'p'});
}

std::string Loopback::GetTypeString() const {
  return "loopback";
}

std::string Loopback::ToString() const {
  std::string dev = GetTypeString() + "@" + properties_.GetLeAddress().ToString();

  return dev;
}

void Loopback::Initialize(const vector<std::string>& args) {
  if (args.size() < 2) return;

  Address addr;
  if (Address::FromString(args[1], addr)) properties_.SetLeAddress(addr);

  if (args.size() < 3) return;

  SetAdvertisementInterval(std::chrono::milliseconds(std::stoi(args[2])));
}

void Loopback::TimerTick() {}

void Loopback::IncomingPacket(packets::LinkLayerPacketView packet) {
  LOG_INFO("Got a packet of type %d", static_cast<int>(packet.GetType()));
  if (packet.GetDestinationAddress() == properties_.GetLeAddress() && packet.GetType() == Link::PacketType::LE_SCAN) {
    LOG_INFO("Got a scan");
    std::unique_ptr<packets::LeAdvertisementBuilder> scan_response = packets::LeAdvertisementBuilder::Create(
        LeAdvertisement::AddressType::PUBLIC, LeAdvertisement::AdvertisementType::SCAN_RESPONSE,
        properties_.GetLeScanResponse());
    std::shared_ptr<packets::LinkLayerPacketBuilder> to_send = packets::LinkLayerPacketBuilder::WrapLeScanResponse(
        std::move(scan_response), properties_.GetLeAddress(), packet.GetSourceAddress());
    std::vector<std::shared_ptr<PhyLayer>> le_phys = phy_layers_[Phy::Type::LOW_ENERGY];
    for (auto phy : le_phys) {
      LOG_INFO("Sending a Scan Response on a Phy");
      phy->Send(to_send);
    }
  }
}

}  // namespace test_vendor_lib