platform_hardware_interfaces/health/2.0
Yifan Hong 2200cffdad health AIDL: add vts tests
This is a direct translation of the health 2.0
and 2.1 VTS tests.

Some slight modifications are made (e.g. use googlemock
matchers) to make the test code more readable.

Test: run on Pixel
Bug: 203245778

Change-Id: Ifc3799a04a6a09d0d53f400b60d19fbfe004a3e3
2021-10-29 15:51:42 -07:00
..
default [LSC] Add LOCAL_LICENSE_KINDS to hardware/interfaces 2021-02-21 20:36:55 -08:00
utils health: drop backup instance for non-recovery. 2021-10-19 12:50:11 -07:00
vts health AIDL: add vts tests 2021-10-29 15:51:42 -07:00
Android.bp [LSC] Add LOCAL_LICENSE_KINDS to hardware/interfaces 2021-02-21 20:36:55 -08:00
IHealth.hal Merge changes from topic "health_report_api" 2018-01-18 18:04:18 +00:00
IHealthInfoCallback.hal Modify IHealthInfoCallback interface to return V2.0 HealthInfo 2018-01-18 21:24:12 +00:00
README health: convert README to markdown format. 2018-08-08 11:06:51 -07:00
README.md health 2.1 implementation. 2019-10-30 11:15:24 -07:00
types.hal Modify IHealthInfoCallback interface to return V2.0 HealthInfo 2018-01-18 21:24:12 +00:00

Implement the 2.1 HAL instead!

It is strongly recommended that you implement the 2.1 HAL directly. See hardware/interfaces/health/2.1/README.md for more details.

Upgrading from Health 1.0 HAL

  1. Remove android.hardware.health@1.0* from PRODUCT_PACKAGES in device/<manufacturer>/<device>/device.mk

  2. If the device does not have a vendor-specific libhealthd AND does not implement storage-related APIs, just do the following:

    PRODUCT_PACKAGES += android.hardware.health@2.0-service
    

    Otherwise, continue to the next step.

  3. Create directory device/<manufacturer>/<device>/health

  4. Create device/<manufacturer>/<device>/health/Android.bp (or equivalent device/<manufacturer>/<device>/health/Android.mk)

    cc_binary {
        name: "android.hardware.health@2.0-service.<device>",
        init_rc: ["android.hardware.health@2.0-service.<device>.rc"],
        proprietary: true,
        relative_install_path: "hw",
        srcs: [
            "HealthService.cpp",
        ],
    
        cflags: [
            "-Wall",
            "-Werror",
        ],
    
        static_libs: [
            "android.hardware.health@2.0-impl",
            "android.hardware.health@1.0-convert",
            "libhealthservice",
            "libbatterymonitor",
        ],
    
        shared_libs: [
            "libbase",
            "libcutils",
            "libhidlbase",
            "libutils",
            "android.hardware.health@2.0",
        ],
    
        header_libs: ["libhealthd_headers"],
    
        overrides: [
            "healthd",
        ],
    }
    
    1. (recommended) To remove healthd from the build, keep "overrides" section.
    2. To keep healthd in the build, remove "overrides" section.
  5. Create device/<manufacturer>/<device>/health/android.hardware.health@2.0-service.<device>.rc

    service vendor.health-hal-2-0 /vendor/bin/hw/android.hardware.health@2.0-service.<device>
        class hal
        user system
        group system
        capabilities WAKE_ALARM
        file /dev/kmsg w
    
  6. Create device/<manufacturer>/<device>/health/HealthService.cpp:

    #include <health2/service.h>
    int main() { return health_service_main(); }
    
  7. libhealthd dependency:

    1. If the device has a vendor-specific libhealthd.<soc>, add it to static_libs.

    2. If the device does not have a vendor-specific libhealthd, add the following lines to HealthService.cpp:

      #include <healthd/healthd.h>
      void healthd_board_init(struct healthd_config*) {}
      
      int healthd_board_battery_update(struct android::BatteryProperties*) {
          // return 0 to log periodic polled battery status to kernel log
          return 0;
      }
      
  8. Storage related APIs:

    1. If the device does not implement IHealth.getDiskStats and IHealth.getStorageInfo, add libhealthstoragedefault to static_libs.

    2. If the device implements one of these two APIs, add and implement the following functions in HealthService.cpp:

      void get_storage_info(std::vector<struct StorageInfo>& info) {
          // ...
      }
      void get_disk_stats(std::vector<struct DiskStats>& stats) {
          // ...
      }
      
  9. Update necessary SELinux permissions. For example,

    # device/<manufacturer>/<device>/sepolicy/vendor/file_contexts
    /vendor/bin/hw/android\.hardware\.health@2\.0-service\.<device> u:object_r:hal_health_default_exec:s0
    
    # device/<manufacturer>/<device>/sepolicy/vendor/hal_health_default.te
    # Add device specific permissions to hal_health_default domain, especially
    # if a device-specific libhealthd is used and/or device-specific storage related
    # APIs are implemented.
    
  10. Implementing health HAL in recovery. The health HAL is used for battery status checks during OTA for non-A/B devices. If the health HAL is not implemented in recovery, is_battery_ok() will always return true.

    1. If the device does not have a vendor-specific libhealthd, nothing needs to be done. A "backup" implementation is provided in android.hardware.health@2.0-impl-default, which is always installed to recovery image by default.

    2. If the device does have a vendor-specific libhealthd, implement the following module and include it in PRODUCT_PACKAGES (replace <device> with appropriate strings):

    // Android.bp
    cc_library_shared {
        name: "android.hardware.health@2.0-impl-<device>",
        recovery_available: true,
        relative_install_path: "hw",
        static_libs: [
            "android.hardware.health@2.0-impl",
            "libhealthd.<device>"
            // Include the following or implement device-specific storage APIs
            "libhealthstoragedefault",
        ],
        srcs: [
            "HealthImpl.cpp",
        ],
        overrides: [
            "android.hardware.health@2.0-impl-default",
        ],
    }
    
    // HealthImpl.cpp
    #include <health2/Health.h>
    #include <healthd/healthd.h>
    using android::hardware::health::V2_0::IHealth;
    using android::hardware::health::V2_0::implementation::Health;
    extern "C" IHealth* HIDL_FETCH_IHealth(const char* name) {
        const static std::string providedInstance{"default"};
        if (providedInstance != name) return nullptr;
        return Health::initInstance(&gHealthdConfig).get();
    }
    
    # device.mk
    PRODUCT_PACKAGES += android.hardware.health@2.0-impl-<device>