1/*
2 * Copyright (c) 2021 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 <stdbool.h>
17#include <stdio.h>
18#include <string.h>
19#include <errno.h>
20
21#include "init_param.h"
22#include "beget_ext.h"
23#include "securec.h"
24#include "systemcapability.h"
25
26#define SYSCAP_MAX_SIZE 100
27#define SYSCAP_PREFIX_NAME "SystemCapability"
28#define CONST_SYSCAP_PREFIX_NAME "const.SystemCapability"
29
30bool HasSystemCapability(const char *cap)
31{
32    char capName[SYSCAP_MAX_SIZE] = { 0 };
33    char paramValue[PARAM_VALUE_LEN_MAX] = { 0 };
34    unsigned int valueLen = PARAM_VALUE_LEN_MAX;
35    int rc = -1;
36
37    if (cap == NULL) {
38        BEGET_LOGE("cap input is null.");
39        return false;
40    }
41
42    if (strncmp(SYSCAP_PREFIX_NAME, cap, sizeof(SYSCAP_PREFIX_NAME) - 1) == 0) {
43        rc = snprintf_s(capName, SYSCAP_MAX_SIZE, SYSCAP_MAX_SIZE - 1, "const.%s", cap);
44        BEGET_ERROR_CHECK(rc != 0, return false, "Failed snprintf_s err=%d", errno);
45    } else if (snprintf_s(capName, SYSCAP_MAX_SIZE, SYSCAP_MAX_SIZE - 1, CONST_SYSCAP_PREFIX_NAME".%s", cap) == -1) {
46        BEGET_LOGE("Failed snprintf_s err=%d", errno);
47        return false;
48    }
49
50    if (SystemGetParameter(capName, paramValue, &valueLen) != 0) {
51        BEGET_LOGE("Failed get paramName.");
52        return false;
53    }
54
55    return true;
56}