diff --git a/bundle.json b/bundle.json index 56af83c360e4363ef94f882cb48c1a8da7d37fc8..af525762e3e3d56aeda8b0bb75405b696a68c0f8 100644 --- a/bundle.json +++ b/bundle.json @@ -84,6 +84,7 @@ "libxml2", "napi", "os_account", + "runtime_core", "safwk", "samgr", "sensor", @@ -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/runninglock:copy_power_manager_runninglock" } ], "test": [ diff --git a/frameworks/ets/taihe/BUILD.gn b/frameworks/ets/taihe/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..028f77be0061b19a54c90b2b4fb1ab95437c1c73 --- /dev/null +++ b/frameworks/ets/taihe/BUILD.gn @@ -0,0 +1,19 @@ +# 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 = [ + "runninglock:runningLock_etc", + "runninglock:power_manager_runninglock_taihe_native", + ] +} \ No newline at end of file diff --git a/frameworks/ets/taihe/runninglock/BUILD.gn b/frameworks/ets/taihe/runninglock/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0469df43a97f373406b09a494dd74f4e366e272d --- /dev/null +++ b/frameworks/ets/taihe/runninglock/BUILD.gn @@ -0,0 +1,77 @@ +# 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_runninglock") { + 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_runninglock" ] + 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" ] +} + +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_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/runninglock/idl/ohos.runningLock.taihe b/frameworks/ets/taihe/runninglock/idl/ohos.runningLock.taihe new file mode 100644 index 0000000000000000000000000000000000000000..f867d5ad2e72548ad976f54ef33a09b732d304fd --- /dev/null +++ b/frameworks/ets/taihe/runninglock/idl/ohos.runningLock.taihe @@ -0,0 +1,32 @@ +/* + * 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. + */ + +@!namespace("@ohos.runningLock", "runningLock") + +@!sts_inject(""" +static { loadLibrary("power_manager_runninglock_taihe_native.z") }""") + +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/runninglock/src/ani_constructor.cpp b/frameworks/ets/taihe/runninglock/src/ani_constructor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d64b11f814e03e21cc062c9f2248dd908f8ac049 --- /dev/null +++ b/frameworks/ets/taihe/runninglock/src/ani_constructor.cpp @@ -0,0 +1,29 @@ +/* + * 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/runninglock/src/ohos.runningLock.impl.cpp b/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ca814d2c729349c71c1d5fcceb0cba6d99bfa90 --- /dev/null +++ b/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp @@ -0,0 +1,103 @@ +/* + * 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 "ohos.runningLock.proj.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "power_mgr_client.h" +#include "power_log.h" +#include "power_mgr_errors.h" +#include "power_errors.h" +#include "ohos.runningLock.impl.hpp" + +using namespace taihe; +using namespace ohos::runningLock; +using namespace OHOS::PowerMgr; + +namespace { +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() {} + + explicit RunningLockImpl(std::shared_ptr runLock) + { + runningLock_ = runLock; + } + + void Hold(int32_t timeout) + { + POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets Hold interface"); + 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]); + } + } + + bool IsHolding() + { + POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets IsHolding interface"); + if (runningLock_ == nullptr) { + POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr"); + return false; + } + bool isUsed = runningLock_->IsUsed(); + return isUsed; + } + + void Unhold() + { + POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets Unhold interface"); + 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]); + } + } + +private: + std::shared_ptr runningLock_ = nullptr; +}; + +ohos::runningLock::RunningLock CreateSync(string_view name, ohos::runningLock::RunningLockType type) +{ + std::shared_ptr runLock = nullptr; + OHOS::PowerMgr::RunningLockType tp = static_cast(type.get_value()); + runLock = PowerMgrClient::GetInstance().CreateRunningLock(std::string(name), tp); + PowerErrors code = PowerMgrClient::GetInstance().GetError(); + if (code != PowerErrors::ERR_OK) { + taihe::set_business_error(static_cast(code), errorTable[code]); + } + return make_holder(runLock); +} +} // namespace + +TH_EXPORT_CPP_API_CreateSync(CreateSync);