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