From befc656568343206af89f9eca339d113e2b23bb0 Mon Sep 17 00:00:00 2001 From: yudechen Date: Fri, 8 Apr 2022 17:16:54 +0800 Subject: [PATCH 1/4] add two napi. Signed-off-by: yudechen Change-Id: I4f1e9fb40b870e31b58af948c9dc238d694d4d52 --- BUILD.gn | 21 +++++++- interfaces/inner_api/syscap_interface.c | 65 +++++++++++++++++++++++++ interfaces/inner_api/syscap_interface.h | 48 ++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 interfaces/inner_api/syscap_interface.c create mode 100644 interfaces/inner_api/syscap_interface.h diff --git a/BUILD.gn b/BUILD.gn index 12f7bad..815bb53 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -92,6 +92,25 @@ ohos_shared_library("syscap_tool_shared") { part_name = "syscap_codec" } +ohos_shared_library("syscap_interface_shared") { + include_dirs = [ "./interfaces/inner_api" ] + sources = [ "./interfaces/inner_api/syscap_interface.c" ] + + deps = [ "//third_party/bounds_checking_function:libsec_static" ] + + if (defined(ohos_lite)) { + deps += [ "//build/lite/config/component/cJSON:cjson_static" ] + } else { + deps += [ "//third_party/cJSON:cjson_static" ] + } + + subsystem_name = "developtools" + part_name = "syscap_codec" +} + group("syscap_codec") { - deps = [ ":syscap_tool_shared" ] + deps = [ + ":syscap_interface_shared", + ":syscap_tool_shared", + ] } diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c new file mode 100644 index 0000000..fd452af --- /dev/null +++ b/interfaces/inner_api/syscap_interface.c @@ -0,0 +1,65 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include "syscap_interface.h" + +#define PCID_OUT_BUFFER 32 +#define MAX_SYSCAP_STR_LEN 128 + +#define PRINT_ERR(...) \ + do { \ + printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + } while (0) + +bool EncodeOsSyscap(int output[32]) +{ + uint8_t *outputArray = (uint8_t *)malloc(sizeof(int) * PCID_OUT_BUFFER); + if (outputArray == NULL) { + PRINT_ERR("malloc failed."); + return false; + } + (void)memset_s(outputArray, sizeof(int) * PCID_OUT_BUFFER, 0, sizeof(int) * PCID_OUT_BUFFER); + + uint16_t countBytes = PCID_OUT_BUFFER * sizeof(int); + for (uint16_t i = 0; i < countBytes; i++) { + outputArray[i] |= 0XFF; + } + int ret = memcpy_s(output, sizeof(int) * PCID_OUT_BUFFER, outputArray, sizeof(int) * PCID_OUT_BUFFER); + if (ret != 0) { + PRINT_ERR("memcpy_s failed."); + return false; + } + return true; +} + +bool EncodePrivateSyscap(char *output, int *outputLen) +{ + static char syscapStr[MAX_SYSCAP_STR_LEN] = "Systemcapability.Ai.AiEngine"; + int ret = strcpy_s(output, MAX_SYSCAP_STR_LEN, syscapStr); + if (ret != 0) { + PRINT_ERR("strcpy_s failed."); + return false; + } + *outputLen = strlen(syscapStr); + + return true; +} \ No newline at end of file diff --git a/interfaces/inner_api/syscap_interface.h b/interfaces/inner_api/syscap_interface.h new file mode 100644 index 0000000..1649a96 --- /dev/null +++ b/interfaces/inner_api/syscap_interface.h @@ -0,0 +1,48 @@ +/* + * 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. + */ + +#ifndef _SYSCAP_INTERFACE_H +#define _SYSCAP_INTERFACE_H + +#include +#include + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +typedef struct ProductCompatibilityIDHead { + uint16_t apiVersion : 15; + uint16_t apiVersionType : 1; + uint16_t systemType : 3; + uint16_t reserved : 13; + uint32_t manufacturerID; +} PCIDHead; // to do + + +bool EncodeOsSyscap(int output[32]); +bool DecodeOsSyscap(int input[32], char **output, int *outputCnt, int** outputLen); +bool EncodePrivateSyscap(char *output, int *outputLen); +bool DecodePrivateSyscap(char *input, int inputLen, char *output, int *outputCnt, int **outputLen); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _SYSCAP_INTERFACE_H */ \ No newline at end of file -- Gitee From 1d5fa4f34889b0c284a46522dca87c8b39d59c72 Mon Sep 17 00:00:00 2001 From: redjie Date: Sat, 9 Apr 2022 20:14:55 +0800 Subject: [PATCH 2/4] add two functions Signed-off-by: redjie Change-Id: I537718683d5d7f29d4add8f496d4f17dc91a1d53 --- interfaces/inner_api/syscap_interface.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index fd452af..489b5db 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -24,6 +24,7 @@ #define PCID_OUT_BUFFER 32 #define MAX_SYSCAP_STR_LEN 128 + #define PRINT_ERR(...) \ do { \ printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); \ @@ -61,5 +62,15 @@ bool EncodePrivateSyscap(char *output, int *outputLen) } *outputLen = strlen(syscapStr); + return true; +} + +bool DecodeOsSyscap(int input[32], char **output, int *outputCnt, int** outputLen) +{ + return false; +} + +bool DecodePrivateSyscap(char *input, int inputLen, char *output, int *outputCnt, int **outputLen) +{ return true; } \ No newline at end of file -- Gitee From 5eeb6d26c377d865549686ba7029c24b49a3e9ed Mon Sep 17 00:00:00 2001 From: redjie Date: Wed, 13 Apr 2022 15:05:58 +0800 Subject: [PATCH 3/4] add two napi Signed-off-by: redjie Change-Id: I5ff3d4ced7e1e83c682e5c41914236380325fc42 --- BUILD.gn | 10 ++- interfaces/inner_api/syscap_interface.c | 108 +++++++++++++++++++----- 2 files changed, 95 insertions(+), 23 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 815bb53..03e0705 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -93,8 +93,14 @@ ohos_shared_library("syscap_tool_shared") { } ohos_shared_library("syscap_interface_shared") { - include_dirs = [ "./interfaces/inner_api" ] - sources = [ "./interfaces/inner_api/syscap_interface.c" ] + include_dirs = [ + "./interfaces/inner_api", + "src", + ] + sources = [ + "./interfaces/inner_api/syscap_interface.c", + "./src/endian_internal.c", + ] deps = [ "//third_party/bounds_checking_function:libsec_static" ] diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 489b5db..f6c3368 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -13,64 +13,130 @@ * limitations under the License. */ -#include -#include -#include -#include #include +#include +#include +#include +#include #include +#include +#include +#include "endian_internal.h" +#include "cJSON.h" #include "syscap_interface.h" -#define PCID_OUT_BUFFER 32 #define MAX_SYSCAP_STR_LEN 128 - #define PRINT_ERR(...) \ do { \ printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); \ printf(__VA_ARGS__); \ } while (0) +static char *inputFile = "~/ohos/out/rk3568/packages/phone/system/etc/rk3568.sc"; + +static void FreeContextBuffer(char *contextBuffer) +{ + (void)free(contextBuffer); +} + +static uint32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) +{ + uint32_t ret; + FILE *fp = NULL; + struct stat statBuf; + char *contextBuffer = NULL; + ret = stat(inputFile, &statBuf); + if (ret != 0) { + PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", inputFile, errno); + return -1; + } + if (!(statBuf.st_mode & S_IRUSR)) { + PRINT_ERR("don't have permission to read the file(%s)\n", inputFile); + return -1; + } + contextBuffer = (char *)malloc(statBuf.st_size + 1); + if (contextBuffer == NULL) { + PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); + return -1; + } + fp = fopen(inputFile, "rb"); + if (fp == NULL) { + PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno); + FreeContextBuffer(contextBuffer); + return -1; + } + ret = fread(contextBuffer, statBuf.st_size, 1, fp); + if (ret != 1) { + PRINT_ERR("read file(%s) failed, errno = %d\n", inputFile, errno); + FreeContextBuffer(contextBuffer); + (void)fclose(fp); + return -1; + } + contextBuffer[statBuf.st_size] = '\0'; + (void)fclose(fp); + + *contextBufPtr = contextBuffer; + *bufferLen = statBuf.st_size + 1; + return 0; +} + bool EncodeOsSyscap(int output[32]) { - uint8_t *outputArray = (uint8_t *)malloc(sizeof(int) * PCID_OUT_BUFFER); - if (outputArray == NULL) { - PRINT_ERR("malloc failed."); + int32_t ret; + int32_t res; + char *contextBuffer = NULL; + uint32_t bufferLen; + + ret = GetFileContext(&contextBuffer, &bufferLen); + if (ret != 0) { + PRINT_ERR("GetFileContext failed, input file : rk3568.sc\n"); return false; } - (void)memset_s(outputArray, sizeof(int) * PCID_OUT_BUFFER, 0, sizeof(int) * PCID_OUT_BUFFER); - uint16_t countBytes = PCID_OUT_BUFFER * sizeof(int); - for (uint16_t i = 0; i < countBytes; i++) { - outputArray[i] |= 0XFF; - } - int ret = memcpy_s(output, sizeof(int) * PCID_OUT_BUFFER, outputArray, sizeof(int) * PCID_OUT_BUFFER); - if (ret != 0) { - PRINT_ERR("memcpy_s failed."); + res = strcpy_s(output, MAX_SYSCAP_STR_LEN, contextBuffer); + if (res != 0) { + PRINT_ERR("strcpy_s failed."); + FreeContextBuffer(contextBuffer); return false; } + + FreeContextBuffer(contextBuffer); return true; } bool EncodePrivateSyscap(char *output, int *outputLen) { - static char syscapStr[MAX_SYSCAP_STR_LEN] = "Systemcapability.Ai.AiEngine"; - int ret = strcpy_s(output, MAX_SYSCAP_STR_LEN, syscapStr); + int32_t ret; + int32_t res; + char *contextBuffer = NULL; + uint32_t bufferLen; + + ret = GetFileContext(&contextBuffer, &bufferLen); if (ret != 0) { + PRINT_ERR("GetFileContext failed, input file : rk3568.sc\n"); + return false; + } + *outputLen = bufferLen - MAX_SYSCAP_STR_LEN - 1; + res = strncpy_s(output, *outputLen + 1, contextBuffer + MAX_SYSCAP_STR_LEN, *outputLen); + if (res != 0) { PRINT_ERR("strcpy_s failed."); + FreeContextBuffer(contextBuffer); return false; } - *outputLen = strlen(syscapStr); + FreeContextBuffer(contextBuffer); return true; } bool DecodeOsSyscap(int input[32], char **output, int *outputCnt, int** outputLen) { - return false; + return true; } bool DecodePrivateSyscap(char *input, int inputLen, char *output, int *outputCnt, int **outputLen) { + char Device[7] = "Device."; + char Vendor[7] = "Vendor."; return true; } \ No newline at end of file -- Gitee From 666d10ba90a15e440a9521412477f8a67a0d9416 Mon Sep 17 00:00:00 2001 From: redjie Date: Thu, 14 Apr 2022 17:04:48 +0800 Subject: [PATCH 4/4] modify three napi functions Signed-off-by: redjie Change-Id: I78b08eb7c8892651bac51ec335b62951d496c583 --- interfaces/inner_api/syscap_interface.c | 72 +++++++++++++++++++++---- interfaces/inner_api/syscap_interface.h | 9 ++-- 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index f6c3368..9d40a18 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -16,16 +16,15 @@ #include #include #include -#include +#include #include #include #include #include -#include "endian_internal.h" -#include "cJSON.h" #include "syscap_interface.h" #define MAX_SYSCAP_STR_LEN 128 +#define MAX_PRISYSCAP_COUNT 32 #define PRINT_ERR(...) \ do { \ @@ -81,11 +80,12 @@ static uint32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) return 0; } -bool EncodeOsSyscap(int output[32]) +bool EncodeOsSyscap(int **output) { int32_t ret; int32_t res; char *contextBuffer = NULL; + char *outputStr = NULL; uint32_t bufferLen; ret = GetFileContext(&contextBuffer, &bufferLen); @@ -93,8 +93,13 @@ bool EncodeOsSyscap(int output[32]) PRINT_ERR("GetFileContext failed, input file : rk3568.sc\n"); return false; } - - res = strcpy_s(output, MAX_SYSCAP_STR_LEN, contextBuffer); + outputStr = (char *)malloc(MAX_SYSCAP_STR_LEN); + if (outputStr == NULL) { + PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", MAX_SYSCAP_STR_LEN, errno); + return false; + } + (void)memset_s(outputStr, MAX_SYSCAP_STR_LEN, 0, MAX_SYSCAP_STR_LEN); + res = strcpy_s(outputStr, MAX_SYSCAP_STR_LEN, contextBuffer); if (res != 0) { PRINT_ERR("strcpy_s failed."); FreeContextBuffer(contextBuffer); @@ -102,14 +107,16 @@ bool EncodeOsSyscap(int output[32]) } FreeContextBuffer(contextBuffer); + *output = outputStr; return true; } -bool EncodePrivateSyscap(char *output, int *outputLen) +bool EncodePrivateSyscap(char **output, int *outputLen) { int32_t ret; int32_t res; char *contextBuffer = NULL; + char *outputStr = NULL; uint32_t bufferLen; ret = GetFileContext(&contextBuffer, &bufferLen); @@ -117,7 +124,14 @@ bool EncodePrivateSyscap(char *output, int *outputLen) PRINT_ERR("GetFileContext failed, input file : rk3568.sc\n"); return false; } + *outputLen = bufferLen - MAX_SYSCAP_STR_LEN - 1; + outputStr = (char *)malloc(*outputLen ); + if (outputStr == NULL) { + PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", *outputLen, errno); + return false; + } + (void)memset_s(outputStr, *outputLen, 0, *outputLen); res = strncpy_s(output, *outputLen + 1, contextBuffer + MAX_SYSCAP_STR_LEN, *outputLen); if (res != 0) { PRINT_ERR("strcpy_s failed."); @@ -126,17 +140,53 @@ bool EncodePrivateSyscap(char *output, int *outputLen) } FreeContextBuffer(contextBuffer); + *output = outputStr; return true; } -bool DecodeOsSyscap(int input[32], char **output, int *outputCnt, int** outputLen) +bool DecodeOsSyscap(int input[32], char ***output, int *outputCnt) { return true; } -bool DecodePrivateSyscap(char *input, int inputLen, char *output, int *outputCnt, int **outputLen) +bool DecodePrivateSyscap(char *input, char ***output, int *outputCnt) { - char Device[7] = "Device."; - char Vendor[7] = "Vendor."; + char **outputArray = NULL; + char *inputStr = input; + int32_t outcharLen = 0; + int32_t ret; + uint32_t bufferLen; + *outputCnt = 0; + + while(NULL != inputStr){ + if(*inputStr = ',') + *outputCnt++; + } + bufferLen = MAX_SYSCAP_STR_LEN*(*outputCnt); + outputArray = (char **)malloc(bufferLen); + if (outputArray == NULL) { + PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", bufferLen, errno); + *outputCnt = 0; + return false; + } + (void)memset_s(outputArray, bufferLen, 0, bufferLen); + + while(inputStr != NULL){ + while(*inputStr != ','){ + outcharLen ++; + } + ret = strcpy_s(*outputArray, outcharLen, inputStr); + if (ret != 0) { + PRINT_ERR("strcpy_s failed."); + *outputCnt = 0; + free(outputArray); + return false; + } + inputStr = inputStr + outcharLen; + outputArray++; + outcharLen = 0; + } + + **output = outputArray; return true; } \ No newline at end of file diff --git a/interfaces/inner_api/syscap_interface.h b/interfaces/inner_api/syscap_interface.h index 1649a96..7819363 100644 --- a/interfaces/inner_api/syscap_interface.h +++ b/interfaces/inner_api/syscap_interface.h @@ -33,11 +33,10 @@ typedef struct ProductCompatibilityIDHead { uint32_t manufacturerID; } PCIDHead; // to do - -bool EncodeOsSyscap(int output[32]); -bool DecodeOsSyscap(int input[32], char **output, int *outputCnt, int** outputLen); -bool EncodePrivateSyscap(char *output, int *outputLen); -bool DecodePrivateSyscap(char *input, int inputLen, char *output, int *outputCnt, int **outputLen); +bool EncodeOsSyscap(int **output); +bool DecodeOsSyscap(int input[32], char ***output, int *outputCnt); +bool EncodePrivateSyscap(char **output, int *outputLen); +bool DecodePrivateSyscap(char *input, char ***output, int *outputCnt); #ifdef __cplusplus #if __cplusplus -- Gitee