From 032d7c8b881e196330bcc0b899cbf71db8764997 Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Fri, 5 May 2017 11:41:51 -0700 Subject: [PATCH] LightsService: open hal less aggressively Only try to reopen the hal if it has been successfully opened before. Test: remove lights hal from marlin, no more spam Test: use with ToT marlin, lights hal still works Fixes: 38006310 Change-Id: Ib21e26184f8a94987d005c8d320ec4d988df66ee --- ...om_android_server_lights_LightsService.cpp | 47 +++++++++++++++---- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/services/core/jni/com_android_server_lights_LightsService.cpp b/services/core/jni/com_android_server_lights_LightsService.cpp index 813dcf5c93e7d..514e996fe5409 100644 --- a/services/core/jni/com_android_server_lights_LightsService.cpp +++ b/services/core/jni/com_android_server_lights_LightsService.cpp @@ -29,16 +29,46 @@ namespace android { -using ILight = ::android::hardware::light::V2_0::ILight; using Brightness = ::android::hardware::light::V2_0::Brightness; using Flash = ::android::hardware::light::V2_0::Flash; -using Type = ::android::hardware::light::V2_0::Type; +using ILight = ::android::hardware::light::V2_0::ILight; using LightState = ::android::hardware::light::V2_0::LightState; using Status = ::android::hardware::light::V2_0::Status; +using Type = ::android::hardware::light::V2_0::Type; template using Return = ::android::hardware::Return; -static sp gLight; +class LightHal { +private: + static sp sLight; + static bool sLightInit; + + LightHal() {} + +public: + static void disassociate() { + sLightInit = false; + sLight = nullptr; + } + + static sp associate() { + if ((sLight == nullptr && !sLightInit) || + (sLight != nullptr && !sLight->ping().isOk())) { + // will return the hal if it exists the first time. + sLight = ILight::getService(); + sLightInit = true; + + if (sLight == nullptr) { + ALOGE("Unable to get ILight interface."); + } + } + + return sLight; + } +}; + +sp LightHal::sLight = nullptr; +bool LightHal::sLightInit = false; static bool validate(jint light, jint flash, jint brightness) { bool valid = true; @@ -103,7 +133,7 @@ static void processReturn( const LightState &state) { if (!ret.isOk()) { ALOGE("Failed to issue set light command."); - gLight = nullptr; + LightHal::disassociate(); return; } @@ -137,12 +167,9 @@ static void setLight_native( return; } - if (gLight == nullptr || !gLight->ping().isOk()) { - gLight = ILight::getService(); - } + sp hal = LightHal::associate(); - if (gLight == nullptr) { - ALOGE("Unable to get ILight interface."); + if (hal == nullptr) { return; } @@ -152,7 +179,7 @@ static void setLight_native( { ALOGD_IF_SLOW(50, "Excessive delay setting light"); - Return ret = gLight->setLight(type, state); + Return ret = hal->setLight(type, state); processReturn(ret, type, state); } }