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