Merge "hidl2aidl: handle nested types in interfaces"
diff --git a/hidl2aidl/AidlTranslate.cpp b/hidl2aidl/AidlTranslate.cpp
index 1840bae..63a8aed 100644
--- a/hidl2aidl/AidlTranslate.cpp
+++ b/hidl2aidl/AidlTranslate.cpp
@@ -30,6 +30,7 @@
 #include "ConstantExpression.h"
 #include "Coordinator.h"
 #include "EnumType.h"
+#include "Interface.h"
 #include "NamedType.h"
 #include "ScalarType.h"
 #include "Scope.h"
@@ -312,21 +313,22 @@
     return base::Join(base::Split(type->fqName().package(), "."), "/");
 }
 
-static bool typeComesFromInterface(const NamedType* type) {
+static std::optional<const Interface*> getParentInterface(const NamedType* type) {
     const Scope* parent = type->parent();
     while (parent != nullptr) {
-        if (parent->isInterface()) {
-            return true;
+        if (parent->definesInterfaces()) {
+            return parent->getInterface();
         }
         parent = parent->parent();
     }
-    return false;
+    return std::nullopt;
 }
 
 static const std::string hidlIncludeFile(const NamedType* type) {
-    if (typeComesFromInterface(type)) {
+    std::optional<const Interface*> parent = getParentInterface(type);
+    if (parent) {
         return "#include \"" + getPackageFilePath(type) + "/" + type->fqName().version() + "/" +
-               type->parent()->fqName().getInterfaceName() + ".h\"\n";
+               parent.value()->fqName().getInterfaceName() + ".h\"\n";
     } else {
         return "#include \"" + getPackageFilePath(type) + "/" + type->fqName().version() +
                "/types.h\"\n";
diff --git a/scripts/hidl2aidl-all-interfaces.sh b/scripts/hidl2aidl-all-interfaces.sh
new file mode 100755
index 0000000..c63a45d
--- /dev/null
+++ b/scripts/hidl2aidl-all-interfaces.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+
+# Runs all released packages through the hidl2aidl tool and reports any failures
+# Requires that hidl2aidl is built.
+# 'm hidl2aidl'
+
+
+function hidl2aidl-all-interfaces-main() {
+    local ANY_FAIL=0
+    local TEST_DIR='/tmp/hidl2aidl_test'
+    set -e
+    mkdir "$TEST_DIR"
+    source "${ANDROID_BUILD_TOP}/system/tools/hidl/scripts/hal-queries.sh"
+
+    for i in $(aosp-released-packages);
+    do
+        hidl2aidl -o "$TEST_DIR" -f "$i" || \
+            { echo "FAIL: $i"; ANY_FAIL=1; }
+    done
+
+    rm -rf "$TEST_DIR"
+
+    [ $ANY_FAIL -eq 0 ] && echo 'All passed!'
+}
+
+hidl2aidl-all-interfaces-main