1 /*
2  * Copyright 2016 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 "model/devices/device.h"
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "log.h"
26 #include "packets/link_layer_packets.h"
27 #include "phy.h"
28 
29 namespace rootcanal {
30 
next_instance_id()31 static uint32_t next_instance_id() {
32   static uint32_t instance_counter = 0;
33   return instance_counter++;
34 }
35 
Device()36 Device::Device() : id_(next_instance_id()) {
37   ASSERT(Address::FromString("BB:BB:BB:BB:BB:AD", address_));
38 }
39 
ToString() const40 std::string Device::ToString() const {
41   return GetTypeString() + "@" + address_.ToString();
42 }
43 
Close()44 void Device::Close() {
45   if (close_callback_ != nullptr) {
46     close_callback_();
47   }
48 }
49 
SendLinkLayerPacket(std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,Phy::Type type,int8_t tx_power)50 void Device::SendLinkLayerPacket(
51     std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,
52     Phy::Type type, int8_t tx_power) {
53   SendLinkLayerPacket(packet->SerializeToBytes(), type, tx_power);
54 }
55 
SendLinkLayerPacket(std::vector<uint8_t> const & packet,Phy::Type type,int8_t tx_power)56 void Device::SendLinkLayerPacket(std::vector<uint8_t> const& packet,
57                                  Phy::Type type, int8_t tx_power) {
58   if (send_ll_ != nullptr) {
59     send_ll_(packet, type, tx_power);
60   }
61 }
62 
RegisterCloseCallback(std::function<void ()> close_callback)63 void Device::RegisterCloseCallback(std::function<void()> close_callback) {
64   close_callback_ = close_callback;
65 }
66 
RegisterLinkLayerChannel(std::function<void (std::vector<uint8_t> const &,Phy::Type,int8_t)> send_ll)67 void Device::RegisterLinkLayerChannel(
68     std::function<void(std::vector<uint8_t> const&, Phy::Type, int8_t)>
69         send_ll) {
70   send_ll_ = send_ll;
71 }
72 
73 }  // namespace rootcanal
74