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 Samgr
18  * @{
19  *
20  * @brief Manages system capabilities.
21  *
22  * This module provides the development framework base of the service-oriented architecture (SOA).
23  * You can develop your own abilities based on the Samgr development framework. \n
24  * This module provides basic models of services, features, and functions, and registration and
25  * discovery capabilities. \n
26  *
27  * @since 1.0
28  * @version 1.0
29  */
30 
31 /**
32  * @file samgr_lite.h
33  *
34  * @brief Manages system capabilities.
35  *
36  * This is used when services, features, and functions are registered with and discovered by Samgr. \n
37  *
38  * @since 1.0
39  * @version 1.0
40  */
41 
42 #ifndef LITE_SAMGR_H
43 #define LITE_SAMGR_H
44 
45 #include "common.h"
46 #include "iunknown.h"
47 #include "service.h"
48 #include "feature.h"
49 
50 #ifdef __cplusplus
51 #if __cplusplus
52 extern "C" {
53 #endif
54 #endif
55 
56 /**
57  * @brief Starts a bootstrap service, which is used by samgr and implemented by system service
58  * developers.
59  */
60 #define BOOTSTRAP_SERVICE "Bootstrap"
61 
62 /**
63  * @brief Defines the maximun num of system capabilities.
64  */
65 #define MAX_SYSCAP_NUM 512
66 
67 /**
68  * @brief Defines the maximun length of the system capabiliy name.
69  */
70 #define MAX_SYSCAP_NAME_LEN 64
71 
72 /**
73  * @brief Enumerates the IDs of the message to be processed for starting the bootstrap service.
74  *
75  * This function is implemented by developers of the system service. \n
76  * Messages sent to the bootstrap service when Samgr is started. \n
77  *
78  */
79 typedef enum BootMessage {
80     /** Message indicating that the core system service is initialized */
81     BOOT_SYS_COMPLETED,
82     /** Message indicating that the system and application-layer services are initialized */
83     BOOT_APP_COMPLETED,
84     /** Message indicating service registration during running */
85     BOOT_REG_SERVICE,
86     /** Maximum number of message IDs */
87     BOOTSTRAP_BUTT
88 } BootMessage;
89 
90 /**
91  * @brief Represents the system ability management class.
92  *
93  * This class is used for registering and discovering services, features, and functions. \n
94  *
95  * @since 1.0
96  * @version 1.0
97  */
98 typedef struct SamgrLite {
99     /**
100      * @brief Registers a service.
101      *
102      * You need to call this function in the startup entry of each service. \n
103      * {@link Service} and {@link Service} structure members to be registered cannot be empty. \n
104      *
105      * @param service Indicates the service to be registered.
106      * @return Returns <b>TRUE</b> if the registration is successful; returns <b>FALSE</b>
107      * if the registration fails.
108      * @since 1.0
109      * @version 1.0
110      */
111     BOOL (*RegisterService)(Service *service);
112 
113     /**
114      * @brief Unregisters a service.
115      *
116      * You need to call this function when the service is no longer required. \n
117      *
118      * @param name Indicates the name of the service to be unregistered.
119      * @attention Before unregistering the service, you must unregister its features and functions.
120      * @return Returns the unregistered service object if the unregistration is successful.
121      * The memory is released by the caller. Returns <b>NULL</b> if the unregistration fails.
122      * @since 1.0
123      * @version 1.0
124      */
125     Service *(*UnregisterService)(const char *name);
126 
127     /**
128      * @brief Registers a feature.
129      *
130      * You need to call this function in the startup entry of each feature. \n
131      * {@link Feature} and {@link Feature} structure members to be registered cannot be empty. \n
132      *
133      * @param feature Indicates the feature to be registered.
134      * @return Returns <b>TRUE</b> if the registration is successful; returns <b>FALSE</b>
135      * if the registration fails.
136      * @since 1.0
137      * @version 1.0
138      */
139     BOOL (*RegisterFeature)(const char *serviceName, Feature *feature);
140 
141     /**
142      * @brief Unregisters a feature.
143      *
144      * You need to call this function when the feature is no longer required. \n
145      *
146      * @param serviceName Indicates the name of the service whose feature will be unregistered.
147      * @param featureName Indicates the name of the feature to be unregistered.
148      * @attention Before unregistering the feature, you must unregister its functions. Otherwise,
149      * the unregistration fails.
150      * @return Returns the unregistered feature object if the unregistration is successful.
151      * The memory is released by the caller. Returns <b>NULL</b> if the unregistration fails.
152      * @since 1.0
153      * @version 1.0
154      */
155     Feature *(*UnregisterFeature)(const char *serviceName, const char *featureName);
156 
157     /**
158      * @brief Registers the API for the default feature of a service.
159      *
160      * You need to call this function after the service is registered. \n
161      * The pointers to the {@link IUnknown} and {@link IUnknown} members to be registered
162      * cannot be empty. \n
163      *
164      * @param service Indicates the name of the service whose default feature's API will be
165      * registered.
166      * @param publicApi Indicates the API to be registered.
167      * @return Returns <b>TRUE</b> if the registration is successful; returns <b>FALSE</b>
168      * if the registration fails.
169      * @since 1.0
170      * @version 1.0
171      */
172     BOOL (*RegisterDefaultFeatureApi)(const char *service, IUnknown *publicApi);
173 
174     /**
175      * @brief Unregisters the API from the default feature of a service.
176      *
177      * You need to call this function to unregister {@link IUnknown} if the service to which
178      * the default feature belongs is no longer required. \n
179      *
180      * @param service Indicates the name of the service whose default feature's API will be
181      * unregistered.
182      * @return Returns the unregistered function object if the unregistration is successful.
183      * The memory is released by the caller. Returns <b>NULL</b> if the unregistration fails.
184      * @since 1.0
185      * @version 1.0
186      */
187     IUnknown *(*UnregisterDefaultFeatureApi)(const char *service);
188 
189     /**
190      * @brief Registers the API for a feature.
191      *
192      * You can call this function only if the feature has been registered. \n
193      * The pointers to the {@link IUnknown} and {@link IUnknown} members to be registered cannot
194      * be empty. \n
195      *
196      * @param service Indicates the name of the service whose API will be registered.
197      * @param feature Indicates the name of the feature whose API will be registered.
198      * @param publicApi Indicates the API to be registered.
199      * @return Returns <b>TRUE</b> if the registration is successful; returns <b>FALSE</b>
200      * if the registration fails.
201      * @since 1.0
202      * @version 1.0
203      */
204     BOOL (*RegisterFeatureApi)(const char *service, const char *feature, IUnknown *publicApi);
205 
206     /**
207      * @brief Unregisters the API from a feature.
208      *
209      * You must call this function before unregistering the feature no longer required. \n
210      *
211      * @param service Indicates the name of the service whose API will be unregistered.
212      * @param feature Indicates the name of the feature whose API will be unregistered.
213      * @return Returns the unregistered function object if the unregistration is successful.
214      * The memory is released by the caller. Returns <b>NULL</b> if the unregistration fails.
215      * @since 1.0
216      * @version 1.0
217      */
218     IUnknown *(*UnregisterFeatureApi)(const char *service, const char *feature);
219 
220     /**
221      * @brief Obtains the API specific to the default feature.
222      *
223      * You need to call this function before using the system capabilities of the service involved. \n
224      *
225      *
226      * @param service Indicates the name of the service to which the default feature belongs.
227      * @return Returns the <b>IUnknown *</b> object that can be called if the operation is
228      * successful; returns <b>NULL</b> if the operation fails.
229      * @since 1.0
230      * @version 1.0
231      */
232     IUnknown *(*GetDefaultFeatureApi)(const char *service);
233 #ifdef MINI_SAMGR_LITE_RPC
234     IUnknown *(*GetRemoteDefaultFeatureApi)(char *deviceId, const char *serviceName);
235 #endif
236     /**
237      * @brief Obtains the API specific to the feature.
238      *
239      * You need to call this function before using the system capabilities of the service involved. \n
240      *
241      *
242      * @param service Indicates the name of the service to which the feature belongs.
243      * @param feature Indicates the name of the feature whose API will be obtained.
244      * @return Returns the <b>IUnknown *</b> object that can be called if the operation is
245      * successful; returns <b>NULL</b> if the operation fails.
246      * @since 1.0
247      * @version 1.0
248      */
249     IUnknown *(*GetFeatureApi)(const char *serviceName, const char *feature);
250 
251     /**
252      * @brief Adds system capability.
253      *
254      * You can call this function to add the system capability. \n
255      *
256      * @param sysCap Indicates the name of the system capablity.
257      * @return Returns <b>EC_SUCCESS</b> if this function is successfully called; returns another error code otherwise.
258      * @since 1.0
259      * @version 1.0
260      */
261     int32 (*AddSystemCapability)(const char *sysCap);
262 
263     /**
264      * @brief Checks system capability is existed.
265      *
266      * You can call this function to check whether the system capability is existed. \n
267      *
268      * @param sysCap Indicates the name of the system capablity.
269      * @return Returns <b>TRUE</b> if the system capablity is existed; returns <b>FALSE</b> otherwise.
270      * @since 1.0
271      * @version 1.0
272      */
273     BOOL (*HasSystemCapability)(const char *sysCap);
274 
275     /**
276      * @brief Obtains all the available system capabilities.
277      *
278      * You need to ensure that there is enough memory to save all available system capabilities
279      * before call this function. \n
280      *
281      * @return Returns <b>EC_SUCCESS</b> if this function is successfully called; returns another error code otherwise.
282      * the num of available system capabilities saved in sysCapNum and
283      * all available system capabilities saved in sysCaps.
284      * @since 1.0
285      * @version 1.0
286      */
287     int32 (*GetSystemAvailableCapabilities)(char sysCaps[MAX_SYSCAP_NUM][MAX_SYSCAP_NAME_LEN], int32 *sysCapNum);
288 } SamgrLite;
289 
290 /**
291  * @brief Obtains the singleton Samgr instance.
292  *
293  * You need to call this function before using the Samgr capabilities. \n
294  *
295  * @return Returns the pointer to the singleton instance {@link SamgrLite}.
296  * @since 1.0
297  * @version 1.0
298  */
299 SamgrLite *SAMGR_GetInstance(void);
300 
301 /**
302  * @brief Starts system services and features.
303  *
304  * This function is called in the <b>main</b> function to start all services when an independent
305  * process is developed. \n
306  * This function is called after the dynamic library (containing system services and features) is
307  * loaded during system running. \n
308  *
309  * @attention This function cannot be called frequently. Otherwise, problems such as repeated
310  * service startup may occur. It is recommended that this function be called once in the
311  * <b>main</b> function or after the dynamic library is loaded.
312  * @since 1.0
313  * @version 1.0
314  */
315 void SAMGR_Bootstrap(void);
316 #ifdef __cplusplus
317 #if __cplusplus
318 }
319 #endif
320 #endif
321 
322 #endif // LITE_SAMGR_H
323 /** @} */
324