Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 35 | using namespace std; |
| 36 | |
| 37 | |
| 38 | namespace android { |
| 39 | namespace vts { |
| 40 | |
| 41 | string 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 | |
| 54 | string 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 | |
| 67 | string 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 | |
| 106 | string 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 | |
| 117 | string GetCppInstanceType(ArgumentSpecificationMessage arg) { |
| 118 | if (arg.has_aggregate_type()) { |
| 119 | if (!strcmp(arg.aggregate_type().c_str(), "struct light_state_t*")) { |
| 120 | return "GenerateLightState()"; |
| 121 | } else if (!strcmp(arg.aggregate_type().c_str(), "GpsCallbacks*")) { |
| 122 | return "GenerateGpsCallbacks()"; |
| 123 | } else if (!strcmp(arg.aggregate_type().c_str(), "GpsUtcTime")) { |
| 124 | return "GenerateGpsUtcTime()"; |
| 125 | } else if (!strcmp(arg.aggregate_type().c_str(), "vts_gps_latitude")) { |
| 126 | return "GenerateLatitude()"; |
| 127 | } else if (!strcmp(arg.aggregate_type().c_str(), "vts_gps_longitude")) { |
| 128 | return "GenerateLongitude()"; |
| 129 | } else if (!strcmp(arg.aggregate_type().c_str(), "vts_gps_accuracy")) { |
| 130 | return "GenerateGpsAccuracy()"; |
| 131 | } else if (!strcmp(arg.aggregate_type().c_str(), "vts_gps_flags_uint16")) { |
| 132 | return "GenerateGpsFlagsUint16()"; |
| 133 | } else if (!strcmp(arg.aggregate_type().c_str(), "GpsPositionMode")) { |
| 134 | return "GenerateGpsPositionMode()"; |
| 135 | } else if (!strcmp(arg.aggregate_type().c_str(), "GpsPositionRecurrence")) { |
| 136 | return "GenerateGpsPositionRecurrence()"; |
Keun Soo Yim | d455988 | 2016-05-13 20:03:12 -0700 | [diff] [blame^] | 137 | } else if (!strcmp(arg.aggregate_type().c_str(), "wifi_handle*")) { |
| 138 | return "(wifi_handle*) malloc(sizeof(wifi_handle))"; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 139 | } else { |
| 140 | cerr << __FILE__ << ":" << __LINE__ << " " |
| 141 | << "unknown instance type " << arg.aggregate_type() << endl; |
| 142 | } |
| 143 | } |
| 144 | if (arg.has_primitive_type()) { |
| 145 | if (!strcmp(arg.primitive_type().c_str(), "uint32_t")) { |
| 146 | return "RandomUint32()"; |
| 147 | } else if (!strcmp(arg.primitive_type().c_str(), "int32_t")) { |
| 148 | return "RandomInt32()"; |
| 149 | } else if (!strcmp(arg.primitive_type().c_str(), "char_pointer")) { |
| 150 | return "RandomCharPointer()"; |
| 151 | } |
| 152 | cerr << __FILE__ << ":" << __LINE__ << " " |
| 153 | << "unknown data type " << arg.primitive_type() << endl; |
| 154 | exit(-1); |
| 155 | } |
| 156 | cerr << __FUNCTION__ << ": neither instance nor data type is set" << endl; |
| 157 | exit(-1); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | int vts_fs_mkdirs(const char* file_path, mode_t mode) { |
| 162 | char* p; |
| 163 | |
| 164 | for (p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) { |
| 165 | *p = '\0'; |
| 166 | if (mkdir(file_path, mode) == -1) { |
| 167 | if (errno != EEXIST) { |
| 168 | *p = '/'; |
| 169 | return -1; |
| 170 | } |
| 171 | } |
| 172 | *p='/'; |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | } // namespace vts |
| 178 | } // namespace android |