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_CODEGENBASE_H_
18 #define VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_DRIVER_CODEGENBASE_H_
19 
20 #include <string>
21 
22 #include <hidl-util/Formatter.h>
23 #include <hidl-util/FQName.h>
24 
25 #include "code_gen/CodeGenBase.h"
26 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
27 
28 using namespace std;
29 
30 namespace android {
31 namespace vts {
32 
33 class DriverCodeGenBase : public CodeGenBase {
34  public:
DriverCodeGenBase(const char * input_vts_file_path)35   explicit DriverCodeGenBase(const char* input_vts_file_path)
36       : CodeGenBase(input_vts_file_path) {}
37 
38   // Generate both a C/C++ file and its header file.
39   virtual void GenerateAll(Formatter& header_out, Formatter& source_out,
40                            const ComponentSpecificationMessage& message);
41 
42   // Generates source file.
43   virtual void GenerateSourceFile(
44       Formatter& out, const ComponentSpecificationMessage& message);
45 
46   // Generates header file.
47   virtual void GenerateHeaderFile(
48       Formatter& out, const ComponentSpecificationMessage& message);
49 
50  protected:
51 
52   // Generates header code for a specific class.
53   virtual void GenerateClassHeader(Formatter& out,
54       const ComponentSpecificationMessage& message,
55       const string& fuzzer_extended_class_name);
56 
57   // Generates source code for a specific class.
58   virtual void GenerateClassImpl(Formatter& out,
59       const ComponentSpecificationMessage& message,
60       const string& fuzzer_extended_class_name);
61 
62   // Generates code for Fuzz(...) function body.
63   virtual void GenerateCppBodyFuzzFunction(Formatter& out,
64       const ComponentSpecificationMessage& message,
65       const string& fuzzer_extended_class_name) = 0;
66 
67   // Generates code for GetAttribute(...) function body.
68   virtual void GenerateCppBodyGetAttributeFunction(Formatter& out,
69       const ComponentSpecificationMessage& message,
70       const string& fuzzer_extended_class_name) = 0;
71 
72   // Generates code for CallFuction(...) function body.
73   virtual void GenerateDriverFunctionImpl(Formatter& out,
74       const ComponentSpecificationMessage& message,
75       const string& fuzzer_extended_class_name);
76 
77   // Generates code for VerifyResults(...) function body.
78   virtual void GenerateVerificationFunctionImpl(Formatter& out,
79       const ComponentSpecificationMessage& message,
80       const string& fuzzer_extended_class_name);
81 
82   // Generates C/C++ code for interface implemetation class.
GenerateCppBodyInterfaceImpl(Formatter &,const ComponentSpecificationMessage &,const string &)83   virtual void GenerateCppBodyInterfaceImpl(
84       Formatter& /*out*/, const ComponentSpecificationMessage& /*message*/,
85       const string& /*fuzzer_extended_class_name*/) {};
86 
87   // Generates header code for interface impl class.
GenerateHeaderInterfaceImpl(Formatter &,const ComponentSpecificationMessage &)88   virtual void GenerateHeaderInterfaceImpl(
89       Formatter& /*out*/, const ComponentSpecificationMessage& /*message*/) {};
90 
91   // Generates header code for construction function.
GenerateClassConstructionFunction(Formatter &,const ComponentSpecificationMessage &,const string &)92   virtual void GenerateClassConstructionFunction(Formatter& /*out*/,
93       const ComponentSpecificationMessage& /*message*/,
94       const string& /*fuzzer_extended_class_name*/) {};
95 
96   // Generates header code for additional function declarations if any.
GenerateAdditionalFuctionDeclarations(Formatter &,const ComponentSpecificationMessage &,const string &)97   virtual void GenerateAdditionalFuctionDeclarations(Formatter& /*out*/,
98       const ComponentSpecificationMessage& /*message*/,
99       const string& /*fuzzer_extended_class_name*/) {};
100 
101   // Generates header code to declare the C/C++ global functions.
102   virtual void GenerateHeaderGlobalFunctionDeclarations(Formatter& out,
103       const ComponentSpecificationMessage& message,
104       const bool print_extern_block = true);
105 
106   // Generates code for the bodies of the C/C++ global functions.
107   virtual void GenerateCppBodyGlobalFunctions(Formatter& out,
108       const ComponentSpecificationMessage& message,
109       const string& fuzzer_extended_class_name,
110       const bool print_extern_block = true);
111 
112   // Generates header code for include declarations.
113   virtual void GenerateHeaderIncludeFiles(Formatter& out,
114       const ComponentSpecificationMessage& message,
115       const string& fuzzer_extended_class_name);
116 
117   // Generates source code for include declarations.
118   virtual void GenerateSourceIncludeFiles(Formatter& out,
119       const ComponentSpecificationMessage& message,
120       const string& fuzzer_extended_class_name);
121 
122   // Generates header code for public function declarations if any.
GeneratePublicFunctionDeclarations(Formatter &,const ComponentSpecificationMessage &)123   virtual void GeneratePublicFunctionDeclarations(
124       Formatter& /*out*/, const ComponentSpecificationMessage& /*message*/) {};
125 
126   // Generates header code for private member declarations if any.
GeneratePrivateMemberDeclarations(Formatter &,const ComponentSpecificationMessage &)127   virtual void GeneratePrivateMemberDeclarations(Formatter& /*out*/,
128       const ComponentSpecificationMessage& /*message*/) {};
129 
130   //**********   Utility functions   *****************
131   // Generates the namespace name of a HIDL component, crashes otherwise.
132   void GenerateNamespaceName(
133       Formatter& out, const ComponentSpecificationMessage& message);
134 
135   // Generates code that opens the default namespaces.
136   void GenerateOpenNameSpaces(
137       Formatter& out, const ComponentSpecificationMessage& message);
138 
139   // Generates code that closes the default namespaces.
140   void GenerateCloseNameSpaces(Formatter& out,
141       const ComponentSpecificationMessage& message);
142 
143   // Generates code that starts the measurement.
144   void GenerateCodeToStartMeasurement(Formatter& out);
145 
146   // Generates code that stops the measurement.
147   void GenerateCodeToStopMeasurement(Formatter& out);
148 
149   void GenerateFuzzFunctionForSubStruct(
150       Formatter& out, const StructSpecificationMessage& message,
151       const string& parent_path);
152 };
153 
154 }  // namespace vts
155 }  // namespace android
156 
157 #endif  // VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_DRIVER_CODEGENBASE_H_
158