From e770153c3e7b4910b086b68d7b47680bb4119758 Mon Sep 17 00:00:00 2001 From: yanxuejun Date: Sat, 19 Apr 2025 22:20:44 +0800 Subject: [PATCH] fix: taihe idl for power_manager Signed-off-by: yanxuejun --- bundle.json | 5 + frameworks/ets/taihe/BUILD.GN | 21 ++++ .../ets/taihe/ability_delegator/BUILD.GN | 82 +++++++++++++ .../idl/ohos.runningLock.taihe | 14 +++ .../ability_delegator/src/ani_constructor.cpp | 28 +++++ .../src/ohos.runningLock.impl.cpp | 110 ++++++++++++++++++ 6 files changed, 260 insertions(+) create mode 100644 frameworks/ets/taihe/BUILD.GN create mode 100644 frameworks/ets/taihe/ability_delegator/BUILD.GN create mode 100644 frameworks/ets/taihe/ability_delegator/idl/ohos.runningLock.taihe create mode 100644 frameworks/ets/taihe/ability_delegator/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/ability_delegator/src/ohos.runningLock.impl.cpp diff --git a/bundle.json b/bundle.json index 56af83c3..f291e64f 100644 --- a/bundle.json +++ b/bundle.json @@ -87,6 +87,7 @@ "safwk", "samgr", "sensor", + "runtime_core", "device_standby", "window_manager", "miscdevice", @@ -108,6 +109,7 @@ ], "fwk_group": [ "//base/powermgr/power_manager/frameworks:power_napi", + "//base/powermgr/power_manager/frameworks/ets/taihe:power_manager_taihe", "//base/powermgr/power_manager/interfaces/inner_api:powermgr_client" ], "service_group": [ @@ -208,6 +210,9 @@ ], "header_base": "//base/powermgr/power_manager/frameworks/cj/power/include" } + }, + { + "name": "//base/powermgr/power_manager/frameworks/ets/taihe/ability_delegator:copy_power_manager" } ], "test": [ diff --git a/frameworks/ets/taihe/BUILD.GN b/frameworks/ets/taihe/BUILD.GN new file mode 100644 index 00000000..1fff36be --- /dev/null +++ b/frameworks/ets/taihe/BUILD.GN @@ -0,0 +1,21 @@ +/* + * 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. + */ + +group("power_manager_taihe") { + deps = [ + "ability_delegator:runningLock_abc_etc", + "ability_delegator:power_manager_runninglock_taihe_native", + ] +} \ No newline at end of file diff --git a/frameworks/ets/taihe/ability_delegator/BUILD.GN b/frameworks/ets/taihe/ability_delegator/BUILD.GN new file mode 100644 index 00000000..f625dbfb --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/BUILD.GN @@ -0,0 +1,82 @@ +/* + * 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("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("../../../../powermgr.gni") + +copy_taihe_idl("copy_power_manager") { + sources = [ "idl/ohos.runningLock.taihe" ] +} + +subsystem_name = "powermgr" +part_name = "power_manager" +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":copy_power_manager" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.runningLock.ani.cpp", + "$taihe_generated_file_path/src/ohos.runningLock.abi.c", + ] +} + +taihe_shared_library("power_manager_runninglock_taihe_native") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + taihe_generated_file_path = "$taihe_generated_file_path" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + sources = get_target_outputs(":run_taihe") + include_dirs = [ "./include" ] + configs = [ + "${powermgr_utils_path}:utils_config", + "${powermgr_utils_path}:coverage_flags", + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.runningLock.impl.cpp", + ] + deps = [ + ":run_taihe", + "${powermgr_inner_api}:powermgr_client", + ] + external_deps = [ "hilog:libhilog" ] + if (has_dfx_hiview_part) { + external_deps += [ "hiview:libxpower_event_js" ] + } +} + +generate_static_abc("runningLock_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.runningLock.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/runningLock_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("runningLock_abc_etc") { + source = "$target_out_dir/runningLock_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":runningLock_abc" ] +} \ No newline at end of file diff --git a/frameworks/ets/taihe/ability_delegator/idl/ohos.runningLock.taihe b/frameworks/ets/taihe/ability_delegator/idl/ohos.runningLock.taihe new file mode 100644 index 00000000..2af7a221 --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/idl/ohos.runningLock.taihe @@ -0,0 +1,14 @@ +@!namespace("@ohos.runningLock", "runninglock") + +enum RunningLockType : i32 { BACKGROUND = 1, PROXIMITY_SCREEN_CONTROL } + +@class +interface RunningLock { + hold(timeout: i32): void; + isHolding(): bool; + unhold(): void; +} + +@gen_async("create") +@gen_promise("create") +function createSync(name: String, type: RunningLockType): RunningLock; \ No newline at end of file diff --git a/frameworks/ets/taihe/ability_delegator/src/ani_constructor.cpp b/frameworks/ets/taihe/ability_delegator/src/ani_constructor.cpp new file mode 100644 index 00000000..4f349441 --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/src/ani_constructor.cpp @@ -0,0 +1,28 @@ +/* + * 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 "ohos.runningLock.ani.hpp" +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) { + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + return ANI_ERROR; + } + if (ANI_OK != ohos::runningLock::ANIRegister(env)) { + std::cerr << "Error from ohos::runningLock::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/ability_delegator/src/ohos.runningLock.impl.cpp b/frameworks/ets/taihe/ability_delegator/src/ohos.runningLock.impl.cpp new file mode 100644 index 00000000..2bf660b9 --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/src/ohos.runningLock.impl.cpp @@ -0,0 +1,110 @@ +/* + * 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 "ohos.runningLock.proj.hpp" +#include "ohos.runningLock.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include "power_mgr_client.h" +#include "power_log.h" +#include "power_mgr_errors.h" +#include "power_errors.h" + +using namespace taihe; +using namespace ohos::runningLock; +using namespace OHOS::PowerMgr; + +namespace { +// To be implemented. +std::map errorTable = { + {PowerErrors::ERR_CONNECTION_FAIL, "Failed to connect to the service."}, + {PowerErrors::ERR_PERMISSION_DENIED, "Permission is denied" }, + {PowerErrors::ERR_SYSTEM_API_DENIED, "System permission is denied" }, + {PowerErrors::ERR_PARAM_INVALID, "Invalid input parameter." } +}; + +class RunningLockImpl { +public: + RunningLockImpl() { + // Don't forget to implement the constructor. + } + + RunningLockImpl(std::shared_ptr runLock) { + // Don't forget to implement the constructor. + runningLock_ = runLock; + } + + void hold(int32_t timeout) { + if (runningLock_ == nullptr) { + POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr"); + return; + } + OHOS::ErrCode code = runningLock_->Lock(timeout); + if (code == E_PERMISSION_DENIED) { + taihe::set_business_error(static_cast(PowerErrors::ERR_PERMISSION_DENIED), + errorTable[PowerErrors::ERR_PERMISSION_DENIED]); + return; + } +#ifdef HAS_DFX_HIVIEW_PART + OHOS::HiviewDFX::ReportXPowerJsStackSysEvent(env, "RunningLockAni::Hold"); +#endif + } + + bool isHolding() { + if (runningLock_ == nullptr) { + POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr"); + return false; + } + bool isUsed = runningLock->IsUsed(); + return isUsed; + } + + void unhold() { + if (runningLock_ == nullptr) { + POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr"); + return; + } + OHOS::ErrCode code = runningLock_->UnLock(); + if (code == E_PERMISSION_DENIED) { + taihe::set_business_error(static_cast(PowerErrors::ERR_PERMISSION_DENIED), + errorTable[PowerErrors::ERR_PERMISSION_DENIED]); + } +#ifdef HAS_DFX_HIVIEW_PART + OHOS::HiviewDFX::ReportXPowerJsStackSysEvent(env, "RunningLockAni::Unhold"); +#endif + } + +private: + std::shared_ptr runningLock_ = nullptr; +}; + +ohos::runningLock::RunningLock createSync(string_view name, ohos::runningLock::RunningLockType type) { + // The parameters in the make_holder function should be of the same type + // as the parameters in the constructor of the actual implementation class. + + std::shared_ptr runningLock_ = nullptr; + OHOS::PowerMgr::RunningLockType type_ = static_cast(type.get_Value()); + runningLock_ = PowerMgrClient::GetInstance().CreateRunningLock(std::string(name), type_); + PowerErrors code = PowerMgrClient::GetInstance().GetError(); + if (code != PowerErrors::ERR_OK) { + taihe::set_business_error(static_cast(code), errorTable[code]); + } + return make_holder(runningLock_); +} +} // namespace + +TH_EXPORT_CPP_API_createSync(createSync); -- Gitee