support legacy hal, and basic spec for wifi hal

Change-Id: I10dc64ab03ad90930ec66eaba5798997605f771a
diff --git a/sysfuzzer/vtscompiler/code_gen/CodeGenBase.cpp b/sysfuzzer/vtscompiler/code_gen/CodeGenBase.cpp
index fb95eeb..05c5462 100644
--- a/sysfuzzer/vtscompiler/code_gen/CodeGenBase.cpp
+++ b/sysfuzzer/vtscompiler/code_gen/CodeGenBase.cpp
@@ -64,7 +64,8 @@
 
   string fuzzer_extended_class_name;
   if (message.component_class() == HAL
-      || message.component_class() == HAL_SUBMODULE) {
+      || message.component_class() == HAL_SUBMODULE
+      || message.component_class() == LEGACY_HAL) {
     fuzzer_extended_class_name = "FuzzerExtended_" + component_name;
   }
 
@@ -81,6 +82,14 @@
   GenerateOpenNameSpaces(h_ss);
   h_ss << "class " << fuzzer_extended_class_name << " : public FuzzerBase {"
       << endl;
+  h_ss << " public:" << endl;
+  h_ss << "  " << fuzzer_extended_class_name << "() : FuzzerBase(";
+
+  if (message.component_class() == HAL) h_ss << "HAL";
+  if (message.component_class() == HAL_SUBMODULE) h_ss << "HAL_SUBMODULE";
+  if (message.component_class() == LEGACY_HAL) h_ss << "LEGACY_HAL";
+
+  h_ss << ") { }" << endl;
   h_ss << " protected:" << endl;
   h_ss << "  bool Fuzz(const FunctionSpecificationMessage& func_msg," << endl;
   h_ss << "            void** result);" << endl;
diff --git a/sysfuzzer/vtscompiler/code_gen/LegacyHalCodeGen.cpp b/sysfuzzer/vtscompiler/code_gen/LegacyHalCodeGen.cpp
new file mode 100644
index 0000000..72310f2
--- /dev/null
+++ b/sysfuzzer/vtscompiler/code_gen/LegacyHalCodeGen.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "code_gen/LegacyHalCodeGen.h"
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
+
+#include "VtsCompilerUtils.h"
+
+using namespace std;
+using namespace android;
+
+namespace android {
+namespace vts {
+
+const char* const LegacyHalCodeGen::kInstanceVariableName = "legacyhal_";
+
+
+void LegacyHalCodeGen::GenerateCppBodyFuzzFunction(
+    std::stringstream& cpp_ss,
+    const InterfaceSpecificationMessage& message,
+    const string& fuzzer_extended_class_name) {
+  cpp_ss << "bool " << fuzzer_extended_class_name << "::Fuzz(" << endl;
+  cpp_ss << "    const FunctionSpecificationMessage& func_msg," << endl;
+  cpp_ss << "    void** result) {" << endl;
+  cpp_ss << "  const char* func_name = func_msg.name().c_str();" << endl;
+  cpp_ss << "  cout << \"Function: \" << func_name << endl;" << endl;
+
+  for (auto const& api : message.api()) {
+    std::stringstream ss;
+
+    cpp_ss << "  if (!strcmp(func_name, \"" << api.name() << "\")) {" << endl;
+
+    // args - definition;
+    int arg_count = 0;
+    for (auto const& arg : api.arg()) {
+      cpp_ss << "    " << GetCppVariableType(arg) << " ";
+      cpp_ss << "arg" << arg_count << " = ";
+      if (arg_count == 0
+          && arg.has_aggregate_type()
+          && !strncmp(arg.aggregate_type().c_str(),
+                      message.original_data_structure_name().c_str(),
+                      message.original_data_structure_name().length())
+          && message.original_data_structure_name().length() > 0) {
+        cpp_ss << "reinterpret_cast<" << GetCppVariableType(arg) << ">("
+            << kInstanceVariableName << ")";
+      } else {
+        cpp_ss << GetCppInstanceType(arg);
+      }
+      cpp_ss << ";" << endl;
+      cpp_ss << "    cout << \"arg" << arg_count << " = \" << arg" << arg_count
+          << " << endl;" << endl;
+      arg_count++;
+    }
+
+    cpp_ss << "    ";
+    cpp_ss << "typedef void* (*";
+    cpp_ss << "func_type_" << api.name() << ")(...";
+    cpp_ss << ");" << endl;
+
+    // actual function call
+    if (api.return_type().has_primitive_type()
+        && !strcmp(api.return_type().primitive_type().c_str(), "void")) {
+      cpp_ss << "*result = NULL;" << endl;
+    } else {
+      cpp_ss << "*result = const_cast<void*>(reinterpret_cast<const void*>(";
+    }
+    cpp_ss << "    ";
+    cpp_ss << "((func_type_" << api.name() << ") "
+        << "target_loader_.GetLoaderFunction(\"" << api.name() << "\"))(";
+    //cpp_ss << "reinterpret_cast<" << message.original_data_structure_name()
+    //    << "*>(" << kInstanceVariableName << ")->" << api.name() << "(";
+
+    if (arg_count > 0) cpp_ss << endl;
+
+    for (int index = 0; index < arg_count; index++) {
+      cpp_ss << "      arg" << index;
+      if (index != (arg_count - 1)) {
+        cpp_ss << "," << endl;
+      }
+    }
+    if (!api.return_type().has_primitive_type()
+        || strcmp(api.return_type().primitive_type().c_str(), "void")) {
+      cpp_ss << "))";
+    }
+    cpp_ss << ");" << endl;
+    cpp_ss << "    return true;" << endl;
+    cpp_ss << "  }" << endl;
+  }
+  // TODO: if there were pointers, free them.
+  cpp_ss << "  return false;" << endl;
+  cpp_ss << "}" << endl;
+}
+
+
+void LegacyHalCodeGen::GenerateHeaderGlobalFunctionDeclarations(
+    std::stringstream& /*h_ss*/,
+    const string& /*function_prototype*/) {}
+
+}  // namespace vts
+}  // namespace android
diff --git a/sysfuzzer/vtscompiler/code_gen/LegacyHalCodeGen.h b/sysfuzzer/vtscompiler/code_gen/LegacyHalCodeGen.h
new file mode 100644
index 0000000..65ba192
--- /dev/null
+++ b/sysfuzzer/vtscompiler/code_gen/LegacyHalCodeGen.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __VTS_SYSFUZZER_COMPILER_LEGACYHALCODEGEN_H__
+#define __VTS_SYSFUZZER_COMPILER_LEGACYHALCODEGEN_H__
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
+
+#include "code_gen/CodeGenBase.h"
+#include "code_gen/HalCodeGen.h"
+
+using namespace std;
+
+namespace android {
+namespace vts {
+
+class LegacyHalCodeGen : public HalCodeGen {
+ public:
+  LegacyHalCodeGen(const char* input_vts_file_path, const char* vts_name)
+      : HalCodeGen(input_vts_file_path, vts_name) {}
+
+ protected:
+  void GenerateCppBodyFuzzFunction(
+      std::stringstream& cpp_ss,
+      const InterfaceSpecificationMessage& message,
+      const string& fuzzer_extended_class_name);
+
+  void GenerateHeaderGlobalFunctionDeclarations(
+      std::stringstream& h_ss,
+      const string& function_prototype);
+
+  // instance variable name (e.g., submodule_);
+  static const char* const kInstanceVariableName;
+};
+
+}  // namespace vts
+}  // namespace android
+
+#endif  // __VTS_SYSFUZZER_COMPILER_LEGACYHALCODEGEN_H__