From 31cc64aa79bbd461d62dff72013edb460cd009a3 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Mon, 6 Nov 2017 16:48:36 -0800 Subject: [PATCH] healthd connect to health@1.0 hal This is the original code path (BatteryService -> healthd -> health 1.0 HAL). This ensures upgrade-ability for old devices to P. This is a partial revert of 287c41fffeb144476092766466b86db9cfaf5e8c, with some modifications to the original healthd_board_* functions for healthd. Bug: 65990106 Test: VTS test for healthd and health service Change-Id: If1c65e64e2fd6750369d52c8051ca6aa2b57ef27 --- healthd/Android.bp | 1 + healthd/HealthServiceHealthd.cpp | 72 +++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/healthd/Android.bp b/healthd/Android.bp index 21c0ef0a8..ed1413e36 100644 --- a/healthd/Android.bp +++ b/healthd/Android.bp @@ -102,6 +102,7 @@ cc_binary { "libhwbinder", "liblog", "libutils", + "android.hardware.health@1.0", "android.hardware.health@2.0", ], diff --git a/healthd/HealthServiceHealthd.cpp b/healthd/HealthServiceHealthd.cpp index 42e76d9d6..72a446ad4 100644 --- a/healthd/HealthServiceHealthd.cpp +++ b/healthd/HealthServiceHealthd.cpp @@ -14,13 +14,75 @@ * limitations under the License. */ +#define LOG_TAG "healthd" +#include + +#include +#include +#include #include +#include -void healthd_board_init(struct healthd_config*) { - // use defaults +using android::OK; +using android::NAME_NOT_FOUND; +using android::hardware::health::V1_0::HealthConfig; +using android::hardware::health::V1_0::HealthInfo; +using android::hardware::health::V1_0::Result; +using android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig; +using android::hardware::health::V1_0::hal_conversion::convertToHealthConfig; +using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo; +using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo; + +using IHealthLegacy = android::hardware::health::V1_0::IHealth; + +static android::sp gHealth_1_0; + +static int healthd_board_get_energy_counter(int64_t* energy) { + if (gHealth_1_0 == nullptr) { + return NAME_NOT_FOUND; + } + + Result result = Result::NOT_SUPPORTED; + gHealth_1_0->energyCounter([energy, &result](Result ret, int64_t energyOut) { + result = ret; + *energy = energyOut; + }); + + return result == Result::SUCCESS ? OK : NAME_NOT_FOUND; } -int healthd_board_battery_update(struct android::BatteryProperties*) { - // return 0 to log periodic polled battery status to kernel log - return 0; +void healthd_board_init(struct healthd_config* config) { + gHealth_1_0 = IHealthLegacy::getService(); + + if (gHealth_1_0 == nullptr) { + return; + } + + HealthConfig halConfig{}; + convertToHealthConfig(config, halConfig); + gHealth_1_0->init(halConfig, [config](const auto& halConfigOut) { + convertFromHealthConfig(halConfigOut, config); + // always redirect energy counter queries + config->energyCounter = healthd_board_get_energy_counter; + }); + LOG(INFO) << LOG_TAG << ": redirecting calls to 1.0 health HAL"; +} + +// TODO(b/68724651): Move this function into healthd_mode_service_2_0_battery_update +// with logthis returned. +int healthd_board_battery_update(struct android::BatteryProperties* props) { + int logthis = 0; + + if (gHealth_1_0 == nullptr) { + return logthis; + } + + HealthInfo info; + convertToHealthInfo(props, info); + gHealth_1_0->update(info, [props, &logthis](int32_t ret, const auto& infoOut) { + logthis = ret; + convertFromHealthInfo(infoOut, props); + }); + + return logthis; }