• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Upgrading from Health 1.0 HAL
2
31. Remove `android.hardware.health@1.0*` from `PRODUCT_PACKAGES`
4   in `device/<manufacturer>/<device>/device.mk`
5
61. If the device does not have a vendor-specific `libhealthd` AND does not
7   implement storage-related APIs, just do the following:
8
9   ```mk
10   PRODUCT_PACKAGES += android.hardware.health@2.0-service
11   ```
12
13   Otherwise, continue to the next step.
14
151. Create directory
16   `device/<manufacturer>/<device>/health`
17
181. Create `device/<manufacturer>/<device>/health/Android.bp`
19   (or equivalent `device/<manufacturer>/<device>/health/Android.mk`)
20
21    ```bp
22    cc_binary {
23        name: "android.hardware.health@2.0-service.<device>",
24        init_rc: ["android.hardware.health@2.0-service.<device>.rc"],
25        proprietary: true,
26        relative_install_path: "hw",
27        srcs: [
28            "HealthService.cpp",
29        ],
30
31        cflags: [
32            "-Wall",
33            "-Werror",
34        ],
35
36        static_libs: [
37            "android.hardware.health@2.0-impl",
38            "android.hardware.health@1.0-convert",
39            "libhealthservice",
40            "libbatterymonitor",
41        ],
42
43        shared_libs: [
44            "libbase",
45            "libcutils",
46            "libhidlbase",
47            "libhidltransport",
48            "libutils",
49            "android.hardware.health@2.0",
50        ],
51
52        header_libs: ["libhealthd_headers"],
53
54        overrides: [
55            "healthd",
56        ],
57    }
58    ```
59
60    1. (recommended) To remove `healthd` from the build, keep "overrides" section.
61    1. To keep `healthd` in the build, remove "overrides" section.
62
631. Create `device/<manufacturer>/<device>/health/android.hardware.health@2.0-service.<device>.rc`
64
65    ```rc
66    service vendor.health-hal-2-0 /vendor/bin/hw/android.hardware.health@2.0-service.<device>
67        class hal
68        user system
69        group system
70        capabilities WAKE_ALARM
71        file /dev/kmsg w
72    ```
73
741. Create `device/<manufacturer>/<device>/health/HealthService.cpp`:
75
76    ```c++
77    #include <health2/service.h>
78    int main() { return health_service_main(); }
79    ```
80
811. `libhealthd` dependency:
82
83    1. If the device has a vendor-specific `libhealthd.<soc>`, add it to static_libs.
84
85    1. If the device does not have a vendor-specific `libhealthd`, add the following
86        lines to `HealthService.cpp`:
87
88        ```c++
89        #include <healthd/healthd.h>
90        void healthd_board_init(struct healthd_config*) {}
91
92        int healthd_board_battery_update(struct android::BatteryProperties*) {
93            // return 0 to log periodic polled battery status to kernel log
94            return 0;
95        }
96        ```
97
981. Storage related APIs:
99
100    1. If the device does not implement `IHealth.getDiskStats` and
101        `IHealth.getStorageInfo`, add `libhealthstoragedefault` to `static_libs`.
102
103    1. If the device implements one of these two APIs, add and implement the
104        following functions in `HealthService.cpp`:
105
106        ```c++
107        void get_storage_info(std::vector<struct StorageInfo>& info) {
108            // ...
109        }
110        void get_disk_stats(std::vector<struct DiskStats>& stats) {
111            // ...
112        }
113        ```
114
1151. Update necessary SELinux permissions. For example,
116
117    ```
118    # device/<manufacturer>/<device>/sepolicy/vendor/file_contexts
119    /vendor/bin/hw/android\.hardware\.health@2\.0-service\.<device> u:object_r:hal_health_default_exec:s0
120
121    # device/<manufacturer>/<device>/sepolicy/vendor/hal_health_default.te
122    # Add device specific permissions to hal_health_default domain, especially
123    # if a device-specific libhealthd is used and/or device-specific storage related
124    # APIs are implemented.
125    ```
126
1271. Implementing health HAL in recovery. The health HAL is used for battery
128status checks during OTA for non-A/B devices. If the health HAL is not
129implemented in recovery, `is_battery_ok()` will always return `true`.
130
131    1. If the device does not have a vendor-specific `libhealthd`, nothing needs to
132    be done. A "backup" implementation is provided in
133    `android.hardware.health@2.0-impl-default`, which is always installed to recovery
134    image by default.
135
136    1. If the device does have a vendor-specific `libhealthd`, implement the following
137    module and include it in `PRODUCT_PACKAGES` (replace `<device>` with appropriate
138    strings):
139
140    ```bp
141    // Android.bp
142    cc_library_shared {
143        name: "android.hardware.health@2.0-impl-<device>",
144        recovery_available: true,
145        relative_install_path: "hw",
146        static_libs: [
147            "android.hardware.health@2.0-impl",
148            "libhealthd.<device>"
149            // Include the following or implement device-specific storage APIs
150            "libhealthstoragedefault",
151        ],
152        srcs: [
153            "HealthImpl.cpp",
154        ],
155        overrides: [
156            "android.hardware.health@2.0-impl-default",
157        ],
158    }
159    ```
160
161    ```c++
162    // HealthImpl.cpp
163    #include <health2/Health.h>
164    #include <healthd/healthd.h>
165    using android::hardware::health::V2_0::IHealth;
166    using android::hardware::health::V2_0::implementation::Health;
167    extern "C" IHealth* HIDL_FETCH_IHealth(const char* name) {
168        const static std::string providedInstance{"default"};
169        if (providedInstance != name) return nullptr;
170        return Health::initInstance(&gHealthdConfig).get();
171    }
172    ```
173
174    ```mk
175    # device.mk
176    PRODUCT_PACKAGES += android.hardware.health@2.0-impl-<device>
177    ```
178