Deal with split between IFoo and IHwFoo.

Add two macros that allow us to:
- Retrieve an interface instance with
  a specific name and version. The implementation
  of this will try to retrieve the interface from
  the hwservicemanager. If that fails, in the future
  it can try to retrieve a pass-through version.
- Register an interface implementation. The implementation
  of this automatically wraps it in a hwbinder object if
  necessary.

Also changed IServiceManager to work correctly with the new
macros in libhwbinder.

TODO (b/30584126): the versions should not need to be passed in,
but instead be retrieved from the generated interfaces directly.

Bug: 30588200
Change-Id: I0567b6a0d3c34b6c8ee54445bea0a42f41a9fcc4
diff --git a/include/hidl/HidlSupport.h b/include/hidl/HidlSupport.h
index 2689ab4..4b0e369 100644
--- a/include/hidl/HidlSupport.h
+++ b/include/hidl/HidlSupport.h
@@ -194,6 +194,7 @@
     bool operator==(const hidl_version& other) {
         return (mMajor == other.get_major() && mMinor == other.get_minor());
     }
+
     uint16_t get_major() const { return mMajor; }
     uint16_t get_minor() const { return mMinor; }
 
@@ -220,8 +221,40 @@
     return hidl_version(major,minor);
 }
 
+#define DECLARE_REGISTER_AND_GET_SERVICE(INTERFACE)                                      \
+    static ::android::sp<I##INTERFACE> getService(                                       \
+            const ::android::String16 &serviceName,                                      \
+            const hidl_version &version);                                                \
+    status_t registerAsService(                                                            \
+            const ::android::String16& serviceName,                                      \
+            const hidl_version &version);
+
+#define IMPLEMENT_REGISTER_AND_GET_SERVICE(INTERFACE)                                    \
+    ::android::sp<I##INTERFACE> I##INTERFACE::getService(                                \
+            const ::android::String16 &serviceName,                                      \
+            const hidl_version &version /* TODO get version from IFoo directly */)       \
+    {                                                                                    \
+        sp<I##INTERFACE> iface;                                                          \
+        const sp<IServiceManager> sm = defaultServiceManager();                          \
+        if (sm != nullptr) {                                                             \
+            sp<IBinder> binderIface = sm->getService(serviceName, version);              \
+            iface = IHw##INTERFACE::asInterface(binderIface);                            \
+        }                                                                                \
+        /* TODO: if we don't have a binder interface, try to instantiate default */      \
+        return iface;                                                                    \
+    }                                                                                    \
+    status_t I##INTERFACE::registerAsService(                                              \
+            const ::android::String16& serviceName,                                      \
+            const hidl_version &version)                                                 \
+    {                                                                                    \
+        sp<Bn##INTERFACE> binderIface = new Bn##INTERFACE(this);                         \
+        const sp<IServiceManager> sm = defaultServiceManager();                          \
+        return sm->addService(serviceName, binderIface, version);                        \
+    }
+
 }  // namespace hardware
 }  // namespace android
 
+
 #endif  // ANDROID_HIDL_SUPPORT_H