Update for getting service notifications.

- Fixed makefiles for IServiceManager.hal so that they can be
  automatically generated with hidl-gen.
- Added IServiceNotifications and
  IServiceManager::registerForNotifications
- Can now say IFoo::registerForNotifications("foo", <instance of
  IServiceNotification>)

Bug: 30804608
Test: hidl_test

Change-Id: I43829a323461e98cb70237b4e494abded59c173f
diff --git a/Android.bp b/Android.bp
index 223839b..6b08c87 100644
--- a/Android.bp
+++ b/Android.bp
@@ -14,36 +14,9 @@
 
 subdirs = [
     "test",
+    "manager/1.0"
 ]
 
-genrule {
-    name: "android.hidl.manager@1.0_genc++",
-    tools: ["hidl-gen"],
-    cmd: "$(location hidl-gen) -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",
-    tools: ["hidl-gen"],
-    cmd: "$(location hidl-gen) -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: [
diff --git a/include/hidl/HidlSupport.h b/include/hidl/HidlSupport.h
index ca98199..83ff80b 100644
--- a/include/hidl/HidlSupport.h
+++ b/include/hidl/HidlSupport.h
@@ -667,13 +667,16 @@
 #define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
 #endif
 
-#define DECLARE_REGISTER_AND_GET_SERVICE(INTERFACE)                                      \
+#define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE)                                  \
     static ::android::sp<I##INTERFACE> getService(                                       \
             const std::string &serviceName, bool getStub=false);                         \
-    ::android::status_t registerAsService(                                               \
-            const std::string &serviceName);                                             \
+    ::android::status_t registerAsService(const std::string &serviceName);               \
+    static bool registerForNotifications(                                                \
+        const std::string &serviceName,                                                  \
+        const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification>        \
+                  &notification);                                                        \
 
-#define IMPLEMENT_REGISTER_AND_GET_SERVICE(INTERFACE, PACKAGE)                           \
+#define IMPLEMENT_SERVICE_MANAGER_INTERACTIONS(INTERFACE, PACKAGE)                       \
     ::android::sp<I##INTERFACE> I##INTERFACE::getService(                                \
             const std::string &serviceName, bool getStub)                                \
     {                                                                                    \
@@ -723,7 +726,6 @@
     {                                                                                    \
         using ::android::sp;                                                             \
         using ::android::hardware::defaultServiceManager;                                \
-        using ::android::hardware::hidl_string;                                          \
         using ::android::hidl::manager::V1_0::IServiceManager;                           \
         sp<Bn##INTERFACE> binderIface = new Bn##INTERFACE(this);                         \
         const sp<IServiceManager> sm = defaultServiceManager();                          \
@@ -735,6 +737,22 @@
                 });                                                                      \
         success = success && ret.getStatus().isOk();                                     \
         return success ? ::android::OK : ::android::UNKNOWN_ERROR;                       \
+    }                                                                                    \
+    bool I##INTERFACE::registerForNotifications(                                         \
+            const std::string &serviceName,                                              \
+            const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification>    \
+                      &notification)                                                     \
+    {                                                                                    \
+        using ::android::sp;                                                             \
+        using ::android::hardware::defaultServiceManager;                                \
+        using ::android::hidl::manager::V1_0::IServiceManager;                           \
+        const sp<IServiceManager> sm = defaultServiceManager();                          \
+        if (sm == nullptr) {                                                             \
+            return false;                                                                \
+        }                                                                                \
+        return sm->registerForNotifications(PACKAGE "::I" #INTERFACE,                    \
+                                            serviceName,                                 \
+                                            notification);                               \
     }
 
 // ----------------------------------------------------------------------
