health: switch to real implementation

Use the default implementation, we need a hook to fix the battery status
path as the PMIC fuel gauge doesn't actually expose the "status"
property, only the charger does.
diff --git a/device-common.mk b/device-common.mk
index d542303..2ed8452 100644
--- a/device-common.mk
+++ b/device-common.mk
@@ -164,8 +164,8 @@
 
 # Health
 PRODUCT_PACKAGES += \
-    android.hardware.health@2.1-impl-cuttlefish \
-    android.hardware.health@2.1-service
+    android.hardware.health@2.1-service \
+    android.hardware.health@2.1-impl-sdm845
 
 # mkbootimg host tool to build boot.img separately
 PRODUCT_HOST_PACKAGES := \
diff --git a/health/Android.bp b/health/Android.bp
new file mode 100644
index 0000000..aa42032
--- /dev/null
+++ b/health/Android.bp
@@ -0,0 +1,35 @@
+cc_library_shared {
+    name: "android.hardware.health@2.1-impl-sdm845",
+    stem: "android.hardware.health@2.0-impl-2.1-sdm845",
+
+    // Install to vendor and recovery.
+    vendor: true,
+    recovery_available: true,
+
+    relative_install_path: "hw",
+
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+        "android.hardware.health@2.1",
+        "android.hardware.health@2.0",
+    ],
+
+    static_libs: [
+        "android.hardware.health@1.0-convert",
+        "libbatterymonitor",
+        "libhealthloop",
+        "libhealth2impl",
+        // "libhealthd.<device>"
+    ],
+
+    srcs: [
+        "HealthImpl.cpp",
+    ],
+
+    // No vintf_fragments because both -impl and -service should have been
+    // installed.
+}
diff --git a/health/HealthImpl.cpp b/health/HealthImpl.cpp
new file mode 100644
index 0000000..1f7a9d1
--- /dev/null
+++ b/health/HealthImpl.cpp
@@ -0,0 +1,79 @@
+#include <memory>
+#include <string_view>
+
+#include <health/utils.h>
+#include <health2impl/Health.h>
+#include <hidl/Status.h>
+#include <log/log.h>
+
+using ::android::sp;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::hardware::health::InitHealthdConfig;
+using ::android::hardware::health::V2_1::IHealth;
+using ::android::hidl::base::V1_0::IBase;
+
+using namespace std::literals;
+
+namespace android {
+namespace hardware {
+namespace health {
+namespace V2_1 {
+namespace implementation {
+
+// android::hardware::health::V2_1::implementation::Health implements most
+// defaults. Uncomment functions that you need to override.
+class HealthImpl : public Health {
+  public:
+    HealthImpl(std::unique_ptr<healthd_config>&& config)
+        : Health(std::move(config)) {}
+
+    // A subclass can override this if these information should be retrieved
+    // differently.
+    // Return<void> getChargeCounter(getChargeCounter_cb _hidl_cb) override;
+    // Return<void> getCurrentNow(getCurrentNow_cb _hidl_cb) override;
+    // Return<void> getCurrentAverage(getCurrentAverage_cb _hidl_cb) override;
+    // Return<void> getCapacity(getCapacity_cb _hidl_cb) override;
+    // Return<void> getEnergyCounter(getEnergyCounter_cb _hidl_cb) override;
+    //Return<void> getChargeStatus(getChargeStatus_cb _hidl_cb) override;
+    // Return<void> getStorageInfo(getStorageInfo_cb _hidl_cb) override;
+    // Return<void> getDiskStats(getDiskStats_cb _hidl_cb) override;
+    // Return<void> getHealthInfo(getHealthInfo_cb _hidl_cb) override;
+
+    // Functions introduced in Health HAL 2.1.
+    // Return<void> getHealthConfig(getHealthConfig_cb _hidl_cb) override;
+    // Return<void> getHealthInfo_2_1(getHealthInfo_2_1_cb _hidl_cb) override;
+    // Return<void> shouldKeepScreenOn(shouldKeepScreenOn_cb _hidl_cb) override;
+
+  protected:
+    // A subclass can override this to modify any health info object before
+    // returning to clients. This is similar to healthd_board_battery_update().
+    // By default, it does nothing.
+    // void UpdateHealthInfo(HealthInfo* health_info) override;
+};
+
+}  // namespace implementation
+}  // namespace V2_1
+}  // namespace health
+}  // namespace hardware
+}  // namespace android
+
+void healthd_board_init(healthd_config *config) {
+        // The battery doesn't expose it's status, only the charger does.
+        config->batteryStatusPath = "/sys/class/power_supply/usb/status";
+}
+
+extern "C" IHealth* HIDL_FETCH_IHealth(const char* instance) {
+    using ::android::hardware::health::V2_1::implementation::HealthImpl;
+    if (instance != "default") {
+        return nullptr;
+    }
+    auto config = std::make_unique<healthd_config>();
+    InitHealthdConfig(config.get());
+
+    healthd_board_init(config.get());
+
+    ALOGI("CA: Creating Health HAL 2.1, status path = %s", config->batteryStatusPath.c_str());
+
+    return new HealthImpl(std::move(config));
+}
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index 4764120..2ef9d0b 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -1,14 +1,16 @@
-/dev/block/platform/soc@0/1d84000\.ufshc/by-name/fsc		u:object_r:modem_block_device:s0
-/dev/block/platform/soc@0/1d84000\.ufshc/by-name/fsg		u:object_r:modem_block_device:s0
-/dev/block/platform/soc@0/1d84000\.ufshc/by-name/modemst[12]	u:object_r:modem_block_device:s0
-/dev/block/platform/soc@0/1d84000\.ufshc/by-name/userdata	u:object_r:userdata_block_device:s0
+/dev/block/platform/soc@0/1d84000\.ufshc/by-name/fsc					u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/1d84000\.ufshc/by-name/fsg					u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/1d84000\.ufshc/by-name/modemst[12]				u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/1d84000\.ufshc/by-name/userdata				u:object_r:userdata_block_device:s0
 
