platform_system_core/healthd/charger_utils.cpp
Yifan Hong 7dcf7b0639 healthd: Remove libhealthd dependency from charger
Clean up charger's libhealthd dependency.

- Charger uses libhealthloop to maintain an infinite
  loop, similar to all health 2.x services.
- Charger tries to open up health 2.1 HAL implementation
  to retrieve health info. If it failed, it falls back
  to the legacy code path where a default BatteryMonitor
  is used, *except* that it won't depend on libhealthd's
  healthd_board_init() and healthd_board_battery_update()
  anymore.
- Remove global static variables because they are hard to
  track.
- Modernize code by converting charger_state in to a C++
  Charger class, transforming all functions into methods,
  and moving all other global states into the class.

- Devices that matches all of the following:
  - have a customized libhealthd (search for modules
    named libhealthd.xxxx)
  - uses charger from system image (look for "class charger"
    in device init.rc scripts; if you see the binary is named
    "/charger" or "/system/bin/charger" then you are using
    charger from system image)
  ... must implement health 2.1 passthrough implementation
  properly in order to have charger continue to work.

See hardware/interfaces/health/2.1/README.md for details.

Test: charger test
Test: manual charger mode
Bug: 127677771
Bug: 142286265

Change-Id: I0f26e5c1fe2be6b5952fc019224457c8419e43e4
2019-10-30 13:48:08 -07:00

68 lines
2.3 KiB
C++

/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "charger_utils.h"
#include <android-base/logging.h>
#include <android/hidl/manager/1.0/IServiceManager.h>
#include <health/utils.h>
#include <health2impl/Health.h>
#include <hidl/ServiceManagement.h>
using android::hardware::getPassthroughServiceManager;
using android::hidl::base::V1_0::IBase;
using android::hidl::manager::V1_0::IServiceManager;
namespace android {
namespace hardware {
namespace health {
sp<V2_1::IHealth> GetPassthroughHealthImpl() {
// Not using getService() because there is no hwservicemanager in charger mode.
sp<IServiceManager> pm = getPassthroughServiceManager();
if (pm == nullptr) {
LOG(WARNING) << "Cannot get passthrough service manager.";
return nullptr;
}
sp<IBase> base = pm->get(V2_0::IHealth::descriptor, "default");
if (base == nullptr) {
LOG(WARNING) << "Cannot find passthrough implementation of health 2.0 HAL for instance "
"'default' on the device.";
return nullptr;
}
sp<V2_1::IHealth> service = V2_1::IHealth::castFrom(base);
if (service == nullptr) {
LOG(WARNING)
<< "Cannot cast passthrough implementation of health 2.0 HAL to 2.1 for instance "
"'default' on the device.";
return nullptr;
}
return service;
}
sp<V2_1::IHealth> GetPassthroughHealth() {
auto impl = GetPassthroughHealthImpl();
if (impl == nullptr) {
LOG(WARNING) << "Charger uses system defaults.";
auto config = std::make_unique<healthd_config>();
InitHealthdConfig(config.get());
impl = new V2_1::implementation::Health(std::move(config));
}
return impl;
}
} // namespace health
} // namespace hardware
} // namespace android