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 
16 #ifndef BASE_STARTUP_INIT_PARAM_H
17 #define BASE_STARTUP_INIT_PARAM_H
18 #include <stdint.h>
19 #include <stdio.h>
20 #ifdef PARAM_SUPPORT_TRIGGER
21 #include "cJSON.h"
22 #endif
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 #define DEFAULT_PARAM_WAIT_TIMEOUT 30 // 30s
30 #define DEFAULT_PARAM_SET_TIMEOUT 10 // 10s
31 
32 #ifndef PARAM_NAME_LEN_MAX
33 #define PARAM_CONST_VALUE_LEN_MAX 4096
34 #define PARAM_VALUE_LEN_MAX  96
35 #define PARAM_NAME_LEN_MAX  96
36 #endif
37 
38 typedef enum {
39     PARAM_CODE_ERROR = -1,
40     PARAM_CODE_SUCCESS = 0,
41     PARAM_CODE_INVALID_PARAM = 100,
42     PARAM_CODE_INVALID_NAME,
43     PARAM_CODE_INVALID_VALUE,
44     PARAM_CODE_REACHED_MAX,
45     PARAM_CODE_NOT_SUPPORT,
46     PARAM_CODE_TIMEOUT,
47     PARAM_CODE_NOT_FOUND,
48     PARAM_CODE_READ_ONLY,
49     PARAM_CODE_IPC_ERROR,
50     PARAM_CODE_NODE_EXIST,
51     PARAM_WATCHER_CALLBACK_EXIST,
52     PARAM_WATCHER_GET_SERVICE_FAILED,
53     PARAM_CODE_MEMORY_MAP_FAILED,
54     PARAM_WORKSPACE_NOT_INIT,
55     PARAM_CODE_FAIL_CONNECT,
56     PARAM_CODE_MEMORY_NOT_ENOUGH,
57     PARAM_CODE_PERMISSION_DENIED,
58     PARAM_DEFAULT_PARAM_MEMORY_NOT_ENOUGH,
59     DAC_RESULT_INVALID_PARAM = 1000,
60     DAC_RESULT_FORBIDED,
61     SELINUX_RESULT_FORBIDED,
62     PARAM_CODE_MAX
63 } PARAM_CODE;
64 
65 typedef enum {
66     EVENT_TRIGGER_PARAM,
67     EVENT_TRIGGER_BOOT,
68     EVENT_TRIGGER_PARAM_WAIT,
69     EVENT_TRIGGER_PARAM_WATCH
70 } EventType;
71 
72 #define LOAD_PARAM_NORMAL 0x00
73 #define LOAD_PARAM_ONLY_ADD 0x01
74 #define LOAD_PARAM_PERSIST 0x02
75 
76 typedef uint32_t ParamHandle;
77 
78 /**
79  * Init 接口
80  * 初始化参数服务
81  *
82  */
83 int InitParamService(void);
84 void LoadSpecialParam(void);
85 
86 /**
87  * Init 接口
88  * 启动参数服务,在main启动的最后调用,阻赛当前线程
89  *
90  */
91 int StartParamService(void);
92 
93 /**
94  * Init 接口
95  * 停止参数服务
96  *
97  */
98 void StopParamService(void);
99 
100 /**
101  * Init 接口
102  * 加载默认的参数值
103  *
104  */
105 int LoadDefaultParams(const char *fileName, uint32_t mode);
106 
107 /**
108  * Init 接口
109  * 加载持久化参数。
110  *
111  */
112 int LoadPersistParams(void);
113 
114 /**
115  * Init 接口
116  * 加载加密目录下持久化参数。
117  *
118  */
119 int LoadPrivatePersistParams(void);
120 
121 /**
122  * Init 接口
123  * 设置参数,主要用于其他进程使用,通过管道修改参数
124  *
125  */
126 int SystemWriteParam(const char *name, const char *value);
127 
128 #ifdef PARAM_SUPPORT_TRIGGER
129 /**
130  * 对外接口
131  * 触发一个trigger操作。
132  *
133  */
134 void PostTrigger(EventType type, const char *content, uint32_t contentLen);
135 
136 /**
137  * 对外接口
138  * 解析trigger文件。
139  *
140  */
141 int ParseTriggerConfig(const cJSON *fileRoot, int (*checkJobValid)(const char *jobName), void *context);
142 
143 /**
144  * 对外接口
145  * 按名字执行对应的trigger。
146  *
147  */
148 void DoTriggerExec(const char *triggerName);
149 void DoJobExecNow(const char *triggerName);
150 
151 /**
152  * 对外接口
153  * 按名字添加一个job,用于group支持。
154  *
155  */
156 int AddCompleteJob(const char *name, const char *condition, const char *cmdContent);
157 
158 void RegisterBootStateChange(void (*bootStateChange)(int start, const char *));
159 
160 /**
161  * 对外接口
162  * dump 参数和trigger信息
163  *
164  */
165 void SystemDumpTriggers(int verbose, int (*dump)(const char *fmt, ...));
166 #endif
167 
168 /**
169  * 对外接口
170  * 设置参数,主要用于其他进程使用,通过管道修改参数。
171  *
172  */
173 int SystemSetParameter(const char *name, const char *value);
174 
175 /**
176  * 对外接口
177  * 保存共享内存中的所有持久化参数
178  *
179 */
180 int SystemSaveParameters(void);
181 
182 /**
183  * Init 接口
184  * 查询参数。
185  *
186  */
187 int SystemReadParam(const char *name, char *value, uint32_t *len);
188 
189 /**
190  * 对外接口
191  * 查询参数,主要用于其他进程使用,需要给定足够的内存保存参数。
192  * 如果 value == null,获取value的长度
193  * 否则value的大小认为是len
194  *
195  */
196 #define SystemGetParameter SystemReadParam
197 
198 
199 /**
200  * 外部接口
201  * 等待某个参数值被修改,阻塞直到参数值被修改或超时
202  *
203  */
204 int SystemWaitParameter(const char *name, const char *value, int32_t timeout);
205 
206 typedef void (*ParameterChangePtr)(const char *key, const char *value, void *context);
207 int SystemWatchParameter(const char *keyprefix, ParameterChangePtr change, void *context);
208 
209 int SystemCheckParamExist(const char *name);
210 
211 void SystemDumpParameters(int verbose, int index, int (*dump)(const char *fmt, ...));
212 
213 int WatchParamCheck(const char *keyprefix);
214 
215 void ResetParamSecurityLabel(void);
216 
217 #ifdef __cplusplus
218 #if __cplusplus
219 }
220 #endif
221 #endif
222 #endif