blob: 02cc4e57adc15aa89af7ae3354b11e8e8daae3b4 [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) {
107 if (arg.has_aggregate_type()) {
108 return arg.aggregate_type();
109 } else if (arg.has_primitive_type()) {
110 return GetCppVariableType(arg.primitive_type());
111 }
112 cerr << __FUNCTION__ << ": neither instance nor data type is set" << endl;
113 exit(-1);
114}
115
116
Keun Soo Yim34067de2016-05-17 09:46:37 -0700117string GetCppInstanceType(ArgumentSpecificationMessage arg, string msg) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700118 if (arg.has_aggregate_type()) {
119 if (!strcmp(arg.aggregate_type().c_str(), "struct light_state_t*")) {
Keun Soo Yim34067de2016-05-17 09:46:37 -0700120 if (msg.length() == 0) {
121 return "GenerateLightState()";
122 } else {
123 return "GenerateLightStateUsingMessage(" + msg + ")";
124 }
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700125 } else if (!strcmp(arg.aggregate_type().c_str(), "GpsCallbacks*")) {
126 return "GenerateGpsCallbacks()";
127 } else if (!strcmp(arg.aggregate_type().c_str(), "GpsUtcTime")) {
128 return "GenerateGpsUtcTime()";
129 } else if (!strcmp(arg.aggregate_type().c_str(), "vts_gps_latitude")) {
130 return "GenerateLatitude()";
131 } else if (!strcmp(arg.aggregate_type().c_str(), "vts_gps_longitude")) {
132 return "GenerateLongitude()";
133 } else if (!strcmp(arg.aggregate_type().c_str(), "vts_gps_accuracy")) {
134 return "GenerateGpsAccuracy()";
135 } else if (!strcmp(arg.aggregate_type().c_str(), "vts_gps_flags_uint16")) {
136 return "GenerateGpsFlagsUint16()";
137 } else if (!strcmp(arg.aggregate_type().c_str(), "GpsPositionMode")) {
138 return "GenerateGpsPositionMode()";
139 } else if (!strcmp(arg.aggregate_type().c_str(), "GpsPositionRecurrence")) {
140 return "GenerateGpsPositionRecurrence()";
Keun Soo Yimd4559882016-05-13 20:03:12 -0700141 } else if (!strcmp(arg.aggregate_type().c_str(), "wifi_handle*")) {
142 return "(wifi_handle*) malloc(sizeof(wifi_handle))";
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700143 } else {
144 cerr << __FILE__ << ":" << __LINE__ << " "
145 << "unknown instance type " << arg.aggregate_type() << endl;
146 }
147 }
148 if (arg.has_primitive_type()) {
149 if (!strcmp(arg.primitive_type().c_str(), "uint32_t")) {
150 return "RandomUint32()";
151 } else if (!strcmp(arg.primitive_type().c_str(), "int32_t")) {
152 return "RandomInt32()";
153 } else if (!strcmp(arg.primitive_type().c_str(), "char_pointer")) {
154 return "RandomCharPointer()";
155 }
156 cerr << __FILE__ << ":" << __LINE__ << " "
157 << "unknown data type " << arg.primitive_type() << endl;
158 exit(-1);
159 }
160 cerr << __FUNCTION__ << ": neither instance nor data type is set" << endl;
161 exit(-1);
162}
163
164
165int vts_fs_mkdirs(const char* file_path, mode_t mode) {
166 char* p;
167
168 for (p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) {
169 *p = '\0';
170 if (mkdir(file_path, mode) == -1) {
171 if (errno != EEXIST) {
172 *p = '/';
173 return -1;
174 }
175 }
176 *p='/';
177 }
178 return 0;
179}
180
181} // namespace vts
182} // namespace android