diff --git a/healthd/Health.cpp b/healthd/Health.cpp index 74f3eec21..d27181128 100644 --- a/healthd/Health.cpp +++ b/healthd/Health.cpp @@ -13,6 +13,8 @@ namespace health { namespace V2_0 { namespace implementation { +sp Health::instance_; + Health::Health(struct healthd_config* c) { battery_monitor_ = std::make_unique(); battery_monitor_->init(c); @@ -154,7 +156,17 @@ void Health::serviceDied(uint64_t /* cookie */, const wp& who) { (void)unregisterCallbackInternal(who.promote()); } -// Methods from ::android::hidl::base::V1_0::IBase follow. +sp Health::initInstance(struct healthd_config* c) { + if (instance_ == nullptr) { + instance_ = new Health(c); + } + return instance_; +} + +sp Health::getImplementation() { + CHECK(instance_ != nullptr); + return instance_; +} } // namespace implementation } // namespace V2_0 diff --git a/healthd/HealthServiceCommon.cpp b/healthd/HealthServiceCommon.cpp index 260ca78a3..68ff526bb 100644 --- a/healthd/HealthServiceCommon.cpp +++ b/healthd/HealthServiceCommon.cpp @@ -33,9 +33,6 @@ using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo; using android::hardware::health::V2_0::IHealth; using android::hardware::health::V2_0::implementation::Health; -// see healthd_common.cpp -android::sp gHealth; - extern int healthd_main(void); static void binder_event(uint32_t /*epevents*/) { @@ -63,8 +60,8 @@ void healthd_mode_service_2_0_init(struct healthd_config* config) { // TODO(b/68724651): healthd_board_* functions should be removed in health@2.0 healthd_board_init(config); - gHealth = new ::android::hardware::health::V2_0::implementation::Health(config); - CHECK_EQ(gHealth->registerAsService(HEALTH_INSTANCE_NAME), android::OK) + android::sp service = Health::initInstance(config); + CHECK_EQ(service->registerAsService(HEALTH_INSTANCE_NAME), android::OK) << LOG_TAG << ": Failed to register HAL"; LOG(INFO) << LOG_TAG << ": Hal init done"; @@ -85,7 +82,7 @@ void healthd_mode_service_2_0_battery_update(struct android::BatteryProperties* HealthInfo info; convertToHealthInfo(prop, info); - static_cast(gHealth.get())->notifyListeners(info); + Health::getImplementation()->notifyListeners(info); } static struct healthd_mode_ops healthd_mode_service_2_0_ops = { diff --git a/healthd/healthd_common.cpp b/healthd/healthd_common.cpp index 19e600f86..140c49d01 100644 --- a/healthd/healthd_common.cpp +++ b/healthd/healthd_common.cpp @@ -91,7 +91,7 @@ static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST; #ifndef HEALTHD_USE_HEALTH_2_0 static BatteryMonitor* gBatteryMonitor = nullptr; #else -extern sp<::android::hardware::health::V2_0::IHealth> gHealth; +using ::android::hardware::health::V2_0::implementation::Health; #endif struct healthd_mode_ops *healthd_mode_ops = nullptr; @@ -160,42 +160,42 @@ status_t healthd_get_property(int id, struct BatteryProperty *val) { status_t err = UNKNOWN_ERROR; switch (id) { case BATTERY_PROP_CHARGE_COUNTER: { - gHealth->getChargeCounter([&](Result r, int32_t v) { + Health::getImplementation()->getChargeCounter([&](Result r, int32_t v) { err = convertStatus(r); val->valueInt64 = v; }); break; } case BATTERY_PROP_CURRENT_NOW: { - gHealth->getCurrentNow([&](Result r, int32_t v) { + Health::getImplementation()->getCurrentNow([&](Result r, int32_t v) { err = convertStatus(r); val->valueInt64 = v; }); break; } case BATTERY_PROP_CURRENT_AVG: { - gHealth->getCurrentAverage([&](Result r, int32_t v) { + Health::getImplementation()->getCurrentAverage([&](Result r, int32_t v) { err = convertStatus(r); val->valueInt64 = v; }); break; } case BATTERY_PROP_CAPACITY: { - gHealth->getCapacity([&](Result r, int32_t v) { + Health::getImplementation()->getCapacity([&](Result r, int32_t v) { err = convertStatus(r); val->valueInt64 = v; }); break; } case BATTERY_PROP_ENERGY_COUNTER: { - gHealth->getEnergyCounter([&](Result r, int64_t v) { + Health::getImplementation()->getEnergyCounter([&](Result r, int64_t v) { err = convertStatus(r); val->valueInt64 = v; }); break; } case BATTERY_PROP_BATTERY_STATUS: { - gHealth->getChargeStatus([&](Result r, BatteryStatus v) { + Health::getImplementation()->getChargeStatus([&](Result r, BatteryStatus v) { err = convertStatus(r); val->valueInt64 = static_cast(v); }); @@ -237,7 +237,7 @@ void healthd_battery_update(void) { #ifndef HEALTHD_USE_HEALTH_2_0 healthd_battery_update_internal(gBatteryMonitor->update()); #else - gHealth->update(); + Health::getImplementation()->update(); #endif } @@ -249,7 +249,7 @@ void healthd_dump_battery_state(int fd) { nativeHandle->data[0] = fd; ::android::hardware::hidl_handle handle; handle.setTo(nativeHandle, true /* shouldOwn */); - gHealth->debug(handle, {} /* options */); + Health::getImplementation()->debug(handle, {} /* options */); #endif fsync(fd); diff --git a/healthd/include/health2/Health.h b/healthd/include/health2/Health.h index 4e78380e7..012b95bf4 100644 --- a/healthd/include/health2/Health.h +++ b/healthd/include/health2/Health.h @@ -22,9 +22,16 @@ using ::android::hidl::base::V1_0::IBase; struct Health : public IHealth, hidl_death_recipient { public: + static sp initInstance(struct healthd_config* c); + // Should only be called by implementation itself (-impl, -service). + // Clients should not call this function. Instead, initInstance() initializes and returns the + // global instance that has fewer functions. + // TODO(b/62229583): clean up and hide these functions after update() logic is simplified. + static sp getImplementation(); + Health(struct healthd_config* c); - // TODO(b/62229583): clean up and hide these functions. + // TODO(b/62229583): clean up and hide these functions after update() logic is simplified. void notifyListeners(const HealthInfo& info); // Methods from IHealth follow. @@ -44,6 +51,8 @@ struct Health : public IHealth, hidl_death_recipient { void serviceDied(uint64_t cookie, const wp& /* who */) override; private: + static sp instance_; + std::mutex callbacks_lock_; std::vector> callbacks_; std::unique_ptr battery_monitor_;