DO NOT MERGE - qt-qpr1-dev-plus-aosp-without-vendor@5915889 into stage-aosp-master

Bug: 142003500
Change-Id: Id97d5d3b02556b67d9b91194dfb1156a98507b8a
diff --git a/Android.bp b/Android.bp
index 819732d..9207656 100644
--- a/Android.bp
+++ b/Android.bp
@@ -30,8 +30,8 @@
 }
 
 cc_library_headers {
-    name: "libhidl_gtest_helpers",
-    export_include_dirs: ["gtest_helpers"],
+    name: "libhidl_gtest_helper",
+    export_include_dirs: ["gtest_helper"],
 }
 
 cc_test {
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 3e17fc4..c4a663e 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -2,6 +2,9 @@
   "presubmit": [
     {
       "name": "libhidl_test"
+    },
+    {
+      "name": "hidl_implementation_test"
     }
   ]
 }
diff --git a/gtest_helpers/hidl/GtestPrinter.h b/gtest_helper/hidl/GtestPrinter.h
similarity index 100%
rename from gtest_helpers/hidl/GtestPrinter.h
rename to gtest_helper/hidl/GtestPrinter.h
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index bb690f4..19d1d52 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -382,13 +382,6 @@
             } else if (!eachLib(handle, "SELF", sym)) {
                 return;
             }
-
-            const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
-            if (vtsRootPath && strlen(vtsRootPath) > 0) {
-                const std::string halLibraryPathVtsOverride =
-                    std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
-                paths.insert(paths.begin(), halLibraryPathVtsOverride);
-            }
         }
 #endif
 
@@ -803,6 +796,7 @@
               "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
               "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
               "condition is possible here.");
+        sleep(1);
     }
 
     for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
diff --git a/transport/include/hidl/LegacySupport.h b/transport/include/hidl/LegacySupport.h
index fdc5a48..7cb72a9 100644
--- a/transport/include/hidl/LegacySupport.h
+++ b/transport/include/hidl/LegacySupport.h
@@ -26,9 +26,12 @@
 namespace android {
 namespace hardware {
 namespace details {
-template <class Interface, typename Func>
+template <typename Interface>
+using RegisterServiceCb = std::function<status_t(const sp<Interface>&, const std::string&)>;
+
+template <class Interface, class ExpectInterface = Interface>
 __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
-    Func registerServiceCb, const std::string& name = "default") {
+        RegisterServiceCb<Interface> registerServiceCb, const std::string& name = "default") {
     sp<Interface> service = Interface::getService(name, true /* getStub */);
 
     if (service == nullptr) {
@@ -40,6 +43,10 @@
     LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!",
             Interface::descriptor, name.c_str());
 
+    sp<ExpectInterface> expected = ExpectInterface::castFrom(service);
+    LOG_FATAL_IF(expected == nullptr, "Implementation of %s/%s is not a %s!", Interface::descriptor,
+                 name.c_str(), ExpectInterface::descriptor);
+
     status_t status = registerServiceCb(service, name);
 
     if (status == OK) {
@@ -57,14 +64,14 @@
 /**
  * Registers passthrough service implementation.
  */
-template <class Interface>
+template <class Interface, class ExpectInterface = Interface>
 __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
-    const std::string& name = "default") {
-    return details::registerPassthroughServiceImplementation<Interface>(
-        [](const sp<Interface>& service, const std::string& name) {
-            return service->registerAsService(name);
-        },
-        name);
+        const std::string& name = "default") {
+    return details::registerPassthroughServiceImplementation<Interface, ExpectInterface>(
+            [](const sp<Interface>& service, const std::string& name) {
+                return service->registerAsService(name);
+            },
+            name);
 }
 
 /**
@@ -72,11 +79,11 @@
  *
  * Return value is exit status.
  */
-template <class Interface>
+template <class Interface, class ExpectInterface = Interface>
 __attribute__((warn_unused_result)) status_t defaultPassthroughServiceImplementation(
-    const std::string& name, size_t maxThreads = 1) {
+        const std::string& name, size_t maxThreads = 1) {
     configureRpcThreadpool(maxThreads, true);
-    status_t result = registerPassthroughServiceImplementation<Interface>(name);
+    status_t result = registerPassthroughServiceImplementation<Interface, ExpectInterface>(name);
 
     if (result != OK) {
         return result;
@@ -85,10 +92,11 @@
     joinRpcThreadpool();
     return UNKNOWN_ERROR;
 }
-template<class Interface>
-__attribute__((warn_unused_result))
-status_t defaultPassthroughServiceImplementation(size_t maxThreads = 1) {
-    return defaultPassthroughServiceImplementation<Interface>("default", maxThreads);
+template <class Interface, class ExpectInterface = Interface>
+__attribute__((warn_unused_result)) status_t defaultPassthroughServiceImplementation(
+        size_t maxThreads = 1) {
+    return defaultPassthroughServiceImplementation<Interface, ExpectInterface>("default",
+                                                                               maxThreads);
 }
 
 /**
@@ -99,10 +107,10 @@
  * through registerPassthroughServiceImplementation, so if that function is used in conjunction with
  * this one, the process may exit while a client is still using the HAL.
  */
-template <class Interface>
+template <class Interface, class ExpectInterface = Interface>
 __attribute__((warn_unused_result)) status_t registerLazyPassthroughServiceImplementation(
-    const std::string& name = "default") {
-    return details::registerPassthroughServiceImplementation<Interface>(
+        const std::string& name = "default") {
+    return details::registerPassthroughServiceImplementation<Interface, ExpectInterface>(
             [](const sp<Interface>& service, const std::string& name) {
                 using android::hardware::LazyServiceRegistrar;
                 return LazyServiceRegistrar::getInstance().registerService(service, name);
@@ -116,11 +124,12 @@
  *
  * Return value is exit status.
  */
-template <class Interface>
+template <class Interface, class ExpectInterface = Interface>
 __attribute__((warn_unused_result)) status_t defaultLazyPassthroughServiceImplementation(
-    const std::string& name, size_t maxThreads = 1) {
+        const std::string& name, size_t maxThreads = 1) {
     configureRpcThreadpool(maxThreads, true);
-    status_t result = registerLazyPassthroughServiceImplementation<Interface>(name);
+    status_t result =
+            registerLazyPassthroughServiceImplementation<Interface, ExpectInterface>(name);
 
     if (result != OK) {
         return result;
@@ -129,10 +138,11 @@
     joinRpcThreadpool();
     return UNKNOWN_ERROR;
 }
-template <class Interface>
+template <class Interface, class ExpectInterface = Interface>
 __attribute__((warn_unused_result)) status_t defaultLazyPassthroughServiceImplementation(
-    size_t maxThreads = 1) {
-    return defaultLazyPassthroughServiceImplementation<Interface>("default", maxThreads);
+        size_t maxThreads = 1) {
+    return defaultLazyPassthroughServiceImplementation<Interface, ExpectInterface>("default",
+                                                                                   maxThreads);
 }
 
 }  // namespace hardware