Update for hidlized hwservicemanager.

Test: end to end
Bug: 32313592

Change-Id: I11496e4c3dd6d0d43f635886b46609cb8e430efc
diff --git a/Android.bp b/Android.bp
index 9bc0b26..f7d69e1 100644
--- a/Android.bp
+++ b/Android.bp
@@ -16,6 +16,34 @@
     "test",
 ]
 
+genrule {
+    name: "android.hidl.manager@1.0_genc++",
+    tool: "hidl-gen",
+    cmd: "$tool -o $genDir -Lc++ -randroid.hidl:system/libhidl android.hidl.manager@1.0",
+    srcs: [
+        "manager/1.0/IServiceManager.hal",
+    ],
+    out: [
+        "android/hidl/manager/1.0/ServiceManagerAll.cpp",
+    ],
+}
+
+genrule {
+    name: "android.hidl.manager@1.0_genc++_headers",
+    tool: "hidl-gen",
+    cmd: "$tool -o $genDir -Lc++ -randroid.hidl:system/libhidl android.hidl.manager@1.0",
+    srcs: [
+        "manager/1.0/IServiceManager.hal",
+    ],
+    out: [
+        "android/hidl/manager/1.0/IServiceManager.h",
+        "android/hidl/manager/1.0/IHwServiceManager.h",
+        "android/hidl/manager/1.0/BnServiceManager.h",
+        "android/hidl/manager/1.0/BpServiceManager.h",
+        "android/hidl/manager/1.0/BsServiceManager.h",
+    ],
+}
+
 cc_library_shared {
     name: "libhidl",
     shared_libs: [
@@ -36,9 +64,14 @@
     sanitize: {
         misc_undefined: ["integer"],
     },
+
+    generated_sources: ["android.hidl.manager@1.0_genc++"],
+    generated_headers: ["android.hidl.manager@1.0_genc++_headers"],
+    export_generated_headers: ["android.hidl.manager@1.0_genc++_headers"],
+
     srcs: [
         "HidlSupport.cpp",
-        "IServiceManager.cpp",
+        "ServiceManagement.cpp",
         "Static.cpp",
         "Status.cpp",
         "TaskRunner.cpp",
diff --git a/IServiceManager.cpp b/IServiceManager.cpp
deleted file mode 100644
index 9a19554..0000000
--- a/IServiceManager.cpp
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "HwServiceManager"
-
-#include <hidl/IServiceManager.h>
-#include <hidl/Static.h>
-#include <hidl/Status.h>
-
-#include <utils/Log.h>
-#include <hwbinder/IPCThreadState.h>
-#include <hwbinder/Parcel.h>
-#include <utils/String8.h>
-#include <utils/SystemClock.h>
-
-#include <unistd.h>
-
-
-namespace android {
-namespace hardware {
-sp<IServiceManager> defaultServiceManager()
-{
-    if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
-    if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
-        // HwBinder not available on this device or not accessible to
-        // this process.
-        return nullptr;
-    }
-    {
-        AutoMutex _l(gDefaultServiceManagerLock);
-        while (gDefaultServiceManager == NULL) {
-            gDefaultServiceManager = interface_cast<IHwServiceManager>(
-                ProcessState::self()->getContextObject(NULL));
-            if (gDefaultServiceManager == NULL)
-                sleep(1);
-        }
-    }
-
-    return gDefaultServiceManager;
-}
-
-// ----------------------------------------------------------------------
-
-class BpServiceManager : public BpInterface<IHwServiceManager>
-{
-public:
-    explicit BpServiceManager(const sp<IBinder>& impl)
-        : BpInterface<IHwServiceManager>(impl)
-    {
-    }
-
-    virtual sp<IBinder> getService(const String16& name, const hidl_version& version) const
-    {
-        unsigned n;
-        for (n = 0; n < 5; n++){
-            sp<IBinder> svc = checkService(name, version);
-            if (svc != NULL) return svc;
-            ALOGI("Waiting for service %s...\n", String8(name).string());
-            sleep(1);
-        }
-        return NULL;
-    }
-
-    virtual sp<IBinder> checkService( const String16& name, const hidl_version& version) const
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(getInterfaceDescriptor());
-        data.writeString16(name);
-        version.writeToParcel(data);
-        remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
-        return reply.readStrongBinder();
-    }
-
-    virtual status_t addService(const String16& name,
-            const sp<IBinder>& service, const hidl_version& version,
-            bool allowIsolated)
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(getInterfaceDescriptor());
-        data.writeString16(name);
-        data.writeStrongBinder(service);
-        version.writeToParcel(data);
-        data.writeInt32(allowIsolated ? 1 : 0);
-        status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
-        if (err == NO_ERROR) {
-            Status status;
-            status.readFromParcel(reply);
-            return status.exceptionCode();
-        } else {
-            return err;
-        }
-    }
-
-    virtual Vector<String16> listServices()
-    {
-        Vector<String16> res;
-        int n = 0;
-
-        for (;;) {
-            Parcel data, reply;
-            data.writeInterfaceToken(getInterfaceDescriptor());
-            data.writeInt32(n++);
-            status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
-            if (err != NO_ERROR)
-                break;
-            res.add(reply.readString16());
-        }
-        return res;
-    }
-};
-
-IMPLEMENT_HWBINDER_META_INTERFACE(ServiceManager, "android.hardware.IServiceManager");
-
-}; // namespace hardware
-}; // namespace android
diff --git a/ServiceManagement.cpp b/ServiceManagement.cpp
new file mode 100644
index 0000000..102fa6c
--- /dev/null
+++ b/ServiceManagement.cpp
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+
+#define LOG_TAG "ServiceManagement"
+
+#include <hidl/ServiceManagement.h>
+#include <hidl/Static.h>
+#include <hidl/Status.h>
+
+#include <hwbinder/IPCThreadState.h>
+#include <hwbinder/Parcel.h>
+#include <utils/Log.h>
+#include <utils/SystemClock.h>
+
+#include <unistd.h>
+
+#include <android/hidl/manager/1.0/IServiceManager.h>
+#include <android/hidl/manager/1.0/IHwServiceManager.h>
+
+using android::hidl::manager::V1_0::IHwServiceManager;
+using android::hidl::manager::V1_0::IServiceManager;
+
+namespace android {
+namespace hardware {
+
+sp<IServiceManager> defaultServiceManager() {
+
+    if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
+    if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
+        // HwBinder not available on this device or not accessible to
+        // this process.
+        return nullptr;
+    }
+    {
+        AutoMutex _l(gDefaultServiceManagerLock);
+        while (gDefaultServiceManager == NULL) {
+            gDefaultServiceManager = interface_cast<IHwServiceManager>(
+                ProcessState::self()->getContextObject(NULL));
+            if (gDefaultServiceManager == NULL)
+                sleep(1);
+        }
+    }
+
+    return gDefaultServiceManager;
+}
+
+}; // namespace hardware
+}; // namespace android
diff --git a/Static.cpp b/Static.cpp
index a7dc08f..3bd0285 100644
--- a/Static.cpp
+++ b/Static.cpp
@@ -23,7 +23,7 @@
 namespace hardware {
 
 Mutex gDefaultServiceManagerLock;
-sp<IServiceManager> gDefaultServiceManager;
+sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
 
 }   // namespace hardware
 }   // namespace android
