• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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_device.h"
18 
19 #include "phy_layer.h"
20 
21 namespace rootcanal {
22 
PhyDevice(Identifier id,std::string type,std::shared_ptr<Device> device)23 PhyDevice::PhyDevice(Identifier id, std::string type,
24                      std::shared_ptr<Device> device)
25     : id(id), type(std::move(type)), device_(std::move(device)) {
26   using namespace std::placeholders;
27   ASSERT(device_ != nullptr);
28   device_->RegisterLinkLayerChannel(
29       std::bind(&PhyDevice::Send, this, _1, _2, _3));
30 }
31 
Register(PhyLayer * phy)32 void PhyDevice::Register(PhyLayer* phy) { phy_layers_.insert(phy); }
33 
Unregister(PhyLayer * phy)34 void PhyDevice::Unregister(PhyLayer* phy) { phy_layers_.erase(phy); }
35 
Tick()36 void PhyDevice::Tick() { device_->Tick(); }
37 
SetAddress(bluetooth::hci::Address address)38 void PhyDevice::SetAddress(bluetooth::hci::Address address) {
39   device_->SetAddress(std::move(address));
40 }
41 
Receive(std::vector<uint8_t> const & packet,Phy::Type type,int8_t rssi)42 void PhyDevice::Receive(std::vector<uint8_t> const& packet, Phy::Type type,
43                         int8_t rssi) {
44   std::shared_ptr<std::vector<uint8_t>> packet_copy =
45       std::make_shared<std::vector<uint8_t>>(packet);
46   model::packets::LinkLayerPacketView packet_view =
47       model::packets::LinkLayerPacketView::Create(
48           bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian>(
49               packet_copy));
50   if (packet_view.IsValid()) {
51     device_->ReceiveLinkLayerPacket(std::move(packet_view), type, rssi);
52   } else {
53     LOG_WARN("received invalid LL packet");
54   }
55 }
56 
Send(std::vector<uint8_t> const & packet,Phy::Type type,int8_t tx_power)57 void PhyDevice::Send(std::vector<uint8_t> const& packet, Phy::Type type,
58                      int8_t tx_power) {
59   for (auto const& phy : phy_layers_) {
60     if (phy->type == type) {
61       phy->Send(packet, tx_power, id);
62     }
63   }
64 }
65 
ToString()66 std::string PhyDevice::ToString() { return device_->ToString(); }
67 
68 }  // namespace rootcanal
69