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 #ifndef VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_DRIVER_HALHIDLCODEGEN_H_
18 #define VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_DRIVER_HALHIDLCODEGEN_H_
19 
20 #include <string>
21 
22 #include "code_gen/driver/DriverCodeGenBase.h"
23 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
24 
25 using namespace std;
26 
27 namespace android {
28 namespace vts {
29 
30 class HalHidlCodeGen : public DriverCodeGenBase {
31  public:
HalHidlCodeGen(const char * input_vts_file_path)32   explicit HalHidlCodeGen(const char* input_vts_file_path)
33       : DriverCodeGenBase(input_vts_file_path) {}
34 
35  protected:
36   void GenerateClassHeader(Formatter& out,
37       const ComponentSpecificationMessage& message,
38       const string& fuzzer_extended_class_name) override;
39 
40   void GenerateClassImpl(Formatter& out,
41       const ComponentSpecificationMessage& message,
42       const string& fuzzer_extended_class_name) override;
43 
44   void GenerateCppBodyFuzzFunction(Formatter& out,
45       const ComponentSpecificationMessage& message,
46       const string& fuzzer_extended_class_name) override;
47 
48   virtual void GenerateDriverFunctionImpl(Formatter& out,
49       const ComponentSpecificationMessage& message,
50       const string& fuzzer_extended_class_name) override;
51 
52   void GenerateVerificationFunctionImpl(Formatter& out,
53       const ComponentSpecificationMessage& message,
54       const string& fuzzer_extended_class_name) override;
55 
56   void GenerateCppBodyGetAttributeFunction(Formatter& out,
57       const ComponentSpecificationMessage& message,
58       const string& fuzzer_extended_class_name) override;
59 
60   void GenerateHeaderInterfaceImpl(
61       Formatter& out, const ComponentSpecificationMessage& message) override;
62 
63   void GenerateCppBodyInterfaceImpl(
64       Formatter& out, const ComponentSpecificationMessage& message,
65       const string& fuzzer_extended_class_name) override;
66 
67   void GenerateClassConstructionFunction(Formatter& out,
68       const ComponentSpecificationMessage& message,
69       const string& fuzzer_extended_class_name) override;
70 
71   void GenerateHeaderGlobalFunctionDeclarations(Formatter& out,
72       const ComponentSpecificationMessage& message,
73       const bool print_extern_block = true) override;
74 
75   void GenerateCppBodyGlobalFunctions(Formatter& out,
76       const ComponentSpecificationMessage& message,
77       const string& fuzzer_extended_class_name,
78       const bool print_extern_block = true) override;
79 
80   void GenerateHeaderIncludeFiles(Formatter& out,
81       const ComponentSpecificationMessage& message,
82       const string& fuzzer_extended_class_name) override;
83 
84   void GenerateSourceIncludeFiles(Formatter& out,
85       const ComponentSpecificationMessage& message,
86       const string& fuzzer_extended_class_name) override;
87 
88   void GenerateAdditionalFuctionDeclarations(Formatter& out,
89       const ComponentSpecificationMessage& message,
90       const string& fuzzer_extended_class_name) override;
91 
92   void GeneratePublicFunctionDeclarations(
93       Formatter& out, const ComponentSpecificationMessage& message) override;
94 
95   void GeneratePrivateMemberDeclarations(Formatter& out,
96       const ComponentSpecificationMessage& message) override;
97 
98  private:
99   void GenerateCppBodyFuzzFunction(Formatter& out,
100       const StructSpecificationMessage& message,
101       const string& fuzzer_extended_class_name,
102       const string& original_data_structure_name, const string& parent_path);
103 
104   // Generates the code to declar the impl class for an interface.
105   void GenerateClassDecalrationForInterface(
106       Formatter& out, const ComponentSpecificationMessage& message);
107 
108   // Generates a scalar type in C/C++.
109   void GenerateScalarTypeInC(Formatter& out, const string& type);
110 
111   // Generates the driver function implementation for hidl reserved methods.
112   void GenerateDriverImplForReservedMethods(Formatter& out);
113 
114   // Generates the driver function implementation for a method.
115   void GenerateDriverImplForMethod(Formatter& out,
116       const FunctionSpecificationMessage& func_msg);
117 
118   // Generates the code to perform a Hal function call.
119   void GenerateHalFunctionCall(Formatter& out,
120       const FunctionSpecificationMessage& func_msg);
121 
122   // Generates the implementation of a callback passed to the Hal function call.
123   void GenerateSyncCallbackFunctionImpl(Formatter& out,
124       const FunctionSpecificationMessage& func_msg);
125 
126   // Generates the driver function declaration for attributes defined within
127   // an interface or in a types.hal.
128   void GenerateDriverDeclForAttribute(Formatter& out,
129       const VariableSpecificationMessage& attribute);
130 
131   // Generates the driver function implementation for attributes defined within
132   // an interface or in a types.hal.
133   void GenerateDriverImplForAttribute(Formatter& out,
134       const VariableSpecificationMessage& attribute);
135 
136   // Generates the driver code for a typed variable.
137   void GenerateDriverImplForTypedVariable(Formatter& out,
138       const VariableSpecificationMessage& val, const string& arg_name,
139       const string& arg_value_name);
140 
141   // Generates the verification function declarations for attributes defined
142   // within an interface or in a types.hal.
143   void GenerateVerificationDeclForAttribute(Formatter& out,
144       const VariableSpecificationMessage& attribute);
145 
146   // Generates the verification function implementation for attributes defined
147   // within an interface or in a types.hal.
148   void GenerateVerificationImplForAttribute(Formatter& out,
149       const VariableSpecificationMessage& attribute);
150 
151   // Generates the verification code for a typed variable.
152   void GenerateVerificationCodeForTypedVariable(Formatter& out,
153       const VariableSpecificationMessage& val, const string& result_value,
154       const string& expected_result);
155 
156   // Generates the SetResult function declarations for attributes defined
157   // within an interface or in a types.hal.
158   void GenerateSetResultDeclForAttribute(Formatter& out,
159       const VariableSpecificationMessage& attribute);
160 
161   // Generates the SetResult function implementation for attributes defined
162   // within an interface or in a types.hal.
163   void GenerateSetResultImplForAttribute(Formatter& out,
164       const VariableSpecificationMessage& attribute);
165 
166   // Generates the SetResult code for a typed variable.
167   void GenerateSetResultCodeForTypedVariable(Formatter& out,
168       const VariableSpecificationMessage& val, const string& result_msg,
169       const string& result_val);
170 
171   // Generates the random function declaration for attributes defined within
172   // an interface or in a types.hal.
173   void GenerateRandomFunctionDeclForAttribute(Formatter& out,
174       const VariableSpecificationMessage& attribute);
175 
176   // Generates the default return value for a typed variable.
177   void GenerateDefaultReturnValForTypedVariable(
178       Formatter& out, const VariableSpecificationMessage& val);
179 
180   // Generates the random function implementation for attributes defined within
181   // an interface or in a types.hal.
182   void GenerateRandomFunctionImplForAttribute(Formatter& out,
183       const VariableSpecificationMessage& attribute);
184 
185   // Generates the getService function implementation for an interface.
186   void GenerateGetServiceImpl(Formatter& out,
187       const ComponentSpecificationMessage& message,
188       const string& fuzzer_extended_class_name);
189 
190   // Generates all function declaration for an attributed.
191   void GenerateAllFunctionDeclForAttribute(Formatter& out,
192       const VariableSpecificationMessage& attribute);
193 
194   // Generates all function implementation for an attributed.
195   void GenerateAllFunctionImplForAttribute(Formatter& out,
196       const VariableSpecificationMessage& attribute);
197 
198   // Returns true if we could omit the callback function and return result
199   // directly.
200   bool CanElideCallback(const FunctionSpecificationMessage& func_msg);
201 
202   // instance variable name (e.g., device_);
203   static const char* const kInstanceVariableName;
204 };
205 
206 }  // namespace vts
207 }  // namespace android
208 
209 #endif  // VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_DRIVER_HALHIDLCODEGEN_H_
210