Merge changes from topic "health2-health1"

* changes:
  healthd connect to health@1.0 hal
  Refactor common code between healthd and health@2.0-service
diff --git a/healthd/Android.bp b/healthd/Android.bp
index 4b3274c..ed1413e 100644
--- a/healthd/Android.bp
+++ b/healthd/Android.bp
@@ -53,7 +53,10 @@
     init_rc: ["android.hardware.health@2.0-service.rc"],
     vendor: true,
     relative_install_path: "hw",
-    srcs: ["HealthService.cpp"],
+    srcs: [
+        "HealthServiceCommon.cpp",
+        "HealthServiceDefault.cpp",
+    ],
 
     cflags: ["-DHEALTH_INSTANCE_NAME=\"default\""],
 
@@ -77,7 +80,10 @@
 
 cc_binary {
     name: "healthd",
-    srcs: ["HealthService.cpp"],
+    srcs: [
+        "HealthServiceCommon.cpp",
+        "HealthServiceHealthd.cpp",
+    ],
     local_include_dirs: ["include"],
 
     cflags: ["-DHEALTH_INSTANCE_NAME=\"backup\""],
@@ -96,6 +102,7 @@
         "libhwbinder",
         "liblog",
         "libutils",
+        "android.hardware.health@1.0",
         "android.hardware.health@2.0",
     ],
 
diff --git a/healthd/HealthService.cpp b/healthd/HealthServiceCommon.cpp
similarity index 93%
rename from healthd/HealthService.cpp
rename to healthd/HealthServiceCommon.cpp
index e8a1a85..160db77 100644
--- a/healthd/HealthService.cpp
+++ b/healthd/HealthServiceCommon.cpp
@@ -44,21 +44,9 @@
     IPCThreadState::self()->handlePolledCommands();
 }
 
-// TODO(b/67463967): healthd_board_* functions should be removed in health@2.0
-int healthd_board_battery_update(struct android::BatteryProperties*)
-{
-    // return 0 to log periodic polled battery status to kernel log
-    return 0;
-}
-
 void healthd_mode_service_2_0_init(struct healthd_config* config) {
     LOG(INFO) << LOG_TAG << " Hal is starting up...";
 
-    // Implementation-defined init logic goes here.
-    // 1. config->periodic_chores_interval_* variables
-    // 2. config->battery*Path variables
-    // 3. config->energyCounter. In this implementation, energyCounter is not defined.
-
     configureRpcThreadpool(1, false /* callerWillJoin */);
     IPCThreadState::self()->disableBackgroundScheduling(true);
     IPCThreadState::self()->setupPolling(&gBinderFd);
@@ -68,6 +56,13 @@
             LOG(ERROR) << LOG_TAG << ": Register for binder events failed";
     }
 
+    // Implementation-defined init logic goes here.
+    // 1. config->periodic_chores_interval_* variables
+    // 2. config->battery*Path variables
+    // 3. config->energyCounter. In this implementation, energyCounter is not defined.
+    // TODO(b/68724651): healthd_board_* functions should be removed in health@2.0
+    healthd_board_init(config);
+
     gHealth = new ::android::hardware::health::V2_0::implementation::Health(config);
     CHECK_EQ(gHealth->registerAsService(HEALTH_INSTANCE_NAME), android::OK)
         << LOG_TAG << ": Failed to register HAL";
@@ -85,7 +80,6 @@
 }
 
 void healthd_mode_service_2_0_battery_update(struct android::BatteryProperties* prop) {
-
     // Implementation-defined update logic goes here. An implementation
     // can make modifications to prop before broadcasting it to all callbacks.
 
diff --git a/healthd/HealthServiceDefault.cpp b/healthd/HealthServiceDefault.cpp
new file mode 100644
index 0000000..42e76d9
--- /dev/null
+++ b/healthd/HealthServiceDefault.cpp
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#include <healthd/healthd.h>
+
+void healthd_board_init(struct healthd_config*) {
+    // use defaults
+}
+
+int healthd_board_battery_update(struct android::BatteryProperties*) {
+    // return 0 to log periodic polled battery status to kernel log
+    return 0;
+}
diff --git a/healthd/HealthServiceHealthd.cpp b/healthd/HealthServiceHealthd.cpp
new file mode 100644
index 0000000..72a446a
--- /dev/null
+++ b/healthd/HealthServiceHealthd.cpp
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2017 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 "healthd"
+#include <android-base/logging.h>
+
+#include <android/hardware/health/1.0/IHealth.h>
+#include <android/hardware/health/1.0/types.h>
+#include <hal_conversion.h>
+#include <healthd/healthd.h>
+#include <hidl/HidlTransportSupport.h>
+
+using android::OK;
+using android::NAME_NOT_FOUND;
+using android::hardware::health::V1_0::HealthConfig;
+using android::hardware::health::V1_0::HealthInfo;
+using android::hardware::health::V1_0::Result;
+using android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
+using android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
+using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
+using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
+
+using IHealthLegacy = android::hardware::health::V1_0::IHealth;
+
+static android::sp<IHealthLegacy> gHealth_1_0;
+
+static int healthd_board_get_energy_counter(int64_t* energy) {
+    if (gHealth_1_0 == nullptr) {
+        return NAME_NOT_FOUND;
+    }
+
+    Result result = Result::NOT_SUPPORTED;
+    gHealth_1_0->energyCounter([energy, &result](Result ret, int64_t energyOut) {
+        result = ret;
+        *energy = energyOut;
+    });
+
+    return result == Result::SUCCESS ? OK : NAME_NOT_FOUND;
+}
+
+void healthd_board_init(struct healthd_config* config) {
+    gHealth_1_0 = IHealthLegacy::getService();
+
+    if (gHealth_1_0 == nullptr) {
+        return;
+    }
+
+    HealthConfig halConfig{};
+    convertToHealthConfig(config, halConfig);
+    gHealth_1_0->init(halConfig, [config](const auto& halConfigOut) {
+        convertFromHealthConfig(halConfigOut, config);
+        // always redirect energy counter queries
+        config->energyCounter = healthd_board_get_energy_counter;
+    });
+    LOG(INFO) << LOG_TAG << ": redirecting calls to 1.0 health HAL";
+}
+
+// TODO(b/68724651): Move this function into healthd_mode_service_2_0_battery_update
+// with logthis returned.
+int healthd_board_battery_update(struct android::BatteryProperties* props) {
+    int logthis = 0;
+
+    if (gHealth_1_0 == nullptr) {
+        return logthis;
+    }
+
+    HealthInfo info;
+    convertToHealthInfo(props, info);
+    gHealth_1_0->update(info, [props, &logthis](int32_t ret, const auto& infoOut) {
+        logthis = ret;
+        convertFromHealthInfo(infoOut, props);
+    });
+
+    return logthis;
+}