diff --git a/manager/1.0/Android.bp b/manager/1.0/Android.bp
new file mode 100644
index 0000000..a41df82
--- /dev/null
+++ b/manager/1.0/Android.bp
@@ -0,0 +1,55 @@
+// This file is autogenerated by hidl-gen. Do not edit manually.
+
+genrule {
+    name: "android.hidl.manager@1.0_genc++",
+    tools: ["hidl-gen"],
+    cmd: "$(location hidl-gen) -o $(genDir) -Lc++ -randroid.hidl:system/libhidl android.hidl.manager@1.0",
+    srcs: [
+        "IServiceManager.hal",
+        "IServiceNotification.hal",
+    ],
+    out: [
+        "android/hidl/manager/1.0/ServiceManagerAll.cpp",
+        "android/hidl/manager/1.0/ServiceNotificationAll.cpp",
+    ],
+}
+
+genrule {
+    name: "android.hidl.manager@1.0_genc++_headers",
+    tools: ["hidl-gen"],
+    cmd: "$(location hidl-gen) -o $(genDir) -Lc++ -randroid.hidl:system/libhidl android.hidl.manager@1.0",
+    srcs: [
+        "IServiceManager.hal",
+        "IServiceNotification.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",
+        "android/hidl/manager/1.0/IServiceNotification.h",
+        "android/hidl/manager/1.0/IHwServiceNotification.h",
+        "android/hidl/manager/1.0/BnServiceNotification.h",
+        "android/hidl/manager/1.0/BpServiceNotification.h",
+        "android/hidl/manager/1.0/BsServiceNotification.h",
+    ],
+}
+
+cc_library_shared {
+    name: "android.hidl.manager@1.0",
+    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"],
+    shared_libs: [
+        "libhidl",
+        "libhwbinder",
+        "libutils",
+        "libcutils",
+    ],
+    export_shared_lib_headers: [
+        "libhidl",
+        "libhwbinder",
+        "libutils",
+    ],
+}
diff --git a/manager/1.0/Android.mk b/manager/1.0/Android.mk
new file mode 100644
index 0000000..588e556
--- /dev/null
+++ b/manager/1.0/Android.mk
@@ -0,0 +1,102 @@
+# This file is autogenerated by hidl-gen. Do not edit manually.
+
+LOCAL_PATH := $(call my-dir)
+
+################################################################################
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hidl.manager@1.0-java
+LOCAL_MODULE_CLASS := JAVA_LIBRARIES
+
+intermediates := $(local-generated-sources-dir)
+
+HIDL := $(HOST_OUT_EXECUTABLES)/hidl-gen$(HOST_EXECUTABLE_SUFFIX)
+
+#
+# Build IServiceManager.hal
+#
+GEN := $(intermediates)/android/hidl/manager/1.0/IServiceManager.java
+$(GEN): $(HIDL)
+$(GEN): PRIVATE_HIDL := $(HIDL)
+$(GEN): PRIVATE_DEPS := $(LOCAL_PATH)/IServiceManager.hal
+$(GEN): PRIVATE_DEPS += $(LOCAL_PATH)/IServiceNotification.hal
+$(GEN): $(LOCAL_PATH)/IServiceNotification.hal
+$(GEN): PRIVATE_OUTPUT_DIR := $(intermediates)
+$(GEN): PRIVATE_CUSTOM_TOOL = \
+        $(PRIVATE_HIDL) -o $(PRIVATE_OUTPUT_DIR) \
+        -Ljava -randroid.hidl:system/libhidl \
+        android.hidl.manager@1.0::IServiceManager
+
+$(GEN): $(LOCAL_PATH)/IServiceManager.hal
+	$(transform-generated-source)
+LOCAL_GENERATED_SOURCES += $(GEN)
+
+#
+# Build IServiceNotification.hal
+#
+GEN := $(intermediates)/android/hidl/manager/1.0/IServiceNotification.java
+$(GEN): $(HIDL)
+$(GEN): PRIVATE_HIDL := $(HIDL)
+$(GEN): PRIVATE_DEPS := $(LOCAL_PATH)/IServiceNotification.hal
+$(GEN): PRIVATE_OUTPUT_DIR := $(intermediates)
+$(GEN): PRIVATE_CUSTOM_TOOL = \
+        $(PRIVATE_HIDL) -o $(PRIVATE_OUTPUT_DIR) \
+        -Ljava -randroid.hidl:system/libhidl \
+        android.hidl.manager@1.0::IServiceNotification
+
+$(GEN): $(LOCAL_PATH)/IServiceNotification.hal
+	$(transform-generated-source)
+LOCAL_GENERATED_SOURCES += $(GEN)
+include $(BUILD_JAVA_LIBRARY)
+
+
+################################################################################
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hidl.manager@1.0-java-static
+LOCAL_MODULE_CLASS := JAVA_LIBRARIES
+
+intermediates := $(local-generated-sources-dir)
+
+HIDL := $(HOST_OUT_EXECUTABLES)/hidl-gen$(HOST_EXECUTABLE_SUFFIX)
+
+#
+# Build IServiceManager.hal
+#
+GEN := $(intermediates)/android/hidl/manager/1.0/IServiceManager.java
+$(GEN): $(HIDL)
+$(GEN): PRIVATE_HIDL := $(HIDL)
+$(GEN): PRIVATE_DEPS := $(LOCAL_PATH)/IServiceManager.hal
+$(GEN): PRIVATE_DEPS += $(LOCAL_PATH)/IServiceNotification.hal
+$(GEN): $(LOCAL_PATH)/IServiceNotification.hal
+$(GEN): PRIVATE_OUTPUT_DIR := $(intermediates)
+$(GEN): PRIVATE_CUSTOM_TOOL = \
+        $(PRIVATE_HIDL) -o $(PRIVATE_OUTPUT_DIR) \
+        -Ljava -randroid.hidl:system/libhidl \
+        android.hidl.manager@1.0::IServiceManager
+
+$(GEN): $(LOCAL_PATH)/IServiceManager.hal
+	$(transform-generated-source)
+LOCAL_GENERATED_SOURCES += $(GEN)
+
+#
+# Build IServiceNotification.hal
+#
+GEN := $(intermediates)/android/hidl/manager/1.0/IServiceNotification.java
+$(GEN): $(HIDL)
+$(GEN): PRIVATE_HIDL := $(HIDL)
+$(GEN): PRIVATE_DEPS := $(LOCAL_PATH)/IServiceNotification.hal
+$(GEN): PRIVATE_OUTPUT_DIR := $(intermediates)
+$(GEN): PRIVATE_CUSTOM_TOOL = \
+        $(PRIVATE_HIDL) -o $(PRIVATE_OUTPUT_DIR) \
+        -Ljava -randroid.hidl:system/libhidl \
+        android.hidl.manager@1.0::IServiceNotification
+
+$(GEN): $(LOCAL_PATH)/IServiceNotification.hal
+	$(transform-generated-source)
+LOCAL_GENERATED_SOURCES += $(GEN)
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/manager/1.0/IServiceManager.hal b/manager/1.0/IServiceManager.hal
index 976fca4..1efaa28 100644
--- a/manager/1.0/IServiceManager.hal
+++ b/manager/1.0/IServiceManager.hal
@@ -16,6 +16,8 @@
 
 package android.hidl.manager@1.0;
 
