From cb97e44176b3c97b46a5490f946b3697f1dd9583 Mon Sep 17 00:00:00 2001 From: yanxuejun Date: Mon, 21 Apr 2025 14:15:02 +0800 Subject: [PATCH] fix: taihe idl for power_manager Signed-off-by: yanxuejun --- bundle.json | 7 +- frameworks/ets/taihe/BUILD.gn | 19 ++++ .../ets/taihe/ability_delegator/BUILD.gn | 90 +++++++++++++++++++ .../ability_delegator/idl/ohos.thermal.taihe | 36 ++++++++ .../include/thermal_level_callback.h | 43 +++++++++ .../ability_delegator/src/ani_constructor.cpp | 29 ++++++ .../src/ohos.thermal.impl.cpp | 69 ++++++++++++++ .../src/thermal_level_callback.cpp | 44 +++++++++ 8 files changed, 336 insertions(+), 1 deletion(-) 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.thermal.taihe create mode 100644 frameworks/ets/taihe/ability_delegator/include/thermal_level_callback.h create mode 100644 frameworks/ets/taihe/ability_delegator/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/ability_delegator/src/ohos.thermal.impl.cpp create mode 100644 frameworks/ets/taihe/ability_delegator/src/thermal_level_callback.cpp diff --git a/bundle.json b/bundle.json index 4626a73..4e094eb 100644 --- a/bundle.json +++ b/bundle.json @@ -52,6 +52,7 @@ "napi", "netmanager_base", "power_manager", + "runtime_core", "safwk", "samgr", "soc_perf", @@ -64,7 +65,8 @@ "fwk_group": [ "//base/powermgr/thermal_manager/application:thermal_application", "//base/powermgr/thermal_manager/frameworks:client", - "//base/powermgr/thermal_manager/frameworks:thermal_napi" + "//base/powermgr/thermal_manager/frameworks:thermal_napi", + "//base/powermgr/thermal_manager/frameworks/ets/taihe:thermal_manager_taihe" ], "service_group": [ "//base/powermgr/thermal_manager/test/common:mock_thermalsrv_client", @@ -95,6 +97,9 @@ ], "header_base": "//base/powermgr/thermal_manager/interfaces/inner_api/native/include" } + }, + { + "name": "//base/powermgr/thermal_manager/frameworks/ets/taihe/ability_delegator:copy_thermal_manager" } ], "test": [ diff --git a/frameworks/ets/taihe/BUILD.gn b/frameworks/ets/taihe/BUILD.gn new file mode 100644 index 0000000..b6940bc --- /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("thermal_manager_taihe") { + deps = [ + "ability_delegator:thermal_etc", + "ability_delegator:thermal_manager_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 0000000..eb71588 --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/BUILD.gn @@ -0,0 +1,90 @@ +# 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("../../../../thermalmgr.gni") + +copy_taihe_idl("copy_thermal_manager") { + sources = [ "idl/ohos.thermal.taihe" ] +} + +subsystem_name = "powermgr" +part_name = "thermal_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_thermal_manager" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.thermal.ani.cpp", + "$taihe_generated_file_path/src/ohos.thermal.abi.c", + ] +} + +config("thermal_private_config") { + include_dirs = [ "${thermal_service_zidl}/include" ] +} + +taihe_shared_library("thermal_manager_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 = [ + "${utils_path}:utils_config", + ":thermal_private_config", + "${utils_path}:coverage_flags", + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.thermal.impl.cpp", + "src/thermal_level_callback.cpp", + ] + deps = [ + ":run_taihe", + "${thermal_inner_api}:thermalmgr_listener", + "${thermal_inner_api}:thermalsrv_client", + ] + external_deps = [ + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "power_manager:powermgr_client", + ] +} + +generate_static_abc("thermal_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.thermal.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/thermal_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("thermal_etc") { + source = "$target_out_dir/thermal_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":thermal_abc" ] +} \ No newline at end of file diff --git a/frameworks/ets/taihe/ability_delegator/idl/ohos.thermal.taihe b/frameworks/ets/taihe/ability_delegator/idl/ohos.thermal.taihe new file mode 100644 index 0000000..dc2f2cc --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/idl/ohos.thermal.taihe @@ -0,0 +1,36 @@ +/* + * 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.thermal", "thermal") + +@!sts_inject(""" +static { loadLibrary("thermal_manager_taihe_native.z") }""") + +enum ThermalLevel: i32 { + COOL = 0, + NORMAL = 1, + WARM = 2, + HOT = 3, + OVERHEATED = 4, + WARNING = 5, + EMERGENCY = 6, + ESCAPE = 7 +} + +function RegisterThermalLevelCallback(thermalCb: (level: ThermalLevel) => void): void; + +function UnregisterThermalLevelCallback(thermalCb: Optional<() => void>): void; + +function GetLevel(): ThermalLevel; \ No newline at end of file diff --git a/frameworks/ets/taihe/ability_delegator/include/thermal_level_callback.h b/frameworks/ets/taihe/ability_delegator/include/thermal_level_callback.h new file mode 100644 index 0000000..ea7b408 --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/include/thermal_level_callback.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#ifndef THERMAL_LEVEL_CALLBACK_H +#define THERMAL_LEVEL_CALLBACK_H + +#include "ohos.thermal.proj.hpp" +#include "ohos.thermal.impl.hpp" +#include "taihe/runtime.hpp" +#include "thermal_level_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +using namespace taihe; +class ThermalLevelCallback : public ThermalLevelCallbackStub { +public: + ThermalLevelCallback() = default; + virtual ~ThermalLevelCallback(); + void UpdateCallback(callback_view thermalCb); + bool OnThermalLevelChanged(OHOS::PowerMgr::ThermalLevel level) override; + void OnThermalLevel(); + +private: + OHOS::PowerMgr::ThermalLevel level_ {OHOS::PowerMgr::ThermalLevel::COOL}; + callback_view callback_ {nullptr}; + std::mutex mutex_; +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // THERMAL_LEVEL_CALLBACK_H \ 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 0000000..e6d2c29 --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/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.thermal.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::thermal::ANIRegister(env)) { + std::cerr << "Error from ohos::thermal::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/ability_delegator/src/ohos.thermal.impl.cpp b/frameworks/ets/taihe/ability_delegator/src/ohos.thermal.impl.cpp new file mode 100644 index 0000000..a800dcb --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/src/ohos.thermal.impl.cpp @@ -0,0 +1,69 @@ +/* + * 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 "stdexcept" +#include "thermal_mgr_client.h" +#include "thermal_level_callback.h" +#include "thermal_log.h" +#include "refbase.h" + +using namespace ohos::thermal; +using namespace OHOS; +using namespace OHOS::PowerMgr; + +namespace { +thread_local auto& g_thermalMgrClient = ThermalMgrClient::GetInstance(); +thread_local sptr g_thermalLevelCallback = new (std::nothrow) ThermalLevelCallback(); + +void RegisterThermalLevelCallback(callback_view thermalCb) +{ + THERMAL_HILOGD(COMP_FWK, "ets RegisterThermalLevelCallback interface"); + if (g_thermalLevelCallback == nullptr) { + THERMAL_HILOGE(COMP_FWK, "g_thermalLevelCallback is nullptr"); + return; + } + g_thermalLevelCallback->UpdateCallback(thermalCb); + g_thermalMgrClient.SubscribeThermalLevelCallback(g_thermalLevelCallback); +} + +void UnregisterThermalLevelCallback(optional_view> thermalCb) +{ + THERMAL_HILOGD(COMP_FWK, "ets UnregisterThermalLevelCallback interface"); + if (g_thermalLevelCallback == nullptr) { + THERMAL_HILOGE(COMP_FWK, "g_thermalLevelCallback is nullptr"); + return; + } + g_thermalMgrClient.UnSubscribeThermalLevelCallback(g_thermalLevelCallback); + if (thermalCb) { + THERMAL_HILOGI(COMP_FWK, "UnregisterThermalLevelCallback thermalCb start"); + (*thermalCb)(); + } else { + THERMAL_HILOGI(COMP_FWK, "UnregisterThermalLevelCallback thermalCb is nullptr"); + } +} + +ohos::thermal::ThermalLevel GetLevel() +{ + THERMAL_HILOGD(COMP_FWK, "ets GetLevel interface"); + OHOS::PowerMgr::ThermalLevel level = g_thermalMgrClient.GetThermalLevel(); + ohos::thermal::ThermalLevel levelValue = static_cast(level); + THERMAL_HILOGI(COMP_FWK, "level is %{public}d", static_cast(levelValue)); + return levelValue; +} +} // namespace + +TH_EXPORT_CPP_API_RegisterThermalLevelCallback(RegisterThermalLevelCallback); +TH_EXPORT_CPP_API_UnregisterThermalLevelCallback(UnregisterThermalLevelCallback); +TH_EXPORT_CPP_API_GetLevel(GetLevel); diff --git a/frameworks/ets/taihe/ability_delegator/src/thermal_level_callback.cpp b/frameworks/ets/taihe/ability_delegator/src/thermal_level_callback.cpp new file mode 100644 index 0000000..4afa27a --- /dev/null +++ b/frameworks/ets/taihe/ability_delegator/src/thermal_level_callback.cpp @@ -0,0 +1,44 @@ +/* + * 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 "thermal_level_callback.h" +#include "thermal_log.h" + +namespace OHOS { +namespace PowerMgr { +ThermalLevelCallback::~ThermalLevelCallback() {} + +void ThermalLevelCallback::UpdateCallback(callback_view thermalCb) +{ + std::lock_guard lock(mutex_); + callback_ = thermalCb; +} + +bool ThermalLevelCallback::OnThermalLevelChanged(OHOS::PowerMgr::ThermalLevel level) +{ + std::lock_guard lock(mutex_); + level_ = level; + OnThermalLevel(); + return true; +} + +void ThermalLevelCallback::OnThermalLevel() +{ + THERMAL_HILOGD(COMP_FWK, "level is: %{public}d", static_cast(level_)); + ohos::thermal::ThermalLevel levelValue = static_cast(level_); + callback_(levelValue); +} +} // namespace PowerMgr +} // namespace OHOS \ No newline at end of file -- Gitee