• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_BOOTSTAGE_H
17 #define BASE_STARTUP_BOOTSTAGE_H
18 
19 #include "hookmgr.h"
20 #include "cJSON.h"
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 enum INIT_BOOTSTAGE {
29     INIT_GLOBAL_INIT       = 0,
30     INIT_PRE_PARAM_SERVICE = 10,
31     INIT_PRE_PARAM_LOAD    = 20,
32     INIT_PRE_CFG_LOAD      = 30,
33     INIT_SERVICE_PARSE     = 35,
34     INIT_POST_PERSIST_PARAM_LOAD   = 40,
35     INIT_POST_CFG_LOAD     = 50,
36     INIT_CMD_RECORD     = 51,
37     INIT_REBOOT            = 55,
38     INIT_SERVICE_CLEAR     = 56,
39     INIT_SERVICE_DUMP      = 57,
40     INIT_SERVICE_FORK_BEFORE       = 58,
41     INIT_SERVICE_SET_PERMS = 59,
42     INIT_SERVICE_FORK_AFTER = 60,
43     INIT_JOB_PARSE         = 70,
44 };
45 
46 HOOK_MGR *GetBootStageHookMgr();
47 
InitAddGlobalInitHook(int prio,OhosHook hook)48 __attribute__((always_inline)) inline int InitAddGlobalInitHook(int prio, OhosHook hook)
49 {
50     return HookMgrAdd(GetBootStageHookMgr(), INIT_GLOBAL_INIT, prio, hook);
51 }
52 
InitAddPreParamServiceHook(int prio,OhosHook hook)53 __attribute__((always_inline)) inline int InitAddPreParamServiceHook(int prio, OhosHook hook)
54 {
55     return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_PARAM_SERVICE, prio, hook);
56 }
57 
InitAddPreParamLoadHook(int prio,OhosHook hook)58 __attribute__((always_inline)) inline int InitAddPreParamLoadHook(int prio, OhosHook hook)
59 {
60     return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_PARAM_LOAD, prio, hook);
61 }
62 
InitAddPreCfgLoadHook(int prio,OhosHook hook)63 __attribute__((always_inline)) inline int InitAddPreCfgLoadHook(int prio, OhosHook hook)
64 {
65     return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_CFG_LOAD, prio, hook);
66 }
67 
InitAddPostCfgLoadHook(int prio,OhosHook hook)68 __attribute__((always_inline)) inline int InitAddPostCfgLoadHook(int prio, OhosHook hook)
69 {
70     return HookMgrAdd(GetBootStageHookMgr(), INIT_POST_CFG_LOAD, prio, hook);
71 }
72 
InitAddPostPersistParamLoadHook(int prio,OhosHook hook)73 __attribute__((always_inline)) inline int InitAddPostPersistParamLoadHook(int prio, OhosHook hook)
74 {
75     return HookMgrAdd(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, prio, hook);
76 }
77 
78 /**
79  * @brief service config parsing context information
80  */
81 typedef struct tagSERVICE_PARSE_CTX {
82     const char *serviceName;    /* Service name */
83     const cJSON *serviceNode;   /* Service JSON node */
84 } SERVICE_PARSE_CTX;
85 
86 /**
87  * @brief job config parsing context information
88  */
89 typedef struct tagJOB_PARSE_CTX {
90     const char *jobName;    /* job name */
91     const cJSON *jobNode;   /* job JSON node */
92 } JOB_PARSE_CTX;
93 
94 /**
95  * @brief service info
96  */
97 typedef struct tagSERVICE_INFO_CTX {
98     const char *serviceName;    /* Service name */
99     const char *reserved;       /* reserved info */
100 } SERVICE_INFO_CTX;
101 
102 /**
103  * @brief init cmd info
104  */
105 typedef struct InitCmdInfo {
106     const char *cmdName;    /* cmd name */
107     const char *cmdContent;    /* cmd content */
108     const char *reserved;       /* reserved info */
109 } INIT_CMD_INFO;
110 
111 /**
112  * @brief service config parse hook function prototype
113  *
114  * @param serviceParseCtx service config parsing context information
115  * @return None
116  */
117 typedef void (*ServiceParseHook)(SERVICE_PARSE_CTX *serviceParseCtx);
118 
119 /**
120  * @brief job config parse hook function prototype
121  *
122  * @param JobParseHook job config parsing context information
123  * @return None
124  */
125 typedef void (*JobParseHook)(JOB_PARSE_CTX *jobParseCtx);
126 
127 /**
128  * @brief service hook function prototype
129  *
130  * @param ServiceHook service info
131  * @return None
132  */
133 typedef void (*ServiceHook)(SERVICE_INFO_CTX *serviceCtx);
134 
135 /**
136  * @brief Register a hook for service config parsing
137  *
138  * @param hook service config parsing hook
139  *   in the hook, we can parse extra fields in the config file.
140  * @return return 0 if succeed; other values if failed.
141  */
142 int InitAddServiceParseHook(ServiceParseHook hook);
143 
144 /**
145  * @brief service config parsing context information
146  */
147 typedef struct tagReboot {
148     char *reason;
149 } RebootHookCtx;
150 
151 /**
152  * @brief service config parse hook function prototype
153  *
154  * @param serviceParseCtx service config parsing context information
155  * @return None
156  */
157 typedef void (*InitRebootHook)(RebootHookCtx *ctx);
158 /**
159  * @brief Register a hook for reboot
160  *
161  * @param hook
162  *
163  * @return return 0 if succeed; other values if failed.
164  */
165 int InitAddRebootHook(InitRebootHook hook);
166 
167 /**
168  * @brief Register a hook for job config parsing
169  *
170  * @param hook job config parsing hook
171  *   in the hook, we can parse extra fields in the config file.
172  * @return return 0 if succeed; other values if failed.
173  */
174 int InitAddJobParseHook(JobParseHook hook);
175 
176 /**
177  * @brief Register a hook for service
178  *
179  * @param hook service hook
180  *   in the hook, we can get service.
181  * @param hookState init boot state
182  * @return return 0 if succeed; other values if failed.
183  */
184 int InitAddServiceHook(ServiceHook hook, int hookState);
185 
186 int InitAddClearServiceHook(ServiceHook hook);
187 
188 #ifdef __cplusplus
189 #if __cplusplus
190 }
191 #endif
192 #endif
193 #endif
194