From 4cb88dc811a8eab69858eea8e14a571f6a72362b Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Thu, 2 Dec 2021 18:58:02 -0800 Subject: [PATCH] fastboot: use health AIDL HAL Bug: 208543110 Test: fastboot getvar Change-Id: Ib83e7ccd53e49367f7af218ffafb7c0a57a514f6 --- fastboot/Android.bp | 4 ++++ fastboot/device/fastboot_device.cpp | 24 +++++++++++++++++++++++- fastboot/device/fastboot_device.h | 6 +++--- fastboot/device/variables.cpp | 17 +++++------------ 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/fastboot/Android.bp b/fastboot/Android.bp index 339f392de..708a677b3 100644 --- a/fastboot/Android.bp +++ b/fastboot/Android.bp @@ -166,8 +166,10 @@ cc_binary { "android.hardware.boot@1.1", "android.hardware.fastboot@1.1", "android.hardware.health@2.0", + "android.hardware.health-V1-ndk", "libasyncio", "libbase", + "libbinder_ndk", "libbootloader_message", "libcutils", "libext2_uuid", @@ -183,8 +185,10 @@ cc_binary { ], static_libs: [ + "android.hardware.health-translate-ndk", "libc++fs", "libhealthhalutils", + "libhealthshim", "libsnapshot_cow", "libsnapshot_nobinder", "update_metadata-protos", diff --git a/fastboot/device/fastboot_device.cpp b/fastboot/device/fastboot_device.cpp index 64a934ddb..e6a834e59 100644 --- a/fastboot/device/fastboot_device.cpp +++ b/fastboot/device/fastboot_device.cpp @@ -21,10 +21,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include "constants.h" @@ -32,16 +34,36 @@ #include "tcp_client.h" #include "usb_client.h" +using std::string_literals::operator""s; using android::fs_mgr::EnsurePathUnmounted; using android::fs_mgr::Fstab; using ::android::hardware::hidl_string; using ::android::hardware::boot::V1_0::IBootControl; using ::android::hardware::boot::V1_0::Slot; using ::android::hardware::fastboot::V1_1::IFastboot; -using ::android::hardware::health::V2_0::get_health_service; namespace sph = std::placeholders; +std::shared_ptr get_health_service() { + using aidl::android::hardware::health::IHealth; + using HidlHealth = android::hardware::health::V2_0::IHealth; + using aidl::android::hardware::health::HealthShim; + auto service_name = IHealth::descriptor + "/default"s; + if (AServiceManager_isDeclared(service_name.c_str())) { + ndk::SpAIBinder binder(AServiceManager_waitForService(service_name.c_str())); + std::shared_ptr health = IHealth::fromBinder(binder); + if (health != nullptr) return health; + LOG(WARNING) << "AIDL health service is declared, but it cannot be retrieved."; + } + LOG(INFO) << "Unable to get AIDL health service, trying HIDL..."; + android::sp hidl_health = android::hardware::health::V2_0::get_health_service(); + if (hidl_health != nullptr) { + return ndk::SharedRefBase::make(hidl_health); + } + LOG(WARNING) << "No health implementation is found."; + return nullptr; +} + FastbootDevice::FastbootDevice() : kCommandMap({ {FB_CMD_SET_ACTIVE, SetActiveHandler}, diff --git a/fastboot/device/fastboot_device.h b/fastboot/device/fastboot_device.h index 35361365c..91ffce3d9 100644 --- a/fastboot/device/fastboot_device.h +++ b/fastboot/device/fastboot_device.h @@ -22,10 +22,10 @@ #include #include +#include #include #include #include -#include #include "commands.h" #include "transport.h" @@ -57,7 +57,7 @@ class FastbootDevice { android::sp fastboot_hal() { return fastboot_hal_; } - android::sp health_hal() { return health_hal_; } + std::shared_ptr health_hal() { return health_hal_; } void set_active_slot(const std::string& active_slot) { active_slot_ = active_slot; } @@ -67,7 +67,7 @@ class FastbootDevice { std::unique_ptr transport_; android::sp boot_control_hal_; android::sp boot1_1_; - android::sp health_hal_; + std::shared_ptr health_hal_; android::sp fastboot_hal_; std::vector download_data_; std::string active_slot_; diff --git a/fastboot/device/variables.cpp b/fastboot/device/variables.cpp index ee1eed876..76e988951 100644 --- a/fastboot/device/variables.cpp +++ b/fastboot/device/variables.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include "fastboot_device.h" @@ -120,23 +119,17 @@ bool GetVariant(FastbootDevice* device, const std::vector& /* args } bool GetBatteryVoltageHelper(FastbootDevice* device, int32_t* battery_voltage) { - using android::hardware::health::V2_0::HealthInfo; - using android::hardware::health::V2_0::Result; + using aidl::android::hardware::health::HealthInfo; auto health_hal = device->health_hal(); if (!health_hal) { return false; } - Result ret; - auto ret_val = health_hal->getHealthInfo([&](Result result, HealthInfo info) { - *battery_voltage = info.legacy.batteryVoltage; - ret = result; - }); - if (!ret_val.isOk() || (ret != Result::SUCCESS)) { - return false; - } - + HealthInfo health_info; + auto res = health_hal->getHealthInfo(&health_info); + if (!res.isOk()) return false; + *battery_voltage = health_info.batteryVoltageMillivolts; return true; }