param_client.c 4.5 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2021 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.
 */
C
cheng_jinsong 已提交
15
#include "init_log.h"
M
Mupceet 已提交
16 17 18 19
#include "init_param.h"
#include "param_manager.h"

#define MIN_SLEEP (100 * 1000)
M
Mupceet 已提交
20
static int g_flags = 0;
M
Mupceet 已提交
21

M
Mupceet 已提交
22
__attribute__((constructor)) static void ClientInit(void);
王liangliang's avatar
王liangliang 已提交
23
static void ClientDeinit(void);
M
Mupceet 已提交
24 25 26

static int InitParamClient(void)
{
M
Mupceet 已提交
27
    if (PARAM_TEST_FLAG(g_flags, WORKSPACE_FLAGS_INIT)) {
M
Mupceet 已提交
28 29
        return 0;
    }
C
cheng_jinsong 已提交
30
    EnableInitLog(INIT_INFO);
M
Mupceet 已提交
31
    PARAM_LOGV("InitParamClient");
A
an_xinwei 已提交
32
    int ret = InitParamWorkSpace(1, NULL);
M
Mupceet 已提交
33
    PARAM_CHECK(ret == 0, return -1, "Failed to init param workspace");
M
Mupceet 已提交
34
    PARAM_SET_FLAG(g_flags, WORKSPACE_FLAGS_INIT);
M
Mupceet 已提交
35 36 37 38 39 40 41
    // init persist to save
    InitPersistParamWorkSpace();
    return 0;
}

void ClientInit(void)
{
R
RingKing 已提交
42
#ifdef __LITEOS_A__
M
Mupceet 已提交
43
#ifndef STARTUP_INIT_TEST
R
RingKing 已提交
44
    PARAM_LOGV("ClientInit");
M
Mupceet 已提交
45 46
    (void)InitParamClient();
#endif
M
Mupceet 已提交
47
#endif
M
Mupceet 已提交
48 49 50 51
}

void ClientDeinit(void)
{
M
Mupceet 已提交
52
    if (PARAM_TEST_FLAG(g_flags, WORKSPACE_FLAGS_INIT)) {
M
Mupceet 已提交
53 54
        CloseParamWorkSpace();
    }
M
Mupceet 已提交
55
    PARAM_SET_FLAG(g_flags, 0);
M
Mupceet 已提交
56 57 58 59 60 61 62
}

int SystemSetParameter(const char *name, const char *value)
{
    PARAM_CHECK(name != NULL && value != NULL, return -1, "Invalid name or value %s", name);
    int ctrlService = 0;
    int ret = CheckParameterSet(name, value, GetParamSecurityLabel(), &ctrlService);
63
    PARAM_CHECK(ret == 0, return ret, "Forbid to set parameter %s", name);
M
Mupceet 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    PARAM_LOGV("SystemSetParameter name %s value: %s ctrlService %d", name, value, ctrlService);
    if ((ctrlService & PARAM_CTRL_SERVICE) != PARAM_CTRL_SERVICE) { // ctrl param
        uint32_t dataIndex = 0;
        ret = WriteParam(name, value, &dataIndex, 0);
        PARAM_CHECK(ret == 0, return ret, "Failed to set param %d name %s %s", ret, name, value);
        ret = WritePersistParam(name, value);
        PARAM_CHECK(ret == 0, return ret, "Failed to set persist param name %s", name);
    } else {
        PARAM_LOGE("SystemSetParameter can not support service ctrl parameter name %s", name);
    }
    return ret;
}

int SystemWaitParameter(const char *name, const char *value, int32_t timeout)
{
    PARAM_CHECK(name != NULL && value != NULL, return -1, "Invalid name or value %s", name);
    long long globalCommit = 0;
    // first check permission
    ParamHandle handle = 0;
    int ret = ReadParamWithCheck(name, DAC_READ, &handle);
    if (ret != PARAM_CODE_NOT_FOUND && ret != 0 && ret != PARAM_CODE_NODE_EXIST) {
        PARAM_CHECK(ret == 0, return ret, "Forbid to wait parameters %s", name);
    }
    uint32_t diff = 0;
    time_t startTime;
    if (timeout <= 0) {
        timeout = DEFAULT_PARAM_WAIT_TIMEOUT;
    }
    (void)time(&startTime);
    do {
        long long commit = GetSystemCommitId();
        if (commit != globalCommit) {
            ParamNode *param = SystemCheckMatchParamWait(name, value);
            if (param != NULL) {
                ret = 0;
                break;
            }
        }
        globalCommit = commit;

        usleep(MIN_SLEEP);
        time_t finishTime;
        (void)time(&finishTime);
        diff = (uint32_t)difftime(finishTime, startTime);
        if (diff >= timeout) {
            ret = PARAM_CODE_TIMEOUT;
            break;
        }
    } while (1);
    PARAM_LOGI("SystemWaitParameter name %s value: %s diff %d timeout %d", name, value, diff, diff);
    return ret;
}

int WatchParamCheck(const char *keyprefix)
{
119
    (void)keyprefix;
M
Mupceet 已提交
120
    return PARAM_CODE_NOT_SUPPORT;
M
Mupceet 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

int SystemCheckParamExist(const char *name)
{
    return SysCheckParamExist(name);
}

int SystemFindParameter(const char *name, ParamHandle *handle)
{
    PARAM_CHECK(name != NULL && handle != NULL, return -1, "The name or handle is null");
    int ret = ReadParamWithCheck(name, DAC_READ, handle);
    if (ret != PARAM_CODE_NOT_FOUND && ret != 0 && ret != PARAM_CODE_NODE_EXIST) {
        PARAM_CHECK(ret == 0, return ret, "Forbid to access parameter %s", name);
    }
    return ret;
136
}