1 /*
2 * Copyright 2015 The Android Open Source Project
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "device_properties.h"
17
18 #include <memory>
19
20 #include "base/files/file_util.h"
21 #include "base/json/json_reader.h"
22 #include "base/values.h"
23
24 #include "os/log.h"
25 #include "osi/include/osi.h"
26
27 using std::vector;
28
29 namespace {
30 // Functions used by JSONValueConverter to read stringified JSON.
ParseUint8t(base::StringPiece value,uint8_t * field)31 bool ParseUint8t(base::StringPiece value, uint8_t* field) {
32 *field = std::stoi(value.as_string());
33 return true;
34 }
35
ParseUint16t(base::StringPiece value,uint16_t * field)36 bool ParseUint16t(base::StringPiece value, uint16_t* field) {
37 *field = std::stoi(value.as_string());
38 return true;
39 }
40
41 } // namespace
42
43 namespace test_vendor_lib {
44
DeviceProperties(const std::string & file_name)45 DeviceProperties::DeviceProperties(const std::string& file_name)
46 : acl_data_packet_size_(1024),
47 sco_data_packet_size_(255),
48 num_acl_data_packets_(10),
49 num_sco_data_packets_(10),
50 version_(static_cast<uint8_t>(bluetooth::hci::HciVersion::V_4_1)),
51 revision_(0),
52 lmp_pal_version_(static_cast<uint8_t>(bluetooth::hci::LmpVersion::V_4_1)),
53 manufacturer_name_(0),
54 lmp_pal_subversion_(0),
55 le_data_packet_length_(27),
56 num_le_data_packets_(15),
57 le_connect_list_size_(15),
58 le_resolving_list_size_(15) {
59 std::string properties_raw;
60
61 ASSERT(Address::FromString("BB:BB:BB:BB:BB:AD", address_));
62 ASSERT(Address::FromString("BB:BB:BB:BB:AD:1E", le_address_));
63 name_ = {'D', 'e', 'f', 'a', 'u', 'l', 't'};
64
65 supported_codecs_ = {0}; // Only SBC is supported.
66 vendor_specific_codecs_ = {};
67
68 for (int i = 0; i < 35; i++) supported_commands_[i] = 0xff;
69 // Mark HCI_LE_Transmitter_Test[v2] and newer commands as unsupported
70 // Use SetSupportedComands() to change what's supported.
71 for (int i = 35; i < 64; i++) supported_commands_[i] = 0x00;
72
73 le_supported_features_ = 0x1f;
74 le_supported_states_ = 0x3ffffffffff;
75 le_vendor_cap_ = {};
76
77 if (file_name.size() == 0) {
78 return;
79 }
80 LOG_INFO("Reading controller properties from %s.", file_name.c_str());
81 if (!base::ReadFileToString(base::FilePath(file_name), &properties_raw)) {
82 LOG_ERROR("Error reading controller properties from file.");
83 return;
84 }
85
86 auto properties_value = base::JSONReader::Read(properties_raw);
87 if (!properties_value) {
88 LOG_ERROR(
89 "Error controller properties may consist of ill-formed JSON, no "
90 "properties read.");
91 return;
92 }
93
94 // Get the underlying base::Value object, which is of type
95 // base::Value::TYPE_DICTIONARY, and read it into member variables.
96 base::Value& properties_dictionary = *properties_value;
97 base::JSONValueConverter<DeviceProperties> converter;
98
99 if (!converter.Convert(properties_dictionary, this))
100 LOG_INFO("Error converting JSON properties into Properties object.");
101 }
102
103 // static
RegisterJSONConverter(base::JSONValueConverter<DeviceProperties> * converter)104 void DeviceProperties::RegisterJSONConverter(base::JSONValueConverter<DeviceProperties>* converter) {
105 // TODO(dennischeng): Use RegisterIntField() here?
106 #define REGISTER_UINT8_T(field_name, field) \
107 converter->RegisterCustomField<uint8_t>(field_name, &DeviceProperties::field, &ParseUint8t);
108 #define REGISTER_UINT16_T(field_name, field) \
109 converter->RegisterCustomField<uint16_t>(field_name, &DeviceProperties::field, &ParseUint16t);
110 REGISTER_UINT16_T("AclDataPacketSize", acl_data_packet_size_);
111 REGISTER_UINT8_T("ScoDataPacketSize", sco_data_packet_size_);
112 REGISTER_UINT8_T("EncryptionKeySize", encryption_key_size_);
113 REGISTER_UINT16_T("NumAclDataPackets", num_acl_data_packets_);
114 REGISTER_UINT16_T("NumScoDataPackets", num_sco_data_packets_);
115 REGISTER_UINT8_T("Version", version_);
116 REGISTER_UINT16_T("Revision", revision_);
117 REGISTER_UINT8_T("LmpPalVersion", lmp_pal_version_);
118 REGISTER_UINT16_T("ManufacturerName", manufacturer_name_);
119 REGISTER_UINT16_T("LmpPalSubversion", lmp_pal_subversion_);
120 #undef REGISTER_UINT8_T
121 #undef REGISTER_UINT16_T
122 }
123
124 } // namespace test_vendor_lib
125