blob: fc6142b0b25a445262b9ae178a9e9515d0a75d50 [file] [log] [blame]
Keun Soo Yimb8edda32016-04-27 17:31:00 -07001/*
2 * Copyright 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 "VtsCompilerUtils.h"
18
19#include <sys/types.h>
20#include <unistd.h>
21#include <sys/stat.h>
22#include <limits.h>
23#include <stdlib.h>
24
25#include <cstdint>
26#include <iostream>
27#include <sstream>
28#include <fstream>
29
30#include "specification_parser/InterfaceSpecificationParser.h"
31
32#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
33
34
35using namespace std;
36
37
38namespace android {
39namespace vts {
40
41string ComponentClassToString(int component_class) {
42 switch(component_class) {
43 case UNKNOWN_CLASS: return "unknown_class";
44 case HAL: return "hal";
45 case SHAREDLIB: return "sharedlib";
46 case HAL_HIDL: return "hal_hidl";
47 case HAL_SUBMODULE: return "hal_submodule";
48 }
49 cerr << "invalid component_class " << component_class << endl;
50 exit(-1);
51}
52
53
54string ComponentTypeToString(int component_type) {
55 switch(component_type) {
56 case UNKNOWN_TYPE: return "unknown_type";
57 case AUDIO: return "audio";
58 case CAMERA: return "camera";
59 case GPS: return "gps";
60 case LIGHT: return "light";
61 }
62 cerr << "invalid component_type " << component_type << endl;
63 exit(-1);
64}
65
66
67string GetCppVariableType(const std::string primitive_type_string) {
68 const char* primitive_type = primitive_type_string.c_str();
69 if (!strcmp(primitive_type, "void")
70 || !strcmp(primitive_type, "bool")
71 || !strcmp(primitive_type, "int32_t")
72 || !strcmp(primitive_type, "uint32_t")
73 || !strcmp(primitive_type, "int8_t")
74 || !strcmp(primitive_type, "uint8_t")
75 || !strcmp(primitive_type, "int64_t")
76 || !strcmp(primitive_type, "uint64_t")
77 || !strcmp(primitive_type, "int16_t")
78 || !strcmp(primitive_type, "uint16_t")
79 || !strcmp(primitive_type, "float")
80 || !strcmp(primitive_type, "double")) {
81 return primitive_type_string;
82 } else if(!strcmp(primitive_type, "ufloat")) {
83 return "unsigned float";
84 } else if(!strcmp(primitive_type, "udouble")) {
85 return "unsigned double";
86 } else if (!strcmp(primitive_type, "string")) {
87 return "std::string";
88 } else if (!strcmp(primitive_type, "pointer")) {
89 return "void*";
90 } else if (!strcmp(primitive_type, "char_pointer")) {
91 return "char*";
92 } else if (!strcmp(primitive_type, "uchar_pointer")) {
93 return "unsigned char*";
94 } else if (!strcmp(primitive_type, "void_pointer")) {
95 return "void*";
96 } else if (!strcmp(primitive_type, "function_pointer")) {
97 return "void*";
98 }
99
100 cerr << __FILE__ << ":" << __LINE__ << " "
101 << "unknown primitive_type " << primitive_type << endl;
102 exit(-1);
103}
104
105
106string GetCppVariableType(ArgumentSpecificationMessage arg) {
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700107 if (arg.aggregate_type().size() == 1) {
108 return arg.aggregate_type(0);
109 } else if (arg.primitive_type().size() == 1) {
110 return GetCppVariableType(arg.primitive_type(0));
111 } else if (arg.primitive_type().size() > 1
112 || arg.aggregate_type().size() > 1) {
113 cerr << __FUNCTION__
114 << ": aggregate type with multiple attributes; not supported" << endl;
115 exit(-1);
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700116 }
117 cerr << __FUNCTION__ << ": neither instance nor data type is set" << endl;
118 exit(-1);
119}
120
121
Keun Soo Yim34067de2016-05-17 09:46:37 -0700122string GetCppInstanceType(ArgumentSpecificationMessage arg, string msg) {
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700123 if (arg.aggregate_type().size() == 1) {
124 if (!strcmp(arg.aggregate_type(0).c_str(), "struct light_state_t*")) {
Keun Soo Yim34067de2016-05-17 09:46:37 -0700125 if (msg.length() == 0) {
126 return "GenerateLightState()";
127 } else {
128 return "GenerateLightStateUsingMessage(" + msg + ")";
129 }
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700130 } else if (!strcmp(arg.aggregate_type(0).c_str(), "GpsCallbacks*")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700131 return "GenerateGpsCallbacks()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700132 } else if (!strcmp(arg.aggregate_type(0).c_str(), "GpsUtcTime")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700133 return "GenerateGpsUtcTime()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700134 } else if (!strcmp(arg.aggregate_type(0).c_str(), "vts_gps_latitude")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700135 return "GenerateLatitude()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700136 } else if (!strcmp(arg.aggregate_type(0).c_str(), "vts_gps_longitude")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700137 return "GenerateLongitude()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700138 } else if (!strcmp(arg.aggregate_type(0).c_str(), "vts_gps_accuracy")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700139 return "GenerateGpsAccuracy()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700140 } else if (!strcmp(arg.aggregate_type(0).c_str(), "vts_gps_flags_uint16")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700141 return "GenerateGpsFlagsUint16()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700142 } else if (!strcmp(arg.aggregate_type(0).c_str(), "GpsPositionMode")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700143 return "GenerateGpsPositionMode()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700144 } else if (!strcmp(arg.aggregate_type(0).c_str(), "GpsPositionRecurrence")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700145 return "GenerateGpsPositionRecurrence()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700146 } else if (!strcmp(arg.aggregate_type(0).c_str(), "wifi_handle*")) {
Keun Soo Yimd4559882016-05-13 20:03:12 -0700147 return "(wifi_handle*) malloc(sizeof(wifi_handle))";
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700148 } else {
149 cerr << __FILE__ << ":" << __LINE__ << " "
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700150 << "unknown instance type " << arg.aggregate_type(0) << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700151 }
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700152 } else if (arg.aggregate_type().size() > 1) {
153 cerr << __FUNCTION__
154 << ": aggregate type with multiple attributes; not supported" << endl;
155 exit(-1);
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700156 }
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700157 if (arg.primitive_type().size() > 1) {
158 cerr << __FUNCTION__
159 << ": a structure with multiple attributes; not supported" << endl;
160 exit(-1);
161 } else if (arg.primitive_type().size() == 1) {
162 if (!strcmp(arg.primitive_type(0).c_str(), "uint32_t")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700163 return "RandomUint32()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700164 } else if (!strcmp(arg.primitive_type(0).c_str(), "int32_t")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700165 return "RandomInt32()";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700166 } else if (!strcmp(arg.primitive_type(0).c_str(), "char_pointer")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700167 return "RandomCharPointer()";
168 }
169 cerr << __FILE__ << ":" << __LINE__ << " "
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700170 << "unknown data type " << arg.primitive_type(0) << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700171 exit(-1);
172 }
173 cerr << __FUNCTION__ << ": neither instance nor data type is set" << endl;
174 exit(-1);
175}
176
177
178int vts_fs_mkdirs(const char* file_path, mode_t mode) {
179 char* p;
180
181 for (p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) {
182 *p = '\0';
183 if (mkdir(file_path, mode) == -1) {
184 if (errno != EEXIST) {
185 *p = '/';
186 return -1;
187 }
188 }
189 *p='/';
190 }
191 return 0;
192}
193
194} // namespace vts
195} // namespace android