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 15 // 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 #define LOAD_PARAM_UPDATE_CONST 0x04 76 77 typedef uint32_t ParamHandle; 78 79 /** 80 * Init 接口 81 * 初始化参数服务 82 * 83 */ 84 int InitParamService(void); 85 void LoadSpecialParam(void); 86 87 /** 88 * Init 接口 89 * 启动参数服务,在main启动的最后调用,阻赛当前线程 90 * 91 */ 92 int StartParamService(void); 93 94 /** 95 * Init 接口 96 * 停止参数服务 97 * 98 */ 99 void StopParamService(void); 100 101 /** 102 * Init 接口 103 * 加载默认的参数值 104 * 105 */ 106 int LoadDefaultParams(const char *fileName, uint32_t mode); 107 108 /** 109 * Init 接口 110 * 加载持久化参数。 111 * 112 */ 113 int LoadPersistParams(void); 114 115 /** 116 * Init 接口 117 * 加载加密目录下持久化参数。 118 * 119 */ 120 int LoadPrivatePersistParams(void); 121 122 /** 123 * Init 接口 124 * 设置参数,主要用于其他进程使用,通过管道修改参数 125 * 126 */ 127 int SystemWriteParam(const char *name, const char *value); 128 129 #ifndef OHOS_LITE 130 /** 131 * Init 内部接口 132 * 更新只读参数,主要用于Init其他进程使用,通过管道更新只读参数。 133 * 134 */ 135 int SystemUpdateConstParam(const char *name, const char *value); 136 #endif 137 138 #ifdef PARAM_SUPPORT_TRIGGER 139 /** 140 * 对外接口 141 * 触发一个trigger操作。 142 * 143 */ 144 void PostTrigger(EventType type, const char *content, uint32_t contentLen); 145 146 /** 147 * 对外接口 148 * 解析trigger文件。 149 * 150 */ 151 int ParseTriggerConfig(const cJSON *fileRoot, int (*checkJobValid)(const char *jobName), void *context); 152 153 /** 154 * 对外接口 155 * 按名字执行对应的trigger。 156 * 157 */ 158 void DoTriggerExec(const char *triggerName); 159 void DoJobExecNow(const char *triggerName); 160 161 /** 162 * 对外接口 163 * 按名字添加一个job,用于group支持。 164 * 165 */ 166 int AddCompleteJob(const char *name, const char *condition, const char *cmdContent); 167 168 void RegisterBootStateChange(void (*bootStateChange)(int start, const char *)); 169 170 /** 171 * 对外接口 172 * dump 参数和trigger信息 173 * 174 */ 175 void SystemDumpTriggers(int verbose, int (*dump)(const char *fmt, ...)); 176 #endif 177 178 /** 179 * 对外接口 180 * 设置参数,主要用于其他进程使用,通过管道修改参数。 181 * 182 */ 183 int SystemSetParameter(const char *name, const char *value); 184 185 /** 186 * 对外接口 187 * 保存共享内存中的所有持久化参数 188 * 189 */ 190 int SystemSaveParameters(void); 191 192 /** 193 * Init 接口 194 * 查询参数。 195 * 196 */ 197 int SystemReadParam(const char *name, char *value, uint32_t *len); 198 199 /** 200 * 对外接口 201 * 查询参数,主要用于其他进程使用,需要给定足够的内存保存参数。 202 * 如果 value == null,获取value的长度 203 * 否则value的大小认为是len 204 * 205 */ 206 #define SystemGetParameter SystemReadParam 207 208 209 /** 210 * 外部接口 211 * 等待某个参数值被修改,阻塞直到参数值被修改或超时 212 * 213 */ 214 int SystemWaitParameter(const char *name, const char *value, int32_t timeout); 215 216 typedef void (*ParameterChangePtr)(const char *key, const char *value, void *context); 217 int SystemWatchParameter(const char *keyprefix, ParameterChangePtr change, void *context); 218 219 int SystemCheckParamExist(const char *name); 220 221 void SystemDumpParameters(int verbose, int index, int (*dump)(const char *fmt, ...)); 222 223 int WatchParamCheck(const char *keyprefix); 224 225 void ResetParamSecurityLabel(void); 226 227 #ifdef __cplusplus 228 #if __cplusplus 229 } 230 #endif 231 #endif 232 #endif