1 /*
2  * Copyright (c) 2020 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 /**
17  * @addtogroup Init OHOS Init
18  * @{
19  *
20  * @brief Provides the entries for initializing and starting services and features.
21  *
22  * This module provides the entries for initializing services and features during service
23  * development. \n
24  * Services and features are initialized in the following sequence: core phase, core system
25  * service, core system feature, system startup, system service, system feature, application-layer
26  * service, and application-layer feature. \n
27  *
28  * @since 1.0
29  * @version 1.0
30  */
31 
32 /**
33  * @file ohos_init.h
34  *
35  * @brief Provides the entries for initializing and starting services and features.
36  *
37  * This file provides the entries for initializing services and features during service
38  * development. \n
39  *
40  * @since 1.0
41  * @version 1.0
42  */
43 
44 #ifndef OHOS_LAYER_INIT_H
45 #define OHOS_LAYER_INIT_H
46 #ifdef __cplusplus
47 #if __cplusplus
48 extern "C" {
49 #endif
50 #endif
51 typedef void (*InitCall)(void);
52 
53 #define USED_ATTR __attribute__((used))
54 
55 #ifdef LAYER_INIT_SHARED_LIB
56 #define LAYER_INIT_LEVEL_0 0
57 #define LAYER_INIT_LEVEL_1 1
58 #define LAYER_INIT_LEVEL_2 2
59 #define LAYER_INIT_LEVEL_3 3
60 #define LAYER_INIT_LEVEL_4 4
61 #define CTOR_VALUE_device 100
62 #define CTOR_VALUE_core 110
63 #define CTOR_VALUE_sys_service 120
64 #define CTOR_VALUE_sys_feature 130
65 #define CTOR_VALUE_app_service 140
66 #define CTOR_VALUE_app_feature 150
67 #define CTOR_VALUE_run  700
68 #define LAYER_INITCALL(func, layer, clayer, priority)                                     \
69     static __attribute__((constructor(CTOR_VALUE_##layer + LAYER_INIT_LEVEL_##priority))) \
70         void BOOT_##layer##priority##func() {func();}
71 #else
72 #define LAYER_INITCALL(func, layer, clayer, priority)            \
73     static const InitCall USED_ATTR __zinitcall_##layer##_##func \
74         __attribute__((section(".zinitcall." clayer #priority ".init"))) = func
75 #endif
76 // Default priority is 2, priority range is [0, 4]
77 #define LAYER_INITCALL_DEF(func, layer, clayer) \
78     LAYER_INITCALL(func, layer, clayer, 2)
79 
80 /**
81  * @brief Identifies the entry for initializing and starting a core phase by the priority 2.
82  *
83  * This macro is used when Samgr is initialized and started. \n
84  * This macro is used to identify the entry called at the priority 2 of the core phase of
85  * the startup process. \n
86  *
87  * @param func Indicates the entry function for initializing and starting a core phase.
88  * The type is void (*)(void).
89  */
90 #define CORE_INIT(func) LAYER_INITCALL_DEF(func, core, "core")
91 /**
92  * @brief Identifies the entry for initializing and starting a core phase by the specified
93  * priority.
94  *
95  * This macro is used when Samgr is initialized and started. \n
96  * This macro is used to identify the entry called at the specified priority of the core phase of
97  * the startup process. \n
98  *
99  * @param func Indicates the entry function for initializing and starting a core phase.
100  * The type is void (*)(void).
101  * @param priority Indicates the calling priority when starting the core phase.
102  * The value range is [0,5), and the calling sequence is 0, 1, 2, 3, and 4.
103  */
104 #define CORE_INIT_PRI(func, priority) LAYER_INITCALL(func, core, "core", priority)
105 
106 /**
107  * @brief Identifies the entry for initializing and starting a core system service by the
108  * priority 2.
109  *
110  * This macro is used to identify the entry called at the priority 2 in the core system
111  * service phase of the startup process. \n
112  *
113  * @param func Indicates the entry function for initializing and starting a core system service.
114  * The type is void (*)(void).
115  */
116 #define SYS_SERVICE_INIT(func) LAYER_INITCALL_DEF(func, sys_service, "sys.service")
117 /**
118  * @brief Identifies the entry for initializing and starting a core system service by the
119  * specified priority.
120  *
121  * This macro is used to identify the entry called at the specified priority in the core system
122  * service phase of the startup process. \n
123  *
124  * @param func Indicates the entry function for initializing and starting a core system service.
125  * The type is void (*)(void).
126  * @param priority Indicates the calling priority when starting the core system service in the
127  * startup phase. The value range is [0,5), and the calling sequence is 0, 1, 2, 3, and 4.
128  */
129 #define SYS_SERVICE_INIT_PRI(func, priority) LAYER_INITCALL(func, sys_service, "sys.service", priority)
130 
131 /**
132  * @brief Identifies the entry for initializing and starting a core system feature by the
133  * priority 2.
134  *
135  * This macro is used to identify the entry called at the priority 2 in the core system
136  * feature phase of the startup process. \n
137  *
138  * @param func Indicates the entry function for initializing and starting a core system service.
139  * The type is void (*)(void).
140  */
141 #define SYS_FEATURE_INIT(func) LAYER_INITCALL_DEF(func, sys_feature, "sys.feature")
142 /**
143  * @brief Identifies the entry for initializing and starting a core system feature by the
144  * specified priority.
145  *
146  * This macro is used to identify the entry called at the specified priority in the core system
147  * feature phase of the startup process. \n
148  *
149  * @param func Indicates the entry function for initializing and starting a core system feature.
150  * The type is void (*)(void).
151  * @param priority Indicates the calling priority when starting the core system feature phase.
152  * The value range is [0, 5), and the calling sequence is 0, 1, 2, 3, and 4.
153  */
154 #define SYS_FEATURE_INIT_PRI(func, priority) LAYER_INITCALL(func, sys_feature, "sys.feature", priority)
155 
156 /**
157  * @brief Identifies the entry for initializing and starting a system running phase by the
158  * priority 2.
159  *
160  * This macro is used to identify the entry called at the priority 2 in the system startup
161  * phase of the startup process. \n
162  *
163  * @param func Indicates the entry function for initializing and starting a system running phase.
164  * The type is void (*)(void).
165  */
166 #define SYS_RUN(func) LAYER_INITCALL_DEF(func, run, "run")
167 /**
168  * @brief Identifies the entry for initializing and starting a system running phase by the
169  * specified priority.
170  *
171  * This macro is used to identify the entry called at the specified priority in the system startup
172  * phase of the startup process. \n
173  *
174  * @param func Indicates the entry function for initializing and starting a system running phase.
175  * The type is void (*)(void).
176  * @param priority Indicates the calling priority when starting the system startup phase.
177  * The value range is [0, 5), and the calling sequence is 0, 1, 2, 3, and 4.
178  */
179 #define SYS_RUN_PRI(func, priority) LAYER_INITCALL(func, run, "run", priority)
180 
181 /**
182  * @brief Identifies the entry for initializing and starting a system service by the priority 2.
183  *
184  * This macro is used to identify the entry called at the priority 2 in the system service
185  * phase of the startup process. \n
186  *
187  * @param func Indicates the entry function for initializing and starting a system service.
188  * The type is void (*)(void).
189  */
190 #define SYSEX_SERVICE_INIT(func) LAYER_INITCALL_DEF(func, app_service, "app.service")
191 /**
192  * @brief Identifies the entry for initializing and starting a system service by the specified
193  * priority.
194  *
195  * This macro is used to identify the entry called at the specified priority of the system service
196  * phase of the startup process. \n
197  *
198  * @param func Indicates the entry function for initializing and starting a system service.
199  * The type is void (*)(void).
200  * @param priority Indicates the calling priority when starting the system service phase.
201  * The value range is [0,5), and the calling sequence is 0, 1, 2, 3, and 4.
202  */
203 #define SYSEX_SERVICE_INIT_PRI(func, priority) LAYER_INITCALL(func, app_service, "app.service", priority)
204 
205 /**
206  * @brief Identifies the entry for initializing and starting a system feature by the priority 2.
207  *
208  * This macro is used to identify the entry called at the priority 2 of the system feature
209  * phase of the startup process. \n
210  *
211  * @param func Indicates the entry function for initializing and starting a system feature.
212  * The type is void (*)(void).
213  */
214 #define SYSEX_FEATURE_INIT(func) LAYER_INITCALL_DEF(func, app_feature, "app.feature")
215 /**
216  * @brief Identifies the entry for initializing and starting a system feature by the specified
217  * priority.
218  *
219  * This macro is used to identify the entry called at the specified priority of the system feature
220  * phase of the startup process. \n
221  *
222  * @param func Indicates the entry function for initializing and starting a system feature.
223  * The type is void (*)(void).
224  * @param priority Indicates the calling priority when starting the system feature phase.
225  * The value range is [0,5), and the calling sequence is 0, 1, 2, 3, and 4.
226  */
227 #define SYSEX_FEATURE_INIT_PRI(func, priority) LAYER_INITCALL(func, app_feature, "app.feature", priority)
228 
229 /**
230  * @brief Identifies the entry for initializing and starting an application-layer service by the
231  * priority 2.
232  *
233  * This macro is used to identify the entry called at the priority 2 of the application-layer
234  * service phase of the startup process. \n
235  *
236  * @param func Indicates the entry function for initializing and starting an application-layer
237  * service. The type is void (*)(void).
238  */
239 #define APP_SERVICE_INIT(func) LAYER_INITCALL_DEF(func, app_service, "app.service")
240 /**
241  * @brief Identifies the entry for initializing and starting an application-layer service by the
242  * specified priority.
243  *
244  * This macro is used to identify the entry called at the specified priority of the
245  * application-layer service phase of the startup process. \n
246  *
247  * @param func Indicates the entry function for initializing and starting an application-layer
248  * service. The type is void (*)(void).
249  * @param priority Indicates the calling priority when starting the application-layer service
250  * phase. The value range is [0,5), and the calling sequence is 0, 1, 2, 3, and 4.
251  */
252 #define APP_SERVICE_INIT_PRI(func, priority) LAYER_INITCALL(func, app_service, "app.service", priority)
253 
254 /**
255  * @brief Identifies the entry for initializing and starting an application-layer feature by the
256  * priority 2.
257  *
258  * This macro is used to identify the entry called at the priority 2 of the application-layer
259  * feature phase of the startup process. \n
260  *
261  * @param func Indicates the entry function for initializing and starting an application-layer
262  * feature. The type is void (*)(void).
263  */
264 #define APP_FEATURE_INIT(func) LAYER_INITCALL_DEF(func, app_feature, "app.feature")
265 /**
266  * @brief Identifies the entry for initializing and starting an application-layer feature by
267  * the specified priority.
268  *
269  * This macro is used to identify the entry called at the specified priority of the
270  * application-layer feature phase of the startup process. \n
271  *
272  * @param func Indicates the entry function for initializing and starting an application-layer
273  * feature. The type is void (*)(void).
274  * @param priority Indicates the calling priority when starting the application-layer feature.
275  * The value range is [0, 5), and the calling sequence is 0, 1, 2, 3, and 4.
276  */
277 #define APP_FEATURE_INIT_PRI(func, priority) LAYER_INITCALL(func, app_feature, "app.feature", priority)
278 
279 #ifdef __cplusplus
280 #if __cplusplus
281 }
282 #endif
283 #endif
284 #endif // OHOS_LAYER_INIT_H
285 /** @} */
286