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 { return GetTypeString() + "@" + address_.ToString(); }
41
Close()42 void Device::Close() {
43 if (close_callback_ != nullptr) {
44 close_callback_();
45 }
46 }
47
SendLinkLayerPacket(std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,Phy::Type type,int8_t tx_power)48 void Device::SendLinkLayerPacket(std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,
49 Phy::Type type, int8_t tx_power) {
50 SendLinkLayerPacket(packet->SerializeToBytes(), type, tx_power);
51 }
52
SendLinkLayerPacket(std::vector<uint8_t> const & packet,Phy::Type type,int8_t tx_power)53 void Device::SendLinkLayerPacket(std::vector<uint8_t> const& packet, Phy::Type type,
54 int8_t tx_power) {
55 if (send_ll_ != nullptr) {
56 send_ll_(packet, type, tx_power);
57 }
58 }
59
RegisterCloseCallback(std::function<void ()> close_callback)60 void Device::RegisterCloseCallback(std::function<void()> close_callback) {
61 close_callback_ = close_callback;
62 }
63
RegisterLinkLayerChannel(std::function<void (std::vector<uint8_t> const &,Phy::Type,int8_t)> send_ll)64 void Device::RegisterLinkLayerChannel(
65 std::function<void(std::vector<uint8_t> const&, Phy::Type, int8_t)> send_ll) {
66 send_ll_ = send_ll;
67 }
68
69 } // namespace rootcanal
70