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 #pragma once
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "hci/address.h"
26 #include "packets/link_layer_packets.h"
27 #include "phy.h"
28 
29 namespace rootcanal {
30 
31 using ::bluetooth::hci::Address;
32 
33 // Represent a Bluetooth Device
34 //  - Provide Get*() and Set*() functions for device attributes.
35 class Device {
36  public:
37   // Unique device identifier.
38   const uint32_t id_;
39 
40   Device();
41   virtual ~Device() = default;
42 
43   // Return a string representation of the type of device.
44   virtual std::string GetTypeString() const = 0;
45 
46   // Return the string representation of the device.
47   virtual std::string ToString() const;
48 
49   // Set the device's Bluetooth address.
SetAddress(Address address)50   void SetAddress(Address address) { address_.address = address.address; }
51 
52   // Get the device's Bluetooth address.
GetAddress()53   const Address& GetAddress() const { return address_; }
54 
Tick()55   virtual void Tick() {}
56   virtual void Close();
57 
ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView,Phy::Type,int8_t)58   virtual void ReceiveLinkLayerPacket(
59       model::packets::LinkLayerPacketView /*packet*/, Phy::Type /*type*/,
60       int8_t /*rssi*/) {}
61 
62   void SendLinkLayerPacket(
63       std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,
64       Phy::Type type, int8_t tx_power = 0);
65 
66   void SendLinkLayerPacket(std::vector<uint8_t> const& packet, Phy::Type type,
67                            int8_t tx_power = 0);
68 
69   void RegisterLinkLayerChannel(
70       std::function<void(std::vector<uint8_t> const&, Phy::Type, int8_t)>
71           send_ll);
72 
73   void RegisterCloseCallback(std::function<void()> close_callback);
74 
75  protected:
76   // Unique device address. Used as public device address for
77   // Bluetooth activities.
78   Address address_;
79 
80   // Callback to be invoked when this device is closed.
81   std::function<void()> close_callback_;
82 
83   // Callback function to send link layer packets.
84   std::function<void(std::vector<uint8_t> const&, Phy::Type, uint8_t)> send_ll_;
85 };
86 
87 }  // namespace rootcanal
88