From 4f7a588280120057ed42965cf185dab027e1b305 Mon Sep 17 00:00:00 2001 From: cat Date: Wed, 18 Jun 2025 14:40:44 +0800 Subject: [PATCH] add arkts1.2 code Signed-off-by: cat --- bundle.json | 6 +- taihe/BUILD.gn | 22 +++ taihe/syscap/BUILD.gn | 86 +++++++++ taihe/syscap/idl/ohos.systemCapability.taihe | 23 +++ taihe/syscap/src/ani_constructor.cpp | 29 +++ .../syscap/src/ohos.systemCapability.impl.cpp | 167 ++++++++++++++++++ 6 files changed, 331 insertions(+), 2 deletions(-) create mode 100644 taihe/BUILD.gn create mode 100644 taihe/syscap/BUILD.gn create mode 100644 taihe/syscap/idl/ohos.systemCapability.taihe create mode 100644 taihe/syscap/src/ani_constructor.cpp create mode 100644 taihe/syscap/src/ohos.systemCapability.impl.cpp diff --git a/bundle.json b/bundle.json index d8415a7..791ff30 100644 --- a/bundle.json +++ b/bundle.json @@ -27,13 +27,15 @@ "components": [ "napi", "bounds_checking_function", - "cJSON" + "cJSON", + "runtime_core" ], "third_party": [] }, "build": { "sub_component": [ - "//developtools/syscap_codec:syscap_codec" + "//developtools/syscap_codec:syscap_codec", + "//developtools/syscap_codec/taihe:taihe_group" ], "inner_kits": [ { diff --git a/taihe/BUILD.gn b/taihe/BUILD.gn new file mode 100644 index 0000000..5f1b0f7 --- /dev/null +++ b/taihe/BUILD.gn @@ -0,0 +1,22 @@ +# 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/ohos.gni") + +group("taihe_group") { + if (support_jsapi) { + deps = [ + "syscap:systemCapability_etc", + "syscap:systemCapability_taihe_native", + ] + } +} \ No newline at end of file diff --git a/taihe/syscap/BUILD.gn b/taihe/syscap/BUILD.gn new file mode 100644 index 0000000..49d1955 --- /dev/null +++ b/taihe/syscap/BUILD.gn @@ -0,0 +1,86 @@ +# 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") + +copy_taihe_idl("copy_systemCapability") { + sources = [ "idl/ohos.systemCapability.taihe" ] +} +subsystem_name = "developtools" +part_name = "syscap_codec" +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_systemCapability" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.systemCapability.ani.cpp", + "$taihe_generated_file_path/src/ohos.systemCapability.abi.c", + ] +} + +taihe_shared_library("systemCapability_taihe_native") { + 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 = [ + "../../src", + "../../interfaces/inner_api", + "../../include", + "../../include/codec_config" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.systemCapability.impl.cpp", + "../../src/syscap_tool.c", + "../../src/create_pcid.c", + "../../src/endian_internal.c", + "../../src/common_method.c", + "../../src/context_tool.c", + "../../interfaces/inner_api/syscap_interface.c", + ] + deps = [ + ":run_taihe", + ] + external_deps = [ + "bounds_checking_function:libsec_shared", + ] + if (defined(ohos_lite)) { + deps += [ "//build/lite/config/component/cJSON:cjson_static" ] + } else { + external_deps += [ "cJSON:cjson_static" ] + } +} + +generate_static_abc("systemCapability") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.systemCapability.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/systemCapability.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("systemCapability_etc") { + source = "$target_out_dir/systemCapability.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":systemCapability" ] +} diff --git a/taihe/syscap/idl/ohos.systemCapability.taihe b/taihe/syscap/idl/ohos.systemCapability.taihe new file mode 100644 index 0000000..c300963 --- /dev/null +++ b/taihe/syscap/idl/ohos.systemCapability.taihe @@ -0,0 +1,23 @@ +/* + * 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.systemCapability", "systemCapability") + +@!sts_inject(""" +static { loadLibrary("systemCapability_taihe_native.z") } +""") + +@gen_async("querySystemCapabilities") +@gen_promise("querySystemCapabilities") +function querySystemCapabilitie(): String; diff --git a/taihe/syscap/src/ani_constructor.cpp b/taihe/syscap/src/ani_constructor.cpp new file mode 100644 index 0000000..5b98ab3 --- /dev/null +++ b/taihe/syscap/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 "taihe/runtime.hpp" +#include "ohos.systemCapability.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::systemCapability::ANIRegister(env)) { + std::cerr << "Error from ohos::systemCapability::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/taihe/syscap/src/ohos.systemCapability.impl.cpp b/taihe/syscap/src/ohos.systemCapability.impl.cpp new file mode 100644 index 0000000..8b93a5e --- /dev/null +++ b/taihe/syscap/src/ohos.systemCapability.impl.cpp @@ -0,0 +1,167 @@ +/* + * 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.systemCapability.proj.hpp" +#include "ohos.systemCapability.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include +#include +#include +#include +#include "syscap_interface.h" +#include "context_tool.h" + +using namespace taihe; + +namespace { +// To be implemented. +constexpr size_t OS_SYSCAP_U32_NUM = 30; +constexpr size_t PCID_MAIN_U32 = OS_SYSCAP_U32_NUM + 2; +constexpr size_t U32_TO_STR_MAX_LEN = 11; +constexpr size_t KEY_BUFFER_SIZE = 32; + +struct SystemCapabilityAsyncContext { + char key[KEY_BUFFER_SIZE] = { 0 }; + size_t keyLen = 0; + char *value = nullptr; + size_t valueLen = 0; + int status = 0; +}; + +static char* CalculateAllStringLength(char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN], + char (*priCapArray)[SINGLE_SYSCAP_LEN], bool retBool, int priCapArrayCnt) +{ + errno_t err = EOK; + char *temp = nullptr; + int retError; + int sumLen = 0; + char *allSyscapBuffer = nullptr; + + if (!retBool) { + PRINT_ERR("get encoded private syscap failed."); + return allSyscapBuffer; + } + + for (size_t i = 0; i < PCID_MAIN_U32; i++) { + sumLen += strlen(osCapArray[i]); + } + for (int i = 0; i < priCapArrayCnt; i++) { + sumLen += strlen(*(priCapArray + i)); + } + sumLen += (PCID_MAIN_U32 + priCapArrayCnt + 1); // split with ',' + + // splicing string + allSyscapBuffer = (char *)malloc(sumLen); + if (allSyscapBuffer == nullptr) { + PRINT_ERR("malloc failed!"); + return allSyscapBuffer; + } + err = memset_s(allSyscapBuffer, sumLen, 0, sumLen); + if (err != EOK) { + PRINT_ERR("memset failed!"); + free(allSyscapBuffer); + return nullptr; + } + temp = *osCapArray; + + for (size_t i = 1; i < PCID_MAIN_U32; i++) { + retError = sprintf_s(allSyscapBuffer, sumLen, "%s,%s", temp, osCapArray[i]); + if (retError == -1) { + PRINT_ERR("splicing os syscap string failed."); + free(allSyscapBuffer); + return nullptr; + } + temp = allSyscapBuffer; + } + for (int i = 0; i < priCapArrayCnt; i++) { + retError = sprintf_s(allSyscapBuffer, sumLen, "%s,%s", temp, *(priCapArray + i)); + if (retError == -1) { + PRINT_ERR("splicing pri syscap string failed."); + free(allSyscapBuffer); + return nullptr; + } + temp = allSyscapBuffer; + } + return allSyscapBuffer; +} + +static char* GetSystemCapability() +{ + bool retBool; + int retError; + int priOutputLen; + int priCapArrayCnt; + char osOutput[SINGLE_SYSCAP_LEN] = {}; + + uint32_t *osCapU32 = nullptr; + char *priOutput = nullptr; + + char *allSyscapBuffer = nullptr; + char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN] = {}; + char (*priCapArray)[SINGLE_SYSCAP_LEN] = nullptr; + + retBool = EncodeOsSyscap(osOutput, PCID_MAIN_BYTES); + if (!retBool) { + PRINT_ERR("get encoded os syscap failed."); + return nullptr; + } + retBool = EncodePrivateSyscap(&priOutput, &priOutputLen); + if (!retBool) { + PRINT_ERR("get encoded private syscap failed."); + goto FREE_PRIOUTPUT; + } + + osCapU32 = reinterpret_cast(osOutput); + for (size_t i = 0; i < PCID_MAIN_U32; i++) { // 2, header of pcid.sc + retError = sprintf_s(osCapArray[i], U32_TO_STR_MAX_LEN, "%u", osCapU32[i]); + if (retError == -1) { + PRINT_ERR("get uint32_t syscap string failed."); + goto FREE_PRIOUTPUT; + } + } + retBool = DecodePrivateSyscap(priOutput, &priCapArray, &priCapArrayCnt); + allSyscapBuffer = CalculateAllStringLength(osCapArray, priCapArray, retBool, priCapArrayCnt); + free(priCapArray); + +FREE_PRIOUTPUT: + free(priOutput); + + return allSyscapBuffer; +} + +string querySystemCapabilitie() +{ + SystemCapabilityAsyncContext *asyncContext = new SystemCapabilityAsyncContext(); + char *syscapStr = GetSystemCapability(); + if (syscapStr != nullptr) { + asyncContext->value = syscapStr; + asyncContext->status = 0; + } else { + asyncContext->status = 1; + } + string value = ""; + if (!asyncContext->status) { + value = asyncContext->value; + } else { + taihe::set_business_error(-1, "key does not exist"); + } + return value; +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_querySystemCapabilitie(querySystemCapabilitie); +// NOLINTEND -- Gitee