blob: 30598894671bff8af706c91743adf49e1294e84e [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 "code_gen/HalCodeGen.h"
18
19#include <fstream>
20#include <iostream>
21#include <sstream>
22#include <string>
23
24#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
25
26#include "VtsCompilerUtils.h"
27
28using namespace std;
29using namespace android;
30
31namespace android {
32namespace vts {
33
34const char* const HalCodeGen::kInstanceVariableName = "device_";
35
36
37void HalCodeGen::GenerateCppBodyFuzzFunction(
38 std::stringstream& cpp_ss,
39 const InterfaceSpecificationMessage& message,
40 const string& fuzzer_extended_class_name) {
41 cpp_ss << "bool " << fuzzer_extended_class_name << "::Fuzz(" << endl;
42 cpp_ss << " const FunctionSpecificationMessage& func_msg," << endl;
43 cpp_ss << " void** result) {" << endl;
44 cpp_ss << " const char* func_name = func_msg.name().c_str();" << endl;
45 cpp_ss << " cout << \"Function: \" << func_name << endl;" << endl;
46
47 for (auto const& api : message.api()) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -070048 cpp_ss << " if (!strcmp(func_name, \"" << api.name() << "\")) {" << endl;
49
50 // args - definition;
51 int arg_count = 0;
52 for (auto const& arg : api.arg()) {
53 cpp_ss << " " << GetCppVariableType(arg) << " ";
54 cpp_ss << "arg" << arg_count << " = ";
55 if (arg_count == 0
Keun Soo Yima8cf69a2016-05-18 18:11:37 -070056 && arg.aggregate_type().size() == 1
57 && !strncmp(arg.aggregate_type(0).c_str(),
Keun Soo Yimb8edda32016-04-27 17:31:00 -070058 message.original_data_structure_name().c_str(),
59 message.original_data_structure_name().length())) {
60 cpp_ss << "reinterpret_cast<" << GetCppVariableType(arg) << ">("
61 << kInstanceVariableName << ")";
62 } else {
Keun Soo Yim34067de2016-05-17 09:46:37 -070063 std::stringstream msg_ss;
64 msg_ss << "func_msg.arg(" << arg_count << ")";
65 string msg = msg_ss.str();
66
67 cpp_ss << "(" << msg << ".primitive_value_size() > 0 ";
68 cpp_ss << "|| " << msg << ".aggregate_value_size() > 0)? ";
69 cpp_ss << GetCppInstanceType(arg, msg);
70 cpp_ss << " : " << GetCppInstanceType(arg);
Keun Soo Yimb8edda32016-04-27 17:31:00 -070071 }
72 cpp_ss << ";" << endl;
73 cpp_ss << " cout << \"arg" << arg_count << " = \" << arg" << arg_count
74 << " << endl;" << endl;
75 arg_count++;
76 }
77
78 // actual function call
Keun Soo Yimffb07ba2016-05-18 16:22:45 -070079 GenerateCodeToStartMeasurement(cpp_ss);
Keun Soo Yim6d944952016-05-31 16:30:56 -070080 cpp_ss << " cout << \"hit2.\" << device_ << endl;" << endl;
81
82 cpp_ss << " " << message.original_data_structure_name()
83 << "* local_device = ";
84 cpp_ss << "reinterpret_cast<" << message.original_data_structure_name()
85 << "*>(" << kInstanceVariableName << ");" << endl;
86
87 cpp_ss << " if (local_device == NULL) {" << endl;
88 cpp_ss << " cout << \"use hmi\" << endl;" << endl;
89 cpp_ss << " local_device = reinterpret_cast<" << message.original_data_structure_name()
90 << "*>(hmi_);" << endl;
91 cpp_ss << " }" << endl;
92 cpp_ss << " if (local_device == NULL) {" << endl;
93 cpp_ss << " cerr << \"both device_ and hmi_ are NULL.\" << endl;" << endl;
94 cpp_ss << " return false;" << endl;
95 cpp_ss << " }" << endl;
96 cpp_ss << " if (reinterpret_cast<" << message.original_data_structure_name()
97 << "*>(local_device)->" << api.name() << " == NULL";
98 cpp_ss << ") {" << endl;
99 cpp_ss << " cerr << \"api not set.\" << endl;" << endl;
100 // todo: consider throwing an exception at least a way to tell more
101 // specifically to the caller.
102 cpp_ss << " return false;" << endl;
103 cpp_ss << " }" << endl;
104
105 cpp_ss << " cout << \"ok. let's call.\" << endl;" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700106 cpp_ss << " ";
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700107 if (api.return_type().primitive_type().size() == 1
108 && !strcmp(api.return_type().primitive_type(0).c_str(), "void")) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700109 cpp_ss << "*result = NULL;" << endl;
110 } else {
111 cpp_ss << "*result = const_cast<void*>(reinterpret_cast<const void*>(";
112 }
Keun Soo Yim6d944952016-05-31 16:30:56 -0700113 cpp_ss << "local_device->" << api.name() << "(";
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700114 if (arg_count > 0) cpp_ss << endl;
115
116 for (int index = 0; index < arg_count; index++) {
117 cpp_ss << " arg" << index;
118 if (index != (arg_count - 1)) {
119 cpp_ss << "," << endl;
120 }
121 }
Keun Soo Yima8cf69a2016-05-18 18:11:37 -0700122 if (api.return_type().primitive_type().size() == 0
123 || (api.return_type().primitive_type().size() == 1
124 && strcmp(api.return_type().primitive_type(0).c_str(), "void"))) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700125 cpp_ss << "))";
126 }
127 cpp_ss << ");" << endl;
Keun Soo Yimffb07ba2016-05-18 16:22:45 -0700128 GenerateCodeToStopMeasurement(cpp_ss);
Keun Soo Yim6d944952016-05-31 16:30:56 -0700129 cpp_ss << " cout << \"called\" << endl;" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700130 cpp_ss << " return true;" << endl;
131 cpp_ss << " }" << endl;
132 }
133 // TODO: if there were pointers, free them.
134 cpp_ss << " return false;" << endl;
135 cpp_ss << "}" << endl;
136}
137
138
139void HalCodeGen::GenerateHeaderGlobalFunctionDeclarations(
140 std::stringstream& h_ss,
141 const string& function_prototype) {
142 h_ss << "extern \"C\" {" << endl;
143 h_ss << "extern " << function_prototype << ";" << endl;
144 h_ss << "}" << endl;
145}
146
147
148void HalCodeGen::GenerateCppBodyGlobalFunctions(
149 std::stringstream& cpp_ss,
150 const string& function_prototype,
151 const string& fuzzer_extended_class_name) {
152 cpp_ss << "extern \"C\" {" << endl;
153 cpp_ss << function_prototype << " {" << endl;
154 cpp_ss << " return (android::vts::FuzzerBase*) "
155 << "new android::vts::" << fuzzer_extended_class_name << "();" << endl;
156 cpp_ss << "}" << endl << endl;
157 cpp_ss << "}" << endl;
158}
159
160} // namespace vts
161} // namespace android