• 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 <errno.h>
16 #include <unistd.h>
17 
18 #include "init_log.h"
19 #include "init_param.h"
20 #include "init_utils.h"
21 #include "param_manager.h"
22 #ifdef PARAM_LOAD_CFG_FROM_CODE
23 #include "param_cfg.h"
24 #endif
25 #ifdef __LITEOS_M__
26 #include "ohos_init.h"
27 #include "cmsis_os2.h"
28 #include "parameter.h"
29 #endif
30 
31 #ifdef PARAM_LOAD_CFG_FROM_CODE
StringTrim(char * buffer,int size,const char * name)32 static const char *StringTrim(char *buffer, int size, const char *name)
33 {
34     char *tmp = (char *)name;
35     while (*tmp != '\0' && *tmp != '"') {
36         tmp++;
37     }
38     if (*tmp == '\0') {
39         return name;
40     }
41     // skip "
42     tmp++;
43     int i = 0;
44     while (*tmp != '\0' && i < size) {
45         buffer[i++] = *tmp;
46         tmp++;
47     }
48     if (i >= size) {
49         return name;
50     }
51     while (i > 0) {
52         if (buffer[i] == '"') {
53             buffer[i] = '\0';
54             return buffer;
55         }
56         i--;
57     }
58     return name;
59 }
60 #endif
61 
InitParamService(void)62 void InitParamService(void)
63 {
64     PARAM_LOGI("InitParamService %s", DATA_PATH);
65     CheckAndCreateDir(PARAM_STORAGE_PATH "/");
66     CheckAndCreateDir(DATA_PATH);
67     // param space
68     int ret = InitParamWorkSpace(0, NULL);
69     PARAM_CHECK(ret == 0, return, "Init parameter workspace fail");
70     ret = InitPersistParamWorkSpace();
71     PARAM_CHECK(ret == 0, return, "Init persist parameter workspace fail");
72 
73     // from build
74     LoadParamFromBuild();
75 #ifdef PARAM_LOAD_CFG_FROM_CODE
76     char *buffer = calloc(1, PARAM_VALUE_LEN_MAX);
77     PARAM_CHECK(buffer != NULL, return, "Failed to malloc for buffer");
78     for (size_t i = 0; i < ARRAY_LENGTH(g_paramDefCfgNodes); i++) {
79         PARAM_LOGV("InitParamService name %s = %s", g_paramDefCfgNodes[i].name, g_paramDefCfgNodes[i].value);
80         uint32_t dataIndex = 0;
81         ret = WriteParam(g_paramDefCfgNodes[i].name,
82             StringTrim(buffer, PARAM_VALUE_LEN_MAX, g_paramDefCfgNodes[i].value), &dataIndex, 0);
83         PARAM_CHECK(ret == 0, continue, "Failed to set param %d name %s %s",
84             ret, g_paramDefCfgNodes[i].name, g_paramDefCfgNodes[i].value);
85     }
86     free(buffer);
87 #endif
88 }
89 
StartParamService(void)90 int StartParamService(void)
91 {
92     return 0;
93 }
94 
StopParamService(void)95 void StopParamService(void)
96 {
97     PARAM_LOGI("StopParamService.");
98     ClosePersistParamWorkSpace();
99     CloseParamWorkSpace();
100 }
101 
SystemWriteParam(const char * name,const char * value)102 int SystemWriteParam(const char *name, const char *value)
103 {
104     int ctrlService = 0;
105     int ret = CheckParameterSet(name, value, GetParamSecurityLabel(), &ctrlService);
106     PARAM_CHECK(ret == 0, return ret, "Forbid to set parameter %s", name);
107     PARAM_LOGV("SystemWriteParam name %s value: %s ctrlService %d", name, value, ctrlService);
108     if ((ctrlService & PARAM_CTRL_SERVICE) != PARAM_CTRL_SERVICE) { // ctrl param
109         uint32_t dataIndex = 0;
110         ret = WriteParam(name, value, &dataIndex, 0);
111         PARAM_CHECK(ret == 0, return ret, "Failed to set param %d name %s %s", ret, name, value);
112         ret = WritePersistParam(name, value);
113         PARAM_CHECK(ret == 0, return ret, "Failed to set persist param name %s", name);
114     } else {
115         PARAM_LOGE("SystemWriteParam can not support service ctrl parameter name %s", name);
116     }
117     return ret;
118 }
119 
120 #ifdef __LITEOS_M__
121 #define OS_DELAY 1000 // * 30 // 30s
122 #define STACK_SIZE 1024
123 
ParamServiceTask(int * arg)124 static void ParamServiceTask(int *arg)
125 {
126     (void)arg;
127     PARAM_LOGI("ParamServiceTask start");
128     while (1) {
129         CheckAndSavePersistParam();
130         osDelay(OS_DELAY);
131     }
132 }
133 
LiteParamService(void)134 void LiteParamService(void)
135 {
136     static int init = 0;
137     if (init) {
138         return;
139     }
140     init = 1;
141     EnableInitLog(INIT_INFO);
142     InitParamService();
143     // get persist param
144     LoadPersistParams();
145 }
146 CORE_INIT(LiteParamService);
147 #endif
148