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 #define LOG_TAG "device_properties"
18
19 #include "device_properties.h"
20
21 #include <memory>
22
23 #include <base/logging.h>
24 #include "base/files/file_util.h"
25 #include "base/json/json_reader.h"
26 #include "base/values.h"
27
28 #include "hci.h"
29 #include "osi/include/log.h"
30 #include "osi/include/osi.h"
31
32 using std::vector;
33
34 namespace {
35 // Functions used by JSONValueConverter to read stringified JSON.
ParseUint8t(base::StringPiece value,uint8_t * field)36 bool ParseUint8t(base::StringPiece value, uint8_t* field) {
37 *field = std::stoi(value.as_string());
38 return true;
39 }
40
ParseUint16t(base::StringPiece value,uint16_t * field)41 bool ParseUint16t(base::StringPiece value, uint16_t* field) {
42 *field = std::stoi(value.as_string());
43 return true;
44 }
45
46 } // namespace
47
48 namespace test_vendor_lib {
49
DeviceProperties(const std::string & file_name)50 DeviceProperties::DeviceProperties(const std::string& file_name)
51 : acl_data_packet_size_(1024), sco_data_packet_size_(255), num_acl_data_packets_(10), num_sco_data_packets_(10),
52 version_(static_cast<uint8_t>(hci::Version::V4_1)), revision_(0),
53 lmp_pal_version_(static_cast<uint8_t>(hci::Version::V4_1)), manufacturer_name_(0), lmp_pal_subversion_(0),
54 le_data_packet_length_(27), num_le_data_packets_(15), le_white_list_size_(15) {
55 std::string properties_raw;
56
57 CHECK(Address::FromString("BB:BB:BB:BB:BB:AD", address_));
58 CHECK(Address::FromString("BB:BB:BB:BB:AD:1E", le_address_));
59 name_ = {'D', 'e', 'f', 'a', 'u', 'l', 't'};
60
61 supported_codecs_ = {0}; // Only SBC is supported.
62 vendor_specific_codecs_ = {};
63
64 for (int i = 0; i < 64; i++) supported_commands_.push_back(0xff);
65
66 le_supported_features_ = 0x1f;
67 le_supported_states_ = 0x3ffffffffff;
68 le_vendor_cap_ = {};
69
70 if (file_name.size() == 0) {
71 return;
72 }
73 LOG_INFO(LOG_TAG, "Reading controller properties from %s.", file_name.c_str());
74 if (!base::ReadFileToString(base::FilePath(file_name), &properties_raw)) {
75 LOG_ERROR(LOG_TAG, "Error reading controller properties from file.");
76 return;
77 }
78
79 std::unique_ptr<base::Value> properties_value_ptr = base::JSONReader::Read(properties_raw);
80 if (properties_value_ptr.get() == nullptr)
81 LOG_INFO(LOG_TAG, "Error controller properties may consist of ill-formed JSON.");
82
83 // Get the underlying base::Value object, which is of type
84 // base::Value::TYPE_DICTIONARY, and read it into member variables.
85 base::Value& properties_dictionary = *(properties_value_ptr.get());
86 base::JSONValueConverter<DeviceProperties> converter;
87
88 if (!converter.Convert(properties_dictionary, this))
89 LOG_INFO(LOG_TAG, "Error converting JSON properties into Properties object.");
90 }
91
92 // static
RegisterJSONConverter(base::JSONValueConverter<DeviceProperties> * converter)93 void DeviceProperties::RegisterJSONConverter(base::JSONValueConverter<DeviceProperties>* converter) {
94 // TODO(dennischeng): Use RegisterIntField() here?
95 #define REGISTER_UINT8_T(field_name, field) \
96 converter->RegisterCustomField<uint8_t>(field_name, &DeviceProperties::field, &ParseUint8t);
97 #define REGISTER_UINT16_T(field_name, field) \
98 converter->RegisterCustomField<uint16_t>(field_name, &DeviceProperties::field, &ParseUint16t);
99 REGISTER_UINT16_T("AclDataPacketSize", acl_data_packet_size_);
100 REGISTER_UINT8_T("ScoDataPacketSize", sco_data_packet_size_);
101 REGISTER_UINT16_T("NumAclDataPackets", num_acl_data_packets_);
102 REGISTER_UINT16_T("NumScoDataPackets", num_sco_data_packets_);
103 REGISTER_UINT8_T("Version", version_);
104 REGISTER_UINT16_T("Revision", revision_);
105 REGISTER_UINT8_T("LmpPalVersion", lmp_pal_version_);
106 REGISTER_UINT16_T("ManufacturerName", manufacturer_name_);
107 REGISTER_UINT16_T("LmpPalSubversion", lmp_pal_subversion_);
108 #undef REGISTER_UINT8_T
109 #undef REGISTER_UINT16_T
110 }
111
112 } // namespace test_vendor_lib
113