add js api querySystemCapabilities.

Signed-off-by: yudechen <chenyude@huawei.com>
Change-Id: I1121a14432b8114e47bbd6880a7dd99ecd0c50af
This commit is contained in:
yudechen
2022-04-24 17:20:01 +08:00
parent a23910bc9c
commit c40730bf68
4 changed files with 241 additions and 1 deletions
+2 -1
View File
@@ -24,7 +24,8 @@
},
"build": {
"sub_component": [
"//developtools/syscap_codec:syscap_codec"
"//developtools/syscap_codec:syscap_codec",
"//developtools/syscap_codec/napi:systemcapability"
],
"inner_kits": [],
"test": [ "//developtools/syscap_codec/test/unittest/common:unittest" ]
+61
View File
@@ -0,0 +1,61 @@
# Copyright (C) 2022-2022 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")
import("//build/ohos/ace/ace.gni")
base_output_path = get_label_info(":query_syscap", "target_out_dir")
query_syscap_obj_path = base_output_path + "/query_syscap.o"
gen_js_obj("query_syscap_js") {
input = "query_syscap.js"
output = query_syscap_obj_path
}
sources_platform_common = [
"../src/syscap_tool.c",
"../src/create_pcid.c",
"../src/endian_internal.c",
"../interfaces/inner_api/syscap_interface.c",
]
ohos_shared_library("systemcapability") {
include_dirs = [
"//third_party/node/src",
"//foundation/ace/napi/interfaces/kits",
"//developtools/syscap_codec/napi",
"//developtools/syscap_codec/interfaces/inner_api",
"//developtools/syscap_codec/include",
]
sources = [ "napi_query_syscap.cpp" ]
sources += sources_platform_common
deps = [
":query_syscap_js",
"//foundation/ace/napi:ace_napi",
"//third_party/bounds_checking_function:libsec_static",
]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
if (defined(ohos_lite)) {
deps += [ "//build/lite/config/component/cJSON:cjson_static" ]
} else {
deps += [ "//third_party/cJSON:cjson_static" ]
}
relative_install_dir = "module"
subsystem_name = "developtools"
part_name = "syscap_codec"
}
+171
View File
@@ -0,0 +1,171 @@
/*
* Copyright (C) 2022-2022 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 <cstring>
#include <unistd.h>
#include <securec.h>
#include <cstdint>
#include "hilog/log.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "syscap_interface.h"
namespace OHOS {
EXTERN_C_START
#define OS_SYSCAP_U32_NUM 30
#define U32_TO_STR_MAX_LEN 11
#define SYSCAP_STR_MAX_LEN 128
#define PRINT_ERR(...) \
do { \
printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
} while (0)
napi_value QuerySystemCapability(napi_env env, napi_callback_info info)
{
bool retBool;
int retError, priOutputLen, priCapArrayCnt, sumLen;
int i = 0;
int *osOutput = nullptr;
uint32_t *osCapU32 = nullptr;
char *priOutput = nullptr;
char *temp = nullptr;
char *allSyscapBUffer = nullptr;
char osCapArray[OS_SYSCAP_U32_NUM][U32_TO_STR_MAX_LEN] = {};
char (*priCapArray)[SYSCAP_STR_MAX_LEN] = nullptr;
napi_status ret = napi_ok;
napi_value result = nullptr;
(void)info;
retBool = EncodeOsSyscap(&osOutput);
if (!retBool) {
PRINT_ERR("get encoded os syscap failed.");
result = nullptr;
goto FREE_OSOUTPUT;
}
retBool = EncodePrivateSyscap(&priOutput, &priOutputLen);
if (!retBool) {
PRINT_ERR("get encoded private syscap failed.");
result = nullptr;
goto FREE_PRIOUTPUT;
}
osCapU32 = (uint32_t *)(osOutput + 2); // 2, header of pcid.sc
for (i = 0; i < OS_SYSCAP_U32_NUM; i++) {
retError = sprintf_s(osCapArray[i], U32_TO_STR_MAX_LEN, "%u", osCapU32[i]);
if (retError == -1) {
PRINT_ERR("get uint32_t syscap string failed.");
result = nullptr;
goto FREE_PRIOUTPUT;
}
}
retBool = DecodePrivateSyscap(priOutput, &priCapArray, &priCapArrayCnt);
if (!retBool) {
PRINT_ERR("get encoded private syscap failed.");
result = nullptr;
goto FREE_PRICAP_ARRAY;
}
// calculate all string length
sumLen = 0;
for (i = 0; i < OS_SYSCAP_U32_NUM; i++) {
sumLen += strlen(osCapArray[i]);
}
for (i = 0; i < priCapArrayCnt; i++) {
sumLen += strlen(*(priCapArray + i));
}
sumLen += (OS_SYSCAP_U32_NUM + priCapArrayCnt + 1); // split with ','
// splicing string
allSyscapBUffer = (char *)malloc(sumLen);
if (allSyscapBUffer ==nullptr) {
PRINT_ERR("malloc failed!");
result = nullptr;
goto FREE_PRICAP_ARRAY;
}
(void)memset_s(allSyscapBUffer, sumLen, 0, sumLen);
temp = *osCapArray;
for (i = 1; i < OS_SYSCAP_U32_NUM; i++) {
retError = sprintf_s(allSyscapBUffer, sumLen, "%s,%s", temp, osCapArray[i]);
if (retError == -1) {
PRINT_ERR("splicing os syscap string failed.");
result = nullptr;
goto FREE_PRICAP_ARRAY;
}
temp = allSyscapBUffer;
}
for (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.");
result = nullptr;
goto FREE_PRICAP_ARRAY;
}
temp = allSyscapBUffer;
}
ret = napi_create_string_utf8(env, allSyscapBUffer, sumLen, &result);
if (ret != napi_ok) {
result = nullptr;
goto FREE_ALL_SYSCAP_BUFFER;
}
FREE_ALL_SYSCAP_BUFFER:
free(allSyscapBUffer);
FREE_PRICAP_ARRAY:
free(priCapArray);
FREE_PRIOUTPUT:
free(priOutput);
FREE_OSOUTPUT:
free(osOutput);
temp = nullptr;
return result;
}
napi_value QuerryExport(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
DECLARE_NAPI_FUNCTION("querySystemCapabilities", QuerySystemCapability),
};
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
return exports;
}
EXTERN_C_END
/*
* Module define
*/
static napi_module systemCapabilityModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = QuerryExport,
.nm_modname = "systemCapability",
.nm_priv = ((void*)0),
.reserved = {0},
};
/*
* Module register function
*/
extern "C" __attribute__((constructor)) void systemCapabilityRegisterModule(void)
{
napi_module_register(&systemCapabilityModule);
}
}
+7
View File
@@ -0,0 +1,7 @@
// 首先需要通过requireInternal函数加载本模块
const systemCapability = requireInternal('systemCapability');
// 这里定义了calc模块对外暴露的所有api
export default {
querySystemCapabilities: systemCapability.querySystemCapabilities
}