From 27afb47f6468549600077c5d0331153a4f8c4fee Mon Sep 17 00:00:00 2001 From: Connor O'Brien Date: Tue, 11 Apr 2017 15:33:04 -0700 Subject: [PATCH] Fix failure to load power HAL implementation Some working implementations of the libhardware power HAL do not implement an open method. Change the HIDL implementation to not rely on that method being available. Test: Flashed marlin with modified power.marlin that omits an open() implementation; power HAL loads successfully and passes VTS Bug: 37245218 Signed-off-by: Connor O'Brien Change-Id: Icc77b3011a0e44330062aa51ae49e39ad349a2a5 --- power/1.0/default/Power.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/power/1.0/default/Power.cpp b/power/1.0/default/Power.cpp index 6ea91673fd..51f87f52f5 100644 --- a/power/1.0/default/Power.cpp +++ b/power/1.0/default/Power.cpp @@ -145,25 +145,27 @@ done: } IPower* HIDL_FETCH_IPower(const char* /* name */) { - int ret = 0; - const hw_module_t* hw_module = NULL; - power_module_t *power_module; - ret = hw_get_module(POWER_HARDWARE_MODULE_ID, &hw_module); - if (ret == 0 && hw_module->methods->open) { - ret = hw_module->methods->open(hw_module, POWER_HARDWARE_MODULE_ID, - reinterpret_cast(&power_module)); - if (ret == 0) { - return new Power(power_module); - } - else { - ALOGE("Passthrough failed to load legacy power HAL."); + const hw_module_t* hw_module = nullptr; + power_module_t* power_module = nullptr; + int err = hw_get_module(POWER_HARDWARE_MODULE_ID, &hw_module); + if (err) { + ALOGE("hw_get_module %s failed: %d", POWER_HARDWARE_MODULE_ID, err); + return nullptr; + } + + if (!hw_module->methods || !hw_module->methods->open) { + power_module = reinterpret_cast( + const_cast(hw_module)); + } else { + err = hw_module->methods->open( + hw_module, POWER_HARDWARE_MODULE_ID, + reinterpret_cast(&power_module)); + if (err) { + ALOGE("Passthrough failed to load legacy HAL."); return nullptr; } } - else { - ALOGE ("hw_get_module %s failed: %d", POWER_HARDWARE_MODULE_ID, ret); - return nullptr; - } + return new Power(power_module); } } // namespace implementation