Change wrapPassthrough function.

This is currently a header function which is a detail of HIDL.
All uses of it currently have the same static casts around it.
By changing the return type to sp<IType>, the purpose of the
function becomes more clear, and there is less of a chance
false assumptions will be used by the (internal HIDL) users
of this function.

Bug: 67104214
Test: hidl's run_all_device_tests.sh
Change-Id: I9315d1b845bafebb2b3abfbf77a6b8d0c71e5cc2
diff --git a/transport/include/hidl/HidlPassthroughSupport.h b/transport/include/hidl/HidlPassthroughSupport.h
index 4fb1ae4..2291060 100644
--- a/transport/include/hidl/HidlPassthroughSupport.h
+++ b/transport/include/hidl/HidlPassthroughSupport.h
@@ -27,9 +27,11 @@
  * Wrap the given interface with the smallest BsChild possible. Will return the
  * argument directly if nullptr or isRemote().
  */
-template<typename IType>
-sp<::android::hidl::base::V1_0::IBase> wrapPassthrough(
-        sp<IType> iface) {
+template <typename IType,
+          typename = std::enable_if_t<std::is_same<i_tag, typename IType::_hidl_tag>::value>>
+sp<IType> wrapPassthrough(sp<IType> iface) {
+    using ::android::hidl::base::V1_0::IBase;
+
     if (iface.get() == nullptr || iface->isRemote()) {
         // doesn't know how to handle it.
         return iface;
@@ -43,7 +45,13 @@
     if (!func) {
         return nullptr;
     }
-    return func(static_cast<void *>(iface.get()));
+
+    sp<IBase> base = func(static_cast<void*>(iface.get()));
+
+    // would normally call castFrom, but gBsConstructorMap guarantees
+    // that its result is of the appropriate type (not necessaryly
+    // BsType, but definitely a child of IType).
+    return sp<IType>(static_cast<IType*>(base.get()));
 }
 
 }  // namespace details