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)
     37       : DriverCodeGenBase(input_vts_file_path) {}
     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 GenerateHeaderInterfaceImpl(
     65       Formatter& out, const ComponentSpecificationMessage& message) override;
     66 
     67   void GenerateCppBodyInterfaceImpl(
     68       Formatter& out, const ComponentSpecificationMessage& message,
     69       const string& fuzzer_extended_class_name) override;
     70 
     71   void GenerateClassConstructionFunction(Formatter& out,
     72       const ComponentSpecificationMessage& message,
     73       const string& fuzzer_extended_class_name) override;
     74 
     75   void GenerateHeaderGlobalFunctionDeclarations(Formatter& out,
     76       const ComponentSpecificationMessage& message,
     77       const bool print_extern_block = true) override;
     78 
     79   void GenerateCppBodyGlobalFunctions(Formatter& out,
     80       const ComponentSpecificationMessage& message,
     81       const string& fuzzer_extended_class_name,
     82       const bool print_extern_block = true) override;
     83 
     84   void GenerateHeaderIncludeFiles(Formatter& out,
     85       const ComponentSpecificationMessage& message,
     86       const string& fuzzer_extended_class_name) override;
     87 
     88   void GenerateSourceIncludeFiles(Formatter& out,
     89       const ComponentSpecificationMessage& message,
     90       const string& fuzzer_extended_class_name) override;
     91 
     92   void GenerateAdditionalFuctionDeclarations(Formatter& out,
     93       const ComponentSpecificationMessage& message,
     94       const string& fuzzer_extended_class_name) override;
     95 
     96   void GeneratePublicFunctionDeclarations(
     97       Formatter& out, const ComponentSpecificationMessage& message) override;
     98 
     99   void GeneratePrivateMemberDeclarations(Formatter& out,
    100       const ComponentSpecificationMessage& message) override;
    101 
    102  private:
    103   void GenerateCppBodyFuzzFunction(Formatter& out,
    104       const StructSpecificationMessage& message,
    105       const string& fuzzer_extended_class_name,
    106       const string& original_data_structure_name, const string& parent_path);
    107 
    108   // Generates the code to declar the impl class for an interface.
    109   void GenerateClassDecalrationForInterface(
    110       Formatter& out, const ComponentSpecificationMessage& message);
    111 
    112   // Generates a scalar type in C/C++.
    113   void GenerateScalarTypeInC(Formatter& out, const string& type);
    114 
    115   // Generates the driver function implementation for hidl reserved methods.
    116   void GenerateDriverImplForReservedMethods(Formatter& out);
    117 
    118   // Generates the driver function implementation for a method.
    119   void GenerateDriverImplForMethod(Formatter& out,
    120       const ComponentSpecificationMessage& message,
    121       const FunctionSpecificationMessage& func_msg);
    122 
    123   // Generates the code to perform a Hal function call.
    124   void GenerateHalFunctionCall(Formatter& out,
    125       const ComponentSpecificationMessage& message,
    126       const FunctionSpecificationMessage& func_msg);
    127 
    128   // Generates the implementation of a callback passed to the Hal function call.
    129   void GenerateSyncCallbackFunctionImpl(Formatter& out,
    130       const ComponentSpecificationMessage& message,
    131       const FunctionSpecificationMessage& func_msg);
    132 
    133   // Generates the driver function declaration for attributes defined within
    134   // an interface or in a types.hal.
    135   void GenerateDriverDeclForAttribute(Formatter& out,
    136       const VariableSpecificationMessage& attribute);
    137 
    138   // Generates the driver function implementation for attributes defined within
    139   // an interface or in a types.hal.
    140   void GenerateDriverImplForAttribute(Formatter& out,
    141       const VariableSpecificationMessage& attribute);
    142 
    143   // Generates the driver code for a typed variable.
    144   void GenerateDriverImplForTypedVariable(Formatter& out,
    145       const VariableSpecificationMessage& val, const string& arg_name,
    146       const string& arg_value_name);
    147 
    148   // Generates the verification function declarations for attributes defined
    149   // within an interface or in a types.hal.
    150   void GenerateVerificationDeclForAttribute(Formatter& out,
    151       const VariableSpecificationMessage& attribute);
    152 
    153   // Generates the verification function implementation for attributes defined
    154   // within an interface or in a types.hal.
    155   void GenerateVerificationImplForAttribute(Formatter& out,
    156       const VariableSpecificationMessage& attribute);
    157 
    158   // Generates the verification code for a typed variable.
    159   void GenerateVerificationCodeForTypedVariable(Formatter& out,
    160       const VariableSpecificationMessage& val, const string& result_value,
    161       const string& expected_result);
    162 
    163   // Generates the SetResult function declarations for attributes defined
    164   // within an interface or in a types.hal.
    165   void GenerateSetResultDeclForAttribute(Formatter& out,
    166       const VariableSpecificationMessage& attribute);
    167 
    168   // Generates the SetResult function implementation for attributes defined
    169   // within an interface or in a types.hal.
    170   void GenerateSetResultImplForAttribute(Formatter& out,
    171       const VariableSpecificationMessage& attribute);
    172 
    173   // Generates the SetResult code for a typed variable.
    174   void GenerateSetResultCodeForTypedVariable(Formatter& out,
    175       const VariableSpecificationMessage& val, const string& result_msg,
    176       const string& result_val);
    177 
    178   // Generates the random function declaration for attributes defined within
    179   // an interface or in a types.hal.
    180   void GenerateRandomFunctionDeclForAttribute(Formatter& out,
    181       const VariableSpecificationMessage& attribute);
    182 
    183   // Generates the random function implementation for attributes defined within
    184   // an interface or in a types.hal.
    185   void GenerateRandomFunctionImplForAttribute(Formatter& out,
    186       const VariableSpecificationMessage& attribute);
    187 
    188   // Generates the getService function implementation for an interface.
    189   void GenerateGetServiceImpl(Formatter& out,
    190       const ComponentSpecificationMessage& message,
    191       const string& fuzzer_extended_class_name);
    192 
    193   // Generates all function declaration for an attributed.
    194   void GenerateAllFunctionDeclForAttribute(Formatter& out,
    195       const VariableSpecificationMessage& attribute);
    196 
    197   // Generates all function implementation for an attributed.
    198   void GenerateAllFunctionImplForAttribute(Formatter& out,
    199       const VariableSpecificationMessage& attribute);
    200 
    201   // Returns true if we could omit the callback function and return result
    202   // directly.
    203   bool CanElideCallback(const FunctionSpecificationMessage& func_msg);
    204   bool isElidableType(const VariableType& type);
    205 
    206   // Returns true if a HIDL type uses 'const' in its native C/C++ form.
    207   bool isConstType(const VariableType& type);
    208 
    209   // instance variable name (e.g., device_);
    210   static const char* const kInstanceVariableName;
    211 };
    212 
    213 }  // namespace vts
    214 }  // namespace android
    215 
    216 #endif  // VTS_COMPILATION_TOOLS_VTSC_CODE_GEN_DRIVER_HALHIDLCODEGEN_H_
    217