Add listener for the initial battery status

Listen to the initial charging status as DPO mode
depends on it;
Change ALOG to LOC_LOG.

Change-Id: Ic61f76038f0e464b18ade8c9b20c058de35748c8
CRs-fixed: 2544828
This commit is contained in:
Yingjie Wang 2019-10-14 18:21:15 +08:00 committed by Gerrit - the friendly Code Review server
parent 5c568ca1bd
commit ae1d9031ef
5 changed files with 45 additions and 34 deletions

View file

@ -56,6 +56,8 @@ void location_on_battery_status_changed(bool charging) {
Gnss::Gnss() { Gnss::Gnss() {
ENTRY_LOG_CALLFLOW(); ENTRY_LOG_CALLFLOW();
sGnss = this; sGnss = this;
// initilize gnss interface at first in case needing notify battery status
sGnss->getGnssInterface()->initialize();
// register health client to listen on battery change // register health client to listen on battery change
loc_extn_battery_properties_listener_init(location_on_battery_status_changed); loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
// clear pending GnssConfig // clear pending GnssConfig

View file

@ -97,6 +97,8 @@ void location_on_battery_status_changed(bool charging) {
Gnss::Gnss() { Gnss::Gnss() {
ENTRY_LOG_CALLFLOW(); ENTRY_LOG_CALLFLOW();
sGnss = this; sGnss = this;
// initilize gnss interface at first in case needing notify battery status
sGnss->getGnssInterface()->initialize();
// register health client to listen on battery change // register health client to listen on battery change
loc_extn_battery_properties_listener_init(location_on_battery_status_changed); loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
// clear pending GnssConfig // clear pending GnssConfig

View file

@ -96,6 +96,8 @@ void location_on_battery_status_changed(bool charging) {
Gnss::Gnss() { Gnss::Gnss() {
ENTRY_LOG_CALLFLOW(); ENTRY_LOG_CALLFLOW();
sGnss = this; sGnss = this;
// initilize gnss interface at first in case needing notify battery status
sGnss->getGnssInterface()->initialize();
// register health client to listen on battery change // register health client to listen on battery change
loc_extn_battery_properties_listener_init(location_on_battery_status_changed); loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
// clear pending GnssConfig // clear pending GnssConfig

View file

@ -26,6 +26,9 @@ LOCAL_SHARED_LIBRARIES := \
android.hardware.power@1.2 \ android.hardware.power@1.2 \
libbase libbase
LOCAL_HEADER_LIBRARIES := \
libgps.utils_headers \
LOCAL_STATIC_LIBRARIES := libhealthhalutils LOCAL_STATIC_LIBRARIES := libhealthhalutils
LOCAL_CFLAGS += -DBATTERY_LISTENER_ENABLED LOCAL_CFLAGS += -DBATTERY_LISTENER_ENABLED

View file

@ -31,12 +31,15 @@
#undef LOG_TAG #undef LOG_TAG
#endif #endif
#define LOG_TAG "LocSvc_BatteryListener" #define LOG_TAG "LocSvc_BatteryListener"
#define LOG_NDEBUG 0
#include <android/hidl/manager/1.0/IServiceManager.h> #include <android/hidl/manager/1.0/IServiceManager.h>
#include <android/hardware/health/2.0/IHealth.h> #include <android/hardware/health/2.0/IHealth.h>
#include <healthhalutils/HealthHalUtils.h> #include <healthhalutils/HealthHalUtils.h>
#include <hidl/HidlTransportSupport.h> #include <hidl/HidlTransportSupport.h>
#include <thread> #include <thread>
#include <log_util.h>
using android::hardware::interfacesEqual; using android::hardware::interfacesEqual;
using android::hardware::Return; using android::hardware::Return;
using android::hardware::Void; using android::hardware::Void;
@ -99,24 +102,25 @@ status_t BatteryListenerImpl::init()
} while(tries < GET_HEALTH_SVC_RETRY_CNT); } while(tries < GET_HEALTH_SVC_RETRY_CNT);
if (mHealth == NULL) { if (mHealth == NULL) {
ALOGE("no health service found, retries %d", tries); LOC_LOGe("no health service found, retries %d", tries);
return NO_INIT; return NO_INIT;
} else { } else {
ALOGI("Get health service in %d tries", tries); LOC_LOGi("Get health service in %d tries", tries);
} }
mStatus = BatteryStatus::UNKNOWN; mStatus = BatteryStatus::UNKNOWN;
auto ret = mHealth->getChargeStatus([&](Result r, BatteryStatus status) { auto ret = mHealth->getChargeStatus([&](Result r, BatteryStatus status) {
if (r != Result::SUCCESS) { if (r != Result::SUCCESS) {
ALOGE("batterylistener: cannot get battery status"); LOC_LOGe("batterylistener: cannot get battery status");
return; return;
} }
mStatus = status; mStatus = status;
}); });
if (!ret.isOk()) if (!ret.isOk()) {
ALOGE("batterylistener: get charge status transaction error"); LOC_LOGe("batterylistener: get charge status transaction error");
}
if (mStatus == BatteryStatus::UNKNOWN) if (mStatus == BatteryStatus::UNKNOWN) {
ALOGW("batterylistener: init: invalid battery status"); LOC_LOGw("batterylistener: init: invalid battery status");
}
mDone = false; mDone = false;
mThread = std::make_unique<std::thread>([this]() { mThread = std::make_unique<std::thread>([this]() {
std::unique_lock<std::mutex> l(mLock); std::unique_lock<std::mutex> l(mLock);
@ -146,7 +150,7 @@ status_t BatteryListenerImpl::init()
} }
default: default:
bool c = statusToBool(local_status); bool c = statusToBool(local_status);
ALOGI("healthInfo cb thread: cb %s", c ? "CHARGING" : "NOT CHARGING"); LOC_LOGi("healthInfo cb thread: cb %s", c ? "CHARGING" : "NOT CHARGING");
l.unlock(); l.unlock();
mCb(c); mCb(c);
l.lock(); l.lock();
@ -156,13 +160,14 @@ status_t BatteryListenerImpl::init()
}); });
auto reg = mHealth->registerCallback(this); auto reg = mHealth->registerCallback(this);
if (!reg.isOk()) { if (!reg.isOk()) {
ALOGE("Transaction error in registeringCb to HealthHAL death: %s", LOC_LOGe("Transaction error in registeringCb to HealthHAL death: %s",
reg.description().c_str()); reg.description().c_str());
} }
auto linked = mHealth->linkToDeath(this, 0 /* cookie */); auto linked = mHealth->linkToDeath(this, 0 /* cookie */);
if (!linked.isOk() || linked == false) { if (!linked.isOk() || linked == false) {
ALOGE("Transaction error in linking to HealthHAL death: %s", linked.description().c_str()); LOC_LOGe("Transaction error in linking to HealthHAL death: %s",
linked.description().c_str());
} }
return NO_ERROR; return NO_ERROR;
} }
@ -181,7 +186,7 @@ BatteryListenerImpl::~BatteryListenerImpl()
mHealth->unlinkToDeath(this); mHealth->unlinkToDeath(this);
auto r = mHealth->unlinkToDeath(this); auto r = mHealth->unlinkToDeath(this);
if (!r.isOk() || r == false) { if (!r.isOk() || r == false) {
ALOGE("Transaction error in unregister to HealthHAL death: %s", LOC_LOGe("Transaction error in unregister to HealthHAL death: %s",
r.description().c_str()); r.description().c_str());
} }
} }
@ -195,10 +200,10 @@ void BatteryListenerImpl::serviceDied(uint64_t cookie __unused,
{ {
std::lock_guard<std::mutex> _l(mLock); std::lock_guard<std::mutex> _l(mLock);
if (mHealth == NULL || !interfacesEqual(mHealth, who.promote())) { if (mHealth == NULL || !interfacesEqual(mHealth, who.promote())) {
ALOGE("health not initialized or unknown interface died"); LOC_LOGe("health not initialized or unknown interface died");
return; return;
} }
ALOGI("health service died, reinit"); LOC_LOGi("health service died, reinit");
mDone = true; mDone = true;
} }
mThread->join(); mThread->join();
@ -212,9 +217,8 @@ void BatteryListenerImpl::serviceDied(uint64_t cookie __unused,
// NOT_CHARGING and CHARGING concurrencies. // NOT_CHARGING and CHARGING concurrencies.
// Replace single var by a list if this assumption is broken // Replace single var by a list if this assumption is broken
Return<void> BatteryListenerImpl::healthInfoChanged( Return<void> BatteryListenerImpl::healthInfoChanged(
const hardware::health::V2_0::HealthInfo& info) const hardware::health::V2_0::HealthInfo& info) {
{ LOC_LOGv("healthInfoChanged: %d", info.legacy.batteryStatus);
ALOGV("healthInfoChanged: %d", info.legacy.batteryStatus);
std::unique_lock<std::mutex> l(mLock); std::unique_lock<std::mutex> l(mLock);
if (info.legacy.batteryStatus != mStatus) { if (info.legacy.batteryStatus != mStatus) {
mStatus = info.legacy.batteryStatus; mStatus = info.legacy.batteryStatus;
@ -224,29 +228,29 @@ Return<void> BatteryListenerImpl::healthInfoChanged(
} }
static sp<BatteryListenerImpl> batteryListener; static sp<BatteryListenerImpl> batteryListener;
status_t batteryPropertiesListenerInit(BatteryListenerImpl::cb_fn_t cb)
{ bool batteryPropertiesListenerIsCharging() {
ALOGV("batteryPropertiesListenerInit entry"); return batteryListener->isCharging();
}
status_t batteryPropertiesListenerInit(BatteryListenerImpl::cb_fn_t cb) {
batteryListener = new BatteryListenerImpl(cb); batteryListener = new BatteryListenerImpl(cb);
bool isCharging = batteryPropertiesListenerIsCharging();
LOC_LOGv("charging status: %s charging", isCharging ? "" : "not");;
if (isCharging) {
cb(isCharging);
}
return NO_ERROR; return NO_ERROR;
} }
status_t batteryPropertiesListenerDeinit() status_t batteryPropertiesListenerDeinit() {
{
batteryListener.clear(); batteryListener.clear();
return OK; return OK;
} }
bool batteryPropertiesListenerIsCharging()
{
return batteryListener->isCharging();
}
} // namespace android } // namespace android
void loc_extn_battery_properties_listener_init(battery_status_change_fn_t fn) void loc_extn_battery_properties_listener_init(battery_status_change_fn_t fn) {
{
ALOGV("loc_extn_battery_properties_listener_init entry");
if (!sIsBatteryListened) { if (!sIsBatteryListened) {
std::thread t1(android::batteryPropertiesListenerInit, std::thread t1(android::batteryPropertiesListenerInit,
[=](bool charging) { fn(charging); }); [=](bool charging) { fn(charging); });
@ -255,12 +259,10 @@ void loc_extn_battery_properties_listener_init(battery_status_change_fn_t fn)
} }
} }
void loc_extn_battery_properties_listener_deinit() void loc_extn_battery_properties_listener_deinit() {
{
android::batteryPropertiesListenerDeinit(); android::batteryPropertiesListenerDeinit();
} }
bool loc_extn_battery_properties_is_charging() bool loc_extn_battery_properties_is_charging() {
{
return android::batteryPropertiesListenerIsCharging(); return android::batteryPropertiesListenerIsCharging();
} }