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