diff --git a/bundle.json b/bundle.json index 4da514723215400e3e682388292c5396857181c6..e80544fa63f8209037a90bd5d7f2b79b9b33e9e1 100644 --- a/bundle.json +++ b/bundle.json @@ -55,6 +55,7 @@ "bounds_checking_function", "openssl", "ffmpeg", + "runtime_core", "zlib", "ffrt", "libevdev", @@ -74,7 +75,8 @@ "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhardware.cfg", "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhfwk_sa_profile", "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", - "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils" + "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", + "//foundation/distributedhardware/distributed_hardware_fwk/taihe:distributed_hardware_taihe" ], "inner_kits": [ { diff --git a/taihe/BUILD.gn b/taihe/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0d6f77d5a96bf0da4456627edb4b27433fa71fed --- /dev/null +++ b/taihe/BUILD.gn @@ -0,0 +1,115 @@ +# 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( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +subsystem_name = "distributedhardware" +part_name = "distributed_hardware_fwk" + +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +copy_taihe_idl("hardware_taihe_idl") { + sources = [ "idl/ohos.distributedHardware.hardwareManager.taihe" ] + + external_deps = [] +} + +ohos_taihe("run_hardware_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":hardware_taihe_idl" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.distributedHardware.hardwareManager.ani.cpp", + "$taihe_generated_file_path/src/ohos.distributedHardware.hardwareManager.abi.c", + ] +} + +taihe_shared_library("hardware_taihe") { + branch_protector_ret = "pac_ret" + sanitize = { + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + integer_overflow = true + ubsan = true + } + taihe_generated_file_path = "$taihe_generated_file_path" + subsystem_name = "$subsystem_name" + part_name = "$part_name" + sources = get_target_outputs(":run_hardware_taihe") + sources += [ + "src/ani_constructor.cpp", + "src/ohos.distributedHardware.hardwareManager.impl.cpp", + ] + include_dirs = [ + "include", + "${av_center_kits_path}/include", + "${av_trans_path}/common/include", + "${av_trans_path}/interface", + "${innerkits_path}/include", + "${common_path}/log/include", + "${common_path}/utils/include", + ] + deps = [ + ":run_hardware_taihe", + "${distributedhardwarefwk_path}/interfaces/inner_kits:libdhfwk_sdk", + ] + external_deps = [ + "access_token:libtokenid_sdk", + "cJSON:cjson", + "c_utils:utils", + "hilog:libhilog", + "hisysevent:libhisysevent", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +generate_static_abc("hardware_taihe_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.distributedHardware.hardwareManager.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/hardware_taihe_abc.abc" + dependencies = [ ":run_hardware_taihe" ] +} + +generate_static_abc("hardware_taihe_test") { + base_url = "${distributedhardwarefwk_path}/taihe/test" + files = + [ "${distributedhardwarefwk_path}/taihe/test/test_main.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/hardware_taihe_test.abc" +} + +ohos_prebuilt_etc("hardware_taihe_etc") { + source = "$target_out_dir/hardware_taihe_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ + ":hardware_taihe_abc", + ":hardware_taihe_test", + ] +} + +group("distributed_hardware_taihe") { + deps = [ + ":hardware_taihe", + ":hardware_taihe_etc", + ] +} diff --git a/taihe/idl/ohos.distributedHardware.hardwareManager.taihe b/taihe/idl/ohos.distributedHardware.hardwareManager.taihe new file mode 100644 index 0000000000000000000000000000000000000000..caab20d828216e6bbd5e939b53007ada94253c51 --- /dev/null +++ b/taihe/idl/ohos.distributedHardware.hardwareManager.taihe @@ -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. + */ + + @!namespace("@ohos.distributedHardware.hardwareManager", "hardwareManager") + @!sts_inject(""" +static { loadLibrary("hardware_taihe.z") } +""") + +enum DistributedHardwareType: i32 { + ALL = 0, + CAMERA = 1, + SCREEN = 8, + MODEM_MIC = 256, + MODEM_SPEAKER = 512, + MIC = 1024, + SPEAKER = 2048 +} + +struct HardwareDescriptor { + type: DistributedHardwareType; + srcNetworkId: Optional; +} + +@gen_promise("pauseDistributedHardware") +function PauseDistributedHardwareSync(description: HardwareDescriptor): void; + +@gen_promise("resumeDistributedHardware") +function ResumeDistributedHardwareSync(description: HardwareDescriptor): void; + +@gen_promise("stopDistributedHardware") +function StopDistributedHardwareSync(description: HardwareDescriptor): void; \ No newline at end of file diff --git a/taihe/src/ani_constructor.cpp b/taihe/src/ani_constructor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c61fe183f6cd23ea53a5c0b15584b1132c599d7b --- /dev/null +++ b/taihe/src/ani_constructor.cpp @@ -0,0 +1,37 @@ +/* + * 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.distributedHardware.hardwareManager.ani.hpp" +#if __has_include() +#include +#elif __has_include() +#include +#else +#error "ani.h not found. Please ensure the Ani SDK is correctly installed." +#endif +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; + } + ani_status status = ANI_OK; + if (ANI_OK != ohos::distributedHardware::hardwareManager::ANIRegister(env)) { + std::cerr << "Error from ohos::distributedHardware::hardwareManager::ANIRegister" << std::endl; + status = ANI_ERROR; + } + *result = ANI_VERSION_1; + return status; +} diff --git a/taihe/src/ohos.distributedHardware.hardwareManager.impl.cpp b/taihe/src/ohos.distributedHardware.hardwareManager.impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..923e844b6c77f386e4aeb43b6724a231cca43aab --- /dev/null +++ b/taihe/src/ohos.distributedHardware.hardwareManager.impl.cpp @@ -0,0 +1,130 @@ +/* + * 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.distributedHardware.hardwareManager.proj.hpp" +#include "ohos.distributedHardware.hardwareManager.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include + +#include "ipc_skeleton.h" +#include "device_type.h" +#include "distributed_hardware_log.h" +#include "distributed_hardware_fwk_kit.h" +#include "tokenid_kit.h" + +using namespace OHOS::DistributedHardware; +#undef DH_LOG_TAG +#define DH_LOG_TAG "DistributedHardwareManagerImpl" + +namespace { +// To be implemented. + +constexpr int32_t ERR_NOT_SYSTEM_APP = 202; + +bool IsSystemApp() +{ + uint64_t tokenId = OHOS::IPCSkeleton::GetSelfTokenID(); + return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId); +} + +void PauseDistributedHardwareSync(::ohos::distributedHardware::hardwareManager::HardwareDescriptor const& description) +{ + DHLOGI("PauseDistributedHardware in"); + if (!IsSystemApp()) { + taihe::set_business_error(ERR_NOT_SYSTEM_APP, "The caller is not a system application."); + return; + } + int32_t hardwareType = description.type; + DHType dhType = DHType::UNKNOWN; + DHSubtype dhSubtype = static_cast(hardwareType); + if (dhSubtype == DHSubtype::AUDIO_MIC || dhSubtype == DHSubtype::AUDIO_SPEAKER) { + dhType = DHType::AUDIO; + } else if (dhSubtype == DHSubtype::CAMERA) { + dhType = DHType::CAMERA; + } + std::string srcNetworkId; + if (description.srcNetworkId.has_value()) { + srcNetworkId = description.srcNetworkId.value(); + } + + std::shared_ptr dhFwkKit = std::make_shared(); + int32_t ret = dhFwkKit->PauseDistributedHardware(dhType, std::string(srcNetworkId)); + if (ret != 0) { + DHLOGE("PauseDistributedHardware for DHType: %{public}u filed", (uint32_t)dhType); + } +} + +void ResumeDistributedHardwareSync(::ohos::distributedHardware::hardwareManager::HardwareDescriptor const& description) +{ + DHLOGI("ResumeDistributedHardware in"); + if (!IsSystemApp()) { + taihe::set_business_error(ERR_NOT_SYSTEM_APP, "The caller is not a system application."); + return; + } + int32_t hardwareType = description.type; + DHType dhType = DHType::UNKNOWN; + DHSubtype dhSubtype = static_cast(hardwareType); + if (dhSubtype == DHSubtype::AUDIO_MIC || dhSubtype == DHSubtype::AUDIO_SPEAKER) { + dhType = DHType::AUDIO; + } else if (dhSubtype == DHSubtype::CAMERA) { + dhType = DHType::CAMERA; + } + std::string srcNetworkId; + if (description.srcNetworkId.has_value()) { + srcNetworkId = description.srcNetworkId.value(); + } + + std::shared_ptr dhFwkKit = std::make_shared(); + int32_t ret = dhFwkKit->ResumeDistributedHardware(dhType, std::string(srcNetworkId)); + if (ret != 0) { + DHLOGE("ResumeDistributedHardware for DHType: %{public}u filed", (uint32_t)dhType); + } +} + +void StopDistributedHardwareSync(::ohos::distributedHardware::hardwareManager::HardwareDescriptor const& description) +{ + DHLOGI("StopDistributedHardware in"); + if (!IsSystemApp()) { + taihe::set_business_error(ERR_NOT_SYSTEM_APP, "The caller is not a system application."); + return; + } + int32_t hardwareType = description.type; + DHType dhType = DHType::UNKNOWN; + DHSubtype dhSubtype = static_cast(hardwareType); + if (dhSubtype == DHSubtype::AUDIO_MIC || dhSubtype == DHSubtype::AUDIO_SPEAKER) { + dhType = DHType::AUDIO; + } else if (dhSubtype == DHSubtype::CAMERA) { + dhType = DHType::CAMERA; + } + std::string srcNetworkId; + if (description.srcNetworkId.has_value()) { + srcNetworkId = description.srcNetworkId.value(); + } + + std::shared_ptr dhFwkKit = std::make_shared(); + int32_t ret = dhFwkKit->StopDistributedHardware(dhType, std::string(srcNetworkId)); + if (ret != 0) { + DHLOGE("StopDistributedHardware for DHType: %{public}u filed", (uint32_t)dhType); + } +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_PauseDistributedHardwareSync(PauseDistributedHardwareSync); +TH_EXPORT_CPP_API_ResumeDistributedHardwareSync(ResumeDistributedHardwareSync); +TH_EXPORT_CPP_API_StopDistributedHardwareSync(StopDistributedHardwareSync); +// NOLINTEND diff --git a/taihe/test/test_main.ets b/taihe/test/test_main.ets new file mode 100644 index 0000000000000000000000000000000000000000..101bf46b885a3432bce2d66aa309075d70db13aa --- /dev/null +++ b/taihe/test/test_main.ets @@ -0,0 +1,89 @@ +/* + * 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 hardwareManager from "@ohos.distributedHardware.hardwareManager"; +import { BusinessError } from "@ohos.base"; + +//function test_pauseDistributedHardware() +//{ +// console.println(`Tag into func test_pauseDistributedHardware`); +// try { +// let description: hardwareManager.HardwareDescriptor = { +// type: hardwareManager.DistributedHardwareType.CAMERA, +// srcNetworkId: '1111' +// }; +// hardwareManager.pauseDistributedHardware(description).then(() => { +// console.println('resume distributed hardware successfully'); +// }).catch((error: Error) => { +// console.println('resume distributed hardware failed, cause:' + error); +// }) +// console.log('resume distributed hardware successfully'); +// } catch (error) { +// console.println('resume distributed hardware failed:' + error); +// } +// console.println(`Tag out func test_pauseDistributedHardware`); +//} +// +//function test_resumeDistributedHardware() +//{ +// console.println(`Tag into func test_resumeDistributedHardware`); +// try { +// let description: hardwareManager.HardwareDescriptor = { +// type: hardwareManager.DistributedHardwareType.CAMERA, +// srcNetworkId: '1111' +// }; +// hardwareManager.resumeDistributedHardware(description).then(() => { +// console.println('resume distributed hardware successfully'); +// }).catch((error: Error) => { +// console.println('resume distributed hardware failed, cause:' + error); +// }) +// console.log('resume distributed hardware successfully'); +// } catch (error) { +// console.println('resume distributed hardware failed:' + error); +// } +// console.println(`Tag out func test_resumeDistributedHardware`); +//} +// +//function test_stopDistributedHardware() +//{ +// console.println(`Tag into func test_stopDistributedHardware`); +// try { +// let description: hardwareManager.HardwareDescriptor = { +// type: hardwareManager.DistributedHardwareType.CAMERA, +// srcNetworkId: '1111' +// }; +// hardwareManager.stopDistributedHardware(description).then(() => { +// console.println('resume distributed hardware successfully'); +// }).catch((error: Error) => { +// console.println('resume distributed hardware failed, cause:' + error); +// }) +// console.log('resume distributed hardware successfully'); +// } catch (error) { +// console.println('resume distributed hardware failed:' + error); +// } +// console.println(`Tag out func test_stopDistributedHardware`); +//} + +function main() { + console.println(`Tag into main`); + //console.println(`Tag *************************************************`); + //test_pauseDistributedHardware(); + //console.println(`Tag *************************************************`); + //test_resumeDistributedHardware(); + //console.println(`Tag *************************************************`); + //test_stopDistributedHardware(); + //console.println(`Tag *************************************************`); + console.println(`Tag out main`); +} \ No newline at end of file