-/dev/dri				u:object_r:dri_device:s0
-/dev/dri/card0				u:object_r:graphics_device:s0
-/dev/dri/renderD128			u:object_r:gpu_device:s0
-/dev/input/.*               u:object_r:input_device:s0
-/dev/qcom_rmtfs_mem1			u:object_r:rmtfs_device:s0
-/dev/ttyMSM0				u:object_r:console_device:s0
+/dev/dri										u:object_r:dri_device:s0
+/dev/dri/card0										u:object_r:graphics_device:s0
+/dev/dri/renderD128									u:object_r:gpu_device:s0
+/dev/input/.*										u:object_r:input_device:s0
+/dev/qcom_rmtfs_mem1									u:object_r:rmtfs_device:s0
+/dev/ttyMSM0										u:object_r:console_device:s0
+
+/sys/(/.*)?										u:object_r:sysfs:s0
 
 /sys/bus/platform/drivers/qcom-q6v5-mss							u:object_r:sysfs_mss:s0
 /sys/devices/platform/f6301000.memory/rmtfs						u:object_r:sysfs_rmtfs:s0
@@ -17,7 +19,9 @@
 /sys/devices/platform/soc@0/ae00000.mdss						u:object_r:sysfs_gpu:s0
 /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/rtc	u:object_r:sysfs_rtc:s0
 
-/sys/class/backlight(/.*)?									u:object_r:sysfs_lights:s0
+/sys/class/backlight(/.*)?								u:object_r:sysfs_lights:s0
+
+/sys/class/power_supply(/.*)?								u:object_r:sysfs_power:s0
 
 /sys/class/remoteproc									u:object_r:sysfs_remoteproc:s0
 /sys/devices/platform/remoteproc-adsp/remoteproc					u:object_r:sysfs_remoteproc:s0
@@ -31,7 +35,7 @@
 
 /vendor/bin/hw/android\.hardware\.gatekeeper@1\.0-service\.software			u:object_r:hal_gatekeeper_default_exec:s0
 /vendor/bin/hw/android\.hardware\.graphics\.allocator@4\.0-service\.minigbm_msm		u:object_r:hal_graphics_allocator_default_exec:s0
-/vendor/bin/hw/android\.hardware\.vibrator@1\.1-service\.sdm845		u:object_r:hal_vibrator_default_exec:s0
+/vendor/bin/hw/android\.hardware\.vibrator@1\.1-service\.sdm845				u:object_r:hal_vibrator_default_exec:s0
 /vendor/bin/hw/android\.hardware\.light@2\.0-service					u:object_r:hal_light_default_exec:s0
 /vendor/bin/pd-mapper									u:object_r:pd_mapper_exec:s0
 /vendor/bin/qrtr-cfg									u:object_r:qrtr_exec:s0
diff --git a/sepolicy/genfs_contexts b/sepolicy/genfs_contexts
index d2248d2..a7b7de6 100644
--- a/sepolicy/genfs_contexts
+++ b/sepolicy/genfs_contexts
@@ -5,3 +5,5 @@
 genfscon sysfs   /devices/platform/soc@0/4080000.remoteproc					u:object_r:sysfs_remoteproc:s0
 genfscon sysfs   /devices/platform/soc@0/ae00000.mdss						u:object_r:sysfs_gpu:s0
 genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000	u:object_r:sysfs_rtc:s0
+genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-02/c440000.spmi:pmic@2:fuel-gauge@4000/power_supply	u:object_r:sysfs_power:s0
+genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-02/c440000.spmi:pmic@2:smb2@1000/power_supply		u:object_r:sysfs_power:s0
diff --git a/sepolicy/hal_health_default.te b/sepolicy/hal_health_default.te
new file mode 100644
index 0000000..26a64ca
--- /dev/null
+++ b/sepolicy/hal_health_default.te
@@ -0,0 +1,5 @@
+
+allow hal_health_default sysfs_power:file { read open write getattr };
+allow hal_health_default sysfs:file { read open write };
+allow hal_health_default sysfs_power:lnk_file { read };
+allow hal_health_default sysfs_power:dir { open search read };