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