1 /*
2 * Copyright 2018 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 "remote_loopback_device"
18
19 #include "remote_loopback_device.h"
20
21 #include "model/setup/device_boutique.h"
22
23 #include "osi/include/log.h"
24 #include "packets/link_layer/link_layer_packet_builder.h"
25 #include "packets/link_layer/link_layer_packet_view.h"
26
27 using std::vector;
28
29 namespace test_vendor_lib {
30
31 using packets::LinkLayerPacketBuilder;
32 using packets::LinkLayerPacketView;
33 using packets::PageResponseBuilder;
34
35 bool RemoteLoopbackDevice::registered_ = DeviceBoutique::Register(LOG_TAG, &RemoteLoopbackDevice::Create);
36
RemoteLoopbackDevice()37 RemoteLoopbackDevice::RemoteLoopbackDevice() {}
38
ToString() const39 std::string RemoteLoopbackDevice::ToString() const {
40 return GetTypeString() + " (no address)";
41 }
42
Initialize(const std::vector<std::string> & args)43 void RemoteLoopbackDevice::Initialize(const std::vector<std::string>& args) {
44 if (args.size() < 2) return;
45
46 Address addr;
47 if (Address::FromString(args[1], addr)) properties_.SetAddress(addr);
48 }
49
IncomingPacket(LinkLayerPacketView packet)50 void RemoteLoopbackDevice::IncomingPacket(LinkLayerPacketView packet) {
51 // TODO: Check sender?
52 // TODO: Handle other packet types
53 Phy::Type phy_type = Phy::Type::BR_EDR;
54
55 Link::PacketType type = packet.GetType();
56 switch (type) {
57 case Link::PacketType::PAGE:
58 SendLinkLayerPacket(LinkLayerPacketBuilder::WrapPageResponse(
59 PageResponseBuilder::Create(true), packet.GetSourceAddress(), packet.GetSourceAddress()),
60 Phy::Type::BR_EDR);
61 break;
62 default: {
63 ALOGW("Resend = %d", static_cast<int>(packet.size()));
64 std::shared_ptr<std::vector<uint8_t>> extracted_packet = std::make_shared<std::vector<uint8_t>>();
65 extracted_packet->reserve(packet.size());
66 for (const auto byte : packet) {
67 extracted_packet->push_back(byte);
68 }
69
70 SendLinkLayerPacket(LinkLayerPacketBuilder::ReWrap(extracted_packet), phy_type);
71 }
72 }
73 }
74
75 } // namespace test_vendor_lib
76