diff --git a/include/hidl/HidlSupport.h b/include/hidl/HidlSupport.h
index 4fbe826..b78d0a9 100644
--- a/include/hidl/HidlSupport.h
+++ b/include/hidl/HidlSupport.h
@@ -553,7 +553,7 @@
 public:
     constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
 
-    bool operator==(const hidl_version& other) {
+    bool operator==(const hidl_version& other) const {
         return (mMajor == other.get_major() && mMinor == other.get_minor());
     }
 
@@ -633,22 +633,32 @@
 #define DECLARE_REGISTER_AND_GET_SERVICE(INTERFACE)                                      \
     static ::android::sp<I##INTERFACE> getService(                                       \
             const std::string &serviceName, bool getStub=false);                         \
-    status_t registerAsService(                                                          \
+    ::android::status_t registerAsService(                                               \
             const std::string &serviceName);                                             \
 
 #define IMPLEMENT_REGISTER_AND_GET_SERVICE(INTERFACE, LIB)                               \
     ::android::sp<I##INTERFACE> I##INTERFACE::getService(                                \
             const std::string &serviceName, bool getStub)                                \
     {                                                                                    \
+        using ::android::sp;                                                             \
+        using ::android::hardware::defaultServiceManager;                                \
+        using ::android::hardware::IBinder;                                              \
+        using ::android::hidl::manager::V1_0::IServiceManager;                           \
         sp<I##INTERFACE> iface;                                                          \
         const struct timespec DELAY {1,0};                                               \
         unsigned retries = 3;                                                            \
         const sp<IServiceManager> sm = defaultServiceManager();                          \
         if (sm != nullptr && !getStub) {                                                 \
             do {                                                                         \
-                sp<IBinder> binderIface =                                                \
-                        sm->checkService(String16(serviceName.c_str()),                  \
-                                         I##INTERFACE::version);                         \
+                sp<IBinder> binderIface;                                                 \
+                IServiceManager::Version version {                                       \
+                    .major = I##INTERFACE::version.get_major(),                          \
+                    .minor = I##INTERFACE::version.get_minor(),                          \
+                };                                                                       \
+                sm->get(serviceName.c_str(), version,                                    \
+                    [&binderIface](sp<IBinder> iface) {                                  \
+                        binderIface = iface;                                             \
+                    });                                                                  \
                 iface = IHw##INTERFACE::asInterface(binderIface);                        \
                 if (iface != nullptr) {                                                  \
                     return iface;                                                        \
@@ -677,13 +687,21 @@
         }                                                                                \
         return iface;                                                                    \
     }                                                                                    \
-    status_t I##INTERFACE::registerAsService(                                            \
+    ::android::status_t I##INTERFACE::registerAsService(                                 \
             const std::string &serviceName)                                              \
     {                                                                                    \
+        using ::android::sp;                                                             \
+        using ::android::hardware::defaultServiceManager;                                \
+        using ::android::hidl::manager::V1_0::IServiceManager;                           \
         sp<Bn##INTERFACE> binderIface = new Bn##INTERFACE(this);                         \
         const sp<IServiceManager> sm = defaultServiceManager();                          \
-        return sm->addService(String16(serviceName.c_str()), binderIface,                \
-                              I##INTERFACE::version);                                    \
+        IServiceManager::Version version {                                               \
+            .major = I##INTERFACE::version.get_major(),                                  \
+            .minor = I##INTERFACE::version.get_minor(),                                  \
+        };                                                                               \
+        sm->add(serviceName.c_str(), binderIface, version);                              \
+        /* TODO return value */                                                          \
+        return ::android::OK;                                                            \
     }
 
 // ----------------------------------------------------------------------
diff --git a/include/hidl/IServiceManager.h b/include/hidl/IServiceManager.h
deleted file mode 100644
index 4013cbf..0000000
--- a/include/hidl/IServiceManager.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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_HARDWARE_ISERVICE_MANAGER_H
-#define ANDROID_HARDWARE_ISERVICE_MANAGER_H
-
-#include <hidl/HidlSupport.h>
-#include <hwbinder/IInterface.h>
-#include <hwbinder/Parcel.h>
-#include <utils/String16.h>
-
-namespace android {
-namespace hardware {
-
-class IServiceManager : virtual public RefBase
-{
-public:
-    /**
-     * Retrieve an existing service, blocking for a few seconds
-     * if it doesn't yet exist.
-     */
-    virtual sp<IBinder>         getService( const String16& name,
-                                            const android::hardware::hidl_version& version
-                                          ) const = 0;
-
-    /**
-     * Retrieve an existing service, non-blocking.
-     */
-    virtual sp<IBinder>         checkService( const String16& name,
-                                              const android::hardware::hidl_version& version
-                                            ) const = 0;
-
-    /**
-     * Register a service.
-     */
-    virtual status_t            addService( const String16& name,
-                                            const sp<IBinder>& service,
-                                            const android::hardware::hidl_version& version,
-                                            bool allowIsolated = false) = 0;
-
-};
-
-struct IHwServiceManager : public IServiceManager, public IInterface {
-    DECLARE_HWBINDER_META_INTERFACE(ServiceManager)
-
-    enum Call {
-        GET_SERVICE_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
-        CHECK_SERVICE_TRANSACTION,
-        ADD_SERVICE_TRANSACTION,
-        LIST_SERVICES_TRANSACTION,
-    };
-};
-
-sp<IServiceManager> defaultServiceManager();
-
-template<typename INTERFACE>
-status_t getService(const String16& name, android::hardware::hidl_version version,
-        sp<INTERFACE>* outService)
-{
-    const sp<IServiceManager> sm = defaultServiceManager();
-    if (sm != NULL) {
-        *outService = interface_cast<INTERFACE>(sm->getService(name, version));
-        if ((*outService) != NULL) return NO_ERROR;
-    }
-    return NAME_NOT_FOUND;
-}
-
-}; // namespace hardware
-}; // namespace android
-
-#endif // ANDROID_HARDWARE_ISERVICE_MANAGER_H
-
diff --git a/include/hidl/LegacySupport.h b/include/hidl/LegacySupport.h
index 5acc1d0..6e030ce 100644
--- a/include/hidl/LegacySupport.h
+++ b/include/hidl/LegacySupport.h
@@ -16,7 +16,6 @@
 
 #include <utils/Log.h>
 
-#include "IServiceManager.h"
 #include <hwbinder/IPCThreadState.h>
 #include <hwbinder/ProcessState.h>
 #include <utils/Errors.h>
diff --git a/include/hidl/ServiceManagement.h b/include/hidl/ServiceManagement.h
new file mode 100644
index 0000000..51faf0b
--- /dev/null
+++ b/include/hidl/ServiceManagement.h
@@ -0,0 +1,40 @@
+/*
+ * 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_HARDWARE_ISERVICE_MANAGER_H
+#define ANDROID_HARDWARE_ISERVICE_MANAGER_H
+
+#include <utils/StrongPointer.h>
+
+namespace android {
+
+namespace hidl {
+namespace manager {
+namespace V1_0 {
+    struct IServiceManager;
+}; // namespace V1_0
+}; // namespace manager
+}; // namespace hidl
+
+namespace hardware {
+
+sp<::android::hidl::manager::V1_0::IServiceManager> defaultServiceManager();
+
+}; // namespace hardware
+}; // namespace android
+
+#endif // ANDROID_HARDWARE_ISERVICE_MANAGER_H
+
diff --git a/include/hidl/Static.h b/include/hidl/Static.h
index a021e6e..00db8a7 100644
--- a/include/hidl/Static.h
+++ b/include/hidl/Static.h
@@ -18,14 +18,14 @@
 // destruction order in the library.
 
 #include <utils/threads.h>
-#include <hidl/IServiceManager.h>
+#include <android/hidl/manager/1.0/IServiceManager.h>
 
 namespace android {
 namespace hardware {
 
-// For IServiceManager.cpp
+// For ServiceManagement.cpp
 extern Mutex gDefaultServiceManagerLock;
-extern sp<IServiceManager> gDefaultServiceManager;
+extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
 
 }   // namespace hardware
 }   // namespace android
diff --git a/manager/1.0/IServiceManager.hal b/manager/1.0/IServiceManager.hal
new file mode 100644
index 0000000..1e76814
--- /dev/null
+++ b/manager/1.0/IServiceManager.hal
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+package android.hidl.manager@1.0;
+
+interface IServiceManager {
+    struct Version {
+        uint16_t major;
+        uint16_t minor;
+    };
+
+    /**
+     * Retrieve an existing service.
+     *
+     * Warning: This function should only be used in specific circumstances. You
+     * are likely looking for 'IMyInterface::getService("name");'.
+     */
+    get(string name, Version version) generates (interface service);
+
+    /**
+     * Register a service.
+     *
+     * Warning: Function 'INTERFACE::registerAsService' should be used
+     * instead of this to register services.
+     *
+     * TODO: namespace out name per interface type.
+     *       register as super classes as well.
+     */
+    add(string name, interface service, Version version) generates (bool success);
+
+};
\ No newline at end of file