Skip to content

Commit

Permalink
battery learned capacity: backup/restore to/from persist data
Browse files Browse the repository at this point in the history
Test:
    - delete file /persist/battery/qcom_charge_full
    - adb logcat to check code flow related [email protected]
    - adb bugreport
    - no "avc: denied" on health vendor service
    - check learned capacity in both
      /sys/class/power_supply/bms/charge_full
      /persist/battery/qcom_charge_full
      -- decrease /sys/class/power_supply/bms/charge_full
         to check backup to /persist/battery/qcom_charge_full
      -- increase /sys/class/power_supply/bms/charge_full
         to check restore from /persist/battery/qcom_charge_full
         (simluate the reset condition)

Bug: 78883741
Change-Id: Ib17bc0938d3a4b78376140a8a1008b4fc82220af
Signed-off-by: Jack Wu <[email protected]>
  • Loading branch information
Jack Wu authored and Thierry Strudel committed May 10, 2018
1 parent 4eb8b24 commit 2fa48ee
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 1 deletion.
1 change: 1 addition & 0 deletions health/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cc_binary {
srcs: [
"HealthService.cpp",
"CycleCountBackupRestore.cpp",
"LearnedCapacityBackupRestore.cpp",
],

cflags: [
Expand Down
8 changes: 7 additions & 1 deletion health/HealthService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
#include <string>

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

using android::hardware::health::V2_0::StorageInfo;
using android::hardware::health::V2_0::DiskStats;
using ::device::google::wahoo::health::CycleCountBackupRestore;
using ::device::google::wahoo::health::LearnedCapacityBackupRestore;

static constexpr int kBackupTrigger = 20;
static CycleCountBackupRestore ccBackupRestore;
static LearnedCapacityBackupRestore lcBackupRestore;

int cycle_count_backup(int battery_level)
{
Expand Down Expand Up @@ -66,11 +69,14 @@ int cycle_count_backup(int battery_level)
void healthd_board_init(struct healthd_config*)
{
ccBackupRestore.Restore();
lcBackupRestore.Restore();
}

int healthd_board_battery_update(struct android::BatteryProperties *props)
{
return cycle_count_backup(props->batteryLevel);
cycle_count_backup(props->batteryLevel);
lcBackupRestore.Backup();
return 0;
}

const char kUFSHealthFile[] = "/sys/kernel/debug/ufshcd0/dump_health_desc";
Expand Down
114 changes: 114 additions & 0 deletions health/LearnedCapacityBackupRestore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (C) 2018 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 "LearnedCapacityBackupRestore.h"

namespace device {
namespace google {
namespace wahoo {
namespace health {

static constexpr char kChgFullFile[] = "sys/class/power_supply/bms/charge_full";
static constexpr char kSysCFPersistFile[] = "/persist/battery/qcom_charge_full";
static constexpr int kBuffSize = 256;

LearnedCapacityBackupRestore::LearnedCapacityBackupRestore() {}

void LearnedCapacityBackupRestore::Restore() {
ReadFromStorage();
ReadFromSRAM();
UpdateAndSave();
}

void LearnedCapacityBackupRestore::Backup() {
ReadFromSRAM();
UpdateAndSave();
}

void LearnedCapacityBackupRestore::ReadFromStorage() {
std::string buffer;

if (!android::base::ReadFileToString(std::string(kSysCFPersistFile), &buffer)) {
LOG(ERROR) << "Cannot read the storage file";
return;
}

if (sscanf(buffer.c_str(), "%d", &sw_cap_) < 1)
LOG(ERROR) << "data format is wrong in the storage file: " << buffer;
else
LOG(INFO) << "Storage data: " << buffer;
}

void LearnedCapacityBackupRestore::SaveToStorage() {
char strData[kBuffSize];

snprintf(strData, kBuffSize, "%d", sw_cap_);

LOG(INFO) << "Save to Storage: " << strData;

if (!android::base::WriteStringToFile(strData, std::string(kSysCFPersistFile)))
LOG(ERROR) << "Write file error: " << strerror(errno);
}

void LearnedCapacityBackupRestore::ReadFromSRAM() {
std::string buffer;

if (!android::base::ReadFileToString(std::string(kChgFullFile), &buffer)) {
LOG(ERROR) << "Read cycle counter error: " << strerror(errno);
return;
}

buffer = android::base::Trim(buffer);

if (sscanf(buffer.c_str(), "%d", &hw_cap_) < 1)
LOG(ERROR) << "Failed to parse SRAM bins: " << buffer;
else
LOG(INFO) << "SRAM data: " << buffer;
}

void LearnedCapacityBackupRestore::SaveToSRAM() {
char strData[kBuffSize];

snprintf(strData, kBuffSize, "%d", hw_cap_);

LOG(INFO) << "Save to SRAM: " << strData;

if (!android::base::WriteStringToFile(strData, std::string(kChgFullFile)))
LOG(ERROR) << "Write data error: " << strerror(errno);
}

void LearnedCapacityBackupRestore::UpdateAndSave() {
bool backup = false;
bool restore = false;
if (hw_cap_) {
if ((hw_cap_ < sw_cap_) || (sw_cap_ == 0)) {
sw_cap_ = hw_cap_;
backup = true;
} else if (hw_cap_ > sw_cap_) {
hw_cap_ = sw_cap_;
restore = true;
}
}
if (restore)
SaveToSRAM();
if (backup)
SaveToStorage();
}

} // namespace health
} // namespace wahoo
} // namespace google
} // namespace device
52 changes: 52 additions & 0 deletions health/LearnedCapacityBackupRestore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2018 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.
*/

#ifndef DEVICE_GOOGLE_WAHOO_HEALTH_LEARNEDCAPACITYBACKUPRESTORE_H
#define DEVICE_GOOGLE_WAHOO_HEALTH_LEARNEDCAPACITYBACKUPRESTORE_H

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

namespace device {
namespace google {
namespace wahoo {
namespace health {

class LearnedCapacityBackupRestore {
public:
LearnedCapacityBackupRestore();
void Restore();
void Backup();

private:
int sw_cap_;
int hw_cap_;

void ReadFromStorage();
void SaveToStorage();
void ReadFromSRAM();
void SaveToSRAM();
void UpdateAndSave();
};

} // namespace health
} // namespace wahoo
} // namespace google
} // namespace device

#endif // #ifndef DEVICE_GOOGLE_WAHOO_HEALTH_LEARNEDCAPACITYBACKUPRESTORE_H
5 changes: 5 additions & 0 deletions init.hardware.rc
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ on early-boot
# HardwareInfo needs to be able to read CC bins
chmod 644 /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998@2:qpnp,fg/cycle_counts_bins

# dumpstate needs to read, vendor.health-hal needs to be able to RW
chown system system /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998@2:qpnp,fg/power_supply/bms/charge_full
# HardwareInfo needs to be able to read charge_full
chmod 644 /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998@2:qpnp,fg/power_supply/bms/charge_full

on boot
mkdir /dev/socket/qmux_radio 0770 radio radio
chmod 2770 /dev/socket/qmux_radio
Expand Down

0 comments on commit 2fa48ee

Please sign in to comment.