From 11677cb811c95b56d51305461880abbed45d2c8e Mon Sep 17 00:00:00 2001 From: ruimingzhang Date: Wed, 10 Apr 2024 09:21:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ffi=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interfaces/kits/ffi/src/config_policy_ffi.cpp | 129 ++++++++++++++++++ interfaces/kits/ffi/src/config_policy_ffi.h | 28 ++++ interfaces/kits/ffi/src/config_policy_log.h | 41 ++++++ .../kits/ffi/src/config_policy_mock.cpp | 22 +++ 4 files changed, 220 insertions(+) create mode 100644 interfaces/kits/ffi/src/config_policy_ffi.cpp create mode 100644 interfaces/kits/ffi/src/config_policy_ffi.h create mode 100644 interfaces/kits/ffi/src/config_policy_log.h create mode 100644 interfaces/kits/ffi/src/config_policy_mock.cpp diff --git a/interfaces/kits/ffi/src/config_policy_ffi.cpp b/interfaces/kits/ffi/src/config_policy_ffi.cpp new file mode 100644 index 0000000..040e939 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_ffi.cpp @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2024 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 "config_policy_ffi.h" + +#include +#include +#include "config_policy_utils.h" +#include "hisysevent_adapter.h" +#include "config_policy_log.h" + +namespace OHOS::Customization::ConfigPolicy { + +char** MallocCStringArr(const std::vector& origin) +{ + if (origin.empty()) { + return nullptr; + } + auto size = origin.size(); + auto arr = static_cast(malloc(sizeof(char*) * size)); + if (arr == nullptr) { + return nullptr; + } + for (size_t i = 0; i < size; i++) { + size_t len = strlen(origin[i]) + 1; + arr[i] = static_cast(malloc(sizeof(char) * len)); + if (arr[i] == nullptr) { + continue; + } + errno_t ret = strcpy_s(arr[i], len, origin[i]); + if (ret != 0) { + free(arr[i]); + arr[i] = nullptr; + } + } + return arr; +} + +extern "C" { + RetDataCArrString CJ_GetCfgDirList() + { + LOGI("CJ_GetCfgDirList start"); + RetDataCArrString ret = { .code = SUCCESS_CODE, .data = { .head = nullptr, .size = 0 } }; + CfgDir *cfgDir = GetCfgDirList(); + + std::vector dirList; + if (cfgDir != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgDir->paths[i] != nullptr) { + dirList.push_back(cfgDir->paths[i]); + } + } + } + + ret.data.head = MallocCStringArr(dirList); + ret.data.size = static_cast(ret.data.head == nullptr ? 0 : dirList.size()); + + FreeCfgDirList(cfgDir); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgDirList", ""); + LOGI("CJ_GetCfgDirList ok"); + return ret; + } + + RetDataCArrString CJ_GetCfgFiles(const char* relPath) + { + LOGI("CJ_GetCfgFiles start"); + RetDataCArrString ret = { .code = SUCCESS_CODE, .data = { .head = nullptr, .size = 0 } }; + LOGI("input path: [%{public}s]", relPath) + std::string extra(""); + CfgFiles *cfgFiles = GetCfgFilesEx(relPath, FOLLOWX_MODE_DEFAULT, extra.c_str()); + + std::vector fileList; + if (cfgFiles != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgFiles->paths[i] != nullptr) { + fileList.push_back(cfgFiles->paths[i]); + } + } + } + + ret.data.head = MallocCStringArr(fileList); + ret.data.size = static_cast(ret.data.head == nullptr ? 0 : fileList.size()); + + FreeCfgFiles(cfgFiles); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgFiles", ""); + return ret; + } + + RetDataCString CJ_GetOneCfgFile(const char* relPath) + { + LOGI("CJ_GetOneCfgFile start"); + RetDataCString ret = { .code = SUCCESS_CODE, .data = nullptr }; + char outBuf[MAX_PATH_LEN] = {0}; + std::string extra(""); + char* filePath = GetOneCfgFileEx(relPath, outBuf, MAX_PATH_LEN, FOLLOWX_MODE_DEFAULT, extra.c_str()); + + if (filePath == nullptr) { + LOGI("GetOneCfgFileEx result is nullptr."); + return ret; + } else { + LOGI("GetOneCfgFile return [%{public}s]", filePath); + } + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getOneCfgFile", ""); + size_t pathLen = strlen(filePath) + 1; + ret.data = static_cast(malloc(sizeof(char) * pathLen)); + if (ret.data == nullptr) { + return ret; + } + errno_t err = strcpy_s(ret.data, pathLen, filePath); + if (err != 0) { + free(ret.data); + ret.data = nullptr; + } + return ret; + } +} +} // namespace OHOS::Customization::ConfigPolicy diff --git a/interfaces/kits/ffi/src/config_policy_ffi.h b/interfaces/kits/ffi/src/config_policy_ffi.h new file mode 100644 index 0000000..45c5008 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_ffi.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 CONFIG_POLICY_FFI_H +#define CONFIG_POLICY_FFI_H + +#include "ffi_remote_data.h" +#include "cj_ffi/cj_common_ffi.h" + +extern "C" { + FFI_EXPORT RetDataCArrString CJ_GetCfgDirList(); + FFI_EXPORT RetDataCArrString CJ_GetCfgFiles(const char* relPath); + FFI_EXPORT RetDataCString CJ_GetOneCfgFile(const char* relPath); +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/ffi/src/config_policy_log.h b/interfaces/kits/ffi/src/config_policy_log.h new file mode 100644 index 0000000..afe7002 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_log.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 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 CONFIG_POLICY_LOG_H +#define CONFIG_POLICY_LOG_H + +#include "hilog/log.h" + +#ifdef LOG_DOMAIN +#undef LOG_DOMAIN +#endif +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_DOMAIN 0xD001E00 +#define LOG_TAG "ConfigPolicyFFI" + +#define LOGI(...) \ +if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_INFO)) { \ + HILOG_INFO(LOG_CORE, ##__VA_ARGS__); \ +} + +#define LOGE(...) \ +if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_ERROR)) { \ + HILOG_ERROR(LOG_CORE, ##__VA_ARGS__); \ +} + +#endif diff --git a/interfaces/kits/ffi/src/config_policy_mock.cpp b/interfaces/kits/ffi/src/config_policy_mock.cpp new file mode 100644 index 0000000..60a6668 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_mock.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 "cj_ffi/cj_common_ffi.h" + +extern "C" { +FFI_EXPORT int CJ_GetCfgDirList = 0; +FFI_EXPORT int CJ_GetCfgFiles = 0; +FFI_EXPORT int CJ_GetOneCfgFile = 0; +} \ No newline at end of file -- Gitee From 3921b33984a828b1bed4bf243482a85df2c1b26f Mon Sep 17 00:00:00 2001 From: ruimingzhang Date: Wed, 10 Apr 2024 09:21:29 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ffi=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ruimingzhang --- interfaces/kits/ffi/src/config_policy_ffi.cpp | 129 ++++++++++++++++++ interfaces/kits/ffi/src/config_policy_ffi.h | 28 ++++ interfaces/kits/ffi/src/config_policy_log.h | 41 ++++++ .../kits/ffi/src/config_policy_mock.cpp | 22 +++ 4 files changed, 220 insertions(+) create mode 100644 interfaces/kits/ffi/src/config_policy_ffi.cpp create mode 100644 interfaces/kits/ffi/src/config_policy_ffi.h create mode 100644 interfaces/kits/ffi/src/config_policy_log.h create mode 100644 interfaces/kits/ffi/src/config_policy_mock.cpp diff --git a/interfaces/kits/ffi/src/config_policy_ffi.cpp b/interfaces/kits/ffi/src/config_policy_ffi.cpp new file mode 100644 index 0000000..040e939 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_ffi.cpp @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2024 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 "config_policy_ffi.h" + +#include +#include +#include "config_policy_utils.h" +#include "hisysevent_adapter.h" +#include "config_policy_log.h" + +namespace OHOS::Customization::ConfigPolicy { + +char** MallocCStringArr(const std::vector& origin) +{ + if (origin.empty()) { + return nullptr; + } + auto size = origin.size(); + auto arr = static_cast(malloc(sizeof(char*) * size)); + if (arr == nullptr) { + return nullptr; + } + for (size_t i = 0; i < size; i++) { + size_t len = strlen(origin[i]) + 1; + arr[i] = static_cast(malloc(sizeof(char) * len)); + if (arr[i] == nullptr) { + continue; + } + errno_t ret = strcpy_s(arr[i], len, origin[i]); + if (ret != 0) { + free(arr[i]); + arr[i] = nullptr; + } + } + return arr; +} + +extern "C" { + RetDataCArrString CJ_GetCfgDirList() + { + LOGI("CJ_GetCfgDirList start"); + RetDataCArrString ret = { .code = SUCCESS_CODE, .data = { .head = nullptr, .size = 0 } }; + CfgDir *cfgDir = GetCfgDirList(); + + std::vector dirList; + if (cfgDir != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgDir->paths[i] != nullptr) { + dirList.push_back(cfgDir->paths[i]); + } + } + } + + ret.data.head = MallocCStringArr(dirList); + ret.data.size = static_cast(ret.data.head == nullptr ? 0 : dirList.size()); + + FreeCfgDirList(cfgDir); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgDirList", ""); + LOGI("CJ_GetCfgDirList ok"); + return ret; + } + + RetDataCArrString CJ_GetCfgFiles(const char* relPath) + { + LOGI("CJ_GetCfgFiles start"); + RetDataCArrString ret = { .code = SUCCESS_CODE, .data = { .head = nullptr, .size = 0 } }; + LOGI("input path: [%{public}s]", relPath) + std::string extra(""); + CfgFiles *cfgFiles = GetCfgFilesEx(relPath, FOLLOWX_MODE_DEFAULT, extra.c_str()); + + std::vector fileList; + if (cfgFiles != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgFiles->paths[i] != nullptr) { + fileList.push_back(cfgFiles->paths[i]); + } + } + } + + ret.data.head = MallocCStringArr(fileList); + ret.data.size = static_cast(ret.data.head == nullptr ? 0 : fileList.size()); + + FreeCfgFiles(cfgFiles); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgFiles", ""); + return ret; + } + + RetDataCString CJ_GetOneCfgFile(const char* relPath) + { + LOGI("CJ_GetOneCfgFile start"); + RetDataCString ret = { .code = SUCCESS_CODE, .data = nullptr }; + char outBuf[MAX_PATH_LEN] = {0}; + std::string extra(""); + char* filePath = GetOneCfgFileEx(relPath, outBuf, MAX_PATH_LEN, FOLLOWX_MODE_DEFAULT, extra.c_str()); + + if (filePath == nullptr) { + LOGI("GetOneCfgFileEx result is nullptr."); + return ret; + } else { + LOGI("GetOneCfgFile return [%{public}s]", filePath); + } + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getOneCfgFile", ""); + size_t pathLen = strlen(filePath) + 1; + ret.data = static_cast(malloc(sizeof(char) * pathLen)); + if (ret.data == nullptr) { + return ret; + } + errno_t err = strcpy_s(ret.data, pathLen, filePath); + if (err != 0) { + free(ret.data); + ret.data = nullptr; + } + return ret; + } +} +} // namespace OHOS::Customization::ConfigPolicy diff --git a/interfaces/kits/ffi/src/config_policy_ffi.h b/interfaces/kits/ffi/src/config_policy_ffi.h new file mode 100644 index 0000000..45c5008 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_ffi.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 CONFIG_POLICY_FFI_H +#define CONFIG_POLICY_FFI_H + +#include "ffi_remote_data.h" +#include "cj_ffi/cj_common_ffi.h" + +extern "C" { + FFI_EXPORT RetDataCArrString CJ_GetCfgDirList(); + FFI_EXPORT RetDataCArrString CJ_GetCfgFiles(const char* relPath); + FFI_EXPORT RetDataCString CJ_GetOneCfgFile(const char* relPath); +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/ffi/src/config_policy_log.h b/interfaces/kits/ffi/src/config_policy_log.h new file mode 100644 index 0000000..afe7002 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_log.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 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 CONFIG_POLICY_LOG_H +#define CONFIG_POLICY_LOG_H + +#include "hilog/log.h" + +#ifdef LOG_DOMAIN +#undef LOG_DOMAIN +#endif +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_DOMAIN 0xD001E00 +#define LOG_TAG "ConfigPolicyFFI" + +#define LOGI(...) \ +if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_INFO)) { \ + HILOG_INFO(LOG_CORE, ##__VA_ARGS__); \ +} + +#define LOGE(...) \ +if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_ERROR)) { \ + HILOG_ERROR(LOG_CORE, ##__VA_ARGS__); \ +} + +#endif diff --git a/interfaces/kits/ffi/src/config_policy_mock.cpp b/interfaces/kits/ffi/src/config_policy_mock.cpp new file mode 100644 index 0000000..60a6668 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_mock.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 "cj_ffi/cj_common_ffi.h" + +extern "C" { +FFI_EXPORT int CJ_GetCfgDirList = 0; +FFI_EXPORT int CJ_GetCfgFiles = 0; +FFI_EXPORT int CJ_GetOneCfgFile = 0; +} \ No newline at end of file -- Gitee From 44a5774d56d7d1984d9ba437360e6b4a10fd8dc1 Mon Sep 17 00:00:00 2001 From: ruimingzhang Date: Wed, 10 Apr 2024 10:10:18 +0800 Subject: [PATCH 3/3] add ffi code Signed-off-by: ruimingzhang --- interfaces/kits/ffi/src/config_policy_ffi.cpp | 129 ++++++++++++++++++ interfaces/kits/ffi/src/config_policy_ffi.h | 28 ++++ interfaces/kits/ffi/src/config_policy_log.h | 41 ++++++ .../kits/ffi/src/config_policy_mock.cpp | 22 +++ 4 files changed, 220 insertions(+) create mode 100644 interfaces/kits/ffi/src/config_policy_ffi.cpp create mode 100644 interfaces/kits/ffi/src/config_policy_ffi.h create mode 100644 interfaces/kits/ffi/src/config_policy_log.h create mode 100644 interfaces/kits/ffi/src/config_policy_mock.cpp diff --git a/interfaces/kits/ffi/src/config_policy_ffi.cpp b/interfaces/kits/ffi/src/config_policy_ffi.cpp new file mode 100644 index 0000000..040e939 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_ffi.cpp @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2024 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 "config_policy_ffi.h" + +#include +#include +#include "config_policy_utils.h" +#include "hisysevent_adapter.h" +#include "config_policy_log.h" + +namespace OHOS::Customization::ConfigPolicy { + +char** MallocCStringArr(const std::vector& origin) +{ + if (origin.empty()) { + return nullptr; + } + auto size = origin.size(); + auto arr = static_cast(malloc(sizeof(char*) * size)); + if (arr == nullptr) { + return nullptr; + } + for (size_t i = 0; i < size; i++) { + size_t len = strlen(origin[i]) + 1; + arr[i] = static_cast(malloc(sizeof(char) * len)); + if (arr[i] == nullptr) { + continue; + } + errno_t ret = strcpy_s(arr[i], len, origin[i]); + if (ret != 0) { + free(arr[i]); + arr[i] = nullptr; + } + } + return arr; +} + +extern "C" { + RetDataCArrString CJ_GetCfgDirList() + { + LOGI("CJ_GetCfgDirList start"); + RetDataCArrString ret = { .code = SUCCESS_CODE, .data = { .head = nullptr, .size = 0 } }; + CfgDir *cfgDir = GetCfgDirList(); + + std::vector dirList; + if (cfgDir != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgDir->paths[i] != nullptr) { + dirList.push_back(cfgDir->paths[i]); + } + } + } + + ret.data.head = MallocCStringArr(dirList); + ret.data.size = static_cast(ret.data.head == nullptr ? 0 : dirList.size()); + + FreeCfgDirList(cfgDir); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgDirList", ""); + LOGI("CJ_GetCfgDirList ok"); + return ret; + } + + RetDataCArrString CJ_GetCfgFiles(const char* relPath) + { + LOGI("CJ_GetCfgFiles start"); + RetDataCArrString ret = { .code = SUCCESS_CODE, .data = { .head = nullptr, .size = 0 } }; + LOGI("input path: [%{public}s]", relPath) + std::string extra(""); + CfgFiles *cfgFiles = GetCfgFilesEx(relPath, FOLLOWX_MODE_DEFAULT, extra.c_str()); + + std::vector fileList; + if (cfgFiles != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgFiles->paths[i] != nullptr) { + fileList.push_back(cfgFiles->paths[i]); + } + } + } + + ret.data.head = MallocCStringArr(fileList); + ret.data.size = static_cast(ret.data.head == nullptr ? 0 : fileList.size()); + + FreeCfgFiles(cfgFiles); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgFiles", ""); + return ret; + } + + RetDataCString CJ_GetOneCfgFile(const char* relPath) + { + LOGI("CJ_GetOneCfgFile start"); + RetDataCString ret = { .code = SUCCESS_CODE, .data = nullptr }; + char outBuf[MAX_PATH_LEN] = {0}; + std::string extra(""); + char* filePath = GetOneCfgFileEx(relPath, outBuf, MAX_PATH_LEN, FOLLOWX_MODE_DEFAULT, extra.c_str()); + + if (filePath == nullptr) { + LOGI("GetOneCfgFileEx result is nullptr."); + return ret; + } else { + LOGI("GetOneCfgFile return [%{public}s]", filePath); + } + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getOneCfgFile", ""); + size_t pathLen = strlen(filePath) + 1; + ret.data = static_cast(malloc(sizeof(char) * pathLen)); + if (ret.data == nullptr) { + return ret; + } + errno_t err = strcpy_s(ret.data, pathLen, filePath); + if (err != 0) { + free(ret.data); + ret.data = nullptr; + } + return ret; + } +} +} // namespace OHOS::Customization::ConfigPolicy diff --git a/interfaces/kits/ffi/src/config_policy_ffi.h b/interfaces/kits/ffi/src/config_policy_ffi.h new file mode 100644 index 0000000..45c5008 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_ffi.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 CONFIG_POLICY_FFI_H +#define CONFIG_POLICY_FFI_H + +#include "ffi_remote_data.h" +#include "cj_ffi/cj_common_ffi.h" + +extern "C" { + FFI_EXPORT RetDataCArrString CJ_GetCfgDirList(); + FFI_EXPORT RetDataCArrString CJ_GetCfgFiles(const char* relPath); + FFI_EXPORT RetDataCString CJ_GetOneCfgFile(const char* relPath); +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/ffi/src/config_policy_log.h b/interfaces/kits/ffi/src/config_policy_log.h new file mode 100644 index 0000000..afe7002 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_log.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 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 CONFIG_POLICY_LOG_H +#define CONFIG_POLICY_LOG_H + +#include "hilog/log.h" + +#ifdef LOG_DOMAIN +#undef LOG_DOMAIN +#endif +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_DOMAIN 0xD001E00 +#define LOG_TAG "ConfigPolicyFFI" + +#define LOGI(...) \ +if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_INFO)) { \ + HILOG_INFO(LOG_CORE, ##__VA_ARGS__); \ +} + +#define LOGE(...) \ +if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_ERROR)) { \ + HILOG_ERROR(LOG_CORE, ##__VA_ARGS__); \ +} + +#endif diff --git a/interfaces/kits/ffi/src/config_policy_mock.cpp b/interfaces/kits/ffi/src/config_policy_mock.cpp new file mode 100644 index 0000000..60a6668 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_mock.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 "cj_ffi/cj_common_ffi.h" + +extern "C" { +FFI_EXPORT int CJ_GetCfgDirList = 0; +FFI_EXPORT int CJ_GetCfgFiles = 0; +FFI_EXPORT int CJ_GetOneCfgFile = 0; +} \ No newline at end of file -- Gitee