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(model::packets::LinkLayerPacketView /*packet*/, 59 Phy::Type /*type*/, int8_t /*rssi*/) {} 60 61 void SendLinkLayerPacket(std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet, 62 Phy::Type type, int8_t tx_power = 0); 63 64 void SendLinkLayerPacket(std::vector<uint8_t> const& packet, Phy::Type type, int8_t tx_power = 0); 65 66 void RegisterLinkLayerChannel( 67 std::function<void(std::vector<uint8_t> const&, Phy::Type, int8_t)> send_ll); 68 69 void RegisterCloseCallback(std::function<void()> close_callback); 70 71 protected: 72 // Unique device address. Used as public device address for 73 // Bluetooth activities. 74 Address address_; 75 76 // Callback to be invoked when this device is closed. 77 std::function<void()> close_callback_; 78 79 // Callback function to send link layer packets. 80 std::function<void(std::vector<uint8_t> const&, Phy::Type, uint8_t)> send_ll_; 81 }; 82 83 } // namespace rootcanal 84