From 43ad0b00b5b924e38467496e06c22a6b1e1823f7 Mon Sep 17 00:00:00 2001 From: cjw123qq Date: Sat, 5 Jul 2025 11:06:31 +0800 Subject: [PATCH] fix: power_stage_2 taihe idl for display Signed-off-by: cjw123qq --- bundle.json | 8 +- displaymgr.gni | 2 + state_manager/frameworks/ets/taihe/BUILD.gn | 24 ++++ .../frameworks/ets/taihe/brightness/BUILD.gn | 79 ++++++++++++ .../brightness/idl/ohos.brightness.taihe | 25 ++++ .../taihe/brightness/src/ani_constructor.cpp | 32 +++++ .../brightness/src/ohos.brightness.impl.cpp | 66 ++++++++++ .../taihe/brightness/test/unittest/BUILD.gn | 58 +++++++++ .../brightness_ani_constructor_test.cpp | 113 ++++++++++++++++++ .../unittest/brightness_taihe_native_test.cpp | 109 +++++++++++++++++ 10 files changed, 515 insertions(+), 1 deletion(-) create mode 100644 state_manager/frameworks/ets/taihe/BUILD.gn create mode 100644 state_manager/frameworks/ets/taihe/brightness/BUILD.gn create mode 100644 state_manager/frameworks/ets/taihe/brightness/idl/ohos.brightness.taihe create mode 100644 state_manager/frameworks/ets/taihe/brightness/src/ani_constructor.cpp create mode 100644 state_manager/frameworks/ets/taihe/brightness/src/ohos.brightness.impl.cpp create mode 100644 state_manager/frameworks/ets/taihe/brightness/test/unittest/BUILD.gn create mode 100644 state_manager/frameworks/ets/taihe/brightness/test/unittest/brightness_ani_constructor_test.cpp create mode 100644 state_manager/frameworks/ets/taihe/brightness/test/unittest/brightness_taihe_native_test.cpp diff --git a/bundle.json b/bundle.json index 97796bd..4cdcce6 100644 --- a/bundle.json +++ b/bundle.json @@ -45,6 +45,7 @@ "image_framework", "napi", "power_manager", + "runtime_core", "safwk", "samgr", "sensor", @@ -57,6 +58,7 @@ "base_group": [], "fwk_group": [ "//base/powermgr/display_manager/state_manager/frameworks/napi:brightness", + "//base/powermgr/display_manager/state_manager/frameworks/ets/taihe:display_manager_taihe", "//base/powermgr/display_manager/state_manager/interfaces/inner_api:displaymgr" ], "service_group": [ @@ -70,13 +72,17 @@ "inner_api": [ { "name": "//base/powermgr/display_manager/state_manager/interfaces/inner_api:displaymgr" + }, + { + "name": "//base/powermgr/display_manager/state_manager/frameworks/ets/taihe/brightness:copy_display_manager" } ], "test": [ "//base/powermgr/display_manager/state_manager/test:displaymgr_coverage_test", "//base/powermgr/display_manager/state_manager/test:displaymgr_native_test", "//base/powermgr/display_manager/state_manager/test:displaymgr_fuzztest", - "//base/powermgr/display_manager/brightness_manager/test:brightness_manager_test" + "//base/powermgr/display_manager/brightness_manager/test:brightness_manager_test", + "//base/powermgr/display_manager/state_manager/frameworks/ets/taihe:display_manager_taihe_test" ] } } diff --git a/displaymgr.gni b/displaymgr.gni index 5e947aa..09ef589 100644 --- a/displaymgr.gni +++ b/displaymgr.gni @@ -54,3 +54,5 @@ displaymgr_inner_api = "${displaymgr_root_path}/interfaces/inner_api" displaymgr_utils_path = "${displaymgr_root_path}/utils" brightnessmgr_root_path = "//base/powermgr/display_manager/brightness_manager" + +taihe_generated_file_path = "${root_out_dir}/taihe/out/powermgr/display_manager" diff --git a/state_manager/frameworks/ets/taihe/BUILD.gn b/state_manager/frameworks/ets/taihe/BUILD.gn new file mode 100644 index 0000000..774bc20 --- /dev/null +++ b/state_manager/frameworks/ets/taihe/BUILD.gn @@ -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. + +group("display_manager_taihe") { + deps = [ + "brightness:brightness_etc", + "brightness:display_manager_brightness_taihe_native", + ] +} + +group("display_manager_taihe_test") { + testonly = true + deps = [ "brightness/test/unittest:brightness_taihe_native_test" ] +} diff --git a/state_manager/frameworks/ets/taihe/brightness/BUILD.gn b/state_manager/frameworks/ets/taihe/brightness/BUILD.gn new file mode 100644 index 0000000..e2ae6d5 --- /dev/null +++ b/state_manager/frameworks/ets/taihe/brightness/BUILD.gn @@ -0,0 +1,79 @@ +# 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("../../../../../displaymgr.gni") + +copy_taihe_idl("copy_display_manager") { + sources = [ "idl/ohos.brightness.taihe" ] +} + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":copy_display_manager" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.brightness.ani.cpp", + "$taihe_generated_file_path/src/ohos.brightness.abi.c", + ] +} + +taihe_shared_library("display_manager_brightness_taihe_native") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + taihe_generated_file_path = "$taihe_generated_file_path" + sources = get_target_outputs(":run_taihe") + include_dirs = [ "./include" ] + configs = [ + "${displaymgr_utils_path}:utils_config", + "${displaymgr_utils_path}:coverage_flags", + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.brightness.impl.cpp", + ] + deps = [ + ":run_taihe", + "${displaymgr_inner_api}:displaymgr", + ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "power_manager:powermgr_client", + ] + + subsystem_name = "powermgr" + part_name = "${displaymgr_part_name}" +} + +generate_static_abc("brightness_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.brightness.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/brightness_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("brightness_etc") { + source = "$target_out_dir/brightness_abc.abc" + module_install_dir = "framework" + subsystem_name = "powermgr" + part_name = "${displaymgr_part_name}" + deps = [ ":brightness_abc" ] +} \ No newline at end of file diff --git a/state_manager/frameworks/ets/taihe/brightness/idl/ohos.brightness.taihe b/state_manager/frameworks/ets/taihe/brightness/idl/ohos.brightness.taihe new file mode 100644 index 0000000..2f470df --- /dev/null +++ b/state_manager/frameworks/ets/taihe/brightness/idl/ohos.brightness.taihe @@ -0,0 +1,25 @@ +/* + * 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.brightness","brightness") + +@!sts_inject(""" +static { loadLibrary("display_manager_brightness_taihe_native.z") }""") + +@overload("setValue") +function SetValueContinuous(value: i32, continuous: bool): void; + +@overload("setValue") +function SetValueInt(value: i32): void; \ No newline at end of file diff --git a/state_manager/frameworks/ets/taihe/brightness/src/ani_constructor.cpp b/state_manager/frameworks/ets/taihe/brightness/src/ani_constructor.cpp new file mode 100644 index 0000000..1923bc3 --- /dev/null +++ b/state_manager/frameworks/ets/taihe/brightness/src/ani_constructor.cpp @@ -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. + */ + +#include "ohos.brightness.ani.hpp" +#include "display_log.h" +using namespace OHOS::DisplayPowerMgr; +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + DISPLAY_HILOGE(COMP_FWK, "Error from GetEnv"); + return ANI_ERROR; + } + if (ANI_OK != ohos::brightness::ANIRegister(env)) { + DISPLAY_HILOGE(COMP_FWK, "Error from ohos::brightness::ANIRegister"); + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/state_manager/frameworks/ets/taihe/brightness/src/ohos.brightness.impl.cpp b/state_manager/frameworks/ets/taihe/brightness/src/ohos.brightness.impl.cpp new file mode 100644 index 0000000..04bfc06 --- /dev/null +++ b/state_manager/frameworks/ets/taihe/brightness/src/ohos.brightness.impl.cpp @@ -0,0 +1,66 @@ +/* + * 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.brightness.proj.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "display_power_mgr_client.h" +#include "display_log.h" +#include "display_mgr_errors.h" +#include "ohos.brightness.impl.hpp" + +using namespace taihe; +using namespace OHOS::DisplayPowerMgr; + +namespace { +const uint32_t MAX_BRIGHTNESS = DisplayPowerMgrClient::GetInstance().GetMaxBrightness(); +const uint32_t MIN_BRIGHTNESS = DisplayPowerMgrClient::GetInstance().GetMinBrightness(); + +std::map g_errorTable = { + {DisplayErrors::ERR_CONNECTION_FAIL, "Failed to connect to the service."}, + {DisplayErrors::ERR_PERMISSION_DENIED, "Permission is denied" }, + {DisplayErrors::ERR_SYSTEM_API_DENIED, "System permission is denied" }, + {DisplayErrors::ERR_PARAM_INVALID, "Invalid input parameter." } +}; + +void SetValueContinuous(int32_t value, bool continuous) +{ + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "ets brightness interface"); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Set brightness: %{public}d, %{public}d", value, continuous); + value = value > static_cast(MAX_BRIGHTNESS) ? static_cast(MAX_BRIGHTNESS) : value; + value = value < static_cast(MIN_BRIGHTNESS) ? static_cast(MIN_BRIGHTNESS) : value; + bool isSucc = DisplayPowerMgrClient::GetInstance().SetBrightness(value, 0, continuous); + if (!isSucc) { + DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Failed to set brightness: %{public}d", value); + DisplayErrors error = DisplayPowerMgrClient::GetInstance().GetError(); + if (error != DisplayErrors::ERR_OK) { + taihe::set_business_error(static_cast(error), g_errorTable[error]); + } + } +} + +void SetValueInt(int32_t value) +{ + bool continuous = false; + SetValueContinuous(value, continuous); +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive +// NOLINTBEGIN +TH_EXPORT_CPP_API_SetValueContinuous(SetValueContinuous); +TH_EXPORT_CPP_API_SetValueInt(SetValueInt); +// NOLINTEND \ No newline at end of file diff --git a/state_manager/frameworks/ets/taihe/brightness/test/unittest/BUILD.gn b/state_manager/frameworks/ets/taihe/brightness/test/unittest/BUILD.gn new file mode 100644 index 0000000..c810b45 --- /dev/null +++ b/state_manager/frameworks/ets/taihe/brightness/test/unittest/BUILD.gn @@ -0,0 +1,58 @@ +# 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/test.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("../../../../../../../displaymgr.gni") + +module_output_path = "display_manager/display_manager" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "${taihe_generated_file_path}/include", + "${taihe_toolchain_path}/include", + "../../include", + ] +} + +ohos_unittest("brightness_taihe_native_test") { + module_out_path = module_output_path + + sources = [ + "brightness_ani_constructor_test.cpp", + "brightness_taihe_native_test.cpp", + "../../src/ohos.brightness.impl.cpp" + ] + + configs = [ + ":module_private_config", + "${displaymgr_utils_path}:coverage_flags", + ] + + deps = [ + "${displaymgr_framework_path}/ets/taihe/brightness:display_manager_brightness_taihe_native", + "${displaymgr_framework_path}/ets/taihe/brightness:run_taihe", + "${displaymgr_inner_api}:displaymgr", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "power_manager:powermgr_client", + "runtime_core:ani", + ] +} \ No newline at end of file diff --git a/state_manager/frameworks/ets/taihe/brightness/test/unittest/brightness_ani_constructor_test.cpp b/state_manager/frameworks/ets/taihe/brightness/test/unittest/brightness_ani_constructor_test.cpp new file mode 100644 index 0000000..84141cd --- /dev/null +++ b/state_manager/frameworks/ets/taihe/brightness/test/unittest/brightness_ani_constructor_test.cpp @@ -0,0 +1,113 @@ +/* + * 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.brightness.ani.hpp" +#include "display_log.h" + +using namespace OHOS::DisplayPowerMgr; + +using namespace testing; +using namespace testing::ext; +namespace { +ani_status g_status1 = ANI_OK; +ani_status g_status2 = ANI_OK; +} + +namespace ohos::brightness { +ani_status ANIRegister(ani_env *env) +{ + return g_status1; +} +} + +namespace { +class BrightnessAniConstructorTest : public ::testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +static ani_status GetEnv(ani_vm *vm, uint32_t version, ani_env **result) +{ + return g_status2; +} +/** + * @tc.name: BrightnessAniConstructorTest_001 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(BrightnessAniConstructorTest, BrightnessAniConstructorTest_001, TestSize.Level1) +{ + DISPLAY_HILOGI(LABEL_TEST, "BrightnessAniConstructorTest_001 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result; + + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_OK); + EXPECT_EQ(result, ANI_VERSION_1); + DISPLAY_HILOGI(LABEL_TEST, "BrightnessAniConstructorTest_001 end"); +} + +/** + * @tc.name: BrightnessAniConstructorTest_002 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(BrightnessAniConstructorTest, BrightnessAniConstructorTest_002, TestSize.Level1) +{ + DISPLAY_HILOGI(LABEL_TEST, "BrightnessAniConstructorTest_002 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result; + + g_status1 = ANI_ERROR; + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_ERROR); + DISPLAY_HILOGI(LABEL_TEST, "BrightnessAniConstructorTest_002 end"); +} + +/** + * @tc.name: BrightnessAniConstructorTest_003 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(BrightnessAniConstructorTest, BrightnessAniConstructorTest_003, TestSize.Level1) +{ + DISPLAY_HILOGI(LABEL_TEST, "BrightnessAniConstructorTest_003 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result; + + g_status1 = ANI_OK; + g_status2 = ANI_ERROR; + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_ERROR); + DISPLAY_HILOGI(LABEL_TEST, "BrightnessAniConstructorTest_003 end"); +} +} \ No newline at end of file diff --git a/state_manager/frameworks/ets/taihe/brightness/test/unittest/brightness_taihe_native_test.cpp b/state_manager/frameworks/ets/taihe/brightness/test/unittest/brightness_taihe_native_test.cpp new file mode 100644 index 0000000..e033baa --- /dev/null +++ b/state_manager/frameworks/ets/taihe/brightness/test/unittest/brightness_taihe_native_test.cpp @@ -0,0 +1,109 @@ +/* + * 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.brightness.proj.hpp" +#include "ohos.brightness.impl.hpp" +#include "ohos.brightness.user.hpp" +#include "taihe/runtime.hpp" +#include "display_power_mgr_client.h" +#include "display_log.h" +#include "display_mgr_errors.h" + +using namespace taihe; +using namespace ohos::brightness; +using namespace OHOS::DisplayPowerMgr; + +using namespace testing; +using namespace testing::ext; + +namespace { +DisplayErrors g_error = DisplayErrors::ERR_OK; +bool g_pass = false; +bool g_ret = false; +} + +namespace taihe { +void set_business_error(int32_t err_code, taihe::string_view msg) +{ + (void)err_code; + (void)msg; +} +} + +namespace OHOS::DisplayPowerMgr { +bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t displayId, bool continuous) +{ + g_pass = true; + return g_ret; +} + +DisplayErrors DisplayPowerMgrClient::GetError() +{ + return g_error; +} +} // OHOS::PowerMgr + +namespace { +class BrightnessTaiheNativeTest : public ::testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() + { + g_pass = false; + g_ret = false; + } +}; + +/** + * @tc.name: BrightnessTaiheNativeTest_001 + * @tc.desc: test brightness taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(BrightnessTaiheNativeTest, BrightnessTaiheNativeTest_001, TestSize.Level1) +{ + DISPLAY_HILOGI(LABEL_TEST, "BrightnessTaiheNativeTest_001 start"); + constexpr int32_t value = 255; + SetValueInt(value); + EXPECT_TRUE(g_pass); + DISPLAY_HILOGI(LABEL_TEST, "BrightnessTaiheNativeTest_001 end"); +} + +/** + * @tc.name: BrightnessTaiheNativeTest_002 + * @tc.desc: test brightness taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(BrightnessTaiheNativeTest, BrightnessTaiheNativeTest_002, TestSize.Level1) +{ + DISPLAY_HILOGI(LABEL_TEST, "BrightnessTaiheNativeTest_002 start"); + constexpr int32_t value = 255; + constexpr bool continuous = true; + SetValueContinuous(value, continuous); + + g_error = DisplayErrors::ERR_SYSTEM_API_DENIED; + SetValueContinuous(value, continuous); + + g_ret = true; + SetValueContinuous(value, continuous); + EXPECT_TRUE(g_pass); + DISPLAY_HILOGI(LABEL_TEST, "BrightnessTaiheNativeTest_002 end"); +} +} -- Gitee