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 "phy_layer_factory"
18
19 #include "phy_layer_factory.h"
20
21 #include "base/logging.h"
22
23 #include "osi/include/log.h"
24
25 namespace test_vendor_lib {
26
PhyLayerFactory(Phy::Type phy_type)27 PhyLayerFactory::PhyLayerFactory(Phy::Type phy_type) : phy_type_(phy_type) {}
28
GetType()29 Phy::Type PhyLayerFactory::GetType() {
30 return phy_type_;
31 }
32
GetPhyLayer(const std::function<void (packets::LinkLayerPacketView)> & device_receive)33 std::shared_ptr<PhyLayer> PhyLayerFactory::GetPhyLayer(
34 const std::function<void(packets::LinkLayerPacketView)>& device_receive) {
35 std::shared_ptr<PhyLayer> new_phy =
36 std::make_shared<PhyLayerImpl>(phy_type_, next_id_++, device_receive, std::shared_ptr<PhyLayerFactory>(this));
37 phy_layers_.push_back(new_phy);
38 return new_phy;
39 }
40
UnregisterPhyLayer(uint32_t id)41 void PhyLayerFactory::UnregisterPhyLayer(uint32_t id) {
42 for (auto it = phy_layers_.begin(); it != phy_layers_.end();) {
43 if ((*it)->GetId() == id) {
44 it = phy_layers_.erase(it);
45 } else {
46 it++;
47 }
48 }
49 }
50
Send(const std::shared_ptr<packets::LinkLayerPacketBuilder> packet,uint32_t id)51 void PhyLayerFactory::Send(const std::shared_ptr<packets::LinkLayerPacketBuilder> packet, uint32_t id) {
52 // Convert from a Builder to a View
53 std::shared_ptr<std::vector<uint8_t>> serialized_packet =
54 std::shared_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>());
55 std::back_insert_iterator<std::vector<uint8_t>> itr(*serialized_packet);
56 serialized_packet->reserve(packet->size());
57 packet->Serialize(itr);
58 packets::LinkLayerPacketView packet_view = packets::LinkLayerPacketView::Create(serialized_packet);
59
60 for (const auto phy : phy_layers_) {
61 if (id != phy->GetId()) {
62 phy->Receive(packet_view);
63 }
64 }
65 }
66
TimerTick()67 void PhyLayerFactory::TimerTick() {
68 for (auto phy : phy_layers_) {
69 phy->TimerTick();
70 }
71 }
72
ToString() const73 std::string PhyLayerFactory::ToString() const {
74 switch (phy_type_) {
75 case Phy::Type::LOW_ENERGY:
76 return "LOW_ENERGY";
77 break;
78 case Phy::Type::BR_EDR:
79 return "BR_EDR";
80 break;
81 default:
82 return "Unknown";
83 }
84 }
85
PhyLayerImpl(Phy::Type phy_type,uint32_t id,const std::function<void (packets::LinkLayerPacketView)> & device_receive,const std::shared_ptr<PhyLayerFactory> & factory)86 PhyLayerImpl::PhyLayerImpl(Phy::Type phy_type, uint32_t id,
87 const std::function<void(packets::LinkLayerPacketView)>& device_receive,
88 const std::shared_ptr<PhyLayerFactory>& factory)
89 : PhyLayer(phy_type, id, device_receive), factory_(factory) {}
90
~PhyLayerImpl()91 PhyLayerImpl::~PhyLayerImpl() {
92 factory_->UnregisterPhyLayer(GetId());
93 PhyLayer::~PhyLayer();
94 }
95
Send(const std::shared_ptr<packets::LinkLayerPacketBuilder> packet)96 void PhyLayerImpl::Send(const std::shared_ptr<packets::LinkLayerPacketBuilder> packet) {
97 factory_->Send(packet, GetId());
98 }
99
Receive(packets::LinkLayerPacketView packet)100 void PhyLayerImpl::Receive(packets::LinkLayerPacketView packet) {
101 transmit_to_device_(packet);
102 }
103
TimerTick()104 void PhyLayerImpl::TimerTick() {}
105
106 } // namespace test_vendor_lib
107