health: Introduce batteryless aidl HAL

Change-Id: I9877cb3cd4a6b55a59249aeae58060e616d2cd28
diff --git a/health/aidl/default/Android.bp b/health/aidl/default/Android.bp
index 3d91cd8..0b4f5cd 100644
--- a/health/aidl/default/Android.bp
+++ b/health/aidl/default/Android.bp
@@ -1,9 +1,58 @@
 /*
- * Copyright (C) 2022-2023 The LineageOS Project
+ * Copyright (C) 2022-2024 The LineageOS Project
  *
  * SPDX-License-Identifier: Apache-2.0
  */
 
+cc_defaults {
+    name: "android.hardware.health-service.batteryless-defaults",
+    relative_install_path: "hw",
+    vintf_fragments: ["android.hardware.health-service.batteryless.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: [
+        "service-batteryless.cpp",
+    ],
+
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+}
+
+cc_binary {
+    name: "android.hardware.health-service.batteryless",
+    recovery: false,
+    vendor: true,
+    defaults: ["android.hardware.health-service.batteryless-defaults"],
+    init_rc: ["android.hardware.health-service.batteryless.rc"],
+    overrides: ["charger"],
+}
+
+cc_binary {
+    name: "android.hardware.health-service.batteryless_recovery",
+    vendor: false,
+    recovery: true,
+    defaults: ["android.hardware.health-service.batteryless-defaults"],
+    init_rc: ["android.hardware.health-service.batteryless_recovery.rc"],
+    overrides: ["charger.recovery"],
+}
+
 cc_binary {
     name: "vendor.lineage.health-service.default",
     defaults: ["health_charging_control_defaults"],
diff --git a/health/aidl/default/android.hardware.health-service.batteryless.rc b/health/aidl/default/android.hardware.health-service.batteryless.rc
new file mode 100644
index 0000000..0e874f3
--- /dev/null
+++ b/health/aidl/default/android.hardware.health-service.batteryless.rc
@@ -0,0 +1,6 @@
+service vendor.health-default /vendor/bin/hw/android.hardware.health-service.batteryless
+    class hal
+    user system
+    group system
+    capabilities WAKE_ALARM BLOCK_SUSPEND
+    file /dev/kmsg w
diff --git a/health/aidl/default/android.hardware.health-service.batteryless.xml b/health/aidl/default/android.hardware.health-service.batteryless.xml
new file mode 100644
index 0000000..e8abb3c
--- /dev/null
+++ b/health/aidl/default/android.hardware.health-service.batteryless.xml
@@ -0,0 +1,13 @@
+<!--
+  Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+  Copyright (C) 2024 The LineageOS Project
+  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/health/aidl/default/android.hardware.health-service.batteryless_recovery.rc b/health/aidl/default/android.hardware.health-service.batteryless_recovery.rc
new file mode 100644
index 0000000..3ed4902
--- /dev/null
+++ b/health/aidl/default/android.hardware.health-service.batteryless_recovery.rc
@@ -0,0 +1,7 @@
+service vendor.health-recovery /system/bin/hw/android.hardware.health-service.batteryless_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/health/aidl/default/service-batteryless.cpp b/health/aidl/default/service-batteryless.cpp
new file mode 100644
index 0000000..a8d1c92
--- /dev/null
+++ b/health/aidl/default/service-batteryless.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * Copyright (C) 2024 The LineageOS Project
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
+#define LOG_TAG "android.hardware.health-service.batteryless"
+
+#include <android-base/logging.h>
+#include <android/binder_interface_utils.h>
+#include <cutils/klog.h>
+#include <health-impl/ChargerUtils.h>
+#include <health-impl/Health.h>
+#include <health/utils.h>
+
+using aidl::android::hardware::health::BatteryHealth;
+using aidl::android::hardware::health::BatteryStatus;
+using aidl::android::hardware::health::HalHealthLoop;
+using aidl::android::hardware::health::Health;
+
+namespace aidl::android::hardware::health {
+class HealthImpl : public Health {
+  public:
+    HealthImpl(std::string_view instance_name, std::unique_ptr<struct healthd_config>&& config)
+        : Health(instance_name, std::move(config)) {}
+
+  protected:
+    void UpdateHealthInfo(HealthInfo* health_info) override {
+        health_info->chargerAcOnline = true;
+        health_info->chargerUsbOnline = false;
+        health_info->chargerWirelessOnline = false;
+        health_info->chargerDockOnline = false;
+        health_info->batteryStatus = BatteryStatus::UNKNOWN;
+        health_info->batteryHealth = BatteryHealth::UNKNOWN;
+        health_info->batteryPresent = false;
+        health_info->batteryLevel = 100;
+    }
+};
+}  // namespace aidl::android::hardware::health
+
+int main() {
+#ifdef __ANDROID_RECOVERY__
+    android::base::InitLogging(NULL, android::base::KernelLogger);
+#endif
+    auto config = std::make_unique<healthd_config>();
+    ::android::hardware::health::InitHealthdConfig(config.get());
+    auto binder = ndk::SharedRefBase::make<aidl::android::hardware::health::HealthImpl>(
+            "default", std::move(config));
+
+    KLOG_INFO(LOG_TAG, "Starting health HAL.");
+    auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, binder);
+    return hal_health_loop->StartLoop();
+}