From ba51d9197fbbca58e311ea4722d0c6c63107dff1 Mon Sep 17 00:00:00 2001 From: roynatech2544 Date: Wed, 5 Oct 2022 09:59:37 +0000 Subject: [PATCH] universal7885: Move parts HAL to AIDL impl --- .../aidl-support/parts/default/Android.bp | 22 +++++ .../aidl-support/parts/default/Battery.cpp | 64 ++++++++++++++ .../apps/aidl-support/parts/default/Battery.h | 32 +++++++ .../parts/default/BatteryConstants.h | 13 +++ .../aidl-support/parts/default/Display.cpp | 42 +++++++++ .../apps/aidl-support/parts/default/Display.h | 26 ++++++ .../aidl-support/parts/default/FlashLight.cpp | 44 ++++++++++ .../aidl-support/parts/default/FlashLight.h | 28 ++++++ .../parts/default/SmartCharge.cpp | 68 +++++++++++++++ .../aidl-support/parts/default/SmartCharge.h | 30 +++++++ .../parts/default/SmartChargingImpl.cpp | 43 ++++++++++ .../parts/default/SmartChargingImpl.h | 30 +++++++ .../apps/aidl-support/parts/default/Swap.cpp | 85 +++++++++++++++++++ .../apps/aidl-support/parts/default/Swap.h | 31 +++++++ .../parts/default/SwapHelpers.cpp | 82 ++++++++++++++++++ .../aidl-support/parts/default/service.cpp | 50 +++++++++++ .../vendor.eureka.hardware.parts-service.rc | 7 ++ .../default/vendor.eureka.hardware.parts.xml | 10 +++ 18 files changed, 707 insertions(+) create mode 100644 universal7885-common/apps/aidl-support/parts/default/Android.bp create mode 100644 universal7885-common/apps/aidl-support/parts/default/Battery.cpp create mode 100644 universal7885-common/apps/aidl-support/parts/default/Battery.h create mode 100644 universal7885-common/apps/aidl-support/parts/default/BatteryConstants.h create mode 100644 universal7885-common/apps/aidl-support/parts/default/Display.cpp create mode 100644 universal7885-common/apps/aidl-support/parts/default/Display.h create mode 100644 universal7885-common/apps/aidl-support/parts/default/FlashLight.cpp create mode 100644 universal7885-common/apps/aidl-support/parts/default/FlashLight.h create mode 100644 universal7885-common/apps/aidl-support/parts/default/SmartCharge.cpp create mode 100644 universal7885-common/apps/aidl-support/parts/default/SmartCharge.h create mode 100644 universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.cpp create mode 100644 universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.h create mode 100644 universal7885-common/apps/aidl-support/parts/default/Swap.cpp create mode 100644 universal7885-common/apps/aidl-support/parts/default/Swap.h create mode 100644 universal7885-common/apps/aidl-support/parts/default/SwapHelpers.cpp create mode 100644 universal7885-common/apps/aidl-support/parts/default/service.cpp create mode 100644 universal7885-common/apps/aidl-support/parts/default/vendor.eureka.hardware.parts-service.rc create mode 100644 universal7885-common/apps/aidl-support/parts/default/vendor.eureka.hardware.parts.xml diff --git a/universal7885-common/apps/aidl-support/parts/default/Android.bp b/universal7885-common/apps/aidl-support/parts/default/Android.bp new file mode 100644 index 0000000..893c1c6 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/Android.bp @@ -0,0 +1,22 @@ +cc_binary { + name: "vendor.eureka.hardware.parts-service", + defaults: ["eureka_defaults"], + srcs: [ + "Battery.cpp", + "FlashLight.cpp", + "Display.cpp", + "Swap.cpp", + "SwapHelpers.cpp", + "SmartChargingImpl.cpp", + "SmartCharge.cpp", + "service.cpp", + ], + shared_libs: [ + "libbase", + "libbinder_ndk", + "libfileio", + "vendor.eureka.hardware.parts-ndk", + ], + init_rc: ["vendor.eureka.hardware.parts-service.rc"], + vintf_fragments: ["vendor.eureka.hardware.parts.xml"], +} diff --git a/universal7885-common/apps/aidl-support/parts/default/Battery.cpp b/universal7885-common/apps/aidl-support/parts/default/Battery.cpp new file mode 100644 index 0000000..efe39ce --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/Battery.cpp @@ -0,0 +1,64 @@ +// Copyright (C) 2021 Eureka Team +// +// 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 + +#include "Battery.h" +#include "BatteryConstants.h" + +#include + +namespace aidl::vendor::eureka::hardware::parts { + +static inline const char *BatterySysToPath(BatterySys type) { + switch (type) { + case BatterySys::CAPACITY_MAX: + return BATTERY_CAPACITY_MAX; + case BatterySys::TEMP: + return BATTERY_TEMP; + case BatterySys::CAPACITY_CURRENT: + return BATTERY_CAPACITY_CURRENT; + case BatterySys::CURRENT: + return BATTERY_CURRENT; + case BatterySys::FASTCHARGE: + return BATTERY_FASTCHARGE; + case BatterySys::CHARGE: + return BATTERY_CHARGE; + default: + return ""; + } +} + +// Methods from ::android::hardware::battery::V1_0::IBattery follow. +::ndk::ScopedAStatus BatteryStats::getBatteryStats(BatterySys stats, + int32_t *_aidl_return) { + *_aidl_return = FileIO::readline(BatterySysToPath(stats)); + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus BatteryStats::setBatteryWritable(BatterySys stats, + bool value) { + bool FastCharge = false; + if (FastCharge) + seteuid(ANDROID_SYSTEM_UID); + + FileIO::writeline(BatterySysToPath(stats), value ? 1 : 0); + + if (FastCharge) + seteuid(ANDROID_ROOT_UID); + + return ::ndk::ScopedAStatus::ok(); +} + +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/Battery.h b/universal7885-common/apps/aidl-support/parts/default/Battery.h new file mode 100644 index 0000000..e64386b --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/Battery.h @@ -0,0 +1,32 @@ +// Copyright (C) 2021 Eureka Team +// +// 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. + +#pragma once + +#include + +#define ANDROID_SYSTEM_UID 1000 +#define ANDROID_ROOT_UID 0 + +namespace aidl::vendor::eureka::hardware::parts { + +struct BatteryStats : public BnBatteryStats { + // Methods from ::aidl::::vendor::eureka::hardware::parts::IBatteryStats + // follow. + ::ndk::ScopedAStatus getBatteryStats(BatterySys stats, + int32_t *_aidl_return) override; + ::ndk::ScopedAStatus setBatteryWritable(BatterySys stats, + bool value) override; +}; +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/BatteryConstants.h b/universal7885-common/apps/aidl-support/parts/default/BatteryConstants.h new file mode 100644 index 0000000..c066181 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/BatteryConstants.h @@ -0,0 +1,13 @@ +#pragma once + +constexpr const char *BATTERY_CAPACITY_MAX = + "/sys/devices/platform/battery/power_supply/battery/charge_full"; +constexpr const char *BATTERY_TEMP = + "/sys/devices/platform/battery/power_supply/battery/batt_temp"; +constexpr const char *BATTERY_CAPACITY_CURRENT = + "/sys/devices/platform/battery/power_supply/battery/capacity"; +constexpr const char *BATTERY_CURRENT = + "/sys/devices/platform/battery/power_supply/battery/current_now"; +constexpr const char *BATTERY_FASTCHARGE = "/sys/class/sec/switch/afc_disable"; +constexpr const char *BATTERY_CHARGE = + "/sys/devices/platform/battery/power_supply/battery/batt_slate_mode"; diff --git a/universal7885-common/apps/aidl-support/parts/default/Display.cpp b/universal7885-common/apps/aidl-support/parts/default/Display.cpp new file mode 100644 index 0000000..4441432 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/Display.cpp @@ -0,0 +1,42 @@ +// Copyright (C) 2021 Eureka Team +// +// 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 "Display.h" + +#include + +#include + +namespace aidl::vendor::eureka::hardware::parts { + +constexpr const char *SEC_TSP_CMD = "/sys/class/sec/tsp/cmd"; + +::ndk::ScopedAStatus DisplayConfigs::writeDisplay(bool enable, + DisplaySys type) { + std::string writevalue; + if (type == DisplaySys::DOUBLE_TAP) { + writevalue = "aot_enable"; + } else if (type == DisplaySys::GLOVE_MODE) { + writevalue = "glove_mode"; + } + writevalue += ","; + if (enable) { + writevalue += "1"; + } else { + writevalue += "0"; + } + FileIO::writeline(SEC_TSP_CMD, writevalue); + return ::ndk::ScopedAStatus::ok(); +} +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/Display.h b/universal7885-common/apps/aidl-support/parts/default/Display.h new file mode 100644 index 0000000..091afe8 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/Display.h @@ -0,0 +1,26 @@ +// Copyright (C) 2021 Eureka Team +// +// 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. + +#pragma once + +#include + +namespace aidl::vendor::eureka::hardware::parts { + +struct DisplayConfigs : public BnDisplayConfigs { + // Methods from ::aidl::vendor::eureka::hardware::parts::IDisplayConfigs + // follow. + ::ndk::ScopedAStatus writeDisplay(bool enable, DisplaySys type); +}; +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/FlashLight.cpp b/universal7885-common/apps/aidl-support/parts/default/FlashLight.cpp new file mode 100644 index 0000000..3d09e9f --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/FlashLight.cpp @@ -0,0 +1,44 @@ +// Copyright (C) 2021 Eureka Team +// +// 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 "FlashLight.h" + +#include + +namespace aidl::vendor::eureka::hardware::parts { + +constexpr const char *TORCH_ENABLE = + "/sys/class/camera/flash/torch_brightness_lvl_enable"; +constexpr const char *TORCH_LVL = + "/sys/class/camera/flash/torch_brightness_lvl"; + +// Methods from ::android::hardware::parts::V1_0::IFlashLight follow. +::ndk::ScopedAStatus FlashBrightness::setFlashlightEnable(bool enable) { + FileIO::writeline(TORCH_ENABLE, enable ? 1 : 0); + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus FlashBrightness::setFlashlightWritable(int32_t value) { + FileIO::writeline(TORCH_LVL, value); + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus +FlashBrightness::readFlashlightstats(bool s2mu106, int32_t *_aidl_return) { + *_aidl_return = + s2mu106 ? FileIO::readline(TORCH_LVL) / 21 : FileIO::readline(TORCH_LVL); + return ::ndk::ScopedAStatus::ok(); +} + +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/FlashLight.h b/universal7885-common/apps/aidl-support/parts/default/FlashLight.h new file mode 100644 index 0000000..f268780 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/FlashLight.h @@ -0,0 +1,28 @@ +// Copyright (C) 2021 Eureka Team +// +// 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. + +#pragma once + +#include + +namespace aidl::vendor::eureka::hardware::parts { + +struct FlashBrightness : public BnFlashBrightness { + // Methods from ::aidl::vendor::eureka::hardware::parts::IFlashBrightness + // follow. + ::ndk::ScopedAStatus setFlashlightEnable(bool enable); + ::ndk::ScopedAStatus setFlashlightWritable(int32_t value); + ::ndk::ScopedAStatus readFlashlightstats(bool s2mu106, int32_t *_aidl_return); +}; +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/SmartCharge.cpp b/universal7885-common/apps/aidl-support/parts/default/SmartCharge.cpp new file mode 100644 index 0000000..3cda360 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/SmartCharge.cpp @@ -0,0 +1,68 @@ +// Copyright (C) 2021 Eureka Team +// +// 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 "SmartCharge.h" +#include "SmartChargingImpl.h" + +#include +#include +#include + +namespace aidl::vendor::eureka::hardware::parts { + +static std::unique_ptr kInst; + +static int limit = 0; +static int restart = 0; + +static int limit_stat = 0; +static int restart_stat = 0; + +::ndk::ScopedAStatus SmartCharge::start(void) { + if (limit == 0 || restart == 0) + return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage( + EX_ILLEGAL_ARGUMENT, "Start called without configuring."); + kInst = std::make_unique(limit, restart); + kInst->start(); + 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; + kInst.reset(); + } + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus SmartCharge::setConfig(int32_t limit_user, + int32_t restart_user) { + limit = limit_user; + restart = restart_user; + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus SmartCharge::getLimitCnt(int32_t *_aidl_return) { + *_aidl_return = limit_stat; + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus SmartCharge::getRestartCnt(int32_t *_aidl_return) { + *_aidl_return = restart_stat; + return ::ndk::ScopedAStatus::ok(); +} + +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/SmartCharge.h b/universal7885-common/apps/aidl-support/parts/default/SmartCharge.h new file mode 100644 index 0000000..bb5c611 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/SmartCharge.h @@ -0,0 +1,30 @@ +// Copyright (C) 2021 Eureka Team +// +// 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. + +#pragma once + +#include + +namespace aidl::vendor::eureka::hardware::parts { + +struct SmartCharge : public BnSmartCharge { + // Methods from ::aidl::vendor::eureka::hardware::parts::ISmartCharge + // follow. + ::ndk::ScopedAStatus start(void); + ::ndk::ScopedAStatus stop(void); + ::ndk::ScopedAStatus setConfig(int32_t limit, int32_t restart); + ::ndk::ScopedAStatus getLimitCnt(int32_t *_aidl_return); + ::ndk::ScopedAStatus getRestartCnt(int32_t *_aidl_return); +}; +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.cpp b/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.cpp new file mode 100644 index 0000000..02739d2 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.cpp @@ -0,0 +1,43 @@ +#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); + monitor->join(); +} + +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 new file mode 100644 index 0000000..77c7474 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/SmartChargingImpl.h @@ -0,0 +1,30 @@ +#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); } diff --git a/universal7885-common/apps/aidl-support/parts/default/Swap.cpp b/universal7885-common/apps/aidl-support/parts/default/Swap.cpp new file mode 100644 index 0000000..cd567e3 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/Swap.cpp @@ -0,0 +1,85 @@ +// Copyright (C) 2021 Eureka Team +// +// 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 "Swap.h" +#include +#include +#include +#include +#include +#include + +static int mSwapSize = 100; +extern int mkswap(const char *filename); +extern void mkfile(int filesize, const char *name); + +constexpr const char *SWAP_PATH = "/data/swap/swapfile"; + +namespace aidl::vendor::eureka::hardware::parts { + +static std::mutex thread_lock; + +static bool swapOnRes = false; + +::ndk::ScopedAStatus SwapOnData::setSwapSize(int32_t size) { + mSwapSize = size; + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus SwapOnData::removeSwapFile(void) { + const std::lock_guard lock(thread_lock); + std::remove(SWAP_PATH); + return ::ndk::ScopedAStatus::ok(); +} + +static void mkfile_swapon_thread(void) { + const std::lock_guard lock(thread_lock); + if (access(SWAP_PATH, F_OK) == 0) { + mkfile(mSwapSize * 10, SWAP_PATH); + mkswap(SWAP_PATH); + } + int res = + swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK); + swapOnRes = res == 0; +} + +::ndk::ScopedAStatus SwapOnData::setSwapOn() { + const std::lock_guard lock(thread_lock); + std::thread mkswapfile(mkfile_swapon_thread); + mkswapfile.join(); + return ::ndk::ScopedAStatus::ok(); +} + +static void swapoff_thread(void) { + const std::lock_guard lock(thread_lock); + swapoff(SWAP_PATH); +} + +::ndk::ScopedAStatus SwapOnData::setSwapOff() { + std::thread swapoff(swapoff_thread); + swapoff.join(); + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus SwapOnData::getSwapOnResult(bool *_aidl_return) { + *_aidl_return = swapOnRes; + return ::ndk::ScopedAStatus::ok(); +} + +::ndk::ScopedAStatus SwapOnData::isMutexLocked(bool *_aidl_return) { + *_aidl_return = !thread_lock.try_lock(); + return ::ndk::ScopedAStatus::ok(); +} + +} // namespace aidl::vendor::eureka::hardware::parts diff --git a/universal7885-common/apps/aidl-support/parts/default/Swap.h b/universal7885-common/apps/aidl-support/parts/default/Swap.h new file mode 100644 index 0000000..6125766 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/Swap.h @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Eureka Team +// +// 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. + +#pragma once + +#include + +namespace aidl::vendor::eureka::hardware::parts { + +struct SwapOnData : public BnSwapOnData { + // Methods from ::aidl::vendor::eureka::hardware::parts::ISwapOnData + // follow. + ::ndk::ScopedAStatus setSwapSize(int32_t size); + ::ndk::ScopedAStatus setSwapOn(void); + ::ndk::ScopedAStatus removeSwapFile(void); + ::ndk::ScopedAStatus setSwapOff(void); + ::ndk::ScopedAStatus getSwapOnResult(bool *_aidl_return); + ::ndk::ScopedAStatus isMutexLocked(bool *_aidl_return); +}; +} // namespace vendor::eureka::hardware::parts::V1_0 diff --git a/universal7885-common/apps/aidl-support/parts/default/SwapHelpers.cpp b/universal7885-common/apps/aidl-support/parts/default/SwapHelpers.cpp new file mode 100644 index 0000000..20b06f3 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/SwapHelpers.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +/* XXX This needs to be obtained from kernel headers. See b/9336527 */ +struct linux_swap_header { + char bootbits[1024]; /* Space for disklabel etc. */ + u_int32_t version; + u_int32_t last_page; + u_int32_t nr_badpages; + unsigned char sws_uuid[16]; + unsigned char sws_volume[16]; + u_int32_t padding[117]; + u_int32_t badpages[1]; +}; +void mkfile(int filesize, const char *path) { + std::vector empty(1024, 0); + std::ofstream ofs(std::string(path), std::ios::binary | std::ios::out); + + for (int i = 0; i < 1024 * filesize; i++) { + ofs.write(&empty[0], empty.size()); + } +} +#define MAGIC_SWAP_HEADER "SWAPSPACE2" +#define MAGIC_SWAP_HEADER_LEN 10 +#define MIN_PAGES 10 +int mkswap(const char *filename) { + int err = 0; + int fd; + ssize_t len; + off_t swap_size; + int pagesize; + struct linux_swap_header sw_hdr; + fd = open(filename, O_WRONLY | O_CLOEXEC); + if (fd < 0) { + err = errno; + return err; + } + pagesize = getpagesize(); + /* Determine the length of the swap file */ + swap_size = lseek(fd, 0, SEEK_END); + if (swap_size < MIN_PAGES * pagesize) { + err = -ENOSPC; + goto err; + } + if (lseek(fd, 0, SEEK_SET)) { + err = errno; + goto err; + } + memset(&sw_hdr, 0, sizeof(sw_hdr)); + sw_hdr.version = 1; + sw_hdr.last_page = (swap_size / pagesize) - 1; + len = write(fd, &sw_hdr, sizeof(sw_hdr)); + if (len != sizeof(sw_hdr)) { + err = errno; + goto err; + } + /* Write the magic header */ + if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) { + err = errno; + goto err; + } + len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN); + if (len != MAGIC_SWAP_HEADER_LEN) { + err = errno; + goto err; + } + if (fsync(fd) < 0) { + err = errno; + goto err; + } +err: + close(fd); + return err; +} diff --git a/universal7885-common/apps/aidl-support/parts/default/service.cpp b/universal7885-common/apps/aidl-support/parts/default/service.cpp new file mode 100644 index 0000000..697c024 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/service.cpp @@ -0,0 +1,50 @@ +// Copyright (C) 2021 Eureka Team +// +// 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 +#include +#include + +#include "Battery.h" +#include "Display.h" +#include "FlashLight.h" +#include "SmartCharge.h" +#include "Swap.h" + +using ::aidl::vendor::eureka::hardware::parts::BatteryStats; +using ::aidl::vendor::eureka::hardware::parts::DisplayConfigs; +using ::aidl::vendor::eureka::hardware::parts::FlashBrightness; +using ::aidl::vendor::eureka::hardware::parts::SmartCharge; +using ::aidl::vendor::eureka::hardware::parts::SwapOnData; + +template static void registerAsService(std::shared_ptr service) { + const std::string instance = std::string() + C::descriptor + "/default"; + binder_status_t status = + AServiceManager_addService(service->asBinder().get(), instance.c_str()); + CHECK(status == STATUS_OK); + LOG(INFO) << "Register done for " << C::descriptor; +} + +int main() { + ABinderProcess_setThreadPoolMaxThreadCount(0); + + registerAsService(ndk::SharedRefBase::make()); + registerAsService(ndk::SharedRefBase::make()); + registerAsService(ndk::SharedRefBase::make()); + registerAsService(ndk::SharedRefBase::make()); + registerAsService(ndk::SharedRefBase::make()); + + ABinderProcess_joinThreadPool(); + return -1; // should never get here +} diff --git a/universal7885-common/apps/aidl-support/parts/default/vendor.eureka.hardware.parts-service.rc b/universal7885-common/apps/aidl-support/parts/default/vendor.eureka.hardware.parts-service.rc new file mode 100644 index 0000000..26bbdab --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/vendor.eureka.hardware.parts-service.rc @@ -0,0 +1,7 @@ +service parts-hal-aidl /system/bin/vendor.eureka.hardware.parts-service + class hal + user root + group root + +on post-fs-data + mkdir /data/swap 0755 root root encryption=None diff --git a/universal7885-common/apps/aidl-support/parts/default/vendor.eureka.hardware.parts.xml b/universal7885-common/apps/aidl-support/parts/default/vendor.eureka.hardware.parts.xml new file mode 100644 index 0000000..dd05885 --- /dev/null +++ b/universal7885-common/apps/aidl-support/parts/default/vendor.eureka.hardware.parts.xml @@ -0,0 +1,10 @@ + + + vendor.eureka.hardware.fmradio + IBatteryStats/default + IDisplayConfigs/default + IFlashBrightness/default + ISwapOnData/default + ISmartCharge/default + +