1*4d7e907cSAndroid Build Coastguard Worker# Health AIDL HAL 2*4d7e907cSAndroid Build Coastguard Worker 3*4d7e907cSAndroid Build Coastguard Worker## Determine whether the example service implementation is sufficient {#determine} 4*4d7e907cSAndroid Build Coastguard Worker 5*4d7e907cSAndroid Build Coastguard WorkerYou need a custom implementation if any of the following is true: 6*4d7e907cSAndroid Build Coastguard Worker 7*4d7e907cSAndroid Build Coastguard Worker* You are migrating from a custom 8*4d7e907cSAndroid Build Coastguard Worker [health 2.1 HIDL HAL implementation](../2.1/README.md). 9*4d7e907cSAndroid Build Coastguard Worker* System properties `ro.charger.enable_suspend` and/or `ro.charger.no_ui` 10*4d7e907cSAndroid Build Coastguard Worker are set to a `true` value. See [below](#charger-sysprops). 11*4d7e907cSAndroid Build Coastguard Worker* The device supports offline charging mode, and the `service` 12*4d7e907cSAndroid Build Coastguard Worker declaration with `class charger` in `init.rc` is different from the one 13*4d7e907cSAndroid Build Coastguard Worker provided by the example implementation. See [below](#charger-init-rc). 14*4d7e907cSAndroid Build Coastguard Worker 15*4d7e907cSAndroid Build Coastguard WorkerIf the example HAL service is sufficient, [install it](#use-example). Otherwise, 16*4d7e907cSAndroid Build Coastguard Worker[implement a custom HAL service](#use-custom). 17*4d7e907cSAndroid Build Coastguard Worker 18*4d7e907cSAndroid Build Coastguard Worker### System properties for charger {#charger-sysprops} 19*4d7e907cSAndroid Build Coastguard Worker 20*4d7e907cSAndroid Build Coastguard WorkerThe health AIDL HAL service also provides functionalities of `charger`. As a 21*4d7e907cSAndroid Build Coastguard Workerresult, the system charger at `/system/bin/charger` is deprecated. 22*4d7e907cSAndroid Build Coastguard Worker 23*4d7e907cSAndroid Build Coastguard WorkerHowever, the health AIDL HAL service is not allowed to read `ro.charger.*` 24*4d7e907cSAndroid Build Coastguard Workersystem properties. These properties include: 25*4d7e907cSAndroid Build Coastguard Worker* `ro.charger.enable_suspend`. If set, you need a custom health AIDL HAL 26*4d7e907cSAndroid Build Coastguard Worker service. See [below](#charger-enable-suspend). 27*4d7e907cSAndroid Build Coastguard Worker* `ro.charger.no_ui`. If set, you need a custom health AIDL HAL service. 28*4d7e907cSAndroid Build Coastguard Worker See [below](#charger-no-ui). 29*4d7e907cSAndroid Build Coastguard Worker* `ro.charger.draw_split_screen`. The system property is deprecated. 30*4d7e907cSAndroid Build Coastguard Worker* `ro.charger.draw_split_offset`. The system property is deprecated. 31*4d7e907cSAndroid Build Coastguard Worker* `ro.charger.disable_init_blank`. The system property is deprecated. 32*4d7e907cSAndroid Build Coastguard Worker 33*4d7e907cSAndroid Build Coastguard WorkerIf you need to set any of the deprecated system properties, contact 34*4d7e907cSAndroid Build Coastguard Worker[OWNERS](OWNERS). 35*4d7e907cSAndroid Build Coastguard Worker 36*4d7e907cSAndroid Build Coastguard Worker### Default `service` declaration for charger in `init.rc` {#charger-init-rc} 37*4d7e907cSAndroid Build Coastguard Worker 38*4d7e907cSAndroid Build Coastguard WorkerSee 39*4d7e907cSAndroid Build Coastguard Worker[android.hardware.health-service.example.rc](default/android.hardware.health-service.example.rc). 40*4d7e907cSAndroid Build Coastguard Worker 41*4d7e907cSAndroid Build Coastguard WorkerCheck the `service` declaration in your device-specific `init.rc` file that 42*4d7e907cSAndroid Build Coastguard Workerhas `class charger`. Most likely, the declaration looks something like this 43*4d7e907cSAndroid Build Coastguard Worker(Below is an excerpt from Pixel 3): 44*4d7e907cSAndroid Build Coastguard Worker 45*4d7e907cSAndroid Build Coastguard Worker```text 46*4d7e907cSAndroid Build Coastguard Workerservice vendor.charger /system/bin/charger 47*4d7e907cSAndroid Build Coastguard Worker class charger 48*4d7e907cSAndroid Build Coastguard Worker seclabel u:r:charger:s0 49*4d7e907cSAndroid Build Coastguard Worker user system 50*4d7e907cSAndroid Build Coastguard Worker group system wakelock input 51*4d7e907cSAndroid Build Coastguard Worker capabilities SYS_BOOT 52*4d7e907cSAndroid Build Coastguard Worker file /dev/kmsg w 53*4d7e907cSAndroid Build Coastguard Worker file /sys/fs/pstore/console-ramoops-0 r 54*4d7e907cSAndroid Build Coastguard Worker file /sys/fs/pstore/console-ramoops r 55*4d7e907cSAndroid Build Coastguard Worker file /proc/last_kmsg r 56*4d7e907cSAndroid Build Coastguard Worker``` 57*4d7e907cSAndroid Build Coastguard Worker 58*4d7e907cSAndroid Build Coastguard WorkerCompare each line against the one provided by the example health AIDL HAL 59*4d7e907cSAndroid Build Coastguard Workerservice in 60*4d7e907cSAndroid Build Coastguard Worker[android.hardware.health-service.example.rc](default/android.hardware.health-service.example.rc). 61*4d7e907cSAndroid Build Coastguard WorkerSpecifically: 62*4d7e907cSAndroid Build Coastguard Worker 63*4d7e907cSAndroid Build Coastguard Worker* For the `service` line, if the name of the service is **NOT** 64*4d7e907cSAndroid Build Coastguard Worker `vendor.charger`, and there are actions 65*4d7e907cSAndroid Build Coastguard Worker in the rc file triggered by `on property:init.svc.<name>=running` where 66*4d7e907cSAndroid Build Coastguard Worker `<name>` is the name of your charger service, then you need a custom health 67*4d7e907cSAndroid Build Coastguard Worker AIDL service. 68*4d7e907cSAndroid Build Coastguard Worker* If your service belongs to additional classes beside `charger`, you need a 69*4d7e907cSAndroid Build Coastguard Worker custom health AIDL service. 70*4d7e907cSAndroid Build Coastguard Worker* Modify the `seclabel` line. Replace `charger` with `charger_vendor`. 71*4d7e907cSAndroid Build Coastguard Worker* If your service has a different `user` (not `system`), you need a custom 72*4d7e907cSAndroid Build Coastguard Worker health AIDL service. 73*4d7e907cSAndroid Build Coastguard Worker* If your service belongs to additional `group`s beside 74*4d7e907cSAndroid Build Coastguard Worker `system wakelock input`, you need a custom health AIDL service. 75*4d7e907cSAndroid Build Coastguard Worker* If your service requires additional capabilities beside `SYS_BOOT`, 76*4d7e907cSAndroid Build Coastguard Worker you need a custom health AIDL service. 77*4d7e907cSAndroid Build Coastguard Worker* If your service requires additional `file`s to be opened prior to execution, 78*4d7e907cSAndroid Build Coastguard Worker you need a custom health AIDL service. 79*4d7e907cSAndroid Build Coastguard Worker 80*4d7e907cSAndroid Build Coastguard Worker## Using the example health AIDL HAL service {#use-example} 81*4d7e907cSAndroid Build Coastguard Worker 82*4d7e907cSAndroid Build Coastguard WorkerIf you [determined](#determine) that the example health AIDL HAL service works 83*4d7e907cSAndroid Build Coastguard Workerfor your device, install it with 84*4d7e907cSAndroid Build Coastguard Worker 85*4d7e907cSAndroid Build Coastguard Worker```mk 86*4d7e907cSAndroid Build Coastguard WorkerPRODUCT_PACKAGES += \ 87*4d7e907cSAndroid Build Coastguard Worker android.hardware.health-service.example \ 88*4d7e907cSAndroid Build Coastguard Worker android.hardware.health-service.example_recovery \ 89*4d7e907cSAndroid Build Coastguard Worker``` 90*4d7e907cSAndroid Build Coastguard Worker 91*4d7e907cSAndroid Build Coastguard WorkerThen, delete any existing `service` with `class charger` in your device-specific 92*4d7e907cSAndroid Build Coastguard Worker`init.rc` files, because 93*4d7e907cSAndroid Build Coastguard Worker[android.hardware.health-service.example.rc](default/android.hardware.health-service.example.rc) 94*4d7e907cSAndroid Build Coastguard Workeralready contains an entry for charger. 95*4d7e907cSAndroid Build Coastguard Worker 96*4d7e907cSAndroid Build Coastguard WorkerIf your device supports charger mode and it has custom charger resources, 97*4d7e907cSAndroid Build Coastguard Worker[move charger resources to `/vendor`](#charger-res) 98*4d7e907cSAndroid Build Coastguard Worker 99*4d7e907cSAndroid Build Coastguard Worker## Implementing a custom health AIDL HAL service {#use-custom} 100*4d7e907cSAndroid Build Coastguard Worker 101*4d7e907cSAndroid Build Coastguard Worker### Override the `Health` class {#health-impl} 102*4d7e907cSAndroid Build Coastguard Worker 103*4d7e907cSAndroid Build Coastguard WorkerSee [`Health.h`](default/include/health-impl/Health.h) for its class 104*4d7e907cSAndroid Build Coastguard Workerdeclaration. Inherit the class to customize for your device. 105*4d7e907cSAndroid Build Coastguard Worker 106*4d7e907cSAndroid Build Coastguard Worker```c++ 107*4d7e907cSAndroid Build Coastguard Workernamespace aidl::android::hardware::health { 108*4d7e907cSAndroid Build Coastguard Workerclass HealthImpl : public Health { 109*4d7e907cSAndroid Build Coastguard Worker // ... 110*4d7e907cSAndroid Build Coastguard Worker}; 111*4d7e907cSAndroid Build Coastguard Worker} // namespace aidl::android::hardware::health 112*4d7e907cSAndroid Build Coastguard Workerint main(int, char**) { 113*4d7e907cSAndroid Build Coastguard Worker // ... 114*4d7e907cSAndroid Build Coastguard Worker auto binder = ndk::SharedRefBase::make<aidl::android::hardware::health::HealthImpl>( 115*4d7e907cSAndroid Build Coastguard Worker "default", std::move(config)); 116*4d7e907cSAndroid Build Coastguard Worker // ... 117*4d7e907cSAndroid Build Coastguard Worker} 118*4d7e907cSAndroid Build Coastguard Worker``` 119*4d7e907cSAndroid Build Coastguard Worker 120*4d7e907cSAndroid Build Coastguard Worker* The logic to modify `healthd_config`, traditionally in `healthd_board_init()` 121*4d7e907cSAndroid Build Coastguard Worker should be called before passing the `healthd_config` struct to your 122*4d7e907cSAndroid Build Coastguard Worker `HealthImpl` class in [`main()`](#main). 123*4d7e907cSAndroid Build Coastguard Worker 124*4d7e907cSAndroid Build Coastguard Worker* The following functions are similar to the ones in the health 2.1 HIDL HAL: 125*4d7e907cSAndroid Build Coastguard Worker 126*4d7e907cSAndroid Build Coastguard Worker| AIDL implementation | HIDL implementation | 127*4d7e907cSAndroid Build Coastguard Worker|-------------------------------------|-----------------------------| 128*4d7e907cSAndroid Build Coastguard Worker| `Health::getChargeCounterUah` | `Health::getChargeCounter` | 129*4d7e907cSAndroid Build Coastguard Worker| `Health::getCurrentNowMicroamps` | `Health::getCurrentNow` | 130*4d7e907cSAndroid Build Coastguard Worker| `Health::getCurrentAverageMicroamps`| `Health::getCurrentAverage` | 131*4d7e907cSAndroid Build Coastguard Worker| `Health::getCapacity` | `Health::getCapacity` | 132*4d7e907cSAndroid Build Coastguard Worker| `Health::getChargeStatus` | `Health::getChargeStatus` | 133*4d7e907cSAndroid Build Coastguard Worker| `Health::getEnergyCounterNwh` | `Health::getEnergyCounter` | 134*4d7e907cSAndroid Build Coastguard Worker| `Health::getDiskStats` | `Health::getDiskStats` | 135*4d7e907cSAndroid Build Coastguard Worker| `Health::getStorageInfo` | `Health::getStorageInfo` | 136*4d7e907cSAndroid Build Coastguard Worker| `Health::BinderEvent` | `BinderHealth::BinderEvent` | 137*4d7e907cSAndroid Build Coastguard Worker| `Health::dump` | `Health::debug` | 138*4d7e907cSAndroid Build Coastguard Worker| `Health::ShouldKeepScreenOn` | `Health::shouldKeepScreenOn`| 139*4d7e907cSAndroid Build Coastguard Worker| `Health::UpdateHealthInfo` | `Health::UpdateHealthInfo` | 140*4d7e907cSAndroid Build Coastguard Worker 141*4d7e907cSAndroid Build Coastguard Worker### Implement `main()` {#main} 142*4d7e907cSAndroid Build Coastguard Worker 143*4d7e907cSAndroid Build Coastguard WorkerSee the [`main.cpp`](default/main.cpp) for the example health AIDL service for 144*4d7e907cSAndroid Build Coastguard Workeran example. 145*4d7e907cSAndroid Build Coastguard Worker 146*4d7e907cSAndroid Build Coastguard WorkerIf you need to modify `healthd_config`, do it before passing it to the 147*4d7e907cSAndroid Build Coastguard Workerconstructor of `HealthImpl` (or `Health` if you did not implement a subclass 148*4d7e907cSAndroid Build Coastguard Workerof it). 149*4d7e907cSAndroid Build Coastguard Worker 150*4d7e907cSAndroid Build Coastguard Worker```c++ 151*4d7e907cSAndroid Build Coastguard Workerint main(int argc, char** argv) { 152*4d7e907cSAndroid Build Coastguard Worker auto config = std::make_unique<healthd_config>(); 153*4d7e907cSAndroid Build Coastguard Worker ::android::hardware::health::InitHealthdConfig(config.get()); 154*4d7e907cSAndroid Build Coastguard Worker healthd_board_init(config.get()); 155*4d7e907cSAndroid Build Coastguard Worker auto binder = ndk::SharedRefBase::make<Health>("default", std::move(config)); 156*4d7e907cSAndroid Build Coastguard Worker // ... 157*4d7e907cSAndroid Build Coastguard Worker} 158*4d7e907cSAndroid Build Coastguard Worker``` 159*4d7e907cSAndroid Build Coastguard Worker 160*4d7e907cSAndroid Build Coastguard WorkerIf your device does not support off-line charging mode, or does not have a UI 161*4d7e907cSAndroid Build Coastguard Workerfor charger (`ro.charger.no_ui=true`), skip the invocation of 162*4d7e907cSAndroid Build Coastguard Worker`ChargerModeMain()` in `main()`. 163*4d7e907cSAndroid Build Coastguard Worker 164*4d7e907cSAndroid Build Coastguard Worker### Build system changes 165*4d7e907cSAndroid Build Coastguard Worker 166*4d7e907cSAndroid Build Coastguard WorkerInstall both the platform and recovery variant of the service. For example: 167*4d7e907cSAndroid Build Coastguard Worker 168*4d7e907cSAndroid Build Coastguard Worker```mk 169*4d7e907cSAndroid Build Coastguard WorkerPRODUCT_PACKAGES += \ 170*4d7e907cSAndroid Build Coastguard Worker android.hardware.health-service.cuttlefish \ 171*4d7e907cSAndroid Build Coastguard Worker android.hardware.health-service.cuttlefish_recovery \ 172*4d7e907cSAndroid Build Coastguard Worker``` 173*4d7e907cSAndroid Build Coastguard Worker 174*4d7e907cSAndroid Build Coastguard Worker### SELinux rules 175*4d7e907cSAndroid Build Coastguard Worker 176*4d7e907cSAndroid Build Coastguard WorkerAdd device specific permissions to the domain where the health HAL 177*4d7e907cSAndroid Build Coastguard Workerprocess is executed, especially if a device-specific `libhealthd` is used 178*4d7e907cSAndroid Build Coastguard Workerand/or device-specific storage related APIs are implemented. 179*4d7e907cSAndroid Build Coastguard Worker 180*4d7e907cSAndroid Build Coastguard WorkerExample (assuming that your health AIDL service runs in domain 181*4d7e907cSAndroid Build Coastguard Worker`hal_health_tuna`: 182*4d7e907cSAndroid Build Coastguard Worker 183*4d7e907cSAndroid Build Coastguard Worker```text 184*4d7e907cSAndroid Build Coastguard Workertype hal_health_tuna, domain; 185*4d7e907cSAndroid Build Coastguard Workerhal_server_domain(hal_health_tuna, hal_health) 186*4d7e907cSAndroid Build Coastguard Workertype hal_health_tuna_exec, exec_type, vendor_file_type, file_type; 187*4d7e907cSAndroid Build Coastguard Worker 188*4d7e907cSAndroid Build Coastguard Worker# allow hal_health_tuna ...; 189*4d7e907cSAndroid Build Coastguard Worker``` 190*4d7e907cSAndroid Build Coastguard Worker 191*4d7e907cSAndroid Build Coastguard WorkerIf you did not define a separate domain, the domain is likely 192*4d7e907cSAndroid Build Coastguard Worker`hal_health_default`. The device-specific rules for it is likely at 193*4d7e907cSAndroid Build Coastguard Worker`device/<manufacturer>/<device>/sepolicy/vendor/hal_health_default.te`. 194*4d7e907cSAndroid Build Coastguard WorkerIn this case, the aforementioned SELinux rules and types has already been 195*4d7e907cSAndroid Build Coastguard Workerdefined. You only need to add device-specific permissions. 196*4d7e907cSAndroid Build Coastguard Worker 197*4d7e907cSAndroid Build Coastguard Worker```text 198*4d7e907cSAndroid Build Coastguard Worker# allow hal_health_default ...; 199*4d7e907cSAndroid Build Coastguard Worker``` 200*4d7e907cSAndroid Build Coastguard Worker 201*4d7e907cSAndroid Build Coastguard Worker### Implementing charger {#charger} 202*4d7e907cSAndroid Build Coastguard Worker 203*4d7e907cSAndroid Build Coastguard Worker#### Move charger resources to `/vendor` 204*4d7e907cSAndroid Build Coastguard Worker 205*4d7e907cSAndroid Build Coastguard WorkerEnsure that charger resources are installed to `/vendor`, not `/product`. 206*4d7e907cSAndroid Build Coastguard Worker 207*4d7e907cSAndroid Build Coastguard Worker`animation.txt` must be moved to the following location: 208*4d7e907cSAndroid Build Coastguard Worker 209*4d7e907cSAndroid Build Coastguard Worker```text 210*4d7e907cSAndroid Build Coastguard Worker/vendor/etc/res/values/charger/animation.txt 211*4d7e907cSAndroid Build Coastguard Worker``` 212*4d7e907cSAndroid Build Coastguard Worker 213*4d7e907cSAndroid Build Coastguard WorkerCharger resources in `/system` is not read by the health HAL service in 214*4d7e907cSAndroid Build Coastguard Worker`/vendor`. Specifically, resources should be installed to the following 215*4d7e907cSAndroid Build Coastguard Workerlocation: 216*4d7e907cSAndroid Build Coastguard Worker 217*4d7e907cSAndroid Build Coastguard Worker``` 218*4d7e907cSAndroid Build Coastguard Worker/vendor/etc/res/images/charger/*.png 219*4d7e907cSAndroid Build Coastguard Worker``` 220*4d7e907cSAndroid Build Coastguard Worker 221*4d7e907cSAndroid Build Coastguard WorkerIf resources are not found in these locations, the health HAL service falls 222*4d7e907cSAndroid Build Coastguard Workerback to the following locations: 223*4d7e907cSAndroid Build Coastguard Worker 224*4d7e907cSAndroid Build Coastguard Worker``` 225*4d7e907cSAndroid Build Coastguard Worker/vendor/etc/res/images/charger/default/*.png 226*4d7e907cSAndroid Build Coastguard Worker``` 227*4d7e907cSAndroid Build Coastguard Worker 228*4d7e907cSAndroid Build Coastguard WorkerYou can use the default resources by installing the default module: 229*4d7e907cSAndroid Build Coastguard Worker 230*4d7e907cSAndroid Build Coastguard Worker```makefile 231*4d7e907cSAndroid Build Coastguard WorkerPRODUCT_PACKAGES += charger_res_images_vendor 232*4d7e907cSAndroid Build Coastguard Worker``` 233*4d7e907cSAndroid Build Coastguard Worker 234*4d7e907cSAndroid Build Coastguard Worker#### Modify `init.rc` for charger 235*4d7e907cSAndroid Build Coastguard Worker 236*4d7e907cSAndroid Build Coastguard WorkerIt is recommended that you move the existing `service` entry with 237*4d7e907cSAndroid Build Coastguard Worker`class charger` to the `init.rc` file in your custom health service. 238*4d7e907cSAndroid Build Coastguard Worker 239*4d7e907cSAndroid Build Coastguard WorkerIf there are existing actions in the rc file triggered by 240*4d7e907cSAndroid Build Coastguard Worker`on property:init.svc.<name>=running`, where `<name>` is the name of your 241*4d7e907cSAndroid Build Coastguard Workerexisting charger service (usually `vendor.charger`), then the name of the 242*4d7e907cSAndroid Build Coastguard Workerservice must be kept as-is. If you modify the name of the service, the actions 243*4d7e907cSAndroid Build Coastguard Workerare not triggered properly. 244*4d7e907cSAndroid Build Coastguard Worker 245*4d7e907cSAndroid Build Coastguard WorkerModify the entry to invoke the health service binary with `--charger` argument. 246*4d7e907cSAndroid Build Coastguard WorkerSee 247*4d7e907cSAndroid Build Coastguard Worker[android.hardware.health-service.example.rc](default/android.hardware.health-service.example.rc) 248*4d7e907cSAndroid Build Coastguard Workerfor an example: 249*4d7e907cSAndroid Build Coastguard Worker 250*4d7e907cSAndroid Build Coastguard Worker```text 251*4d7e907cSAndroid Build Coastguard Workerservice vendor.charger /vendor/bin/hw/android.hardware.health-service-tuna --charger 252*4d7e907cSAndroid Build Coastguard Worker class charger 253*4d7e907cSAndroid Build Coastguard Worker seclabel u:r:charger_vendor:s0 254*4d7e907cSAndroid Build Coastguard Worker # ... 255*4d7e907cSAndroid Build Coastguard Worker``` 256*4d7e907cSAndroid Build Coastguard Worker 257*4d7e907cSAndroid Build Coastguard Worker#### No charger mode {#no-charger} 258*4d7e907cSAndroid Build Coastguard Worker 259*4d7e907cSAndroid Build Coastguard WorkerIf your device does not support off-line charging mode, skip the invocation of 260*4d7e907cSAndroid Build Coastguard Worker`ChargerModeMain()` in `main()`. 261*4d7e907cSAndroid Build Coastguard Worker 262*4d7e907cSAndroid Build Coastguard Worker```c++ 263*4d7e907cSAndroid Build Coastguard Workerint main(int, char**) { 264*4d7e907cSAndroid Build Coastguard Worker // ... 265*4d7e907cSAndroid Build Coastguard Worker // Skip checking if arguments contain "--charger" 266*4d7e907cSAndroid Build Coastguard Worker auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, binder); 267*4d7e907cSAndroid Build Coastguard Worker return hal_health_loop->StartLoop(); 268*4d7e907cSAndroid Build Coastguard Worker} 269*4d7e907cSAndroid Build Coastguard Worker``` 270*4d7e907cSAndroid Build Coastguard Worker 271*4d7e907cSAndroid Build Coastguard WorkerYou may optionally delete the `service` entry with `class charger` in the 272*4d7e907cSAndroid Build Coastguard Worker`init.rc` file. 273*4d7e907cSAndroid Build Coastguard Worker 274*4d7e907cSAndroid Build Coastguard Worker#### No charger UI {#charger-no-ui} 275*4d7e907cSAndroid Build Coastguard Worker 276*4d7e907cSAndroid Build Coastguard WorkerIf your device does not have a UI for charger (`ro.charger.no_ui=true`), skip 277*4d7e907cSAndroid Build Coastguard Workerthe invocation of `ChargerModeMain()` in `main()`. 278*4d7e907cSAndroid Build Coastguard Worker 279*4d7e907cSAndroid Build Coastguard WorkerYou may want to keep the `KernelLogger` so that charger still logs battery 280*4d7e907cSAndroid Build Coastguard Workerinformation to the kernel logs. 281*4d7e907cSAndroid Build Coastguard Worker 282*4d7e907cSAndroid Build Coastguard Worker```c++ 283*4d7e907cSAndroid Build Coastguard Workerint main(int argc, char** argv) { 284*4d7e907cSAndroid Build Coastguard Worker // ... 285*4d7e907cSAndroid Build Coastguard Worker if (argc >= 2 && argv[1] == "--charger"sv) { 286*4d7e907cSAndroid Build Coastguard Worker android::base::InitLogging(argv, &android::base::KernelLogger); 287*4d7e907cSAndroid Build Coastguard Worker // fallthrough to HalHealthLoop::StartLoop() 288*4d7e907cSAndroid Build Coastguard Worker } 289*4d7e907cSAndroid Build Coastguard Worker auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, binder); 290*4d7e907cSAndroid Build Coastguard Worker return hal_health_loop->StartLoop(); 291*4d7e907cSAndroid Build Coastguard Worker} 292*4d7e907cSAndroid Build Coastguard Worker``` 293*4d7e907cSAndroid Build Coastguard Worker 294*4d7e907cSAndroid Build Coastguard Worker#### Enable suspend {#charger-enable-suspend} 295*4d7e907cSAndroid Build Coastguard Worker 296*4d7e907cSAndroid Build Coastguard WorkerIf your device has `ro.charger.enable_suspend=true`, implement a new class, 297*4d7e907cSAndroid Build Coastguard Worker`ChargerCallbackImpl`, that inherits from 298*4d7e907cSAndroid Build Coastguard Worker[`ChargerCallback`](default/include/health-impl/ChargerUtils.h). Then 299*4d7e907cSAndroid Build Coastguard Workeroverride the `ChargerEnableSuspend` function to return `true`. Then pass an 300*4d7e907cSAndroid Build Coastguard Workerinstance of `ChargerCallbackImpl` to `ChargerModeMain()` instead. 301*4d7e907cSAndroid Build Coastguard Worker 302*4d7e907cSAndroid Build Coastguard Worker```c++ 303*4d7e907cSAndroid Build Coastguard Workernamespace aidl::android::hardware::health { 304*4d7e907cSAndroid Build Coastguard Workerclass ChargerCallbackImpl : public ChargerCallback { 305*4d7e907cSAndroid Build Coastguard Worker bool ChargerEnableSuspend() override { return true; } 306*4d7e907cSAndroid Build Coastguard Worker}; 307*4d7e907cSAndroid Build Coastguard Worker} // namespace aidl::android::hardware::health 308*4d7e907cSAndroid Build Coastguard Workerint main(int argc, char** argv) { 309*4d7e907cSAndroid Build Coastguard Worker // ... 310*4d7e907cSAndroid Build Coastguard Worker if (argc >= 2 && argv[1] == "--charger"sv) { 311*4d7e907cSAndroid Build Coastguard Worker android::base::InitLogging(argv, &android::base::KernelLogger); 312*4d7e907cSAndroid Build Coastguard Worker#if !CHARGER_FORCE_NO_UI 313*4d7e907cSAndroid Build Coastguard Worker return ChargerModeMain(binder, 314*4d7e907cSAndroid Build Coastguard Worker std::make_shared<aidl::android::hardware::health::ChargerCallbackImpl>(binder)); 315*4d7e907cSAndroid Build Coastguard Worker#endif 316*4d7e907cSAndroid Build Coastguard Worker } 317*4d7e907cSAndroid Build Coastguard Worker // ... 318*4d7e907cSAndroid Build Coastguard Worker} 319*4d7e907cSAndroid Build Coastguard Worker``` 320*4d7e907cSAndroid Build Coastguard Worker 321*4d7e907cSAndroid Build Coastguard Worker#### SELinux rules for charger 322*4d7e907cSAndroid Build Coastguard Worker 323*4d7e907cSAndroid Build Coastguard WorkerIf your health AIDL service runs in a domain other than `hal_health_default`, 324*4d7e907cSAndroid Build Coastguard Workeradd `charger_type` to it so the health HAL service can have charger-specific 325*4d7e907cSAndroid Build Coastguard Workerpermissions. Example (assuming that your health AIDL service runs in domain 326*4d7e907cSAndroid Build Coastguard Worker`hal_health_tuna`: 327*4d7e907cSAndroid Build Coastguard Worker 328*4d7e907cSAndroid Build Coastguard Worker```text 329*4d7e907cSAndroid Build Coastguard Workerdomain_trans(init, hal_health_tuna_exec, charger_vendor) 330*4d7e907cSAndroid Build Coastguard Worker``` 331