Introduce fromBinder() helper.

To safely cast binder objects back to HIDL interfaces.

Modified toBinder(), to take into account IHw
classes no longer exist.

Removed an obsolete conversion of IFoo to BnFoo
in the servicemanager add macro - BnFoo is transport
specific and does not belong there.

Test: mma, hidl_test, hidl_test_java
Bug: 33173166
Change-Id: Iae84c900584a008c9cc8e03d1914e95d0fae77ce
diff --git a/transport/include/hidl/HidlTransportUtils.h b/transport/include/hidl/HidlTransportUtils.h
new file mode 100644
index 0000000..d890988
--- /dev/null
+++ b/transport/include/hidl/HidlTransportUtils.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 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 ANDROID_HIDL_TRANSPORT_UTILS_H
+#define ANDROID_HIDL_TRANSPORT_UTILS_H
+
+#include <android/hidl/base/1.0/IBase.h>
+
+namespace android {
+namespace hardware {
+
+/*
+ * Verifies the interface chain of 'interface' contains 'castTo'
+ */
+inline bool canCastInterface(::android::hidl::base::V1_0::IBase* interface, const char* castTo) {
+    if (interface == nullptr) {
+        return false;
+    }
+
+    bool canCast = false;
+    interface->interfaceChain([&](const hidl_vec<hidl_string> &types) {
+        for (size_t i = 0; i < types.size(); i++) {
+            if (types[i] == castTo) {
+                canCast = true;
+                break;
+            }
+        }
+    });
+    return canCast;
+}
+
+}   // namespace hardware
+}   // namespace android
+
+#endif //ANDROID_HIDL_TRANSPORT_UTILS_H