From d9adb63d295b100dfac4556ff7a0ee625296b6d4 Mon Sep 17 00:00:00 2001 From: roynatech2544 Date: Mon, 17 Oct 2022 18:38:47 +0900 Subject: [PATCH] universal7885: parts-aidl: Move smartcharge code to one file --- .../smartcharge/SmartChargeFragment.kt | 1 - .../aidl-support/parts/default/Android.bp | 1 - .../parts/default/SmartCharge.cpp | 35 ++++++++++------ .../parts/default/SmartChargingImpl.cpp | 42 ------------------- .../parts/default/SmartChargingImpl.h | 30 ------------- 5 files changed, 22 insertions(+), 87 deletions(-) delete mode 100644 universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.cpp delete mode 100644 universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.h diff --git a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/smartcharge/SmartChargeFragment.kt b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/smartcharge/SmartChargeFragment.kt index 07d607a..8f3673d 100644 --- a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/smartcharge/SmartChargeFragment.kt +++ b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/smartcharge/SmartChargeFragment.kt @@ -164,7 +164,6 @@ class SmartChargeFragment : PreferenceFragmentCompat(), OnMainSwitchChangeListen mPoolExecutor.scheduleWithFixedDelay(mScheduler, 0, 5, TimeUnit.MINUTES) } else { mSmartCharge.stop() - mPoolExecutor.shutdown() } mSharedPreferences.edit().putBoolean(PREF_SMARTCHARGE_MAIN, isChecked).apply() } diff --git a/universal7885-common/apps/aidl-support/parts/default/Android.bp b/universal7885-common/apps/aidl-support/parts/default/Android.bp index f0a9fb1..8a5cea6 100644 --- a/universal7885-common/apps/aidl-support/parts/default/Android.bp +++ b/universal7885-common/apps/aidl-support/parts/default/Android.bp @@ -7,7 +7,6 @@ cc_binary { "Display.cpp", "Swap.cpp", "SwapHelpers.cpp", - "SmartChargingImpl.cpp", "SmartCharge.cpp", "service.cpp", ], diff --git a/universal7885-common/apps/aidl-support/parts/default/SmartCharge.cpp b/universal7885-common/apps/aidl-support/parts/default/SmartCharge.cpp index 22145df..5250439 100644 --- a/universal7885-common/apps/aidl-support/parts/default/SmartCharge.cpp +++ b/universal7885-common/apps/aidl-support/parts/default/SmartCharge.cpp @@ -13,37 +13,46 @@ // limitations under the License. #include "SmartCharge.h" -#include "SmartChargingImpl.h" -#include -#include +#include +#include namespace aidl::vendor::eureka::hardware::parts { -static SmartChargeImpl *kInst; - static int limit = 0; static int restart = 0; static int limit_stat = 0; static int restart_stat = 0; +static std::thread monitor_th = nullptr; + +static void battery_monitor(void) { + while (true) { + auto batt = FileIO::readline(BATTERY_CAPACITY_CURRENT); + if (batt >= limit) { + FileIO::writeline(BATTERY_CHARGE, 0); + limit_stat += 1; + } else if (batt <= restart) { + FileIO::writeline(BATTERY_CHARGE, 1); + restart_stat += 1; + } + std::this_thread::sleep_for(std::chrono::seconds(5)); + } +} + ::ndk::ScopedAStatus SmartCharge::start(void) { if (limit == 0 || restart == 0) return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage( EX_ILLEGAL_ARGUMENT, "Start called without configuring."); - kInst = new SmartChargeImpl(limit, restart); - kInst->start(); + + monitor_th = std::thread(battery_monitor); return ::ndk::ScopedAStatus::ok(); } ::ndk::ScopedAStatus SmartCharge::stop(void) { - if (kInst != nullptr) { - kInst->stop(); - limit_stat += kInst->charge_limit_cnt; - restart_stat += kInst->restart_cnt; - delete kInst; - kInst = nullptr; + if (monitor_th != nullptr) { + monitor_th = nullptr; } return ::ndk::ScopedAStatus::ok(); } diff --git a/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.cpp b/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.cpp deleted file mode 100644 index 3b9af85..0000000 --- a/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "SmartChargingImpl.h" - -#include -#include - -namespace aidl::vendor::eureka::hardware::parts { - -static std::thread *monitor; -static bool shouldRun = false; - -SmartChargeImpl::SmartChargeImpl(int limit, int restart) - : limit_percent(limit), restart_percent(restart) { - charge_limit_cnt = 0; - restart_cnt = 0; -} - -static void battery_monitor(const int limit_percent, const int restart_percent, - int *charge_limit_cnt, int *restart_cnt) { - while (shouldRun) { - auto batt = FileIO::readline(BATTERY_CAPACITY_CURRENT); - if (batt >= limit_percent) { - disableSysfs(BATTERY_CHARGE); - *charge_limit_cnt += 1; - } else if (batt <= restart_percent) { - enableSysfs(BATTERY_CHARGE); - *restart_cnt += 1; - } - std::this_thread::sleep_for(std::chrono::seconds(5)); - } -} - -void SmartChargeImpl::start(void) { - shouldRun = true; - monitor = new std::thread(battery_monitor, limit_percent, restart_percent, - &charge_limit_cnt, &restart_cnt); -} - -void SmartChargeImpl::stop(void) { - shouldRun = false; - monitor = nullptr; -} -} // namespace vendor::eureka::hardware::parts::V1_0 diff --git a/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.h b/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.h deleted file mode 100644 index 77c7474..0000000 --- a/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.h +++ /dev/null @@ -1,30 +0,0 @@ -#include "BatteryConstants.h" - -#include -#include - -#include - -namespace aidl::vendor::eureka::hardware::parts { - -class SmartChargeImpl { -public: - SmartChargeImpl(int limit, int restart); - void start(void); - void stop(void); - - int charge_limit_cnt; - int restart_cnt; - -private: - const int limit_percent; - const int restart_percent; -}; - -} // namespace vendor::eureka::hardware::parts::V1_0 - -static inline void disableSysfs(const char *path) { - FileIO::writeline(path, 0); -} - -static inline void enableSysfs(const char *path) { FileIO::writeline(path, 1); }