• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <chrono>
20 #include <cstdint>
21 #include <map>
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:
Device()37   Device() { ASSERT(Address::FromString("BB:BB:BB:BB:BB:AD", address_)); }
38   virtual ~Device() = default;
39 
40   // Return a string representation of the type of device.
41   virtual std::string GetTypeString() const = 0;
42 
43   // Return the string representation of the device.
44   virtual std::string ToString() const;
45 
46   // Set the device's Bluetooth address.
SetAddress(Address address)47   void SetAddress(Address address) { address_.address = address.address; }
48 
49   // Get the device's Bluetooth address.
GetAddress()50   const Address& GetAddress() const { return address_; }
51 
Tick()52   virtual void Tick() {}
53   virtual void Close();
54 
ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView packet,Phy::Type type,int8_t rssi)55   virtual void ReceiveLinkLayerPacket(
56       model::packets::LinkLayerPacketView packet, Phy::Type type,
57       int8_t rssi){};
58 
59   void SendLinkLayerPacket(
60       std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,
61       Phy::Type type, int8_t tx_power = 0);
62 
63   void SendLinkLayerPacket(std::vector<uint8_t> const& packet, Phy::Type type,
64                            int8_t tx_power = 0);
65 
66   void RegisterLinkLayerChannel(
67       std::function<void(std::vector<uint8_t> const&, Phy::Type, int8_t)>
68           send_ll);
69 
70   void RegisterCloseCallback(std::function<void()> close_callback);
71 
72  protected:
73   // Unique device address. Used as public device address for
74   // Bluetooth activities.
75   Address address_;
76 
77   // Callback to be invoked when this device is closed.
78   std::function<void()> close_callback_;
79 
80   // Callback function to send link layer packets.
81   std::function<void(std::vector<uint8_t> const&, Phy::Type, uint8_t)> send_ll_;
82 };
83 
84 }  // namespace rootcanal
85