1 /*
2  * Copyright 2015 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 <array>
20 #include <cstdint>
21 #include <string>
22 #include <vector>
23 
24 #include "base/json/json_value_converter.h"
25 #include "hci/address.h"
26 #include "hci/hci_packets.h"
27 #include "os/log.h"
28 
29 namespace test_vendor_lib {
30 
31 using ::bluetooth::hci::Address;
32 using ::bluetooth::hci::ClassOfDevice;
33 
34 class DeviceProperties {
35  public:
36   explicit DeviceProperties(const std::string& file_name = "");
37 
38   // Access private configuration data
39 
40   // Specification Version 4.2, Volume 2, Part E, Section 7.4.1
41   const std::vector<uint8_t>& GetVersionInformation() const;
42 
43   // Specification Version 4.2, Volume 2, Part E, Section 7.4.2
GetSupportedCommands()44   const std::vector<uint8_t>& GetSupportedCommands() const {
45     return supported_commands_;
46   }
47 
48   // Specification Version 4.2, Volume 2, Part E, Section 7.4.3
GetSupportedFeatures()49   uint64_t GetSupportedFeatures() const {
50     return extended_features_[0];
51   }
52 
SetExtendedFeatures(uint64_t features,uint8_t page_number)53   void SetExtendedFeatures(uint64_t features, uint8_t page_number) {
54     ASSERT(page_number < extended_features_.size());
55     extended_features_[page_number] = features;
56   }
57 
58   // Specification Version 4.2, Volume 2, Part E, Section 7.4.4
GetExtendedFeaturesMaximumPageNumber()59   uint8_t GetExtendedFeaturesMaximumPageNumber() const {
60     return extended_features_.size() - 1;
61   }
62 
GetExtendedFeatures(uint8_t page_number)63   uint64_t GetExtendedFeatures(uint8_t page_number) const {
64     ASSERT(page_number < extended_features_.size());
65     return extended_features_[page_number];
66   }
67 
68   // Specification Version 4.2, Volume 2, Part E, Section 7.4.5
GetAclDataPacketSize()69   uint16_t GetAclDataPacketSize() const {
70     return acl_data_packet_size_;
71   }
72 
GetSynchronousDataPacketSize()73   uint8_t GetSynchronousDataPacketSize() const {
74     return sco_data_packet_size_;
75   }
76 
GetEncryptionKeySize()77   uint8_t GetEncryptionKeySize() const { return encryption_key_size_; }
78 
GetTotalNumAclDataPackets()79   uint16_t GetTotalNumAclDataPackets() const {
80     return num_acl_data_packets_;
81   }
82 
GetTotalNumSynchronousDataPackets()83   uint16_t GetTotalNumSynchronousDataPackets() const {
84     return num_sco_data_packets_;
85   }
86 
GetAddress()87   const Address& GetAddress() const {
88     return address_;
89   }
90 
SetAddress(const Address & address)91   void SetAddress(const Address& address) {
92     address_ = address;
93   }
94 
95   // Specification Version 4.2, Volume 2, Part E, Section 7.4.8
GetSupportedCodecs()96   const std::vector<uint8_t>& GetSupportedCodecs() const {
97     return supported_codecs_;
98   }
99 
GetVendorSpecificCodecs()100   const std::vector<uint32_t>& GetVendorSpecificCodecs() const {
101     return vendor_specific_codecs_;
102   }
103 
GetVersion()104   uint8_t GetVersion() const {
105     return version_;
106   }
107 
GetRevision()108   uint16_t GetRevision() const {
109     return revision_;
110   }
111 
GetLmpPalVersion()112   uint8_t GetLmpPalVersion() const {
113     return lmp_pal_version_;
114   }
115 
GetLmpPalSubversion()116   uint16_t GetLmpPalSubversion() const {
117     return lmp_pal_subversion_;
118   }
119 
GetManufacturerName()120   uint16_t GetManufacturerName() const {
121     return manufacturer_name_;
122   }
123 
GetAuthenticationEnable()124   uint8_t GetAuthenticationEnable() const {
125     return authentication_enable_;
126   }
127 
SetAuthenticationEnable(uint8_t enable)128   void SetAuthenticationEnable(uint8_t enable) {
129     authentication_enable_ = enable;
130   }
131 
GetClassOfDevice()132   ClassOfDevice GetClassOfDevice() const {
133     return class_of_device_;
134   }
135 
SetClassOfDevice(uint8_t b0,uint8_t b1,uint8_t b2)136   void SetClassOfDevice(uint8_t b0, uint8_t b1, uint8_t b2) {
137     class_of_device_.cod[0] = b0;
138     class_of_device_.cod[1] = b1;
139     class_of_device_.cod[2] = b2;
140   }
141 
SetClassOfDevice(uint32_t class_of_device)142   void SetClassOfDevice(uint32_t class_of_device) {
143     class_of_device_.cod[0] = class_of_device & 0xff;
144     class_of_device_.cod[1] = (class_of_device >> 8) & 0xff;
145     class_of_device_.cod[2] = (class_of_device >> 16) & 0xff;
146   }
147 
SetName(const std::vector<uint8_t> & name)148   void SetName(const std::vector<uint8_t>& name) {
149     name_.fill(0);
150     for (size_t i = 0; i < 248 && i < name.size(); i++) {
151       name_[i] = name[i];
152     }
153   }
154 
GetName()155   const std::array<uint8_t, 248>& GetName() const { return name_; }
156 
SetExtendedInquiryData(const std::vector<uint8_t> & eid)157   void SetExtendedInquiryData(const std::vector<uint8_t>& eid) {
158     extended_inquiry_data_ = eid;
159   }
160 
GetExtendedInquiryData()161   const std::vector<uint8_t>& GetExtendedInquiryData() const {
162     return extended_inquiry_data_;
163   }
164 
GetPageScanRepetitionMode()165   uint8_t GetPageScanRepetitionMode() const {
166     return page_scan_repetition_mode_;
167   }
168 
SetPageScanRepetitionMode(uint8_t mode)169   void SetPageScanRepetitionMode(uint8_t mode) {
170     page_scan_repetition_mode_ = mode;
171   }
172 
GetClockOffset()173   uint16_t GetClockOffset() const {
174     return clock_offset_;
175   }
176 
SetClockOffset(uint16_t offset)177   void SetClockOffset(uint16_t offset) {
178     clock_offset_ = offset;
179   }
180 
181   // Low-Energy functions
GetLeAddress()182   const Address& GetLeAddress() const {
183     return le_address_;
184   }
185 
SetLeAddress(const Address & address)186   void SetLeAddress(const Address& address) {
187     le_address_ = address;
188   }
189 
GetLeAddressType()190   uint8_t GetLeAddressType() const {
191     return le_address_type_;
192   }
193 
SetLeAddressType(uint8_t addr_type)194   void SetLeAddressType(uint8_t addr_type) {
195     le_address_type_ = addr_type;
196   }
197 
GetLeAdvertisementType()198   uint8_t GetLeAdvertisementType() const {
199     return le_advertisement_type_;
200   }
201 
GetLeAdvertisingIntervalMin()202   uint16_t GetLeAdvertisingIntervalMin() const {
203     return le_advertising_interval_min_;
204   }
205 
GetLeAdvertisingIntervalMax()206   uint16_t GetLeAdvertisingIntervalMax() const {
207     return le_advertising_interval_max_;
208   }
209 
GetLeAdvertisingOwnAddressType()210   uint8_t GetLeAdvertisingOwnAddressType() const {
211     return le_advertising_own_address_type_;
212   }
213 
GetLeAdvertisingPeerAddressType()214   uint8_t GetLeAdvertisingPeerAddressType() const {
215     return le_advertising_peer_address_type_;
216   }
217 
GetLeAdvertisingPeerAddress()218   Address GetLeAdvertisingPeerAddress() const {
219     return le_advertising_peer_address_;
220   }
221 
GetLeAdvertisingChannelMap()222   uint8_t GetLeAdvertisingChannelMap() const {
223     return le_advertising_channel_map_;
224   }
225 
GetLeAdvertisingFilterPolicy()226   uint8_t GetLeAdvertisingFilterPolicy() const {
227     return le_advertising_filter_policy_;
228   }
229 
SetLeAdvertisingParameters(uint16_t interval_min,uint16_t interval_max,uint8_t ad_type,uint8_t own_address_type,uint8_t peer_address_type,Address peer_address,uint8_t channel_map,uint8_t filter_policy)230   void SetLeAdvertisingParameters(uint16_t interval_min, uint16_t interval_max, uint8_t ad_type,
231                                   uint8_t own_address_type, uint8_t peer_address_type, Address peer_address,
232                                   uint8_t channel_map, uint8_t filter_policy) {
233     le_advertisement_type_ = ad_type;
234     le_advertising_interval_min_ = interval_min;
235     le_advertising_interval_max_ = interval_max;
236     le_advertising_own_address_type_ = own_address_type;
237     le_advertising_peer_address_type_ = peer_address_type;
238     le_advertising_peer_address_ = peer_address;
239     le_advertising_channel_map_ = channel_map;
240     le_advertising_filter_policy_ = filter_policy;
241   }
242 
SetLeAdvertisementType(uint8_t ad_type)243   void SetLeAdvertisementType(uint8_t ad_type) {
244     le_advertisement_type_ = ad_type;
245   }
246 
SetLeAdvertisement(const std::vector<uint8_t> & ad)247   void SetLeAdvertisement(const std::vector<uint8_t>& ad) {
248     le_advertisement_ = ad;
249   }
250 
GetLeAdvertisement()251   const std::vector<uint8_t>& GetLeAdvertisement() const {
252     return le_advertisement_;
253   }
254 
SetLeScanResponse(const std::vector<uint8_t> & response)255   void SetLeScanResponse(const std::vector<uint8_t>& response) {
256     le_scan_response_ = response;
257   }
258 
GetLeScanResponse()259   const std::vector<uint8_t>& GetLeScanResponse() const {
260     return le_scan_response_;
261   }
262 
263   // Specification Version 4.2, Volume 2, Part E, Section 7.8.2
GetLeDataPacketLength()264   uint16_t GetLeDataPacketLength() const {
265     return le_data_packet_length_;
266   }
267 
GetTotalNumLeDataPackets()268   uint8_t GetTotalNumLeDataPackets() const {
269     return num_le_data_packets_;
270   }
271 
272   // Specification Version 4.2, Volume 2, Part E, Section 7.8.3
GetLeSupportedFeatures()273   uint64_t GetLeSupportedFeatures() const {
274     return le_supported_features_;
275   }
276 
SetLeSupportedFeatures(uint64_t features)277   void SetLeSupportedFeatures(uint64_t features) {
278     le_supported_features_ = features;
279   }
280 
281   // Specification Version 4.2, Volume 2, Part E, Section 7.8.14
GetLeConnectListSize()282   uint8_t GetLeConnectListSize() const { return le_connect_list_size_; }
283 
284   // Specification Version 4.2, Volume 2, Part E, Section 7.8.27
GetLeSupportedStates()285   uint64_t GetLeSupportedStates() const {
286     return le_supported_states_;
287   }
288 
289   // Specification Version 4.2, Volume 2, Part E, Section 7.8.41
GetLeResolvingListSize()290   uint8_t GetLeResolvingListSize() const { return le_resolving_list_size_; }
291 
292   // Vendor-specific commands
GetLeVendorCap()293   const std::vector<uint8_t>& GetLeVendorCap() const {
294     return le_vendor_cap_;
295   }
296 
297   static void RegisterJSONConverter(base::JSONValueConverter<DeviceProperties>* converter);
298 
299  private:
300   // Classic
301   uint16_t acl_data_packet_size_;
302   uint8_t sco_data_packet_size_;
303   uint16_t num_acl_data_packets_;
304   uint16_t num_sco_data_packets_;
305   uint8_t version_;
306   uint16_t revision_;
307   uint8_t lmp_pal_version_;
308   uint16_t manufacturer_name_;
309   uint16_t lmp_pal_subversion_;
310   uint64_t supported_features_{};
311   uint8_t authentication_enable_{};
312   std::vector<uint8_t> supported_codecs_;
313   std::vector<uint32_t> vendor_specific_codecs_;
314   std::vector<uint8_t> supported_commands_;
315   std::vector<uint64_t> extended_features_{{0x875b3fd8fe8ffeff, 0x0f}};
316   ClassOfDevice class_of_device_{{0, 0, 0}};
317   std::vector<uint8_t> extended_inquiry_data_;
318   std::array<uint8_t, 248> name_{};
319   Address address_{};
320   uint8_t page_scan_repetition_mode_{};
321   uint16_t clock_offset_{};
322   uint8_t encryption_key_size_{10};
323 
324   // Low Energy
325   uint16_t le_data_packet_length_;
326   uint8_t num_le_data_packets_;
327   uint8_t le_connect_list_size_;
328   uint8_t le_resolving_list_size_;
329   uint64_t le_supported_features_{0x075b3fd8fe8ffeff};
330   uint64_t le_supported_states_;
331   std::vector<uint8_t> le_vendor_cap_;
332   Address le_address_{};
333   uint8_t le_address_type_{};
334 
335   uint16_t le_advertising_interval_min_{};
336   uint16_t le_advertising_interval_max_{};
337   uint8_t le_advertising_own_address_type_{};
338   uint8_t le_advertising_peer_address_type_{};
339   Address le_advertising_peer_address_{};
340   uint8_t le_advertising_channel_map_{};
341   uint8_t le_advertising_filter_policy_{};
342   uint8_t le_advertisement_type_{};
343   std::vector<uint8_t> le_advertisement_;
344   std::vector<uint8_t> le_scan_response_;
345 };
346 
347 }  // namespace test_vendor_lib
348