Skip to content

Commit

Permalink
twrp: use health services for battery capacity
Browse files Browse the repository at this point in the history
Change-Id: I99dfb82238e2b3ae90932af0c7fd74822d971232
Signed-off-by: Mohd Faraz <[email protected]>
  • Loading branch information
AndroiableDroid committed Sep 17, 2023
1 parent 44e2346 commit d4df003
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 54 deletions.
9 changes: 2 additions & 7 deletions Android.mk
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ else
LOCAL_CFLAGS += -DTW_EXCLUDE_APEX
endif

LOCAL_STATIC_LIBRARIES += libavb libtwrpinstall libminadbd_services libinit libsnapshot_nobinder update_metadata-protos
LOCAL_SHARED_LIBRARIES += libfs_mgr libhardware [email protected] [email protected] [email protected] libprotobuf-cpp-lite liblp libutils libhidlbase
LOCAL_STATIC_LIBRARIES += libavb libtwrpinstall libminadbd_services libinit libsnapshot_nobinder update_metadata-protos librecovery_utils libhealthhalutils
LOCAL_SHARED_LIBRARIES += libfs_mgr libhardware [email protected] [email protected] [email protected] libprotobuf-cpp-lite liblp libutils libhidlbase [email protected]
LOCAL_C_INCLUDES += \
system/core/fs_mgr/libfs_avb/include/ \
system/core/fs_mgr/include_fstab/ \
Expand Down Expand Up @@ -378,11 +378,6 @@ endif
ifneq ($(TW_CUSTOM_BATTERY_PATH),)
LOCAL_CFLAGS += -DTW_CUSTOM_BATTERY_PATH=$(TW_CUSTOM_BATTERY_PATH)
endif
ifneq ($(TW_BATTERY_SYSFS_WAIT_SECONDS),)
LOCAL_CFLAGS += -DTW_BATTERY_SYSFS_WAIT_SECONDS=$(TW_BATTERY_SYSFS_WAIT_SECONDS)
else
LOCAL_CFLAGS += -DTW_BATTERY_SYSFS_WAIT_SECONDS=3
endif
ifneq ($(TW_CUSTOM_CPU_TEMP_PATH),)
LOCAL_CFLAGS += -DTW_CUSTOM_CPU_TEMP_PATH=$(TW_CUSTOM_CPU_TEMP_PATH)
endif
Expand Down
38 changes: 8 additions & 30 deletions data.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "set_metadata.h"
#include "gui/gui.hpp"
#include "infomanager.hpp"
#include "recovery_utils/battery_utils.h"

#define DEVID_MAX 64
#define HWID_MAX 32
Expand Down Expand Up @@ -1041,37 +1042,14 @@ int DataManager::GetMagicValue(const string& varName, string& value)
gettimeofday(&curTime, NULL);
if (curTime.tv_sec > nextSecCheck)
{
char cap_s[4];
#ifdef TW_CUSTOM_BATTERY_PATH
string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
capacity_file += "/capacity";
FILE * cap = fopen(capacity_file.c_str(),"rt");
#else
FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
#endif
if (cap) {
fgets(cap_s, 4, cap);
fclose(cap);
lastVal = atoi(cap_s);
if (lastVal > 100) lastVal = 101;
if (lastVal < 0) lastVal = 0;
}
#ifdef TW_CUSTOM_BATTERY_PATH
string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
status_file += "/status";
cap = fopen(status_file.c_str(),"rt");
#else
cap = fopen("/sys/class/power_supply/battery/status","rt");
#endif
if (cap) {
fgets(cap_s, 2, cap);
fclose(cap);
if (cap_s[0] == 'C')
charging = '+';
else
charging = ' ';
auto battery_info = GetBatteryInfo();
if (battery_info.charging) {
charging = '+';
} else {
charging = ' ';
}
nextSecCheck = curTime.tv_sec + 60;
lastVal = battery_info.capacity;
nextSecCheck = curTime.tv_sec + 1;
}

sprintf(tmp, "%i%%%c", lastVal, charging);
Expand Down
4 changes: 0 additions & 4 deletions kernel_module_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ bool KernelModuleLoader::Load_Vendor_Modules() {

android::base::SetProperty("twrp.modules.loaded", "true");

#ifdef TW_BATTERY_SYSFS_WAIT_SECONDS
TWFunc::Wait_For_Battery(std::chrono::seconds(TW_BATTERY_SYSFS_WAIT_SECONDS));
#endif

return true;
}

Expand Down
12 changes: 0 additions & 12 deletions twrp-functions.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -424,18 +424,6 @@ bool TWFunc::Wait_For_File(const string& path, std::chrono::nanoseconds timeout)
return false;
}

bool TWFunc::Wait_For_Battery(std::chrono::nanoseconds timeout) {
std::string battery_path;
#ifdef TW_CUSTOM_BATTERY_PATH
battery_path = EXPAND(TW_CUSTOM_BATTERY_PATH);
#else
battery_path = "/sys/class/power_supply/battery";
#endif
if (!battery_path.empty()) return TWFunc::Wait_For_File(battery_path, timeout);

return false;
}

#ifndef BUILD_TWRPTAR_MAIN

// Returns "/path" from a full /path/to/file.name
Expand Down
1 change: 0 additions & 1 deletion twrp-functions.hpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class TWFunc
static timespec timespec_diff(timespec& start, timespec& end); // Return a diff for 2 times
static int32_t timespec_diff_ms(timespec& start, timespec& end); // Returns diff in ms
static bool Wait_For_File(const string& path, std::chrono::nanoseconds timeout); // Wait For File, True is success, False is timeout;
static bool Wait_For_Battery(std::chrono::nanoseconds timeout); // Wait For /sys/class/power_supply/battery or TW_CUSTOM_BATTERY_PATH, True is success, False is timeout;

#ifndef BUILD_TWRPTAR_MAIN
static int Recursive_Mkdir(string Path); // Recursively makes the entire path
Expand Down

0 comments on commit d4df003

Please sign in to comment.