+import IServiceNotification;
+
 /**
  * Manages all the hidl hals on a device.
  *
@@ -35,7 +37,7 @@
 interface IServiceManager {
 
     /**
-     * Retrieve an existing service.
+     * Retrieve an existing service that supports the requested version.
      *
      * WARNING: This function is for libhidl/HwBinder use only. You are likely
      * looking for 'IMyInterface::getService("name")' instead.
@@ -44,12 +46,13 @@
      * @param name     Instance name. Same as in IServiceManager::add.
      *
      * @return service Handle to requested service, same as provided in
-     *                 IServiceManager::add.
+     *                 IServiceManager::add. Will be nullptr if not available.
      */
     get(string fqName, string name) generates (interface service);
 
     /**
-     * Register a service.
+     * Register a service. The service manager must be registered as all of the
+     * services that it inherits from.
      *
      * WARNING: This function is for libhidl/HwBinder use only. You are likely
      * looking for 'INTERFACE::registerAsService("name")' instead.
@@ -82,4 +85,24 @@
      */
     listByInterface(string fqName) generates (vec<string> instanceNames);
 
+    /**
+     * Register for service notifications for a particular service. Must support
+     * multiple registrations.
+     *
+     * onRegistration must be sent out for all services which support the
+     * version provided in the fqName. For instance, if a client registers for
+     * notifications from "android.hardware.foo@1.0", they must also get
+     * notifications from "android.hardware.foo@1.1".
+     *
+     * @param fqName   Fully-qualified interface name.
+     * @param name     Instance name. If name is empty, notifications must be
+     *                 sent out for all names.
+     * @param callback Client callback to recieve notifications.
+     *
+     * @return success Whether or not registration was successful.
+     */
+    registerForNotifications(string fqName,
+                             string name,
+                             IServiceNotification callback)
+        generates (bool success);
 };
\ No newline at end of file
diff --git a/manager/1.0/IServiceNotification.hal b/manager/1.0/IServiceNotification.hal
new file mode 100644
index 0000000..a6151e3
--- /dev/null
+++ b/manager/1.0/IServiceNotification.hal
@@ -0,0 +1,34 @@
+/*
+ * 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 IServiceNotification {
+
+    /**
+     * Must be called when a service is registered.
+     *
+     * @param fqInstanceName Fully-qualified instance name (see IServiceManager)
+     * @param preexisting    If true, this means the registration was
+     *                       pre-existing at the time this IServiceNotification
+     *                       object is itself registered. Otherwise, this means
+     *                       onRegistration is triggered by a newly registered
+     *                       service.
+     */
+    oneway onRegistration(string fqName, string name, bool preexisting);
+
+
+};
\ No newline at end of file
diff --git a/manager/1.0/update-makefiles.sh b/manager/1.0/update-makefiles.sh
new file mode 100755
index 0000000..f054795
--- /dev/null
+++ b/manager/1.0/update-makefiles.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+if [ ! -d system/libhidl ] ; then
+  echo "Where is system/libhidl?";
+  exit 1;
+fi
+
+hidl-gen -Lmakefile -r android.hidl:system/libhidl android.hidl.manager@1.0
+hidl-gen -Landroidbp -r android.hidl:system/libhidl android.hidl.manager@1.0
\ No newline at end of file