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 "model/devices/device_properties.h" 26 #include "model/setup/phy_layer.h" 27 #include "packets/link_layer/link_layer_packet_builder.h" 28 #include "packets/link_layer/link_layer_packet_view.h" 29 #include "types/address.h" 30 31 #include "stack/include/btm_ble_api.h" 32 33 namespace test_vendor_lib { 34 35 // Represent a Bluetooth Device 36 // - Provide Get*() and Set*() functions for device attributes. 37 class Device { 38 public: 39 Device(const std::string properties_filename = "") time_stamp_(std::chrono::steady_clock::now ())40 : time_stamp_(std::chrono::steady_clock::now()), properties_(properties_filename) {} 41 virtual ~Device() = default; 42 43 // Initialize the device based on the values of |args|. 44 virtual void Initialize(const std::vector<std::string>& args) = 0; 45 46 // Return a string representation of the type of device. 47 virtual std::string GetTypeString() const = 0; 48 49 // Return the string representation of the device. 50 virtual std::string ToString() const; 51 52 // Decide whether to accept a connection request 53 // May need to be extended to check peer address & type, and other 54 // connection parameters. 55 // Return true if the device accepts the connection request. LeConnect()56 virtual bool LeConnect() { 57 return false; 58 } 59 60 // Set the advertisement interval in milliseconds. SetAdvertisementInterval(std::chrono::milliseconds ms)61 void SetAdvertisementInterval(std::chrono::milliseconds ms) { 62 advertising_interval_ms_ = ms; 63 } 64 65 // Returns true if the host could see an advertisement in the next 66 // |scan_time| milliseconds. 67 virtual bool IsAdvertisementAvailable(std::chrono::milliseconds scan_time) const; 68 69 // Let the device know that time has passed. TimerTick()70 virtual void TimerTick() {} 71 72 void RegisterPhyLayer(std::shared_ptr<PhyLayer> phy); 73 74 void UnregisterPhyLayer(std::shared_ptr<PhyLayer> phy); 75 IncomingPacket(packets::LinkLayerPacketView)76 virtual void IncomingPacket(packets::LinkLayerPacketView){}; 77 78 virtual void SendLinkLayerPacket(std::shared_ptr<packets::LinkLayerPacketBuilder> packet, Phy::Type phy_type); 79 80 protected: 81 std::map<Phy::Type, std::vector<std::shared_ptr<PhyLayer>>> phy_layers_; 82 83 std::chrono::steady_clock::time_point time_stamp_; 84 85 // The time between page scans. 86 std::chrono::milliseconds page_scan_delay_ms_; 87 88 // The spec defines the advertising interval as a 16-bit value, but since it 89 // is never sent in packets, we use std::chrono::milliseconds. 90 std::chrono::milliseconds advertising_interval_ms_; 91 92 DeviceProperties properties_; 93 }; 94 95 } // namespace test_vendor_lib 96