Coral/flame health HAL 2.1

Bug: b/137790244
Test: lshal debug (health service)

Change-Id: Id15d203bd791d0867972da98f6998610723b9332
Merged-In: Id15d203bd791d0867972da98f6998610723b9332
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 4c19260..d40c2d0 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -99,3 +99,9 @@
 # Remove generic atrace HAL
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/etc/init/android.hardware.atrace@1.0-service.rc)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/etc/vintf/manifest/android.hardware.atrace@1.0-service.xml)
+
+# Update to health HAL 2.1
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/bin/hw/android.hardware.health@2.0-service.coral)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/etc/init/android.hardware.health@2.0-service.coral.rc)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/bin/hw/android.hardware.health@2.1-service)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/etc/init/android.hardware.health@2.1-service.rc)
diff --git a/config.fs b/config.fs
index 592a62f..fbe2dd7 100644
--- a/config.fs
+++ b/config.fs
@@ -71,12 +71,6 @@
 group: AID_RADIO
 caps: NET_BIND_SERVICE
 
-[vendor/bin/hw/android.hardware.health@2.0-service.coral]
-mode: 0755
-user: AID_SYSTEM
-group: AID_SYSTEM
-caps: WAKE_ALARM
-
 [vendor/bin/loc_launcher]
 mode: 0755
 user:  AID_GPS
diff --git a/device.mk b/device.mk
index cd38229..a4e6fde 100644
--- a/device.mk
+++ b/device.mk
@@ -402,7 +402,8 @@
     android.hardware.usb@1.2-service.coral
 
 PRODUCT_PACKAGES += \
-    android.hardware.health@2.0-service.coral
+    android.hardware.health@2.1-impl-coral \
+    android.hardware.health@2.1-service
 
 # Storage health HAL
 PRODUCT_PACKAGES += \
diff --git a/health/Android.bp b/health/Android.bp
index 0f21a0e..b6a0a81 100644
--- a/health/Android.bp
+++ b/health/Android.bp
@@ -14,13 +14,14 @@
  * limitations under the License.
  */
 
