From 403570ef7fc1c1371023e73fa1161d5b2a616520 Mon Sep 17 00:00:00 2001 From: cjw123qq Date: Fri, 30 May 2025 19:14:40 +0800 Subject: [PATCH] fix: battery ani transform first stage Signed-off-by: cjw123qq --- bundle.json | 2 + frameworks/ani/BUILD.gn | 20 +++++++ frameworks/ani/battery_info/BUILD.gn | 53 ++++++++++++++++ .../battery_info/ets/@ohos.batteryInfo.ets | 24 ++++++++ .../ani/battery_info/src/battery_ani.cpp | 60 +++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 frameworks/ani/BUILD.gn create mode 100644 frameworks/ani/battery_info/BUILD.gn create mode 100644 frameworks/ani/battery_info/ets/@ohos.batteryInfo.ets create mode 100644 frameworks/ani/battery_info/src/battery_ani.cpp diff --git a/bundle.json b/bundle.json index 419c139..4254caf 100644 --- a/bundle.json +++ b/bundle.json @@ -66,6 +66,7 @@ "napi", "player_framework", "power_manager", + "runtime_core", "safwk", "samgr", "ui_lite", @@ -81,6 +82,7 @@ "fwk_group": [ "//base/powermgr/battery_manager/frameworks:battery_napi", "//base/powermgr/battery_manager/frameworks:battery_capi", + "//base/powermgr/battery_manager/frameworks/ani:battery_ani_packages", "//base/powermgr/battery_manager/interfaces/inner_api:batterysrv_client" ], "service_group": [ diff --git a/frameworks/ani/BUILD.gn b/frameworks/ani/BUILD.gn new file mode 100644 index 0000000..1940b77 --- /dev/null +++ b/frameworks/ani/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# 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. +import("//build/ohos.gni") + +group("battery_ani_packages") { + deps = [ + "./battery_info:battery_ani", + "./battery_info:battery_info_abc_etc", + ] +} diff --git a/frameworks/ani/battery_info/BUILD.gn b/frameworks/ani/battery_info/BUILD.gn new file mode 100644 index 0000000..b6f859f --- /dev/null +++ b/frameworks/ani/battery_info/BUILD.gn @@ -0,0 +1,53 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# 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. + +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("../../../batterymgr.gni") + +ohos_shared_library("battery_ani") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + public_configs = [] + include_dirs = [ "./include" ] + sources = [ "./src/battery_ani.cpp" ] + deps = [ "${battery_inner_api}:batterysrv_client" ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "runtime_core:ani", + ] + + subsystem_name = "powermgr" + part_name = "battery_manager" +} + +generate_static_abc("batteryInfo") { + base_url = "./ets" + files = [ "./ets/@ohos.batteryInfo.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/batteryInfo.abc" +} + +ohos_prebuilt_etc("battery_info_abc_etc") { + source = "$target_out_dir/batteryInfo.abc" + module_install_dir = "framework" + subsystem_name = "powermgr" + part_name = "battery_manager" + deps = [ ":batteryInfo" ] +} diff --git a/frameworks/ani/battery_info/ets/@ohos.batteryInfo.ets b/frameworks/ani/battery_info/ets/@ohos.batteryInfo.ets new file mode 100644 index 0000000..2c3ee48 --- /dev/null +++ b/frameworks/ani/battery_info/ets/@ohos.batteryInfo.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * 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. + */ + +class batteryInfo { + static { loadLibrary("battery_ani.z") } + readonly batterySOC: int; + static get batterySOC(): int { + return batteryInfo.getBatterySOC(); + } + static native getBatterySOC(): int; +} +export default batteryInfo; \ No newline at end of file diff --git a/frameworks/ani/battery_info/src/battery_ani.cpp b/frameworks/ani/battery_info/src/battery_ani.cpp new file mode 100644 index 0000000..138b3ba --- /dev/null +++ b/frameworks/ani/battery_info/src/battery_ani.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * 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_srv_client.h" +#include "battery_log.h" + +using namespace OHOS::PowerMgr; + +thread_local static BatterySrvClient& g_battClient = BatterySrvClient::GetInstance(); +static ani_int GetBatterySOC([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object) +{ + int32_t capacity = g_battClient.GetCapacity(); + BATTERY_HILOGD(FEATURE_BATT_INFO, "capacity= %{public}d", capacity); + return capacity; +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + BATTERY_HILOGI(FEATURE_BATT_INFO, "ANI_Constructor"); + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + BATTERY_HILOGE(FEATURE_BATT_INFO, "GetEnv failed"); + return ANI_ERROR; + } + + static const char *className = "L@ohos/batteryInfo/batteryInfo;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + BATTERY_HILOGE(FEATURE_BATT_INFO, "Not found %{public}s", className); + return ANI_ERROR; + } + + std::array methods = { + ani_native_function {"getBatterySOC", ":I", reinterpret_cast(GetBatterySOC)}, + }; + + if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) { + BATTERY_HILOGE(FEATURE_BATT_INFO, "Cannot bind native methods to %{public}s", className); + return ANI_ERROR; + }; + BATTERY_HILOGI(FEATURE_BATT_INFO, "ANI_Constructor finish"); + + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file -- Gitee