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 <iostream>
21 #include <sstream>
22 #include <string>
23
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 cerr << "Unable to open file. " << file_path << endl;
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 cerr << __FUNCTION__ << ": Can't parse a given proto file " << file_path
51 << "." << endl;
52 cerr << data << endl;
53 return false;
54 }
55 return true;
56 }
57
GetFunctionNamePrefix(const ComponentSpecificationMessage & message)58 string GetFunctionNamePrefix(const ComponentSpecificationMessage& message) {
59 stringstream prefix_ss;
60 if (message.component_class() != HAL_HIDL) {
61 prefix_ss << VTS_INTERFACE_SPECIFICATION_FUNCTION_NAME_PREFIX
62 << message.component_class() << "_" << message.component_type()
63 << "_" << GetVersionString(message.component_type_version(), true)
64 << "_";
65 } else {
66 string package_as_function_name(message.package());
67 ReplaceSubString(package_as_function_name, ".", "_");
68 prefix_ss << VTS_INTERFACE_SPECIFICATION_FUNCTION_NAME_PREFIX
69 << message.component_class() << "_" << package_as_function_name << "_"
70 << GetVersionString(message.component_type_version(), true) << "_"
71 << message.component_name() << "_";
72 }
73 return prefix_ss.str();
74 }
75
76 #define DEFAULT_FACTOR 10000
77
GetVersionString(float version,bool for_macro)78 string GetVersionString(float version, bool for_macro) {
79 std::ostringstream out;
80 if (for_macro) {
81 out << "V";
82 }
83 long version_long = version * DEFAULT_FACTOR;
84 out << (version_long / DEFAULT_FACTOR);
85 if (!for_macro) {
86 out << ".";
87 } else {
88 out << "_";
89 }
90 version_long -= (version_long / DEFAULT_FACTOR) * DEFAULT_FACTOR;
91 bool first = true;
92 long factor = DEFAULT_FACTOR / 10;
93 while (first || (version_long > 0 && factor > 1)) {
94 out << (version_long / factor);
95 version_long -= (version_long / factor) * factor;
96 factor /= 10;
97 first = false;
98 }
99 return out.str();
100 }
101
GetHidlHalDriverLibName(const string & package_name,const float version)102 string GetHidlHalDriverLibName(const string& package_name,
103 const float version) {
104 return package_name + "@" + GetVersionString(version) + "-vts.driver.so";
105 }
106
GetPackageName(const string & type_name)107 string GetPackageName(const string& type_name) {
108 string str = type_name.substr(0, type_name.find('V') - strlen("::"));
109 if (str.find("::") == 0) {
110 str = str.substr(strlen("::"));
111 }
112 ReplaceSubString(str, "::", ".");
113 return str;
114 }
115
GetVersion(const string & type_name)116 float GetVersion(const string& type_name) {
117 string str = type_name.substr(type_name.find('V') + 1);
118 string version_str = str.substr(0, str.find("::"));
119 string major_version = version_str.substr(0, version_str.find("_"));
120 string minor_version = version_str.substr(version_str.find("_") + 1);
121 // TODO(zhuoyao): handle the case when minor_version >= 10
122 assert(std::stof(minor_version) < 10.0);
123 return std::stof(major_version) + 0.1 * (std::stof(minor_version));
124 }
125
GetComponentName(const string & type_name)126 string GetComponentName(const string& type_name) {
127 string str = type_name.substr(type_name.find('V'));
128 return str.substr(str.find("::") + strlen("::"));
129 }
130
131 } // namespace vts
132 } // namespace android
133