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( 39 const char* input_vts_file_path, const string& vts_name) : 40 CodeGenBase(input_vts_file_path, vts_name) {} 41 42 // Generate both a C/C++ file and its header file. 43 virtual void GenerateAll(Formatter& header_out, Formatter& source_out, 44 const ComponentSpecificationMessage& message); 45 46 // Generates source file. 47 virtual void GenerateSourceFile( 48 Formatter& out, const ComponentSpecificationMessage& message); 49 50 // Generates header file. 51 virtual void GenerateHeaderFile( 52 Formatter& out, const ComponentSpecificationMessage& message); 53 54 protected: 55 56 // Generates header code for a specific class. 57 virtual void GenerateClassHeader(Formatter& out, 58 const ComponentSpecificationMessage& message, 59 const string& fuzzer_extended_class_name); 60 61 // Generates source code for a specific class. 62 virtual void GenerateClassImpl(Formatter& out, 63 const ComponentSpecificationMessage& message, 64 const string& fuzzer_extended_class_name); 65 66 // Generates code for Fuzz(...) function body. 67 virtual void GenerateCppBodyFuzzFunction(Formatter& out, 68 const ComponentSpecificationMessage& message, 69 const string& fuzzer_extended_class_name) = 0; 70 71 // Generates code for GetAttribute(...) function body. 72 virtual void GenerateCppBodyGetAttributeFunction(Formatter& out, 73 const ComponentSpecificationMessage& message, 74 const string& fuzzer_extended_class_name) = 0; 75 76 // Generates code for CallFuction(...) function body. 77 virtual void GenerateDriverFunctionImpl(Formatter& out, 78 const ComponentSpecificationMessage& message, 79 const string& fuzzer_extended_class_name); 80 81 // Generates code for VerifyResults(...) function body. 82 virtual void GenerateVerificationFunctionImpl(Formatter& out, 83 const ComponentSpecificationMessage& message, 84 const string& fuzzer_extended_class_name); 85 86 // Generates C/C++ code for callback functions. 87 virtual void GenerateCppBodyCallbackFunction(Formatter& /*out*/, 88 const ComponentSpecificationMessage& /*message*/, 89 const string& /*fuzzer_extended_class_name*/) {}; 90 91 // Generates header code for construction function. 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. 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 105 // Generates code for the bodies of the C/C++ global functions. 106 virtual void GenerateCppBodyGlobalFunctions(Formatter& out, 107 const ComponentSpecificationMessage& message, 108 const string& fuzzer_extended_class_name); 109 110 // Generates header code for include declarations. 111 virtual void GenerateHeaderIncludeFiles(Formatter& out, 112 const ComponentSpecificationMessage& message, 113 const string& fuzzer_extended_class_name); 114 115 // Generates source code for include declarations. 116 virtual void GenerateSourceIncludeFiles(Formatter& out, 117 const ComponentSpecificationMessage& message, 118 const string& fuzzer_extended_class_name); 119 120 // Generates header code for private member declarations if any. 121 virtual void GeneratePrivateMemberDeclarations(Formatter& /*out*/, 122 const ComponentSpecificationMessage& /*message*/) {}; 123 124 //********** Utility functions ***************** 125 // Generates the namespace name of a HIDL component, crashes otherwise. 126 void GenerateNamespaceName( 127 Formatter& out, const ComponentSpecificationMessage& message); 128 129 // Generates code that opens the default namespaces. 130 void GenerateOpenNameSpaces( 131 Formatter& out, const ComponentSpecificationMessage& message); 132 133 // Generates code that closes the default namespaces. 134 void GenerateCloseNameSpaces(Formatter& out, 135 const ComponentSpecificationMessage& message); 136 137 // Generates code that starts the measurement. 138 void GenerateCodeToStartMeasurement(Formatter& out); 139 140 // Generates code that stops the measurement. 141 void GenerateCodeToStopMeasurement(Formatter& out); 142 143 void GenerateFuzzFunctionForSubStruct( 144 Formatter& out, const StructSpecificationMessage& message, 145 const string& parent_path); 146 }; 147 148 } // namespace vts 149 } // namespace android 150 151 #endif // VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_DRIVER_CODEGENBASE_H_ 152