health: add health HAL and it's default implementation.

The default Health HAL implementation continues to depend on
libhealthd.<target> BOARD_HAL_STATIC_LIBRARY in order to make
sure healthd continues to work.

New device specific Health HAL implentations don't need to
compile with healthd STATIC_HAL.

Test: Tested healthd with and without the static hal implementation
on Angler.

BUG: b/32724915

Change-Id: I25d439f24a4178666614faf196423ffc8ccafafb
Signed-off-by: Sandeep Patil <sspatil@google.com>
diff --git a/health/1.0/default/Android.mk b/health/1.0/default/Android.mk
new file mode 100644
index 0000000..324912e
--- /dev/null
+++ b/health/1.0/default/Android.mk
@@ -0,0 +1,37 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.health@1.0-impl
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_C_INCLUDES := system/core/healthd/include system/core/base/include
+LOCAL_SRC_FILES := \
+    Health.cpp \
+
+LOCAL_SHARED_LIBRARIES := \
+    libcutils \
+    libhidl \
+    libhwbinder \
+    liblog \
+    libutils \
+    android.hardware.health@1.0 \
+
+LOCAL_STATIC_LIBRARIES := android.hardware.health@1.0-convert
+
+LOCAL_HAL_STATIC_LIBRARIES := libhealthd
+
+include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.health@1.0-convert
+LOCAL_SRC_FILES := convert.cpp
+LOCAL_C_INCLUDES := system/core/healthd/include system/core/base/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_SHARED_LIBRARIES := \
+    libcutils \
+    libhidl \
+    libutils \
+    android.hardware.health@1.0 \
+
+include $(BUILD_STATIC_LIBRARY)
+
+include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/health/1.0/default/Health.cpp b/health/1.0/default/Health.cpp
new file mode 100644
index 0000000..1a02956
--- /dev/null
+++ b/health/1.0/default/Health.cpp
@@ -0,0 +1,93 @@
+/*
+ * 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 "health-hal"
+
+#include <Health.h>
+#include <include/hal_conversion.h>
+
+namespace android {
+namespace hardware {
+namespace health {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
+using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
+using ::android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
+using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
+
+// Methods from ::android::hardware::health::V1_0::IHealth follow.
+Return<void> Health::init(const HealthConfig& config, init_cb _hidl_cb)  {
+    struct healthd_config healthd_config = {};
+    HealthConfig configOut;
+
+    // To keep working with existing healthd static HALs,
+    // convert the new HealthConfig to the old healthd_config
+    // and back.
+
+    convertFromHealthConfig(config, &healthd_config);
+    healthd_board_init(&healthd_config);
+    mGetEnergyCounter = healthd_config.energyCounter;
+    convertToHealthConfig(&healthd_config, configOut);
+
+    _hidl_cb(configOut);
+
+    return Void();
+}
+
+Return<void> Health::update(const HealthInfo& info, update_cb _hidl_cb)  {
+    struct android::BatteryProperties p = {};
+    HealthInfo infoOut;
+
+    // To keep working with existing healthd static HALs,
+    // convert the new HealthInfo to android::Batteryproperties
+    // and back.
+
+    convertFromHealthInfo(info, &p);
+    int skipLogging = healthd_board_battery_update(&p);
+    convertToHealthInfo(&p, infoOut);
+
+    _hidl_cb(!!skipLogging, infoOut);
+
+    return Void();
+}
+
+Return<void> Health::energyCounter(energyCounter_cb _hidl_cb) {
+    int64_t energy = 0;
+    Result result = Result::NOT_SUPPORTED;
+
+    if (mGetEnergyCounter) {
+        int status = mGetEnergyCounter(&energy);
+        if (status == 0) {
+            result = Result::SUCCESS;
+        }
+    }
+
+    _hidl_cb(result, energy);
+
+   return Void();
+}
+
+IHealth* HIDL_FETCH_IHealth(const char* /* name */) {
+    return new Health();
+}
+
+} // namespace implementation
+}  // namespace V1_0
+}  // namespace health
+}  // namespace hardware
+}  // namespace android
diff --git a/health/1.0/default/Health.h b/health/1.0/default/Health.h
new file mode 100644
index 0000000..c05751f
--- /dev/null
+++ b/health/1.0/default/Health.h
@@ -0,0 +1,42 @@
+#ifndef HIDL_GENERATED_android_hardware_health_V1_0_Health_H_
+#define HIDL_GENERATED_android_hardware_health_V1_0_Health_H_
+
+#include <android/hardware/health/1.0/IHealth.h>
+#include <hidl/Status.h>
+#include <hidl/MQDescriptor.h>
+#include <healthd/healthd.h>
+#include <utils/String8.h>
+
+namespace android {
+namespace hardware {
+namespace health {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hardware::health::V1_0::HealthInfo;
+using ::android::hardware::health::V1_0::HealthConfig;
+using ::android::hardware::health::V1_0::IHealth;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::hidl_string;
+using ::android::sp;
+
+struct Health : public IHealth {
+    // Methods from ::android::hardware::health::V1_0::IHealth follow.
+    Return<void> init(const HealthConfig& config, init_cb _hidl_cb)  override;
+    Return<void> update(const HealthInfo& info, update_cb _hidl_cb)  override;
+    Return<void> energyCounter(energyCounter_cb _hidl_cb) override;
+private:
+    std::function<int(int64_t *)> mGetEnergyCounter;
+};
+
+extern "C" IHealth* HIDL_FETCH_IHealth(const char* name);
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace health
+}  // namespace hardware
+}  // namespace android
+
+#endif  // HIDL_GENERATED_android_hardware_health_V1_0_Health_H_
diff --git a/health/1.0/default/convert.cpp b/health/1.0/default/convert.cpp
new file mode 100644
index 0000000..7f1e3c4
--- /dev/null
+++ b/health/1.0/default/convert.cpp
@@ -0,0 +1,148 @@
+/*
+ * 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.
+ */
+
+#include "include/hal_conversion.h"
+
+namespace android {
+namespace hardware {
+namespace health {
+namespace V1_0 {
+namespace hal_conversion {
+
+void convertToHealthConfig(const struct healthd_config *hc, HealthConfig& config) {
+    config.periodicChoresIntervalFast = hc->periodic_chores_interval_fast;
+    config.periodicChoresIntervalSlow = hc->periodic_chores_interval_slow;
+
+    config.batteryStatusPath        = hc->batteryStatusPath.string();
+    config.batteryHealthPath        = hc->batteryHealthPath.string();
+    config.batteryPresentPath       = hc->batteryPresentPath.string();
+    config.batteryCapacityPath      = hc->batteryCapacityPath.string();
+    config.batteryVoltagePath       = hc->batteryVoltagePath.string();
+    config.batteryTemperaturePath   = hc->batteryTemperaturePath.string();
+    config.batteryTechnologyPath    = hc->batteryTechnologyPath.string();
+    config.batteryCurrentNowPath    = hc->batteryCurrentNowPath.string();
+    config.batteryCurrentAvgPath    = hc->batteryCurrentAvgPath.string();
+    config.batteryChargeCounterPath = hc->batteryChargeCounterPath.string();
+    config.batteryFullChargePath    = hc->batteryFullChargePath.string();
+    config.batteryCycleCountPath    = hc->batteryCycleCountPath.string();
+
+}
+
+void convertFromHealthConfig(const HealthConfig& c, struct healthd_config *hc) {
+    hc->periodic_chores_interval_fast = c.periodicChoresIntervalFast;
+    hc->periodic_chores_interval_slow = c.periodicChoresIntervalSlow;
+
+    hc->batteryStatusPath =
+        android::String8(c.batteryStatusPath.c_str(),
+                         c.batteryStatusPath.size());
+
+    hc->batteryHealthPath =
+        android::String8(c.batteryHealthPath.c_str(),
+                         c.batteryHealthPath.size());
+
+    hc->batteryPresentPath =
+        android::String8(c.batteryPresentPath.c_str(),
+                         c.batteryPresentPath.size());
+
+    hc->batteryCapacityPath =
+        android::String8(c.batteryCapacityPath.c_str(),
+                         c.batteryCapacityPath.size());
+
+    hc->batteryVoltagePath =
+        android::String8(c.batteryVoltagePath.c_str(),
+                         c.batteryVoltagePath.size());
+
+    hc->batteryTemperaturePath =
+        android::String8(c.batteryTemperaturePath.c_str(),
+                         c.batteryTemperaturePath.size());
+
+    hc->batteryTechnologyPath =
+        android::String8(c.batteryTechnologyPath.c_str(),
+                         c.batteryTechnologyPath.size());
+
+    hc->batteryCurrentNowPath =
+        android::String8(c.batteryCurrentNowPath.c_str(),
+                         c.batteryCurrentNowPath.size());
+
+    hc->batteryCurrentAvgPath =
+        android::String8(c.batteryCurrentAvgPath.c_str(),
+                         c.batteryCurrentNowPath.size());
+
+    hc->batteryChargeCounterPath =
+        android::String8(c.batteryChargeCounterPath.c_str(),
+                         c.batteryChargeCounterPath.size());
+
+    hc->batteryFullChargePath =
+        android::String8(c.batteryFullChargePath.c_str(),
+                         c.batteryFullChargePath.size());
+
+    hc->batteryCycleCountPath =
+        android::String8(c.batteryCycleCountPath.c_str(),
+                         c.batteryCycleCountPath.size());
+
+    // energyCounter is handled through special means so all calls to
+    // the function go across the HALs
+
+    // boot_min_cap - never used in Android (only in charger-mode).
+
+    // screen_on - never used in Android (only in charger mode).
+}
+
+void convertToHealthInfo(const struct android::BatteryProperties *p,
+                                 HealthInfo& info) {
+    info.chargerAcOnline        = p->chargerAcOnline;
+    info.chargerUsbOnline       = p->chargerUsbOnline;
+    info.chargerWirelessOnline  = p->chargerWirelessOnline;
+    info.maxChargingCurrent     = p->maxChargingCurrent;
+    info.maxChargingVoltage     = p->maxChargingVoltage;
+    info.batteryStatus          = static_cast<BatteryStatus>(p->batteryStatus);
+    info.batteryHealth          = static_cast<BatteryHealth>(p->batteryHealth);
+    info.batteryPresent         = p->batteryPresent;
+    info.batteryLevel           = p->batteryLevel;
+    info.batteryVoltage         = p->batteryVoltage;
+    info.batteryTemperature     = p->batteryTemperature;
+    info.batteryCurrent         = p->batteryCurrent;
+    info.batteryCycleCount      = p->batteryCycleCount;
+    info.batteryFullCharge      = p->batteryFullCharge;
+    info.batteryChargeCounter   = p->batteryChargeCounter;
+    info.batteryTechnology      = p->batteryTechnology;
+}
+
+void convertFromHealthInfo(const HealthInfo& info,
+                                   struct android::BatteryProperties *p) {
+    p->chargerAcOnline          = info.chargerAcOnline;
+    p->chargerUsbOnline         = info.chargerUsbOnline;
+    p->chargerWirelessOnline    = info.chargerWirelessOnline;
+    p->maxChargingCurrent       = info.maxChargingCurrent;
+    p->maxChargingVoltage       = info.maxChargingVoltage;
+    p->batteryStatus            = static_cast<int>(info.batteryStatus);
+    p->batteryHealth            = static_cast<int>(info.batteryHealth);
+    p->batteryPresent           = info.batteryPresent;
+    p->batteryLevel             = info.batteryLevel;
+    p->batteryVoltage           = info.batteryVoltage;
+    p->batteryTemperature       = info.batteryTemperature;
+    p->batteryCurrent           = info.batteryCurrent;
+    p->batteryCycleCount        = info.batteryCycleCount;
+    p->batteryFullCharge        = info.batteryFullCharge;
+    p->batteryChargeCounter     = info.batteryChargeCounter;
+    p->batteryTechnology        = android::String8(info.batteryTechnology.c_str());
+}
+
+} // namespace hal_conversion
+} // namespace V1_0
+} // namespace health
+} // namespace hardware
+} // namespace android
diff --git a/health/1.0/default/include/hal_conversion.h b/health/1.0/default/include/hal_conversion.h
new file mode 100644
index 0000000..a92b208
--- /dev/null
+++ b/health/1.0/default/include/hal_conversion.h
@@ -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.
+ */
+
+#ifndef HARDWARE_INTERFACES_HEALTH_V1_0_DEFAULT_INCLUDE_HAL_CONVERSION_H_
+#define HARDWARE_INTERFACES_HEALTH_V1_0_DEFAULT_INCLUDE_HAL_CONVERSION_H_
+
+#include <android/hardware/health/1.0/IHealth.h>
+#include <healthd/healthd.h>
+
+namespace android {
+namespace hardware {
+namespace health {
+namespace V1_0 {
+namespace hal_conversion {
+
+void convertToHealthConfig(const struct healthd_config *hc,
+                            HealthConfig& config);
+void convertFromHealthConfig(const HealthConfig& c, struct healthd_config *hc);
+
+void convertToHealthInfo(const struct android::BatteryProperties *p,
+                                 HealthInfo& info);
+void convertFromHealthInfo(const HealthInfo& info,
+                                 struct android::BatteryProperties *p);
+
+}  // namespace hal_conversion
+}  // namespace V1_0
+}  // namespace sensors
+}  // namespace hardware
+}  // namespace android
+
+#endif  // HARDWARE_INTERFACES_HEALTH_V1_0_DEFAULT_INCLUDE_HAL_CONVERSION_H_
diff --git a/health/1.0/default/libhealthd/Android.mk b/health/1.0/default/libhealthd/Android.mk
new file mode 100644
index 0000000..a5f4445
--- /dev/null
+++ b/health/1.0/default/libhealthd/Android.mk
@@ -0,0 +1,10 @@
+# Copyright 2016 The Android Open Source Project
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := healthd_board_default.cpp
+LOCAL_MODULE := libhealthd.default
+LOCAL_CFLAGS := -Werror
+LOCAL_C_INCLUDES := system/core/healthd/include system/core/base/include
+include $(BUILD_STATIC_LIBRARY)
diff --git a/health/1.0/default/libhealthd/healthd_board_default.cpp b/health/1.0/default/libhealthd/healthd_board_default.cpp
new file mode 100644
index 0000000..127f98e
--- /dev/null
+++ b/health/1.0/default/libhealthd/healthd_board_default.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2013 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;
+}