Merge f1dda97a23b3945a13580b8d055a4d95a437a3bb on remote branch

Change-Id: Id6ba6cf2831d0a19e7222b6b72d7da6075606683
diff --git a/aidl/Android.bp b/aidl/Android.bp
new file mode 100644
index 0000000..63b07f2
--- /dev/null
+++ b/aidl/Android.bp
@@ -0,0 +1,46 @@
+cc_defaults {
+    name: "android.hardware.health-service.qti-defaults",
+    relative_install_path: "hw",
+    vintf_fragments: ["android.hardware.health-service.qti.xml"],
+    vendor: true,
+    recovery_available: true,
+
+    defaults: [
+        "libhealth_aidl_impl_user",
+    ],
+
+    include_dirs: [
+        "system/core/healthd",
+        "system/core/healthd/include",
+        "system/core/healthd/include_charger"
+    ],
+
+    static_libs: [
+        "libhealth_aidl_impl",
+    ],
+
+    srcs: [
+        "main.cpp",
+    ],
+
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+}
+
+cc_binary {
+    name: "android.hardware.health-service.qti",
+    vendor: true,
+    defaults: ["android.hardware.health-service.qti-defaults"],
+    init_rc: ["android.hardware.health-service.qti.rc"],
+    overrides: ["charger"],
+}
+
+cc_binary {
+    name: "android.hardware.health-service.qti_recovery",
+    vendor: true,
+    defaults: ["android.hardware.health-service.qti-defaults"],
+    init_rc: ["android.hardware.health-service.qti_recovery.rc"],
+    overrides: ["charger.recovery"],
+}
diff --git a/aidl/android.hardware.health-service.qti.rc b/aidl/android.hardware.health-service.qti.rc
new file mode 100644
index 0000000..a3b434a
--- /dev/null
+++ b/aidl/android.hardware.health-service.qti.rc
@@ -0,0 +1,14 @@
+service vendor.health-default /vendor/bin/hw/android.hardware.health-service.qti
+    class hal
+    user system
+    group system
+    capabilities WAKE_ALARM BLOCK_SUSPEND
+    file /dev/kmsg w
+
+service vendor.charger /vendor/bin/hw/android.hardware.health-service.qti --charger
+    class charger
+    seclabel u:r:charger_vendor:s0
+    user system
+    group system wakelock input graphics
+    capabilities SYS_BOOT WAKE_ALARM BLOCK_SUSPEND
+    file /dev/kmsg w
diff --git a/aidl/android.hardware.health-service.qti.xml b/aidl/android.hardware.health-service.qti.xml
new file mode 100644
index 0000000..df9c7ac
--- /dev/null
+++ b/aidl/android.hardware.health-service.qti.xml
@@ -0,0 +1,12 @@
+<!--
+  Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+  SPDX-License-Identifier: BSD-3-Clause-Clear
+-->
+
+<manifest version="1.0" type="device">
+    <hal format="aidl">
+        <name>android.hardware.health</name>
+        <version>1</version>
+        <fqname>IHealth/default</fqname>
+    </hal>
+</manifest>
diff --git a/aidl/android.hardware.health-service.qti_recovery.rc b/aidl/android.hardware.health-service.qti_recovery.rc
new file mode 100644
index 0000000..f9a007a
--- /dev/null
+++ b/aidl/android.hardware.health-service.qti_recovery.rc
@@ -0,0 +1,7 @@
+service vendor.health-recovery /system/bin/hw/android.hardware.health-service.qti_recovery
+    class hal
+    seclabel u:r:hal_health_default:s0
+    user system
+    group system
+    capabilities WAKE_ALARM BLOCK_SUSPEND
+    file /dev/kmsg w
diff --git a/aidl/main.cpp b/aidl/main.cpp
new file mode 100644
index 0000000..943a537
--- /dev/null
+++ b/aidl/main.cpp
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
+#define LOG_TAG "android.hardware.health-service.qti"
+
+#include <android-base/logging.h>
+#include <android/binder_interface_utils.h>
+#include <health/utils.h>
+#include <health-impl/ChargerUtils.h>
+#include <health-impl/Health.h>
+#include <cutils/klog.h>
+
+using aidl::android::hardware::health::HalHealthLoop;
+using aidl::android::hardware::health::Health;
+
+#if !CHARGER_FORCE_NO_UI
+using aidl::android::hardware::health::charger::ChargerCallback;
+using aidl::android::hardware::health::charger::ChargerModeMain;
+#endif
+
+static constexpr const char* gInstanceName = "default";
+static constexpr std::string_view gChargerArg{"--charger"};
+
+constexpr char ucsiPSYName[]{"ucsi-source-psy-soc:qcom,pmic_glink:qcom,ucsi1"};
+
+#define RETRY_COUNT    100
+
+void qti_healthd_board_init(struct healthd_config *hc)
+{
+    int fd;
+    unsigned char retries = RETRY_COUNT;
+    int ret = 0;
+    unsigned char buf;
+
+    hc->ignorePowerSupplyNames.push_back(android::String8(ucsiPSYName));
+retry:
+    if (!retries) {
+        KLOG_ERROR(LOG_TAG, "Cannot open battery/capacity, fd=%d\n", fd);
+        return;
+    }
+
+    fd = open("/sys/class/power_supply/battery/capacity", 0440);
+    if (fd >= 0) {
+        KLOG_INFO(LOG_TAG, "opened battery/capacity after %d retries\n", RETRY_COUNT - retries);
+        while (retries) {
+            ret = read(fd, &buf, 1);
+            if(ret >= 0) {
+                KLOG_INFO(LOG_TAG, "Read Batt Capacity after %d retries ret : %d\n", RETRY_COUNT - retries, ret);
+                close(fd);
+                return;
+            }
+
+            retries--;
+            usleep(100000);
+        }
+
+        KLOG_ERROR(LOG_TAG, "Failed to read Battery Capacity ret=%d\n", ret);
+        close(fd);
+        return;
+    }
+
+    retries--;
+    usleep(100000);
+    goto retry;
+}
+
+int main(int argc, char** argv) {
+#ifdef __ANDROID_RECOVERY__
+    android::base::InitLogging(argv, android::base::KernelLogger);
+#endif
+    auto config = std::make_unique<healthd_config>();
+    qti_healthd_board_init(config.get());
+    ::android::hardware::health::InitHealthdConfig(config.get());
+    auto binder = ndk::SharedRefBase::make<Health>(gInstanceName, std::move(config));
+
+    if (argc >= 2 && argv[1] == gChargerArg) {
+#if !CHARGER_FORCE_NO_UI
+        KLOG_INFO(LOG_TAG, "Starting charger mode with UI.");
+        return ChargerModeMain(binder, std::make_shared<ChargerCallback>(binder));
+#endif
+        KLOG_INFO(LOG_TAG, "Starting charger mode without UI.");
+    } else {
+        KLOG_INFO(LOG_TAG, "Starting health HAL.");
+    }
+
+    auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, binder);
+    return hal_health_loop->StartLoop();
+}
diff --git a/health-vendor-product.mk b/health-vendor-product.mk
new file mode 100644
index 0000000..d488b82
--- /dev/null
+++ b/health-vendor-product.mk
@@ -0,0 +1,2 @@
+PRODUCT_PACKAGES += android.hardware.health@2.1-impl-qti
+PRODUCT_PACKAGES += android.hardware.health@2.1-service