Health 2.1 中使用的部分核心介面 (例如 /sys/class/power_supply/*/capacity_level 和 /sys/class/power_supply/*/time_to_full_now) 可能為選用介面。不過,為避免因缺少核心介面而導致架構行為不正確,建議您先挑選 CL 1398913,再建構 Health HAL 2.1 服務。
[[["容易理解","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"]],["上次更新時間:2025-08-13 (世界標準時間)。"],[],[],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)."]]