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 OHOS_MODULES_MANAGER_H__
17#define OHOS_MODULES_MANAGER_H__
18
19#ifdef __cplusplus
20#if __cplusplus
21extern "C" {
22#endif
23#endif
24
25/**
26 * @brief Module constructor function
27 *
28 * For static modules, this code is executed before main().
29 * For dynamic modules, this code is executed when ModuleMgrInstall().
30 *
31 * Usage example:
32 *   MODULE_CONSTRUCTOR(void)
33 *   {
34 *       ...
35 *   }
36 */
37#define MODULE_CONSTRUCTOR(void) static void _init(void) __attribute__((constructor)); static void _init(void)
38
39/**
40 * @brief Module destructor function
41 *
42 * For static modules, this code will not be executed.
43 * For dynamic modules, this code is executed when ModuleMgrUninstall().
44 *
45 * Usage example:
46 *   MODULE_DESTRUCTOR(void)
47 *   {
48 *       ...
49 *   }
50 */
51#define MODULE_DESTRUCTOR(void) static void _destroy(void) __attribute__((destructor)); static void _destroy(void)
52
53// Forward declaration
54typedef struct tagMODULE_MGR MODULE_MGR;
55
56/**
57 * @brief Create dynamic module manager
58 *
59 * This dynamic module manager will manager modules
60 * in the directory: /system/lib/{name}/
61 * @param name module manager name
62 * @return return module manager handle if succeed; return NULL if failed.
63 */
64MODULE_MGR *ModuleMgrCreate(const char *name);
65
66/**
67 * @brief Destroy dynamic module manager
68 *
69 * It will uninstall all modules managed by this moduleMgr
70 * @param moduleMgr module manager handle
71 * @return None
72 */
73void ModuleMgrDestroy(MODULE_MGR *moduleMgr);
74
75/**
76 * @brief Install a module
77 *
78 * The final module path is: /system/lib/{moduleMgrPath}/{moduleName}.z.so
79 *
80 * @param moduleMgr module manager handle
81 * @param moduleName module name
82 * @param argc argument counts for installing
83 * @param argv arguments for installing, the last argument is NULL.
84 * @return module handle returned by dlopen
85 */
86int ModuleMgrInstall(MODULE_MGR *moduleMgr, const char *moduleName,
87                     int argc, const char *argv[]);
88
89/**
90 * @brief Module install arguments
91 */
92typedef struct {
93    int argc;
94    const char **argv;
95} MODULE_INSTALL_ARGS;
96
97/**
98 * @brief Get module install arguments
99 *
100 * This function is available only in MODULE_CONSTRUCTOR.
101 *
102 * @return install args if succeed; return NULL if failed.
103 */
104const MODULE_INSTALL_ARGS *ModuleMgrGetArgs(void);
105
106/**
107 * @brief Scan and install all modules in specified directory
108 *
109 * @param modulePath path for modules to be installed
110 * @return install args if succeed; return NULL if failed.
111 */
112MODULE_MGR *ModuleMgrScan(const char *modulePath);
113
114/**
115 * @brief Uninstall module
116 *
117 * @param moduleMgr module manager handle
118 * @param name module name. If name is NULL, it will uninstall all modules.
119 * @return install args if succeed; return NULL if failed.
120 */
121void ModuleMgrUninstall(MODULE_MGR *moduleMgr, const char *name);
122
123/**
124 * @brief Get number of modules in module manager
125 *
126 * @param hookMgr module manager handle
127 * @return number of modules, return 0 if none
128 */
129int ModuleMgrGetCnt(const MODULE_MGR *moduleMgr);
130
131/**
132 * @brief Module information for traversing modules
133 */
134typedef struct tagMODULE_INFO {
135    const char *name;     /* module name */
136    void *handle;         /* module handler */
137    void *cookie;         /* hook execution cookie */
138} MODULE_INFO;
139
140/**
141 * @brief Module traversal function prototype
142 *
143 * @param moduleInfo MODULE_INFO for traversing each module.
144 * @return None
145 */
146typedef void (*OhosModuleTraversal)(const MODULE_INFO *moduleInfo);
147
148/**
149 * @brief Traversing all modules in the ModuleManager
150 *
151 * @param moduleMgr module manager handle
152 * @param cookie traversal cookie.
153 * @param traversal traversal function.
154 * @return None.
155 */
156void ModuleMgrTraversal(const MODULE_MGR *moduleMgr, void *cookie, OhosModuleTraversal traversal);
157
158#ifdef __cplusplus
159#if __cplusplus
160}
161#endif
162#endif
163#endif
164