Home | History | Annotate | Download | only in src
      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 "osi/include/log.h"
     29 #include "osi/include/osi.h"
     30 #include "stack/include/hcidefs.h"
     31 
     32 using std::vector;
     33 
     34 namespace {
     35 // Functions used by JSONValueConverter to read stringified JSON.
     36 bool ParseUint8t(const base::StringPiece& value, uint8_t* field) {
     37   *field = std::stoi(value.as_string());
     38   return true;
     39 }
     40 
     41 bool ParseUint16t(const 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 
     50 DeviceProperties::DeviceProperties(const std::string& file_name)
     51     : acl_data_packet_size_(1024),
     52       sco_data_packet_size_(255),
     53       num_acl_data_packets_(10),
     54       num_sco_data_packets_(10),
     55       version_(HCI_PROTO_VERSION_4_1),
     56       revision_(0),
     57       lmp_pal_version_(7), /* 4.1 */
     58       manufacturer_name_(0),
     59       lmp_pal_subversion_(0),
     60       le_data_packet_length_(27),
     61       num_le_data_packets_(15),
     62       le_white_list_size_(15) {
     63   std::string properties_raw;
     64 
     65   local_extended_features_ = {0xffffffffffffffff, 0x7};
     66 
     67   CHECK(address_.FromString("01:02:03:04:05:06"));
     68   local_name_ = "DefaultName";
     69 
     70   supported_codecs_ = {1};
     71   vendor_specific_codecs_ = {};
     72 
     73   for (int i = 0; i < 64; i++) local_supported_commands_.push_back(0xff);
     74 
     75   le_supported_features_ = 0x1f;
     76   le_supported_states_ = 0x3ffffffffff;
     77   le_vendor_cap_ = {};
     78 
     79   LOG_INFO(LOG_TAG, "Reading controller properties from %s.",
     80            file_name.c_str());
     81   if (!base::ReadFileToString(base::FilePath(file_name), &properties_raw)) {
     82     LOG_ERROR(LOG_TAG, "Error reading controller properties from file.");
     83     return;
     84   }
     85 
     86   std::unique_ptr<base::Value> properties_value_ptr =
     87       base::JSONReader::Read(properties_raw);
     88   if (properties_value_ptr.get() == nullptr)
     89     LOG_INFO(LOG_TAG,
     90              "Error controller properties may consist of ill-formed JSON.");
     91 
     92   // Get the underlying base::Value object, which is of type
     93   // base::Value::TYPE_DICTIONARY, and read it into member variables.
     94   base::Value& properties_dictionary = *(properties_value_ptr.get());
     95   base::JSONValueConverter<DeviceProperties> converter;
     96 
     97   if (!converter.Convert(properties_dictionary, this))
     98     LOG_INFO(LOG_TAG,
     99              "Error converting JSON properties into Properties object.");
    100 }
    101 
    102 // static
    103 void DeviceProperties::RegisterJSONConverter(
    104     base::JSONValueConverter<DeviceProperties>* converter) {
    105 // TODO(dennischeng): Use RegisterIntField() here?
    106 #define REGISTER_UINT8_T(field_name, field) \
    107   converter->RegisterCustomField<uint8_t>(  \
    108       field_name, &DeviceProperties::field, &ParseUint8t);
    109 #define REGISTER_UINT16_T(field_name, field) \
    110   converter->RegisterCustomField<uint16_t>(  \
    111       field_name, &DeviceProperties::field, &ParseUint16t);
    112   REGISTER_UINT16_T("AclDataPacketSize", acl_data_packet_size_);
    113   REGISTER_UINT8_T("ScoDataPacketSize", sco_data_packet_size_);
    114   REGISTER_UINT16_T("NumAclDataPackets", num_acl_data_packets_);
    115   REGISTER_UINT16_T("NumScoDataPackets", num_sco_data_packets_);
    116   REGISTER_UINT8_T("Version", version_);
    117   REGISTER_UINT16_T("Revision", revision_);
    118   REGISTER_UINT8_T("LmpPalVersion", lmp_pal_version_);
    119   REGISTER_UINT16_T("ManufacturerName", manufacturer_name_);
    120   REGISTER_UINT16_T("LmpPalSubversion", lmp_pal_subversion_);
    121 #undef REGISTER_UINT8_T
    122 #undef REGISTER_UINT16_T
    123 }
    124 
    125 }  // namespace test_vendor_lib
    126