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 #include "phy_layer_factory.h"
18 #include <sstream>
19 
20 namespace test_vendor_lib {
21 
PhyLayerFactory(Phy::Type phy_type,uint32_t factory_id)22 PhyLayerFactory::PhyLayerFactory(Phy::Type phy_type, uint32_t factory_id)
23     : phy_type_(phy_type), factory_id_(factory_id) {}
24 
GetType()25 Phy::Type PhyLayerFactory::GetType() {
26   return phy_type_;
27 }
28 
GetFactoryId()29 uint32_t PhyLayerFactory::GetFactoryId() {
30   return factory_id_;
31 }
32 
GetPhyLayer(const std::function<void (model::packets::LinkLayerPacketView)> & device_receive,uint32_t device_id)33 std::shared_ptr<PhyLayer> PhyLayerFactory::GetPhyLayer(
34     const std::function<void(model::packets::LinkLayerPacketView)>&
35         device_receive,
36     uint32_t device_id) {
37   std::shared_ptr<PhyLayer> new_phy = std::make_shared<PhyLayerImpl>(
38       phy_type_, next_id_++, device_receive, device_id, this);
39   phy_layers_.push_back(new_phy);
40   return new_phy;
41 }
42 
UnregisterPhyLayer(uint32_t id)43 void PhyLayerFactory::UnregisterPhyLayer(uint32_t id) {
44   for (auto phy : phy_layers_) {
45     if (phy->GetId() == id) {
46       phy_layers_.remove(phy);
47       return;
48     }
49   }
50 }
51 
UnregisterAllPhyLayers()52 void PhyLayerFactory::UnregisterAllPhyLayers() {
53   while (!phy_layers_.empty()) {
54     if (phy_layers_.begin() != phy_layers_.end()) {
55       auto id = (*phy_layers_.begin())->GetId();
56       UnregisterPhyLayer(id);
57     }
58   }
59 }
60 
Send(const std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,uint32_t id)61 void PhyLayerFactory::Send(
62     const std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,
63     uint32_t id) {
64   // Convert from a Builder to a View
65   auto bytes = std::make_shared<std::vector<uint8_t>>();
66   bluetooth::packet::BitInserter i(*bytes);
67   bytes->reserve(packet->size());
68   packet->Serialize(i);
69   auto packet_view =
70       bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian>(bytes);
71   auto link_layer_packet_view =
72       model::packets::LinkLayerPacketView::Create(packet_view);
73   ASSERT(link_layer_packet_view.IsValid());
74 
75   Send(link_layer_packet_view, id);
76 }
77 
Send(model::packets::LinkLayerPacketView packet,uint32_t id)78 void PhyLayerFactory::Send(model::packets::LinkLayerPacketView packet,
79                            uint32_t id) {
80   for (const auto& phy : phy_layers_) {
81     if (id != phy->GetId()) {
82       phy->Receive(packet);
83     }
84   }
85 }
86 
TimerTick()87 void PhyLayerFactory::TimerTick() {
88   for (auto& phy : phy_layers_) {
89     phy->TimerTick();
90   }
91 }
92 
ToString() const93 std::string PhyLayerFactory::ToString() const {
94   std::stringstream factory;
95   switch (phy_type_) {
96     case Phy::Type::LOW_ENERGY:
97       factory << "LOW_ENERGY: ";
98       break;
99     case Phy::Type::BR_EDR:
100       factory << "BR_EDR: ";
101       break;
102     default:
103       factory << "Unknown: ";
104   }
105   for (auto& phy : phy_layers_) {
106     factory << phy->GetDeviceId();
107     factory << ",";
108   }
109 
110   return factory.str();
111 }
112 
PhyLayerImpl(Phy::Type phy_type,uint32_t id,const std::function<void (model::packets::LinkLayerPacketView)> & device_receive,uint32_t device_id,PhyLayerFactory * factory)113 PhyLayerImpl::PhyLayerImpl(
114     Phy::Type phy_type, uint32_t id,
115     const std::function<void(model::packets::LinkLayerPacketView)>&
116         device_receive,
117     uint32_t device_id, PhyLayerFactory* factory)
118     : PhyLayer(phy_type, id, device_receive, device_id), factory_(factory) {}
119 
~PhyLayerImpl()120 PhyLayerImpl::~PhyLayerImpl() {
121 }
122 
Send(const std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet)123 void PhyLayerImpl::Send(
124     const std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet) {
125   factory_->Send(packet, GetId());
126 }
127 
Send(model::packets::LinkLayerPacketView packet)128 void PhyLayerImpl::Send(model::packets::LinkLayerPacketView packet) {
129   factory_->Send(packet, GetId());
130 }
131 
Unregister()132 void PhyLayerImpl::Unregister() {
133   factory_->UnregisterPhyLayer(GetId());
134 }
135 
IsFactoryId(uint32_t id)136 bool PhyLayerImpl::IsFactoryId(uint32_t id) {
137   return factory_->GetFactoryId() == id;
138 }
139 
Receive(model::packets::LinkLayerPacketView packet)140 void PhyLayerImpl::Receive(model::packets::LinkLayerPacketView packet) {
141   transmit_to_device_(packet);
142 }
143 
TimerTick()144 void PhyLayerImpl::TimerTick() {}
145 
146 }  // namespace test_vendor_lib
147