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