• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::array<uint8_t, 64>& GetSupportedCommands() const {
45     return supported_commands_;
46   }
47 
SetSupportedCommands(const std::array<uint8_t,64> & commands)48   void SetSupportedCommands(const std::array<uint8_t, 64>& commands) {
49     supported_commands_ = commands;
50   }
51 
52   // Specification Version 4.2, Volume 2, Part E, Section 7.4.3
GetSupportedFeatures()53   uint64_t GetSupportedFeatures() const {
54     return extended_features_[0];
55   }
56 
SetExtendedFeatures(uint64_t features,uint8_t page_number)57   void SetExtendedFeatures(uint64_t features, uint8_t page_number) {
58     ASSERT(page_number < extended_features_.size());
59     extended_features_[page_number] = features;
60   }
61 
GetSecureSimplePairingSupported()62   bool GetSecureSimplePairingSupported() const {
63     uint64_t ssp_bit = 0x1;
64     return extended_features_[1] & ssp_bit;
65   }
66 
SetSecureSimplePairingSupport(bool supported)67   void SetSecureSimplePairingSupport(bool supported) {
68     uint64_t ssp_bit = 0x1;
69     extended_features_[1] &= ~ssp_bit;
70     if (supported) {
71       extended_features_[1] = extended_features_[1] | ssp_bit;
72     }
73   }
74 
SetLeHostSupport(bool le_supported)75   void SetLeHostSupport(bool le_supported) {
76     uint64_t le_bit = 0x2;
77     extended_features_[1] &= ~le_bit;
78     if (le_supported) {
79       extended_features_[1] = extended_features_[1] | le_bit;
80     }
81   }
82 
SetSecureConnections(bool supported)83   void SetSecureConnections(bool supported) {
84     uint64_t secure_bit = 0x8;
85     extended_features_[1] &= ~secure_bit;
86     if (supported) {
87       extended_features_[1] = extended_features_[1] | secure_bit;
88     }
89   }
90 
91   // Specification Version 4.2, Volume 2, Part E, Section 7.4.4
GetExtendedFeaturesMaximumPageNumber()92   uint8_t GetExtendedFeaturesMaximumPageNumber() const {
93     return extended_features_.size() - 1;
94   }
95 
GetExtendedFeatures(uint8_t page_number)96   uint64_t GetExtendedFeatures(uint8_t page_number) const {
97     ASSERT(page_number < extended_features_.size());
98     return extended_features_[page_number];
99   }
100 
101   // Specification Version 4.2, Volume 2, Part E, Section 7.4.5
GetAclDataPacketSize()102   uint16_t GetAclDataPacketSize() const {
103     return acl_data_packet_size_;
104   }
105 
GetSynchronousDataPacketSize()106   uint8_t GetSynchronousDataPacketSize() const {
107     return sco_data_packet_size_;
108   }
109 
GetEncryptionKeySize()110   uint8_t GetEncryptionKeySize() const { return encryption_key_size_; }
111 
GetVoiceSetting()112   uint16_t GetVoiceSetting() const { return voice_setting_; }
113 
SetVoiceSetting(uint16_t voice_setting)114   void SetVoiceSetting(uint16_t voice_setting) {
115     voice_setting_ = voice_setting;
116   }
117 
GetConnectionAcceptTimeout()118   uint16_t GetConnectionAcceptTimeout() const {
119     return connection_accept_timeout_;
120   }
121 
SetConnectionAcceptTimeout(uint16_t connection_accept_timeout)122   void SetConnectionAcceptTimeout(uint16_t connection_accept_timeout) {
123     connection_accept_timeout_ = connection_accept_timeout;
124   }
125 
GetTotalNumAclDataPackets()126   uint16_t GetTotalNumAclDataPackets() const {
127     return num_acl_data_packets_;
128   }
129 
GetTotalNumSynchronousDataPackets()130   uint16_t GetTotalNumSynchronousDataPackets() const {
131     return num_sco_data_packets_;
132   }
133 
GetAddress()134   const Address& GetAddress() const {
135     return address_;
136   }
137 
SetAddress(const Address & address)138   void SetAddress(const Address& address) {
139     address_ = address;
140   }
141 
142   // Specification Version 4.2, Volume 2, Part E, Section 7.4.8
GetSupportedCodecs()143   const std::vector<uint8_t>& GetSupportedCodecs() const {
144     return supported_codecs_;
145   }
146 
GetVendorSpecificCodecs()147   const std::vector<uint32_t>& GetVendorSpecificCodecs() const {
148     return vendor_specific_codecs_;
149   }
150 
GetVersion()151   uint8_t GetVersion() const {
152     return version_;
153   }
154 
GetRevision()155   uint16_t GetRevision() const {
156     return revision_;
157   }
158 
GetLmpPalVersion()159   uint8_t GetLmpPalVersion() const {
160     return lmp_pal_version_;
161   }
162 
GetLmpPalSubversion()163   uint16_t GetLmpPalSubversion() const {
164     return lmp_pal_subversion_;
165   }
166 
GetManufacturerName()167   uint16_t GetManufacturerName() const {
168     return manufacturer_name_;
169   }
170 
GetAuthenticationEnable()171   uint8_t GetAuthenticationEnable() const {
172     return authentication_enable_;
173   }
174 
SetAuthenticationEnable(uint8_t enable)175   void SetAuthenticationEnable(uint8_t enable) {
176     authentication_enable_ = enable;
177   }
178 
GetClassOfDevice()179   ClassOfDevice GetClassOfDevice() const {
180     return class_of_device_;
181   }
182 
SetClassOfDevice(uint8_t b0,uint8_t b1,uint8_t b2)183   void SetClassOfDevice(uint8_t b0, uint8_t b1, uint8_t b2) {
184     class_of_device_.cod[0] = b0;
185     class_of_device_.cod[1] = b1;
186     class_of_device_.cod[2] = b2;
187   }
188 
SetClassOfDevice(uint32_t class_of_device)189   void SetClassOfDevice(uint32_t class_of_device) {
190     class_of_device_.cod[0] = class_of_device & 0xff;
191     class_of_device_.cod[1] = (class_of_device >> 8) & 0xff;
192     class_of_device_.cod[2] = (class_of_device >> 16) & 0xff;
193   }
194 
SetName(const std::vector<uint8_t> & name)195   void SetName(const std::vector<uint8_t>& name) {
196     name_.fill(0);
197     for (size_t i = 0; i < 248 && i < name.size(); i++) {
198       name_[i] = name[i];
199     }
200   }
201 
GetName()202   const std::array<uint8_t, 248>& GetName() const { return name_; }
203 
SetExtendedInquiryData(const std::vector<uint8_t> & eid)204   void SetExtendedInquiryData(const std::vector<uint8_t>& eid) {
205     extended_inquiry_data_ = eid;
206   }
207 
GetExtendedInquiryData()208   const std::vector<uint8_t>& GetExtendedInquiryData() const {
209     return extended_inquiry_data_;
210   }
211 
GetPageScanRepetitionMode()212   uint8_t GetPageScanRepetitionMode() const {
213     return page_scan_repetition_mode_;
214   }
215 
SetPageScanRepetitionMode(uint8_t mode)216   void SetPageScanRepetitionMode(uint8_t mode) {
217     page_scan_repetition_mode_ = mode;
218   }
219 
GetClockOffset()220   uint16_t GetClockOffset() const {
221     return clock_offset_;
222   }
223 
SetClockOffset(uint16_t offset)224   void SetClockOffset(uint16_t offset) {
225     clock_offset_ = offset;
226   }
227 
GetEventMask()228   uint64_t GetEventMask() const { return event_mask_; }
229 
SetEventMask(uint64_t mask)230   void SetEventMask(uint64_t mask) { event_mask_ = mask; }
231 
232   // Low-Energy functions
GetLeAddress()233   const Address& GetLeAddress() const {
234     return le_address_;
235   }
236 
SetLeAddress(const Address & address)237   void SetLeAddress(const Address& address) {
238     le_address_ = address;
239   }
240 
GetLeAddressType()241   uint8_t GetLeAddressType() const {
242     return le_address_type_;
243   }
244 
SetLeAddressType(uint8_t addr_type)245   void SetLeAddressType(uint8_t addr_type) {
246     le_address_type_ = addr_type;
247   }
248 
GetLeAdvertisementType()249   uint8_t GetLeAdvertisementType() const {
250     return le_advertisement_type_;
251   }
252 
GetLeAdvertisingIntervalMin()253   uint16_t GetLeAdvertisingIntervalMin() const {
254     return le_advertising_interval_min_;
255   }
256 
GetLeAdvertisingIntervalMax()257   uint16_t GetLeAdvertisingIntervalMax() const {
258     return le_advertising_interval_max_;
259   }
260 
GetLeAdvertisingOwnAddressType()261   uint8_t GetLeAdvertisingOwnAddressType() const {
262     return le_advertising_own_address_type_;
263   }
264 
GetLeAdvertisingPeerAddressType()265   uint8_t GetLeAdvertisingPeerAddressType() const {
266     return le_advertising_peer_address_type_;
267   }
268 
GetLeAdvertisingPeerAddress()269   Address GetLeAdvertisingPeerAddress() const {
270     return le_advertising_peer_address_;
271   }
272 
GetLeAdvertisingChannelMap()273   uint8_t GetLeAdvertisingChannelMap() const {
274     return le_advertising_channel_map_;
275   }
276 
GetLeAdvertisingFilterPolicy()277   uint8_t GetLeAdvertisingFilterPolicy() const {
278     return le_advertising_filter_policy_;
279   }
280 
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)281   void SetLeAdvertisingParameters(uint16_t interval_min, uint16_t interval_max, uint8_t ad_type,
282                                   uint8_t own_address_type, uint8_t peer_address_type, Address peer_address,
283                                   uint8_t channel_map, uint8_t filter_policy) {
284     le_advertisement_type_ = ad_type;
285     le_advertising_interval_min_ = interval_min;
286     le_advertising_interval_max_ = interval_max;
287     le_advertising_own_address_type_ = own_address_type;
288     le_advertising_peer_address_type_ = peer_address_type;
289     le_advertising_peer_address_ = peer_address;
290     le_advertising_channel_map_ = channel_map;
291     le_advertising_filter_policy_ = filter_policy;
292   }
293 
SetLeAdvertisementType(uint8_t ad_type)294   void SetLeAdvertisementType(uint8_t ad_type) {
295     le_advertisement_type_ = ad_type;
296   }
297 
SetLeAdvertisement(const std::vector<uint8_t> & ad)298   void SetLeAdvertisement(const std::vector<uint8_t>& ad) {
299     le_advertisement_ = ad;
300   }
301 
GetLeAdvertisement()302   const std::vector<uint8_t>& GetLeAdvertisement() const {
303     return le_advertisement_;
304   }
305 
SetLeScanResponse(const std::vector<uint8_t> & response)306   void SetLeScanResponse(const std::vector<uint8_t>& response) {
307     le_scan_response_ = response;
308   }
309 
GetLeScanResponse()310   const std::vector<uint8_t>& GetLeScanResponse() const {
311     return le_scan_response_;
312   }
313 
314   // Specification Version 4.2, Volume 2, Part E, Section 7.8.2
GetLeDataPacketLength()315   uint16_t GetLeDataPacketLength() const {
316     return le_data_packet_length_;
317   }
318 
GetTotalNumLeDataPackets()319   uint8_t GetTotalNumLeDataPackets() const {
320     return num_le_data_packets_;
321   }
322 
323   // Specification Version 4.2, Volume 2, Part E, Section 7.8.3
GetLeSupportedFeatures()324   uint64_t GetLeSupportedFeatures() const {
325     return le_supported_features_;
326   }
327 
328   // Specification Version 5.2, Volume 4, Part E, Section 7.8.6
GetLeAdvertisingPhysicalChannelTxPower()329   int8_t GetLeAdvertisingPhysicalChannelTxPower() const {
330     return le_advertising_physical_channel_tx_power_;
331   }
332 
SetLeSupportedFeatures(uint64_t features)333   void SetLeSupportedFeatures(uint64_t features) {
334     le_supported_features_ = features;
335   }
336 
GetLeEventSupported(bluetooth::hci::SubeventCode subevent_code)337   bool GetLeEventSupported(bluetooth::hci::SubeventCode subevent_code) const {
338     return le_event_mask_ & (1u << static_cast<uint64_t>(subevent_code));
339   }
340 
GetLeEventMask()341   uint64_t GetLeEventMask() const { return le_event_mask_; }
342 
SetLeEventMask(uint64_t mask)343   void SetLeEventMask(uint64_t mask) { le_event_mask_ = mask; }
344 
345   // Specification Version 4.2, Volume 2, Part E, Section 7.8.14
GetLeConnectListSize()346   uint8_t GetLeConnectListSize() const { return le_connect_list_size_; }
347 
348   // Specification Version 4.2, Volume 2, Part E, Section 7.8.27
GetLeSupportedStates()349   uint64_t GetLeSupportedStates() const {
350     return le_supported_states_;
351   }
352 
353   // Specification Version 4.2, Volume 2, Part E, Section 7.8.41
GetLeResolvingListSize()354   uint8_t GetLeResolvingListSize() const { return le_resolving_list_size_; }
355 
356   // Vendor-specific commands
GetLeVendorCap()357   const std::vector<uint8_t>& GetLeVendorCap() const {
358     return le_vendor_cap_;
359   }
360 
361   static void RegisterJSONConverter(base::JSONValueConverter<DeviceProperties>* converter);
362 
363  private:
364   // Classic
365   uint16_t acl_data_packet_size_;
366   uint8_t sco_data_packet_size_;
367   uint16_t num_acl_data_packets_;
368   uint16_t num_sco_data_packets_;
369   uint8_t version_;
370   uint16_t revision_;
371   uint8_t lmp_pal_version_;
372   uint16_t manufacturer_name_;
373   uint16_t lmp_pal_subversion_;
374   uint64_t supported_features_{};
375   uint64_t event_mask_{0x00001fffffffffff};
376   uint8_t authentication_enable_{};
377   std::vector<uint8_t> supported_codecs_;
378   std::vector<uint32_t> vendor_specific_codecs_;
379   std::array<uint8_t, 64> supported_commands_;
380   std::vector<uint64_t> extended_features_{{0x875b3fd8fe8ffeff, 0x04}};
381   ClassOfDevice class_of_device_{{0, 0, 0}};
382   std::vector<uint8_t> extended_inquiry_data_;
383   std::array<uint8_t, 248> name_{};
384   Address address_{};
385   uint8_t page_scan_repetition_mode_{};
386   uint16_t clock_offset_{};
387   uint8_t encryption_key_size_{10};
388   uint16_t voice_setting_{0x0060};
389   uint16_t connection_accept_timeout_{0x7d00};
390 
391   // Low Energy
392   uint16_t le_data_packet_length_;
393   uint8_t num_le_data_packets_;
394   uint8_t le_connect_list_size_;
395   uint8_t le_resolving_list_size_;
396   uint64_t le_supported_features_{0x075b3fd8fe8ffeff};
397   int8_t le_advertising_physical_channel_tx_power_{0x00};
398   uint64_t le_supported_states_;
399   uint64_t le_event_mask_{0x01f};
400   std::vector<uint8_t> le_vendor_cap_;
401   Address le_address_{};
402   uint8_t le_address_type_{};
403 
404   uint16_t le_advertising_interval_min_{};
405   uint16_t le_advertising_interval_max_{};
406   uint8_t le_advertising_own_address_type_{};
407   uint8_t le_advertising_peer_address_type_{};
408   Address le_advertising_peer_address_{};
409   uint8_t le_advertising_channel_map_{};
410   uint8_t le_advertising_filter_policy_{};
411   uint8_t le_advertisement_type_{};
412   std::vector<uint8_t> le_advertisement_;
413   std::vector<uint8_t> le_scan_response_;
414 };
415 
416 }  // namespace test_vendor_lib
417