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 
ParseInterfaceSpec(const char * file_path,ComponentSpecificationMessage * message)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 
GetFunctionNamePrefix(const ComponentSpecificationMessage & message)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               << "_"
62               << GetVersionString(message.component_type_version_major(),
63                                   message.component_type_version_minor(),
64                                   true)  // set flag to true, use macro version,
65                                          // e.g. V1_10.
66               << "_";
67   } else {
68     string package_as_function_name(message.package());
69     ReplaceSubString(package_as_function_name, ".", "_");
70     prefix_ss << VTS_INTERFACE_SPECIFICATION_FUNCTION_NAME_PREFIX
71               << message.component_class() << "_" << package_as_function_name
72               << "_"
73               << GetVersionString(message.component_type_version_major(),
74                                   message.component_type_version_minor(),
75                                   true)  // set flag to true, use macro version,
76                                          // e.g. V1_10.
77               << "_" << message.component_name() << "_";
78   }
79   return prefix_ss.str();
80 }
81 
82 #define DEFAULT_FACTOR 10000
83 
84 // deprecated
GetVersionString(float version,bool for_macro)85 string GetVersionString(float version, bool for_macro) {
86   std::ostringstream out;
87   if (for_macro) {
88     out << "V";
89   }
90   long version_long = version * DEFAULT_FACTOR;
91   out << (version_long / DEFAULT_FACTOR);
92   if (!for_macro) {
93     out << ".";
94   } else {
95     out << "_";
96   }
97   version_long -= (version_long / DEFAULT_FACTOR) * DEFAULT_FACTOR;
98   bool first = true;
99   long factor = DEFAULT_FACTOR / 10;
100   while (first || (version_long > 0 && factor > 1)) {
101     out << (version_long / factor);
102     version_long -= (version_long / factor) * factor;
103     factor /= 10;
104     first = false;
105   }
106   return out.str();
107 }
108 
GetVersionString(int version_major,int version_minor,bool for_macro)109 string GetVersionString(int version_major, int version_minor, bool for_macro) {
110   std::ostringstream out;
111 
112   if (for_macro) out << "V";
113 
114   out << version_major;
115   out << (for_macro ? "_" : ".");
116   out << version_minor;
117   return out.str();
118 }
119 
GetHidlHalDriverLibName(const string & package_name,const int version_major,const int version_minor)120 string GetHidlHalDriverLibName(const string& package_name,
121                                const int version_major,
122                                const int version_minor) {
123   return package_name + "@" + GetVersionString(version_major, version_minor) +
124          "-vts.driver.so";
125 }
126 
GetInterfaceFQName(const string & package_name,const int version_major,const int version_minor,const string & interface_name)127 string GetInterfaceFQName(const string& package_name, const int version_major,
128                           const int version_minor,
129                           const string& interface_name) {
130   return package_name + "@" + GetVersionString(version_major, version_minor) +
131          "::" + interface_name;
132 }
133 
GetPackageName(const string & type_name)134 string GetPackageName(const string& type_name) {
135   string str = type_name.substr(0, type_name.find('V') - strlen("::"));
136   if (str.find("::") == 0) {
137     str = str.substr(strlen("::"));
138   }
139   ReplaceSubString(str, "::", ".");
140   return str;
141 }
142 
GetVersion(const string & type_name)143 string GetVersion(const string& type_name) {
144   string str = type_name.substr(type_name.find('V') + 1);
145   string version_str = str.substr(0, str.find("::"));
146   return version_str;
147 }
148 
GetVersionMajor(const string & version,bool for_macro)149 int GetVersionMajor(const string& version, bool for_macro) {
150   if (for_macro) return std::stoi(version.substr(0, version.find("_")));
151   return std::stoi(version.substr(0, version.find(".")));
152 }
153 
GetVersionMinor(const string & version,bool for_macro)154 int GetVersionMinor(const string& version, bool for_macro) {
155   if (for_macro) return std::stoi(version.substr(version.find("_") + 1));
156   return std::stoi(version.substr(version.find(".") + 1));
157 }
158 
GetComponentName(const string & type_name)159 string GetComponentName(const string& type_name) {
160   string str = type_name.substr(type_name.find('V'));
161   return str.substr(str.find("::") + strlen("::"));
162 }
163 
164 }  // namespace vts
165 }  // namespace android
166