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 0000000000000000000000000000000000000000..040e9394e0ed2d3f7c473c0bae993255b23d768d --- /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 0000000000000000000000000000000000000000..45c500877b4ba2caffecc32a60f5cc5ed4ae471f --- /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 0000000000000000000000000000000000000000..afe700251bdb0b24270be7a7e2f361adc8df81c9 --- /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 0000000000000000000000000000000000000000..60a66680a7814544240488b25893303a93b0c282 --- /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