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#include "init_log.h"
16#include "init_param.h"
17#include "param_init.h"
18#include "param_manager.h"
19
20#define MIN_SLEEP (100 * 1000)
21
22#ifdef __LITEOS_A__
23static int g_flags = 0;
24__attribute__((constructor)) static int ClientInit(void)
25{
26    if (PARAM_TEST_FLAG(g_flags, WORKSPACE_FLAGS_INIT)) {
27        return 0;
28    }
29    EnableInitLog(INIT_INFO);
30    PARAM_LOGV("InitParamClient");
31    int ret = InitParamWorkSpace(1, NULL);
32    PARAM_CHECK(ret == 0, return -1, "Failed to init param workspace");
33    PARAM_SET_FLAG(g_flags, WORKSPACE_FLAGS_INIT);
34    // init persist to save
35    InitPersistParamWorkSpace();
36    return 0;
37}
38
39__attribute__((destructor)) static void ClientDeinit(void)
40{
41    if (PARAM_TEST_FLAG(g_flags, WORKSPACE_FLAGS_INIT)) {
42        CloseParamWorkSpace();
43    }
44    PARAM_SET_FLAG(g_flags, 0);
45}
46#endif
47
48int SystemSetParameter(const char *name, const char *value)
49{
50    PARAM_CHECK(name != NULL && value != NULL, return -1, "Invalid name or value %s", name);
51    int ctrlService = 0;
52    int ret = CheckParameterSet(name, value, GetParamSecurityLabel(), &ctrlService);
53    PARAM_CHECK(ret == 0, return ret, "Forbid to set parameter %s", name);
54    PARAM_LOGV("SystemSetParameter name %s value: %s ctrlService %d", name, value, ctrlService);
55    if ((ctrlService & PARAM_CTRL_SERVICE) != PARAM_CTRL_SERVICE) { // ctrl param
56        uint32_t dataIndex = 0;
57        ret = WriteParam(name, value, &dataIndex, 0);
58        PARAM_CHECK(ret == 0, return ret, "Failed to set param %d name %s %s", ret, name, value);
59        ret = WritePersistParam(name, value);
60        PARAM_CHECK(ret == 0, return ret, "Failed to set persist param name %s", name);
61    } else {
62        PARAM_LOGE("SystemSetParameter can not support service ctrl parameter name %s", name);
63    }
64    return ret;
65}
66
67int SystemWaitParameter(const char *name, const char *value, int32_t timeout)
68{
69    PARAM_CHECK(name != NULL && value != NULL, return PARAM_CODE_INVALID_PARAM,
70        "SystemWaitParameter failed! name is: %s, errNum is: %d", name, PARAM_CODE_INVALID_PARAM);
71    long long globalCommit = 0;
72    // first check permission
73    int ret = CheckParamPermission(GetParamSecurityLabel(), name, DAC_READ);
74    PARAM_CHECK(ret == 0, return ret, "SystemWaitParameter failed! name is: %s, errNum is: %d", name, ret);
75    uint32_t diff = 0;
76    struct timespec startTime = {0};
77    if (timeout <= 0) {
78        timeout = DEFAULT_PARAM_WAIT_TIMEOUT;
79    }
80    (void)clock_gettime(CLOCK_MONOTONIC, &startTime);
81    do {
82        long long commit = GetSystemCommitId();
83        if (commit != globalCommit) {
84            ParamNode *param = SystemCheckMatchParamWait(name, value);
85            if (param != NULL) {
86                ret = 0;
87                break;
88            }
89        }
90        globalCommit = commit;
91
92        usleep(MIN_SLEEP);
93        struct timespec finishTime = {0};
94        (void)clock_gettime(CLOCK_MONOTONIC, &finishTime);
95        diff = IntervalTime(&finishTime, &startTime);
96        if (diff >= timeout) {
97            ret = PARAM_CODE_TIMEOUT;
98            break;
99        }
100    } while (1);
101    PARAM_LOGI("SystemWaitParameter name %s value: %s diff %d timeout %d", name, value, diff, diff);
102    if (ret != 0) {
103        PARAM_LOGE("SystemWaitParameter failed! name is:%s, errNum is %d", name, ret);
104    }
105    return ret;
106}
107
108int SystemSaveParameters(void)
109{
110    CheckAndSavePersistParam();
111    return PARAM_CODE_SUCCESS;
112}
113
114int WatchParamCheck(const char *keyprefix)
115{
116    (void)keyprefix;
117    return PARAM_CODE_NOT_SUPPORT;
118}
119
120int SystemCheckParamExist(const char *name)
121{
122    return SysCheckParamExist(name);
123}
124