Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2016 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 "utils/InterfaceSpecUtil.h"
     18 
     19 #include <fstream>
     20 #include <sstream>
     21 #include <string>
     22 
     23 #include <android-base/logging.h>
     24 #include <assert.h>
     25 #include <google/protobuf/message.h>
     26 #include <google/protobuf/text_format.h>
     27 
     28 #include "utils/StringUtil.h"
     29 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
     30 
     31 using namespace std;
     32 
     33 namespace android {
     34 namespace vts {
     35 
     36 bool ParseInterfaceSpec(const char* file_path,
     37                         ComponentSpecificationMessage* message) {
     38   ifstream in_file(file_path);
     39   stringstream str_stream;
     40   if (!in_file.is_open()) {
     41     LOG(ERROR) << "Unable to open file. " << file_path;
     42     return false;
     43   }
     44   str_stream << in_file.rdbuf();
     45   in_file.close();
     46   const string data = str_stream.str();
     47 
     48   message->Clear();
     49   if (!google::protobuf::TextFormat::MergeFromString(data, message)) {
     50     LOG(ERROR) << "Can't parse a given proto file " << file_path;
     51     return false;
     52   }
     53   return true;
     54 }
     55 
     56 string GetFunctionNamePrefix(const ComponentSpecificationMessage& message) {
     57   stringstream prefix_ss;
     58   if (message.component_class() != HAL_HIDL) {
     59     prefix_ss << VTS_INTERFACE_SPECIFICATION_FUNCTION_NAME_PREFIX
     60               << message.component_class() << "_" << message.component_type()
     61               << "_" << GetVersionString(message.component_type_version(), true)
     62               << "_";
     63   } else {
     64     string package_as_function_name(message.package());
     65     ReplaceSubString(package_as_function_name, ".", "_");
     66     prefix_ss << VTS_INTERFACE_SPECIFICATION_FUNCTION_NAME_PREFIX
     67         << message.component_class() << "_" << package_as_function_name << "_"
     68         << GetVersionString(message.component_type_version(), true) << "_"
     69         << message.component_name() << "_";
     70   }
     71   return prefix_ss.str();
     72 }
     73 
     74 #define DEFAULT_FACTOR 10000
     75 
     76 string GetVersionString(float version, bool for_macro) {
     77   std::ostringstream out;
     78   if (for_macro) {
     79     out << "V";
     80   }
     81   long version_long = version * DEFAULT_FACTOR;
     82   out << (version_long / DEFAULT_FACTOR);
     83   if (!for_macro) {
     84     out << ".";
     85   } else {
     86     out << "_";
     87   }
     88   version_long -= (version_long / DEFAULT_FACTOR) * DEFAULT_FACTOR;
     89   bool first = true;
     90   long factor = DEFAULT_FACTOR / 10;
     91   while (first || (version_long > 0 && factor > 1)) {
     92     out << (version_long / factor);
     93     version_long -= (version_long / factor) * factor;
     94     factor /= 10;
     95     first = false;
     96   }
     97   return out.str();
     98 }
     99 
    100 string GetHidlHalDriverLibName(const string& package_name,
    101                                const float version) {
    102   return package_name + "@" + GetVersionString(version) + "-vts.driver.so";
    103 }
    104 
    105 string GetInterfaceFQName(const string& package_name, const float version,
    106                           const string& interface_name) {
    107   return package_name + "@" + GetVersionString(version) + "::" + interface_name;
    108 }
    109 
    110 string GetPackageName(const string& type_name) {
    111   string str = type_name.substr(0, type_name.find('V') - strlen("::"));
    112   if (str.find("::") == 0) {
    113     str = str.substr(strlen("::"));
    114   }
    115   ReplaceSubString(str, "::", ".");
    116   return str;
    117 }
    118 
    119 float GetVersion(const string& type_name) {
    120   string str = type_name.substr(type_name.find('V') + 1);
    121   string version_str = str.substr(0, str.find("::"));
    122   string major_version = version_str.substr(0, version_str.find("_"));
    123   string minor_version = version_str.substr(version_str.find("_") + 1);
    124   // TODO(zhuoyao): handle the case when minor_version >= 10
    125   assert(std::stof(minor_version) < 10.0);
    126   return std::stof(major_version) + 0.1 * (std::stof(minor_version));
    127 }
    128 
    129 string GetComponentName(const string& type_name) {
    130   string str = type_name.substr(type_name.find('V'));
    131   return str.substr(str.find("::") + strlen("::"));
    132 }
    133 
    134 }  // namespace vts
    135 }  // namespace android
    136