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