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 <log.h>
20
21 #include "phy_layer.h"
22
23 namespace rootcanal {
24
PhyDevice(std::string type,std::shared_ptr<Device> device)25 PhyDevice::PhyDevice(std::string type, std::shared_ptr<Device> device)
26 : id(device->id_), type(std::move(type)), device_(std::move(device)) {
27 using namespace std::placeholders;
28 ASSERT(device_ != nullptr);
29 device_->RegisterLinkLayerChannel(
30 std::bind(&PhyDevice::Send, this, _1, _2, _3));
31 }
32
Register(PhyLayer * phy)33 void PhyDevice::Register(PhyLayer* phy) { phy_layers_.insert(phy); }
34
Unregister(PhyLayer * phy)35 void PhyDevice::Unregister(PhyLayer* phy) { phy_layers_.erase(phy); }
36
Tick()37 void PhyDevice::Tick() { device_->Tick(); }
38
GetAddress() const39 bluetooth::hci::Address PhyDevice::GetAddress() const {
40 return device_->GetAddress();
41 }
42
GetDevice() const43 std::shared_ptr<Device> PhyDevice::GetDevice() const {
44 return device_;
45 }
46
SetAddress(bluetooth::hci::Address address)47 void PhyDevice::SetAddress(bluetooth::hci::Address address) {
48 device_->SetAddress(std::move(address));
49 }
50
Receive(std::vector<uint8_t> const & packet,Phy::Type type,int8_t rssi)51 void PhyDevice::Receive(std::vector<uint8_t> const& packet, Phy::Type type,
52 int8_t rssi) {
53 std::shared_ptr<std::vector<uint8_t>> packet_copy =
54 std::make_shared<std::vector<uint8_t>>(packet);
55 model::packets::LinkLayerPacketView packet_view =
56 model::packets::LinkLayerPacketView::Create(
57 pdl::packet::slice(packet_copy));
58 if (packet_view.IsValid()) {
59 device_->ReceiveLinkLayerPacket(std::move(packet_view), type, rssi);
60 } else {
61 WARNING("received invalid LL packet");
62 }
63 }
64
Send(std::vector<uint8_t> const & packet,Phy::Type type,int8_t tx_power)65 void PhyDevice::Send(std::vector<uint8_t> const& packet, Phy::Type type,
66 int8_t tx_power) {
67 for (auto const& phy : phy_layers_) {
68 if (phy->type == type) {
69 phy->Send(packet, tx_power, id);
70 }
71 }
72 }
73
ToString()74 std::string PhyDevice::ToString() { return device_->ToString(); }
75
76 } // namespace rootcanal
77