Skip to content

Commit

Permalink
Merge pull request #647 from ix5/health-simplify
Browse files Browse the repository at this point in the history
health: Simplify
  • Loading branch information
jerpelea committed Nov 5, 2019
2 parents 9a97d4b + a026a55 commit 0ddccf9
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 272 deletions.
85 changes: 9 additions & 76 deletions hardware/health/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ cc_binary {
cflags: [
"-Wall",
"-Werror",
"-Wno-unused",
"-fexceptions",
],
srcs: [
"HealthService.cpp",
"CycleCountBackupRestore.cpp",
"LearnedCapacityBackupRestore.cpp",
],
static_libs: [
"android.hardware.health@2.0-impl",
Expand All @@ -18,23 +22,20 @@ cc_binary {
"libhealthservice",
// Storage is needed by the HAL definition, use default
"libhealthstoragedefault",
"libhealthboardcommon.sony",
"libhealthd.sony",
],
shared_libs: [
"android.hardware.health@2.0",
"libbase",
"libcutils",
"libhidlbase",
"libhidltransport",
"libhwbinder",
"liblog",
"libutils",
"android.hardware.health@2.0",
],
// HealthService.cpp never uses healthd_board_* functions, so the compiler
// drops them. But BatteryMonitor.cpp and Health.cpp depend on those
// functions to be defined(not only declared).
whole_static_libs: [
"libhealthd.sony",
header_libs: [
// We only need BatteryService.h
"libbatteryservice_headers",
],

// Only availbale in current Android Q master.
Expand All @@ -48,71 +49,3 @@ cc_binary {
"healthd",
],
}

cc_library_static {
// Vendor-specific functions
name: "libhealthboardcommon.sony",
// recovery_available is not yet available in Pie, need to use master or wait for Q
//recovery_available: true,
vendor_available: true,
cflags: [
"-Wall",
"-Werror",
"-fexceptions",
],
srcs: [
"HealthBoardCommon.cpp",
"CycleCountBackupRestore.cpp",
"LearnedCapacityBackupRestore.cpp",
],
shared_libs: [
"android.hardware.health@2.0",
"libbase",
"libhidlbase",
"libhidltransport",
"libhwbinder",
"liblog",
"libcutils",
"libutils",
],
header_libs: [
// We only need BatteryService.h for the common lib
"libbatteryservice_headers",
],
// Needed to find own headers
local_include_dirs: ["include"],
}

cc_library_static {
// Shim lib that maps legacy healthd_board_* hooks to vendor lib functions
// Needed until the upstream health rework is complete
name: "libhealthd.sony",
// recovery_available is not yet available in Pie, need to use master or wait for Q
//recovery_available: true,
vendor_available: true,
cflags: [
"-Wall",
"-Werror",
],
shared_libs: [
"android.hardware.health@2.0",
"libbase",
"libhidlbase",
"libhidltransport",
"libhwbinder",
"liblog",
"libutils",
"libcutils",
],
srcs: [
"libhealthd_board.cpp",
],
header_libs: [
// To access healthd/healthd.h and BatteryService.h in libhealthd_board.h
"libhealthd_headers",
],
// Needed to find own headers
local_include_dirs: ["include"],
// Let HealthService find headers
export_include_dirs: ["include"],
}
41 changes: 23 additions & 18 deletions hardware/health/CycleCountBackupRestore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
* limitations under the License.
*/

#include <stdexcept>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/strings.h>

#include "CycleCountBackupRestore.h"

namespace device {
Expand All @@ -29,18 +35,18 @@ namespace health {
static constexpr int kCCBackupTrigger = 20;

CycleCountBackupRestore::CycleCountBackupRestore() {
sw_cycles_ = 0;
hw_cycles_ = 0;
persist_cycles = 0;
sysfs_cycles = 0;
saved_soc_ = -1;
soc_inc_ = 0;
}

void CycleCountBackupRestore::Restore() {
/* Our battery driver doesn't seem to report a battery
* serial number, so we have to instruct users to wipe
/* Sony's battery driver doesn't seem to report a battery
* serial number, so we'll have to instruct users to wipe
* /mnt/vendor/persist/battery/ when they swap batteries */
Read(kPersistCycleFile, sw_cycles_);
Read(kSysCycleFile, hw_cycles_);
Read(kPersistCycleFile, persist_cycles);
Read(kSysCycleFile, sysfs_cycles);
UpdateAndSave();
}

Expand All @@ -56,8 +62,7 @@ void CycleCountBackupRestore::Backup(int battery_level) {
saved_soc_ = battery_level;
// To avoid writting file too often just rate limit it
if (soc_inc_ >= kCCBackupTrigger) {
LOG(VERBOSE) << "CC: Triggered Read(kSysCycleFile) and UpdateAndSave() !";
Read(kSysCycleFile, hw_cycles_);
Read(kSysCycleFile, sysfs_cycles);
UpdateAndSave();
soc_inc_ = 0;
}
Expand All @@ -67,18 +72,18 @@ void CycleCountBackupRestore::Read(const std::string &path, int &cycles) {
std::string buffer;

if (!android::base::ReadFileToString(path, &buffer)) {
LOG(ERROR) << "Failed to read battery cycles from " << path;
LOG(WARNING) << "Failed to read battery cycles from " << path;
return;
}

buffer = ::android::base::Trim(buffer);
try {
cycles = std::stoi(buffer);
} catch (std::out_of_range &e) {
LOG(ERROR) << "Battery cycle count in persist storage file is out of bounds: " << path;
LOG(WARNING) << "Battery cycle count in persist storage file is out of bounds: " << path;
return;
} catch (std::invalid_argument &e) {
LOG(ERROR) << "Data format is wrong in persist storage file: " << path;
LOG(WARNING) << "Data format is wrong in persist storage file: " << path;
return;
}
LOG(VERBOSE) << "Read " << cycles << " battery cycles from " << path;
Expand All @@ -88,19 +93,19 @@ void CycleCountBackupRestore::Write(int cycles, const std::string &path) {
std::string str_data = std::to_string(cycles);

if (!android::base::WriteStringToFile(str_data, path)) {
LOG(ERROR) << "Write battery cycles to " << path << " error: " << strerror(errno);
LOG(WARNING) << "Error writing battery cycles to " << path << ": " << strerror(errno);
return;
}
LOG(INFO) << "Wrote " << str_data << " battery cycles to " << path;
}

void CycleCountBackupRestore::UpdateAndSave() {
if (hw_cycles_ < sw_cycles_) {
hw_cycles_ = sw_cycles_;
Write(hw_cycles_, kSysCycleFile);
} else if (hw_cycles_ > sw_cycles_) {
sw_cycles_ = hw_cycles_;
Write(sw_cycles_, kPersistCycleFile);
if (sysfs_cycles < persist_cycles) {
sysfs_cycles = persist_cycles;
Write(sysfs_cycles, kSysCycleFile);
} else if (sysfs_cycles > persist_cycles) {
persist_cycles = sysfs_cycles;
Write(persist_cycles, kPersistCycleFile);
}
}

Expand Down
15 changes: 4 additions & 11 deletions hardware/health/CycleCountBackupRestore.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@
#ifndef DEVICE_SONY_HEALTH_CYCLECOUNTBACKUPRESTORE_H
#define DEVICE_SONY_HEALTH_CYCLECOUNTBACKUPRESTORE_H

#include <stdexcept>
#include <string>

#include <android-base/logging.h>
#include <android-base/file.h>
#include <android-base/strings.h>

namespace device {
namespace sony {
namespace health {

static constexpr char kSysCycleFile[] = "/sys/class/power_supply/bms/cycle_count";
static constexpr char kPersistCycleFile[] = "/mnt/vendor/persist/battery/battery_cycle_count";
static const std::string kSysCycleFile = "/sys/class/power_supply/bms/cycle_count";
static const std::string kPersistCycleFile = "/mnt/vendor/persist/battery/battery_cycle_count";

class CycleCountBackupRestore {
public:
Expand All @@ -39,8 +32,8 @@ class CycleCountBackupRestore {
void Backup(int battery_level);

private:
int sw_cycles_;
int hw_cycles_;
int persist_cycles;
int sysfs_cycles;
int saved_soc_;
int soc_inc_;

Expand Down
51 changes: 0 additions & 51 deletions hardware/health/HealthBoardCommon.cpp

This file was deleted.

33 changes: 31 additions & 2 deletions hardware/health/HealthService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,37 @@
/* For health_service_main() */
#include <health2/service.h>

#include <libhealthd_board/libhealthd_board.h>
#include <batteryservice/BatteryService.h>
#include <health2/Health.h>
#include <healthd/healthd.h>

#include "CycleCountBackupRestore.h"
#include "LearnedCapacityBackupRestore.h"

namespace {
using ::device::sony::health::CycleCountBackupRestore;
using ::device::sony::health::LearnedCapacityBackupRestore;
static CycleCountBackupRestore ccBackupRestore;
static LearnedCapacityBackupRestore lcBackupRestore;
} // namespace

/* healthd_board_init() is called from health@2.0:Health.cpp when
* the IHealth object is initialized */
void healthd_board_init(struct healthd_config *) {
ccBackupRestore.Restore();
lcBackupRestore.Restore();
}

/* Called from libbatterymonitor/BatteryMonitor.cpp */
/* BatteryMonitor::update() { */
/* logthis = !healthd_board_battery_update(&props); */
/* } */
int healthd_board_battery_update(struct android::BatteryProperties *props) {
ccBackupRestore.Backup(props->batteryLevel);
lcBackupRestore.Backup();
// return 0 to log periodic polled battery status to kernel log
return 0;
}

int main() {

Expand All @@ -41,5 +71,4 @@ int main() {
/* After healthd_init() is done, healthd_common.cpp starts healthd_mainloop() */
/* The mainloop then sets up some polling and runs forever, reacting to */
/* "SUBSYSTEM=power_supply" uevents and setting timers */

}
Loading

0 comments on commit 0ddccf9

Please sign in to comment.