diff --git a/common.mk b/common.mk index c886bdc..90c5c18 100644 --- a/common.mk +++ b/common.mk @@ -362,6 +362,11 @@ PRODUCT_PACKAGES += \ android.hardware.power-service.lineage-libperfmgr \ libqti-perfd-client + +# Fast Charge HAL +PRODUCT_PACKAGES += \ + vendor.lineage.fastcharge@1.0-service.qcom + PRODUCT_COPY_FILES += \ $(LOCAL_PATH)/configs/powerhint.json:$(TARGET_COPY_OUT_VENDOR)/etc/powerhint.json diff --git a/fastcharge/1.0/Android.bp b/fastcharge/1.0/Android.bp new file mode 100644 index 0000000..9c918bc --- /dev/null +++ b/fastcharge/1.0/Android.bp @@ -0,0 +1,34 @@ +// +// Copyright (C) 2020-2024 The LineageOS 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. + +cc_binary { + name: "vendor.lineage.fastcharge@1.0-service.qcom", + relative_install_path: "hw", + proprietary: true, + srcs: [ + "FastCharge.cpp", + "service.cpp", + ], + init_rc: [ "vendor.lineage.fastcharge@1.0-service.qcom.rc" ], + vintf_fragments: [ "vendor.lineage.fastcharge@1.0-service.qcom.xml" ], + shared_libs: [ + "libbase", + "libbinder", + "libcutils", + "libhidlbase", + "libutils", + "vendor.lineage.fastcharge@1.0", + ], +} diff --git a/fastcharge/1.0/FastCharge.cpp b/fastcharge/1.0/FastCharge.cpp new file mode 100644 index 0000000..3b77d6e --- /dev/null +++ b/fastcharge/1.0/FastCharge.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2020-2024 The LineageOS 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. + */ + +#define LOG_TAG "fastcharge@1.0-service" +#define FASTCHARGE_PATH "/sys/class/qcom-battery/restrict_chg" +#define FASTCHARGE_DEFAULT_SETTING true + +#include "FastCharge.h" +#include +#include + +#include +#include + +namespace vendor { +namespace lineage { +namespace fastcharge { +namespace V1_0 { +namespace implementation { + +static constexpr const char* kFastChargingProp = "persist.vendor.fastchg_enabled"; + +/* + * Write value to path and close file. + */ +template +static void set(const std::string& path, const T& value) { + std::ofstream file(path); + + if (!file) { + PLOG(ERROR) << "Failed to open: " << path; + return; + } + + LOG(DEBUG) << "write: " << path << " value: " << value; + + file << value << std::endl; + + if (!file) { + PLOG(ERROR) << "Failed to write: " << path << " value: " << value; + } +} + +template +static T get(const std::string& path, const T& def) { + std::ifstream file(path); + + if (!file) { + PLOG(ERROR) << "Failed to open: " << path; + return def; + } + + T result; + + file >> result; + + if (file.fail()) { + PLOG(ERROR) << "Failed to read: " << path; + return def; + } else { + LOG(DEBUG) << "read: " << path << " value: " << result; + return result; + } +} + +FastCharge::FastCharge() { + setEnabled(property_get_bool(kFastChargingProp, FASTCHARGE_DEFAULT_SETTING)); +} + +Return FastCharge::isEnabled() { + return get(FASTCHARGE_PATH, 0) < 1; +} + +Return FastCharge::setEnabled(bool enable) { + set(FASTCHARGE_PATH, enable ? 0 : 1); + + bool enabled = isEnabled(); + property_set(kFastChargingProp, enabled ? "true" : "false"); + + return enabled; +} + +} // namespace implementation +} // namespace V1_0 +} // namespace fastcharge +} // namespace lineage +} // namespace vendor diff --git a/fastcharge/1.0/FastCharge.h b/fastcharge/1.0/FastCharge.h new file mode 100644 index 0000000..acfbdb7 --- /dev/null +++ b/fastcharge/1.0/FastCharge.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020-2024 The LineageOS 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. + */ + +#pragma once + +#include +#include +#include + +namespace vendor { +namespace lineage { +namespace fastcharge { +namespace V1_0 { +namespace implementation { + +using ::android::sp; +using ::android::hardware::hidl_array; +using ::android::hardware::hidl_memory; +using ::android::hardware::hidl_string; +using ::android::hardware::hidl_vec; +using ::android::hardware::Return; +using ::android::hardware::Void; + +using ::vendor::lineage::fastcharge::V1_0::IFastCharge; + + +struct FastCharge : public IFastCharge { + FastCharge(); + + Return isEnabled() override; + Return setEnabled(bool enable) override; +}; + +} // namespace implementation +} // namespace V1_0 +} // namespace fastcharge +} // namespace lineage +} // namespace vendor diff --git a/fastcharge/1.0/service.cpp b/fastcharge/1.0/service.cpp new file mode 100644 index 0000000..8ea0417 --- /dev/null +++ b/fastcharge/1.0/service.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020-2024 The LineageOS 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. + */ + +#define LOG_TAG "fastcharge@1.0-service" + +#include +#include + +#include "FastCharge.h" + +using android::hardware::configureRpcThreadpool; +using android::hardware::joinRpcThreadpool; + +using vendor::lineage::fastcharge::V1_0::IFastCharge; +using vendor::lineage::fastcharge::V1_0::implementation::FastCharge; + +using android::OK; +using android::status_t; + +int main() { + android::sp service = new FastCharge(); + + configureRpcThreadpool(1, true); + + status_t status = service->registerAsService(); + if (status != OK) { + LOG(ERROR) << "Cannot register FastCharge HAL service."; + return 1; + } + + LOG(INFO) << "FastCharge HAL service ready."; + + joinRpcThreadpool(); + + LOG(ERROR) << "FastCharge HAL service failed to join thread pool."; + return 1; +} diff --git a/fastcharge/1.0/vendor.lineage.fastcharge@1.0-service.qcom.rc b/fastcharge/1.0/vendor.lineage.fastcharge@1.0-service.qcom.rc new file mode 100644 index 0000000..a065782 --- /dev/null +++ b/fastcharge/1.0/vendor.lineage.fastcharge@1.0-service.qcom.rc @@ -0,0 +1,2 @@ +service vendor.fastcharge-hal-1-0 /vendor/bin/hw/vendor.lineage.fastcharge@1.0-service.qcom + class hal diff --git a/fastcharge/1.0/vendor.lineage.fastcharge@1.0-service.qcom.xml b/fastcharge/1.0/vendor.lineage.fastcharge@1.0-service.qcom.xml new file mode 100644 index 0000000..0c55ea5 --- /dev/null +++ b/fastcharge/1.0/vendor.lineage.fastcharge@1.0-service.qcom.xml @@ -0,0 +1,11 @@ + + + vendor.lineage.fastcharge + hwbinder + 1.0 + + IFastCharge + default + + + diff --git a/sepolicy/vendor/file_contexts b/sepolicy/vendor/file_contexts index dd9dffd..12daa75 100644 --- a/sepolicy/vendor/file_contexts +++ b/sepolicy/vendor/file_contexts @@ -114,3 +114,6 @@ /sys/devices/virtual/input/input[0-9]+/wakeup[0-9]+(/.*)? u:object_r:sysfs_wakeup:s0 /sys/devices/platform/soc/[^*]+/wakeup/wakeup[0-9]+(/.*)? u:object_r:sysfs_wakeup:s0 /sys/devices/virtual/misc/[^*]+/wakeup[0-9]+(/.*)? u:object_r:sysfs_wakeup:s0 + +# Fast Charge HAL +/vendor/bin/hw/vendor\.lineage\.fastcharge@1\.0-service\.qcom u:object_r:hal_lineage_fastcharge_default_exec:s0 diff --git a/sepolicy/vendor/hal_fastcharge_default.te b/sepolicy/vendor/hal_fastcharge_default.te new file mode 100644 index 0000000..0f3522d --- /dev/null +++ b/sepolicy/vendor/hal_fastcharge_default.te @@ -0,0 +1,6 @@ +# Allow read/write access to /sys/class/qcom-battery/.* +allow hal_lineage_fastcharge_default sysfs_battery_supply:file rw_file_perms; +allow hal_lineage_fastcharge_default sysfs_battery_supply:dir search; + +# Allow control of sysprop persist.vendor.fastchg_enabled +set_prop(hal_lineage_fastcharge_default, vendor_fastcharge_prop) diff --git a/sepolicy/vendor/property.te b/sepolicy/vendor/property.te index 167417c..96a2c2a 100644 --- a/sepolicy/vendor/property.te +++ b/sepolicy/vendor/property.te @@ -5,3 +5,6 @@ vendor_internal_prop(vendor_mot_fingerprint_prop); vendor_internal_prop(vendor_mot_hw_prop); vendor_internal_prop(vendor_mot_touch_prop); vendor_internal_prop(vendor_mot_nfc_prop); + +# For Fast Charge HAL +vendor_internal_prop(vendor_fastcharge_prop) diff --git a/sepolicy/vendor/property_contexts b/sepolicy/vendor/property_contexts index f1e63b1..dcfca64 100644 --- a/sepolicy/vendor/property_contexts +++ b/sepolicy/vendor/property_contexts @@ -21,3 +21,6 @@ vendor.nfc.fw_status u:object_r:vendor_mot_nfc_prop:s0 persist.vendor.hardware.fingerprint u:object_r:vendor_mot_fingerprint_prop:s0 vendor.hw.fps.ident u:object_r:vendor_mot_fingerprint_prop:s0 vendor.hw.fingerprint.status u:object_r:vendor_mot_fingerprint_prop:s0 + +# Fast Charge +persist.vendor.fastchg_enabled u:object_r:vendor_fastcharge_prop:s0