diff --git a/universal7885-common/interfaces/Android.bp b/universal7885-common/interfaces/Android.bp new file mode 100644 index 0000000..4f71477 --- /dev/null +++ b/universal7885-common/interfaces/Android.bp @@ -0,0 +1,3 @@ +hidl_package_root{ + name: "vendor.eureka", +} diff --git a/universal7885-common/interfaces/hardware/battery/1.0/Android.bp b/universal7885-common/interfaces/hardware/battery/1.0/Android.bp new file mode 100644 index 0000000..d6917b5 --- /dev/null +++ b/universal7885-common/interfaces/hardware/battery/1.0/Android.bp @@ -0,0 +1,10 @@ +hidl_interface { + name: "vendor.eureka.hardware.battery@1.0", + root: "vendor.eureka", + srcs: [ + "types.hal", + "IBattery.hal", + ], + interfaces: [ "android.hidl.base@1.0" ], + gen_java: true, +} diff --git a/universal7885-common/interfaces/hardware/battery/1.0/IBattery.hal b/universal7885-common/interfaces/hardware/battery/1.0/IBattery.hal new file mode 100644 index 0000000..0dc7e36 --- /dev/null +++ b/universal7885-common/interfaces/hardware/battery/1.0/IBattery.hal @@ -0,0 +1,20 @@ +// 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. + +package vendor.eureka.hardware.battery@1.0; + +interface IBattery { + getBatteryStats(SysfsType stats) generates (int32_t result); + setBatteryWritable(SysfsType stats, Number value) generates (int32_t result); +}; diff --git a/universal7885-common/interfaces/hardware/battery/1.0/default/Android.bp b/universal7885-common/interfaces/hardware/battery/1.0/default/Android.bp new file mode 100644 index 0000000..3fe3db0 --- /dev/null +++ b/universal7885-common/interfaces/hardware/battery/1.0/default/Android.bp @@ -0,0 +1,17 @@ +// FIXME: your file license if you have one + +cc_binary { + name: "vendor.eureka.hardware.battery@1.0-service", + relative_install_path: "hw", + proprietary: true, + srcs: [ + "Battery.cpp", + "service.cpp", + ], + shared_libs: [ + "libhidlbase", + "libutils", + "liblog", + "vendor.eureka.hardware.battery@1.0", + ], +} diff --git a/universal7885-common/interfaces/hardware/battery/1.0/default/Battery.cpp b/universal7885-common/interfaces/hardware/battery/1.0/default/Battery.cpp new file mode 100644 index 0000000..627379c --- /dev/null +++ b/universal7885-common/interfaces/hardware/battery/1.0/default/Battery.cpp @@ -0,0 +1,102 @@ +// 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 "Battery.h" +#include +#include +#include +namespace vendor::eureka::hardware::battery::V1_0 { + +// Methods from ::android::hardware::battery::V1_0::IBattery follow. +Return Battery::getBatteryStats(battery::V1_0::SysfsType stats) { + std::ifstream file; + std::string filename; + switch (stats){ + case SysfsType::CAPACITY_MAX: + filename = "/sys/devices/platform/battery/power_supply/battery/charge_full"; + break; + case SysfsType::TEMP: + filename = "/sys/devices/platform/battery/power_supply/battery/batt_temp"; + break; + case SysfsType::CAPACITY_CURRENT: + filename = "/sys/devices/platform/battery/power_supply/battery/capacity"; + break; + case SysfsType::CURRENT: + filename = "/sys/devices/platform/battery/power_supply/battery/current_now"; + break; + case SysfsType::FASTCHARGE: + filename = "/sys/class/sec/switch/afc_disable"; + break; + case SysfsType::CHARGE: + filename = "/sys/devices/platform/battery/power_supply/battery/batt_slate_mode"; + break; + default: + filename = ""; + break; + } + std::string value; + int32_t intvalue; + file.open(filename); + if (file.is_open()) { + getline(file, value); + file.close(); + std::stringstream val(value); + val >> intvalue; + return intvalue; + } + return -1; +} + +Return Battery::setBatteryWritable(battery::V1_0::SysfsType stats, battery::V1_0::Number value) { + std::ofstream file; + std::string filename; + switch (stats){ + case SysfsType::CAPACITY_MAX: + filename = "/sys/devices/platform/battery/power_supply/battery/charge_full"; + break; + case SysfsType::TEMP: + filename = "/sys/devices/platform/battery/power_supply/battery/batt_temp"; + break; + case SysfsType::CAPACITY_CURRENT: + filename = "/sys/devices/platform/battery/power_supply/battery/capacity"; + break; + case SysfsType::CURRENT: + filename = "/sys/devices/platform/battery/power_supply/battery/current_now"; + break; + case SysfsType::FASTCHARGE: + filename = "/sys/class/sec/switch/afc_disable"; + break; + case SysfsType::CHARGE: + filename = "/sys/devices/platform/battery/power_supply/battery/batt_slate_mode"; + break; + default: + filename = ""; + break; + } + file.open(filename); + int write; + if (value == Number::ENABLE){ + write = 1; + }else{ + write = 0; + } + file << write; + file.close(); + return 0; +} + +IBattery *Battery::getInstance(void){ + return new Battery(); +} +} // namespace android::hardware::battery::implementation diff --git a/universal7885-common/interfaces/hardware/battery/1.0/default/Battery.h b/universal7885-common/interfaces/hardware/battery/1.0/default/Battery.h new file mode 100644 index 0000000..5331afc --- /dev/null +++ b/universal7885-common/interfaces/hardware/battery/1.0/default/Battery.h @@ -0,0 +1,40 @@ +// 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 +#include +#include + +namespace vendor::eureka::hardware::battery::V1_0 { + +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 ::android::sp; + +struct Battery : public IBattery { + // Methods from ::vendor::eureka::hardware::battery::V1_0::IBattery follow. + Return getBatteryStats(SysfsType stats) override; + Return setBatteryWritable(SysfsType stats, Number value) override; + + // Methods from ::android::hidl::base::V1_0::IBase follow. + static IBattery* getInstance(void); + +}; +} // namespace android::hardware::battery::implementation diff --git a/universal7885-common/interfaces/hardware/battery/1.0/default/service.cpp b/universal7885-common/interfaces/hardware/battery/1.0/default/service.cpp new file mode 100644 index 0000000..22ae43b --- /dev/null +++ b/universal7885-common/interfaces/hardware/battery/1.0/default/service.cpp @@ -0,0 +1,49 @@ +// 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. + + +#define LOG_TAG "vendor.eureka.hardware.battery@1.0-service" + +#include + +#include + +#include "Battery.h" + +using vendor::eureka::hardware::battery::V1_0::IBattery; +using vendor::eureka::hardware::battery::V1_0::Battery; +using android::hardware::configureRpcThreadpool; +using android::hardware::joinRpcThreadpool; +using android::sp; + +int main() { + int ret; + android::sp service = Battery::getInstance(); + configureRpcThreadpool(1, true /*callerWillJoin*/); + + if (service != nullptr) { + ret = service->registerAsService(); + if(ret != 0) { + ALOGE("Can't register instance of Battery HAL, nullptr"); + }else{ + ALOGI("registered Battery HAL"); + } + } else { + ALOGE("Can't create instance of Battery HAL, nullptr"); + } + + joinRpcThreadpool(); + + return -1; // should never get here +} diff --git a/universal7885-common/interfaces/hardware/battery/1.0/default/vendor.eureka.hardware.battery@1.0-service.rc b/universal7885-common/interfaces/hardware/battery/1.0/default/vendor.eureka.hardware.battery@1.0-service.rc new file mode 100644 index 0000000..728b77d --- /dev/null +++ b/universal7885-common/interfaces/hardware/battery/1.0/default/vendor.eureka.hardware.battery@1.0-service.rc @@ -0,0 +1,5 @@ +service vendor.battery-hal /vendor/bin/hw/vendor.eureka.hardware.battery@1.0-service + interface vendor.eureka.hardware.battery@1.0::IBattery default + class hal + user system + group system diff --git a/universal7885-common/interfaces/hardware/battery/1.0/types.hal b/universal7885-common/interfaces/hardware/battery/1.0/types.hal new file mode 100644 index 0000000..b29acfc --- /dev/null +++ b/universal7885-common/interfaces/hardware/battery/1.0/types.hal @@ -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. +package vendor.eureka.hardware.battery@1.0; + +enum SysfsType : int32_t { + CAPACITY_MAX, + TEMP, + CAPACITY_CURRENT, + CURRENT, + FASTCHARGE, + CHARGE, +}; + +enum Number : int32_t { + ENABLE = 1, + DISABLE = 0, +}; diff --git a/universal7885-common/manifest.xml b/universal7885-common/manifest.xml index f4ea1cd..8013120 100644 --- a/universal7885-common/manifest.xml +++ b/universal7885-common/manifest.xml @@ -241,4 +241,13 @@ imsd2 + + vendor.eureka.hardware.battery + hwbinder + 1.0 + + IBattery + default + +