出于安全方面的考虑,不允许应用直接调用 Health HAL。在 Android 9 及更高版本中,Binder 服务 IBatteryPropertiesRegistrar 由 BatteryService(而非 healthd)提供。BatteryService 将调用委派给 Health HAL 以检索请求的信息。
BatteryService。在 Android 9 及更高版本中,BatteryService 使用 HealthServiceWrapper 来决定是使用来自 vendor 的默认 Health 服务实例还是使用来自 healthd 的备用 Health 服务实例。然后,BatteryService 通过 IHealth.registerCallback 监听健康事件。
Storaged。在 Android 9 及更高版本中,storaged 使用 libhealthhalutils 来决定是使用来自 vendor 的默认 Health 服务实例还是使用来自 healthd 的备用 Health 服务实例。然后,storaged 通过 IHealth.registerCallback 监听健康事件,并检索存储信息。
Health 2.1 中使用的某些内核接口(如 /sys/class/power_supply/*/capacity_level 和 /sys/class/power_supply/*/time_to_full_now)可能是可选的。不过,为了防止因缺少内核接口而导致错误的框架行为,建议您在构建 Health HAL 2.1 服务之前择优挑选 CL 1398913。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Implement Health 2.1\n\n| **Note:** The Health 2.1 HAL is deprecated. It is recommended to [implement the\n| health AIDL HAL](/docs/core/perf/health#aidl-impl) directly, even on devices launching before Android 11. Documentation for implementing Health 2.1 HAL is kept here for reference.\n\nIn Android 11, all `healthd` code is refactored into\n`libhealthloop` and `libhealth2impl`, then modified to implement the health@2.1\nHAL. These two libraries are linked statically by `health@2.0-impl-2.1`,\nthe passthrough implementation of Health 2.1. The statically linked libraries\nenable `health@2.0-impl-2.1` to do the same work as `healthd`, such as running\n`healthd_mainloop` and polling. In init, the `health@2.1-service` registers an\nimplementation of the interface `IHealth` to `hwservicemanager`. When upgrading\ndevices with an Android 8.x or 9\nvendor image and an Android 11 framework,\nthe vendor image might not provide the health@2.1 service. Backward\ncompatibility with old vendor images is enforced by the\n[deprecation schedule](/docs/core/architecture/vintf/fcm#hal-version-deprecation).\n\nTo ensure backwards compatibility:\n\n1. `healthd` registers `IHealth` to `hwservicemanager` despite being a system daemon. `IHealth` is added to the system manifest, with the instance name \"backup\".\n2. The framework and `storaged` communicate with `healthd` through `hwbinder` instead of `binder`.\n3. The code for framework and `storaged` are changed to fetch the instance \"default\" if available, then \"backup\".\n - C++ client code uses the logic defined in `libhealthhalutils`.\n - Java client code uses the logic defined in `HealthServiceWrapper`.\n4. After IHealth/default is widely available and Android 8.1 vendor images are deprecated, IHealth/backup and `healthd` can be deprecated.\n\nBoard-specific build variables for healthd\n------------------------------------------\n\n`BOARD_PERIODIC_CHORES_INTERVAL_*` are board-specific variables used to build\n`healthd`. As part of the system/vendor build split, board-specific values\n**cannot** be defined for system modules. These values used to be overridden\nin the deprecated function `healthd_board_init`.\n\nIn health@2.1, vendors can override\nthese two periodic chores interval values in the `healthd_config` struct before\npassing to the health implementation class constructor. The health\nimplementation class should inherit from\n`android::hardware::health::V2_1::implementation::Health`.\n\nImplement the Health 2.1 service\n--------------------------------\n\nFor information on implementing the Health 2.1 service, see\n[hardware/interfaces/health/2.1/README.md](https://android.googlesource.com/platform/hardware/interfaces/+/android16-release/health/2.1/README.md).\n\nHealth clients\n--------------\n\nhealth@2.x has the following clients:\n\n- **charger.** The use of `libbatterymonitor` and `healthd_common` code is wrapped in `health@2.0-impl`.\n- **recovery.** The linkage to `libbatterymonitor` is wrapped in `health@2.0-impl`. All calls to `BatteryMonitor` are replaced by calls into the `Health` implementation class.\n- **BatteryManager.** `BatteryManager.queryProperty(int id)` was the only\n client of `IBatteryPropertiesRegistrar.getProperty`.\n `IBatteryPropertiesRegistrar.getProperty` was provided by\n `healthd` and directly read `/sys/class/power_supply`.\n\n As a security consideration, apps aren't allowed to call into health HAL\n directly. In Android 9 and higher, the binder\n service `IBatteryPropertiesRegistrar` is provided by `BatteryService`\n instead of `healthd`. `BatteryService` delegates the call to the health HAL\n to retrieve the requested information.\n- **BatteryService.** In Android 9 and higher,\n `BatteryService` uses `HealthServiceWrapper` to determine whether to use the\n *default* health service instance from `vendor` or to use the *backup*\n health service instance from `healthd`. `BatteryService` then listens for\n health events through `IHealth.registerCallback`.\n\n- **Storaged.** In Android 9 and higher,\n `storaged` uses `libhealthhalutils` to determine whether to use the\n *default* health service instance from `vendor` or to use the *backup*\n health service instance from `healthd`. `storaged` then\n listens for health events through `IHealth.registerCallback` and retrieves\n storage information.\n\nSELinux changes\n---------------\n\nThe health@2.1 HAL includes the following SELinux changes in the platform:\n\n- Adds `android.hardware.health@2.1-service` to `file_contexts`.\n\nFor devices with their own implementation, some vendor SELinux changes may be\nnecessary. Example: \n\n # device/\u003cmanufacturer\u003e/\u003cdevice\u003e/sepolicy/vendor/hal_health_default.te\n # Add device specific permissions to hal_health_default domain, especially\n # if it links to board-specific libhealthd or implements storage APIs.\n\nKernel interfaces\n-----------------\n\nThe `healthd` daemon and the default implementation\n`android.hardware.health@2.0-impl-2.1` access the following kernel interfaces to\nretrieve battery information:\n\n- `/sys/class/power_supply/*/capacity_level` (added in Health 2.1)\n- `/sys/class/power_supply/*/capacity`\n- `/sys/class/power_supply/*/charge_counter`\n- `/sys/class/power_supply/*/charge_full`\n- `/sys/class/power_supply/*/charge_full_design` (added in Health 2.1)\n- `/sys/class/power_supply/*/current_avg`\n- `/sys/class/power_supply/*/current_max`\n- `/sys/class/power_supply/*/current_now`\n- `/sys/class/power_supply/*/cycle_count`\n- `/sys/class/power_supply/*/health`\n- `/sys/class/power_supply/*/online`\n- `/sys/class/power_supply/*/present`\n- `/sys/class/power_supply/*/status`\n- `/sys/class/power_supply/*/technology`\n- `/sys/class/power_supply/*/temp`\n- `/sys/class/power_supply/*/time_to_full_now` (added in Health 2.1)\n- `/sys/class/power_supply/*/type`\n- `/sys/class/power_supply/*/voltage_max`\n- `/sys/class/power_supply/*/voltage_now`\n\nAny device-specific health HAL implementation that uses `libbatterymonitor`\naccesses these kernel interfaces by default, unless overridden in the health\nimplementation class constructor.\n\nIf these files are missing or are inaccessible from `healthd` or from the\ndefault service (for example, the file is a symlink to a vendor-specific folder\nthat denies access because of misconfigured SELinux policy), they might not\nfunction correctly. So additional vendor-specific SELinux changes might be\nnecessary even though the default implementation is used.\n\nSome kernel interfaces used in Health 2.1, such as\n`/sys/class/power_supply/*/capacity_level` and\n`/sys/class/power_supply/*/time_to_full_now`, may be optional. However, to\nprevent incorrect framework behaviors resulting from missing kernel interfaces,\nit is recommended to cherry-pick\n[CL 1398913](https://r.android.com/1398913)\nbefore building the Health HAL 2.1 service.\n\nTesting\n-------\n\nAndroid 11 includes new\n[VTS tests](/docs/compatibility/vts)\nwritten specifically for the health@2.1 HAL. If a device declares\nhealth@2.1 HAL in the device manifest, it must pass the corresponding VTS tests.\nTests are written for both the default instance (to ensure that the device\nimplements the HAL correctly) and the backup instance (to ensure that `healthd`\ncontinues to function correctly before it is removed).\n\n### Battery information requirements\n\nThe Health 2.0 HAL states a set of requirements on the HAL interface, but the\ncorresponding VTS tests are relatively relaxed on enforcing them.\nIn Android 11, new VTS tests are added to enforce the\nfollowing requirements on devices launching with Android\n11 and higher:\n\n- The units of intataneous and average battery current must be microamps (μA).\n- The sign of instantaneous and average battery current must be correct. Specifically:\n - current == 0 when battery status is `UNKNOWN`\n - current \\\u003e 0 when battery status is `CHARGING`\n - current \\\u003c= 0 when battery status is `NOT_CHARGING`\n - current \\\u003c 0 when battery status is `DISCHARGING`\n - Not enforced when battery status is `FULL`\n- The battery status must be correct against whether or not a power source is connected. Specifically:\n - battery status must be one of `CHARGING`, `NOT_CHARGING`, or `FULL` if and only if a power source is connected;\n - battery status must be `DISCHARGING` if and only if a power source is disconnected.\n\nIf you use `libbatterymonitor` in your implementation and pass through values\nfrom kernel interfaces, ensure the sysfs nodes are reporting correct values:\n\n- Ensure the battery current is reported with the correct sign and units. This includes the following sysfs nodes:\n - `/sys/class/power_supply/*/current_avg`\n - `/sys/class/power_supply/*/current_max`\n - `/sys/class/power_supply/*/current_now`\n - Positive values indicate incoming current into the battery.\n - Values should be in microamps (μA).\n- Ensure the battery voltage is reported in microvolts (μV). This includes the following sysfs nodes:\n - `/sys/class/power_supply/*/voltage_max`\n - `/sys/class/power_supply/*/voltage_now`\n - Note that the default HAL implementation divides `voltage_now` by 1000 and reports values in millivolts (mV). See [@1.0::HealthInfo](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android10-release/health/1.0/types.hal).\n\nFor details, see\n[Linux power supply class](https://www.kernel.org/doc/Documentation/power/power_supply_class.txt)."]]