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 <string> 22 #include <vector> 23 24 #include "bt_address.h" 25 26 #include "hci/include/hci_hal.h" 27 #include "stack/include/btm_ble_api.h" 28 29 namespace test_vendor_lib { 30 31 // Represent a Bluetooth Device 32 // - Provide Get*() and Set*() functions for device attributes. 33 class Device { 34 public: Device()35 Device() : time_stamp_(std::chrono::steady_clock::now()) {} 36 virtual ~Device() = default; 37 38 // Initialize the device based on the values of |args|. 39 virtual void Initialize(const std::vector<std::string>& args) = 0; 40 41 // Return a string representation of the type of device. 42 virtual std::string GetTypeString() const = 0; 43 44 // Return the string representation of the device. 45 virtual std::string ToString() const; 46 47 // Return a reference to the address. GetBtAddress()48 const BtAddress& GetBtAddress() const { return address_; } 49 50 // Set the address to the |addr|. SetBtAddress(const BtAddress & addr)51 void SetBtAddress(const BtAddress& addr) { address_ = addr; } 52 53 // Return the address type. GetAddressType()54 uint8_t GetAddressType() const { return address_type_; } 55 56 // Decide whether to accept a connection request 57 // May need to be extended to check peer address & type, and other 58 // connection parameters. 59 // Return true if the device accepts the connection request. LeConnect()60 virtual bool LeConnect() { return false; } 61 62 // Return the advertisement data. GetAdvertisement()63 const std::vector<uint8_t>& GetAdvertisement() const { return adv_data_; } 64 65 // Return the advertisement type. GetAdvertisementType()66 uint8_t GetAdvertisementType() const { return advertising_type_; } 67 68 // Set the advertisement interval in milliseconds. SetAdvertisementInterval(std::chrono::milliseconds ms)69 void SetAdvertisementInterval(std::chrono::milliseconds ms) { 70 advertising_interval_ms_ = ms; 71 } 72 73 // Return true if there is a scan response (allows for empty responses). HasScanResponse()74 bool HasScanResponse() const { return scan_response_present_; } 75 76 // Return the scan response data. GetScanResponse()77 const std::vector<uint8_t>& GetScanResponse() const { return scan_data_; } 78 79 // Returns true if the host could see an advertisement in the next 80 // |scan_time| milliseconds. 81 virtual bool IsAdvertisementAvailable( 82 std::chrono::milliseconds scan_time) const; 83 84 // Returns true if the host could see a page scan now. 85 virtual bool IsPageScanAvailable() const; 86 87 // Return the device class. 88 // The device class is a 3-byte value. Look for DEV_CLASS in 89 // stack/include/bt_types.h GetDeviceClass()90 uint32_t GetDeviceClass() const { return device_class_; } 91 92 // Return the clock offset, which is a defined in the Spec as: 93 // (CLKN_16-2 slave - CLKN_16-2 master ) mod 2**15. 94 // Bluetooth Core Specification Version 4.2, Volume 2, Part C, Section 4.3.2 GetClockOffset()95 uint16_t GetClockOffset() const { return clock_offset_; } 96 97 // Set the clock offset. SetClockOffset(uint16_t offset)98 void SetClockOffset(uint16_t offset) { clock_offset_ = offset; } 99 100 // Return the page scan repetition mode. 101 // Bluetooth Core Specification Version 4.2, Volume 2, Part B, Section 8.3.1 102 // The values are: 103 // 0 - R0 T_page_scan <= 1.28s and T_page_scan == T_window and 104 // 1 - R1 T_page_scan <= 1.28s 105 // 2 - R2 T_page_scan <= 2.56s GetPageScanRepetitionMode()106 uint8_t GetPageScanRepetitionMode() const { 107 return page_scan_repetition_mode_; 108 } 109 110 // Return the extended inquiry data. GetExtendedInquiryData()111 const std::vector<uint8_t>& GetExtendedInquiryData() const { 112 return extended_inquiry_data_; 113 } 114 115 // Let the device know that time has passed. TimerTick()116 virtual void TimerTick() {} 117 118 protected: 119 BtAddress address_; 120 121 // Address type is defined in the spec: 122 // 0x00 Public Device Address 123 // 0x01 Random Device Address 124 // 0x02 Public Identity Address 125 // 0x03 Random (static) Identity Address 126 // 0x04 – 0xFF Reserved for future use 127 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.12 128 uint8_t address_type_; 129 130 std::chrono::steady_clock::time_point time_stamp_; 131 132 // Return the device class. 133 // The device class is a 3-byte value. Look for DEV_CLASS in 134 // stack/include/bt_types.h 135 uint32_t device_class_; 136 137 // Return the page scan repetition mode. 138 // Bluetooth Core Specification Version 4.2, Volume 2, Part B, Section 8.3.1 139 // The values are: 140 // 0 - R0 T_page_scan <= 1.28s and T_page_scan == T_window and 141 // 1 - R1 T_page_scan <= 1.28s 142 // 2 - R2 T_page_scan <= 2.56s 143 uint8_t page_scan_repetition_mode_; 144 145 // The time between page scans. 146 std::chrono::milliseconds page_scan_delay_ms_; 147 148 std::vector<uint8_t> extended_inquiry_data_; 149 150 // Classic Bluetooth CLKN_slave[16..2] - CLKN_master[16..2] 151 // Bluetooth Core Specification Version 4.2, Volume 2, Part C, Section 4.3.2 152 uint16_t clock_offset_; 153 154 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.5 155 uint8_t advertising_type_; 156 157 // The spec defines the advertising interval as a 16-bit value, but since it 158 // is never sent in packets, we use std::chrono::milliseconds. 159 std::chrono::milliseconds advertising_interval_ms_; 160 161 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.7 162 163 // Bluetooth Core Specification Version 4.2, Volume 3, Part C, Section 164 // 11.1 165 // https://www.bluetooth.com/specifications/assigned-numbers 166 // Supplement to Bluetooth Core Specification | CSSv6, Part A 167 std::vector<uint8_t> adv_data_ = {0x07, // Length 168 BTM_BLE_AD_TYPE_NAME_CMPL, 169 'd', 170 'e', 171 'v', 172 'i', 173 'c', 174 'e'}; 175 176 bool scan_response_present_ = true; 177 std::vector<uint8_t> scan_data_ = {0x04, // Length 178 BTM_BLE_AD_TYPE_NAME_SHORT, 'd', 'e', 'v'}; 179 180 public: 181 static const uint8_t kBtAddressTypePublic = 0x00; 182 static const uint8_t kBtAddressTypeRandom = 0x01; 183 static const uint8_t kBtAddressTypePublicIdentity = 0x02; 184 static const uint8_t kBtAddressTypeRandomIdentity = 0x03; 185 }; 186 187 } // namespace test_vendor_lib 188