-cc_binary {
-    name: "android.hardware.health@2.0-service.coral",
-    init_rc: ["android.hardware.health@2.0-service.coral.rc"],
+cc_library_shared {
+    name: "android.hardware.health@2.1-impl-coral",
+    stem: "android.hardware.health@2.0-impl-2.1-coral",
+
     proprietary: true,
     relative_install_path: "hw",
     srcs: [
-        "HealthService.cpp",
+        "Health.cpp",
     ],
 
     cflags: [
@@ -29,10 +30,10 @@
     ],
 
     static_libs: [
-        "android.hardware.health@2.0-impl",
         "android.hardware.health@1.0-convert",
-        "libhealthservice",
         "libbatterymonitor",
+        "libhealth2impl",
+        "libhealthloop",
     ],
 
     shared_libs: [
@@ -44,11 +45,6 @@
         "libpixelhealth",
         "libutils",
         "android.hardware.health@2.0",
-    ],
-
-    header_libs: ["libhealthd_headers"],
-
-    overrides: [
-        "healthd",
+        "android.hardware.health@2.1",
     ],
 }
diff --git a/health/HealthService.cpp b/health/Health.cpp
similarity index 63%
rename from health/HealthService.cpp
rename to health/Health.cpp
index 045d925..698b571 100644
--- a/health/HealthService.cpp
+++ b/health/Health.cpp
@@ -13,16 +13,17 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#define LOG_TAG "android.hardware.health@2.0-service.coral"
+#define LOG_TAG "android.hardware.health@2.1-impl-coral"
 #include <android-base/logging.h>
 
 #include <android-base/file.h>
 #include <android-base/parseint.h>
 #include <android-base/strings.h>
-#include <health2/Health.h>
-#include <health2/service.h>
-#include <healthd/healthd.h>
-#include <hidl/HidlTransportSupport.h>
+#include <android/hardware/health/2.0/types.h>
+#include <health2impl/Health.h>
+#include <health/utils.h>
+#include <hal_conversion.h>
+
 #include <pixelhealth/BatteryMetricsLogger.h>
 #include "pixelhealth/BatteryThermalControl.h"
 #include <pixelhealth/CycleCountBackupRestore.h>
@@ -36,9 +37,17 @@
 
 namespace {
 
+using namespace std::literals;
+
+using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
+using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
 using android::hardware::health::V2_0::DiskStats;
 using android::hardware::health::V2_0::StorageAttribute;
 using android::hardware::health::V2_0::StorageInfo;
+using android::hardware::health::V2_0::Result;
+using ::android::hardware::health::V2_1::IHealth;
+using android::hardware::health::InitHealthdConfig;
+
 using hardware::google::pixel::health::BatteryMetricsLogger;
 using hardware::google::pixel::health::BatteryThermalControl;
 using hardware::google::pixel::health::CycleCountBackupRestore;
@@ -101,14 +110,12 @@
   attr->name = kUFSName;
 }
 
-}  // anonymous namespace
-
-void healthd_board_init(struct healthd_config *hc) {
+void private_healthd_board_init(struct healthd_config *hc) {
   hc->ignorePowerSupplyNames.push_back(android::String8(kTCPMPSYName));
   ccBackupRestore.Restore();
 }
 
-int healthd_board_battery_update(struct android::BatteryProperties *props) {
+int private_healthd_board_battery_update(struct android::BatteryProperties *props) {
   deviceHealth.update(props);
   battThermalControl.updateThermalState(props);
   battMetricsLogger.logBatteryProperties(props);
@@ -117,7 +124,7 @@
   return 0;
 }
 
-void get_storage_info(std::vector<StorageInfo> &vec_storage_info) {
+void private_get_storage_info(std::vector<StorageInfo> &vec_storage_info) {
   vec_storage_info.resize(1);
   StorageInfo *storage_info = &vec_storage_info[0];
   fill_ufs_storage_attribute(&storage_info->attr);
@@ -129,7 +136,7 @@
   return;
 }
 
-void get_disk_stats(std::vector<DiskStats> &vec_stats) {
+void private_get_disk_stats(std::vector<DiskStats> &vec_stats) {
   vec_stats.resize(1);
   DiskStats *stats = &vec_stats[0];
   fill_ufs_storage_attribute(&stats->attr);
@@ -142,5 +149,75 @@
       stats->ioTicks >> stats->ioInQueue;
   return;
 }
+}  // anonymous namespace
 
-int main(void) { return health_service_main(); }
+namespace android {
+namespace hardware {
+namespace health {
+namespace V2_1 {
+namespace implementation {
+class HealthImpl : public Health {
+ public:
+  HealthImpl(std::unique_ptr<healthd_config>&& config)
+    : Health(std::move(config)) {}
+
+  Return<void> getStorageInfo(getStorageInfo_cb _hidl_cb) override;
+  Return<void> getDiskStats(getDiskStats_cb _hidl_cb) override;
+
+ protected:
+  void UpdateHealthInfo(HealthInfo* health_info) override;
+
+};
+
+void HealthImpl::UpdateHealthInfo(HealthInfo* health_info) {
+  struct BatteryProperties props;
+  convertFromHealthInfo(health_info->legacy.legacy, &props);
+  private_healthd_board_battery_update(&props);
+  convertToHealthInfo(&props, health_info->legacy.legacy);
+}
+
+Return<void> HealthImpl::getStorageInfo(getStorageInfo_cb _hidl_cb)
+{
+  std::vector<struct StorageInfo> info;
+  private_get_storage_info(info);
+  hidl_vec<struct StorageInfo> info_vec(info);
+  if (!info.size()) {
+      _hidl_cb(Result::NOT_SUPPORTED, info_vec);
+  } else {
+      _hidl_cb(Result::SUCCESS, info_vec);
+  }
+  return Void();
+}
+
+Return<void> HealthImpl::getDiskStats(getDiskStats_cb _hidl_cb)
+{
+  std::vector<struct DiskStats> stats;
+  private_get_disk_stats(stats);
+  hidl_vec<struct DiskStats> stats_vec(stats);
+  if (!stats.size()) {
+      _hidl_cb(Result::NOT_SUPPORTED, stats_vec);
+  } else {
+      _hidl_cb(Result::SUCCESS, stats_vec);
+  }
+  return Void();
+}
+
+}  // namespace implementation
+}  // namespace V2_1
+}  // namespace health
+}  // namespace hardware
+}  // namespace android
+
+extern "C" IHealth* HIDL_FETCH_IHealth(const char* instance) {
+  using ::android::hardware::health::V2_1::implementation::HealthImpl;
+  if (instance != "default"sv) {
+      return nullptr;
+  }
+  auto config = std::make_unique<healthd_config>();
+  InitHealthdConfig(config.get());
+
+  private_healthd_board_init(config.get());
+
+  return new HealthImpl(std::move(config));
+}
+
diff --git a/health/android.hardware.health@2.0-service.coral.rc b/health/android.hardware.health@2.0-service.coral.rc
deleted file mode 100644
index cce6b32..0000000
--- a/health/android.hardware.health@2.0-service.coral.rc
+++ /dev/null
@@ -1,5 +0,0 @@
-service vendor.health-hal-2-0 /vendor/bin/hw/android.hardware.health@2.0-service.coral
-    class hal
-    user system
-    group system
-    file /dev/kmsg w
diff --git a/init.hardware.rc b/init.hardware.rc
index e87b0d3..cb71809 100644
--- a/init.hardware.rc
+++ b/init.hardware.rc
@@ -165,6 +165,9 @@
     start modem_svc
     start vendor.per_mgr
 
+    # Health HAL
+    chown system system /sys/devices/virtual/thermal/tz-by-name/soc/mode
+
 on late-init
     setprop vendor.thermal.config thermal_info_config.json
     chown system system /sys/devices/virtual/thermal/tz-by-name/sdm-therm-monitor/trip_point_0_temp
diff --git a/manifest.xml b/manifest.xml
index 0eff43c..6b9c395 100644
--- a/manifest.xml
+++ b/manifest.xml
@@ -37,15 +37,6 @@
         </interface>
     </hal>
     <hal format="hidl">
-        <name>android.hardware.health</name>
-        <transport>hwbinder</transport>
-        <version>2.0</version>
-        <interface>
-            <name>IHealth</name>
-            <instance>default</instance>
-        </interface>
-    </hal>
-    <hal format="hidl">
         <name>android.hardware.audio</name>
         <transport>hwbinder</transport>
         <version>5.0</version>