1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <unistd.h>
17#include <securec.h>
18#include "codec_hcb_util.h"
19#include "hdf_log.h"
20
21#ifdef __OHOS_STANDARD_SYS__
22#define HOST_CONFIG_PATH HDF_CONFIG_DIR
23#define HOST_CHIP_PROD_CONFIG_PATH HDF_CHIP_PROD_CONFIG_DIR
24#else
25#define HOST_CONFIG_PATH "/system/etc/hdfconfig"
26#endif
27#define PRODUCT_PROPERTY "ro.build.product"
28#define PRODUCT_NAME_MAX 128
29
30static bool GetConfigFilePath(const char *productName, char *configPath, size_t configPathLen)
31{
32    static const char *adapterConfigPath[] = {
33        HOST_CONFIG_PATH,
34        HOST_CHIP_PROD_CONFIG_PATH,
35    };
36
37    size_t pathNum = sizeof(adapterConfigPath) / sizeof(adapterConfigPath[0]);
38    for (size_t i = 0; i < pathNum; ++i) {
39        if (sprintf_s(configPath, configPathLen - 1, "%s/hdf_%s.hcb", adapterConfigPath[i], productName) < 0) {
40            HDF_LOGE("failed to generate file path");
41            continue;
42        }
43
44        if (access(configPath, F_OK | R_OK) == 0) {
45            return true;
46        }
47        HDF_LOGD("invalid config file path or permission:%{public}s", configPath);
48    }
49    return false;
50}
51
52const struct DeviceResourceNode *HdfGetHcsRootNode(void)
53{
54    char productName[PRODUCT_NAME_MAX] = { 0 };
55    char configPath[PATH_MAX] = { 0 };
56
57    int ret = strcpy_s(productName, PRODUCT_NAME_MAX, "default");
58    if (ret != HDF_SUCCESS) {
59        return NULL;
60    }
61
62    if (!GetConfigFilePath(productName, configPath, PATH_MAX)) {
63        HDF_LOGE("failed to get config file path");
64        return NULL;
65    }
66
67    SetHcsBlobPath(configPath);
68    const struct DeviceResourceNode *mgrRoot = HcsGetRootNode();
69    return mgrRoot;
70}
71