Adds DAHDI support alongside Zaptel. DAHDI usage favored, but all Zap stuff should...
[asterisk-bristuff.git] / include / asterisk / module.h
blob4c1e40b1dba8a2f335b16458142fb97152a7455d
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * Kevin P. Fleming <kpfleming@digium.com>
8 * Luigi Rizzo <rizzo@icir.org>
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
21 /*! \file
22 * \brief Asterisk module definitions.
24 * This file contains the definitons for functions Asterisk modules should
25 * provide and some other module related functions.
28 #ifndef _ASTERISK_MODULE_H
29 #define _ASTERISK_MODULE_H
31 #include "asterisk/utils.h"
33 #if defined(__cplusplus) || defined(c_plusplus)
34 extern "C" {
35 #endif
37 /*! \brief The text the key() function should return. */
38 #define ASTERISK_GPL_KEY \
39 "This paragraph is copyright (c) 2006 by Digium, Inc. \
40 In order for your module to load, it must return this \
41 key via a function called \"key\". Any code which \
42 includes this paragraph must be licensed under the GNU \
43 General Public License version 2 or later (at your \
44 option). In addition to Digium's general reservations \
45 of rights, Digium expressly reserves the right to \
46 allow other parties to license this paragraph under \
47 different terms. Any use of Digium, Inc. trademarks or \
48 logos (including \"Asterisk\" or \"Digium\") without \
49 express written permission of Digium, Inc. is prohibited.\n"
51 #define AST_MODULE_CONFIG "modules.conf" /*!< \brief Module configuration file */
53 enum ast_module_unload_mode {
54 AST_FORCE_SOFT = 0, /*!< Softly unload a module, only if not in use */
55 AST_FORCE_FIRM = 1, /*!< Firmly unload a module, even if in use */
56 AST_FORCE_HARD = 2, /*!< as FIRM, plus dlclose() on the module. Not recommended
57 as it may cause crashes */
60 enum ast_module_load_result {
61 AST_MODULE_LOAD_SUCCESS = 0, /*!< Module loaded and configured */
62 AST_MODULE_LOAD_DECLINE = 1, /*!< Module is not configured */
63 AST_MODULE_LOAD_SKIP = 2, /*!< Module was skipped for some reason */
64 AST_MODULE_LOAD_FAILURE = -1, /*!< Module could not be loaded properly */
67 /*!
68 * \brief Load a module.
69 * \param resource_name The name of the module to load.
71 * This function is run by the PBX to load the modules. It performs
72 * all loading and initilization tasks. Basically, to load a module, just
73 * give it the name of the module and it will do the rest.
75 * \return See possible enum values for ast_module_load_result.
77 enum ast_module_load_result ast_load_resource(const char *resource_name);
79 /*!
80 * \brief Unload a module.
81 * \param resource_name The name of the module to unload.
82 * \param ast_module_unload_mode The force flag. This should be set using one of the AST_FORCE flags.
84 * This function unloads a module. It will only unload modules that are not in
85 * use (usecount not zero), unless #AST_FORCE_FIRM or #AST_FORCE_HARD is
86 * specified. Setting #AST_FORCE_FIRM or #AST_FORCE_HARD will unload the
87 * module regardless of consequences (NOT RECOMMENDED).
89 * \return Zero on success, -1 on error.
91 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode);
93 /*!
94 * \brief Notify when usecount has been changed.
96 * This function calulates use counts and notifies anyone trying to keep track
97 * of them. It should be called whenever your module's usecount changes.
99 * \note The LOCAL_USER macros take care of calling this function for you.
101 void ast_update_use_count(void);
103 /*!
104 * \brief Ask for a list of modules, descriptions, and use counts.
105 * \param modentry A callback to an updater function.
106 * \param like
108 * For each of the modules loaded, modentry will be executed with the resource,
109 * description, and usecount values of each particular module.
111 * \return the number of modules loaded
113 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
114 const char *like);
116 /*!
117 * \brief Add a procedure to be run when modules have been updated.
118 * \param updater The function to run when modules have been updated.
120 * This function adds the given function to a linked list of functions to be
121 * run when the modules are updated.
123 * \return Zero on success and -1 on failure.
125 int ast_loader_register(int (*updater)(void));
127 /*!
128 * \brief Remove a procedure to be run when modules are updated.
129 * \param updater The updater function to unregister.
131 * This removes the given function from the updater list.
133 * \return Zero on success, -1 on failure.
135 int ast_loader_unregister(int (*updater)(void));
138 * \brief Run the unload() callback for all loaded modules
140 * This function should be called when Asterisk is shutting down gracefully.
142 void ast_module_shutdown(void);
144 /*!
145 * \brief Match modules names for the Asterisk cli.
146 * \param line Unused by this function, but this should be the line we are
147 * matching.
148 * \param word The partial name to match.
149 * \param pos The position the word we are completing is in.
150 * \param state The possible match to return.
151 * \param rpos The position we should be matching. This should be the same as
152 * pos.
153 * \param needsreload This should be 1 if we need to reload this module and 0
154 * otherwise. This function will only return modules that are reloadble
155 * if this is 1.
157 * \return A possible completion of the partial match, or NULL if no matches
158 * were found.
160 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload);
162 /* Opaque type for module handles generated by the loader */
164 struct ast_module;
166 /* User count routines keep track of which channels are using a given module
167 resource. They can help make removing modules safer, particularly if
168 they're in use at the time they have been requested to be removed */
170 struct ast_module_user;
171 struct ast_module_user_list;
173 /*! \page ModMngmnt The Asterisk Module management interface
175 * All modules must implement the module API (load, unload...)
176 * whose functions are exported through fields of a "struct module_symbol";
179 enum ast_module_flags {
180 AST_MODFLAG_DEFAULT = 0,
181 AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0),
182 AST_MODFLAG_BUILDSUM = (1 << 1),
185 struct ast_module_info {
188 * The 'self' pointer for a module; it will be set by the loader before
189 * it calls the module's load_module() entrypoint, and used by various
190 * other macros that need to identify the module.
193 struct ast_module *self;
194 enum ast_module_load_result (*load)(void); /* register stuff etc. Optional. */
195 int (*reload)(void); /* config etc. Optional. */
196 int (*unload)(void); /* unload. called with the module locked */
197 const char *name; /* name of the module for loader reference and CLI commands */
198 const char *description; /* user friendly description of the module. */
200 /*!
201 * This holds the ASTERISK_GPL_KEY, signifiying that you agree to the terms of
202 * the Asterisk license as stated in the ASTERISK_GPL_KEY. Your module will not
203 * load if it does not return the EXACT key string.
206 const char *key;
207 unsigned int flags;
209 /*! The value of AST_BUILDOPT_SUM when this module was compiled */
210 const char buildopt_sum[33];
213 void ast_module_register(const struct ast_module_info *);
214 void ast_module_unregister(const struct ast_module_info *);
216 struct ast_module_user *__ast_module_user_add(struct ast_module *, struct ast_channel *);
217 void __ast_module_user_remove(struct ast_module *, struct ast_module_user *);
218 void __ast_module_user_hangup_all(struct ast_module *);
220 #define ast_module_user_add(chan) __ast_module_user_add(ast_module_info->self, chan)
221 #define ast_module_user_remove(user) __ast_module_user_remove(ast_module_info->self, user)
222 #define ast_module_user_hangup_all() __ast_module_user_hangup_all(ast_module_info->self)
224 struct ast_module *ast_module_ref(struct ast_module *);
225 void ast_module_unref(struct ast_module *);
227 #if defined(__cplusplus) || defined(c_plusplus)
228 #define AST_MODULE_INFO(keystr, flags_to_set, desc, load_func, unload_func, reload_func) \
229 static struct ast_module_info __mod_info = { \
230 NULL, \
231 load_func, \
232 reload_func, \
233 unload_func, \
234 AST_MODULE, \
235 desc, \
236 keystr, \
237 flags_to_set | AST_MODFLAG_BUILDSUM, \
238 AST_BUILDOPT_SUM, \
239 }; \
240 static void __attribute__ ((constructor)) __reg_module(void) \
242 ast_module_register(&__mod_info); \
244 static void __attribute__ ((destructor)) __unreg_module(void) \
246 ast_module_unregister(&__mod_info); \
248 const static __attribute__((unused)) struct ast_module_info *ast_module_info = &__mod_info
250 #define AST_MODULE_INFO_STANDARD(keystr, desc) \
251 AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
252 load_module, \
253 unload_module, \
254 NULL \
256 #else
257 /* forward declare this pointer in modules, so that macro/function
258 calls that need it can get it, since it will actually be declared
259 and populated at the end of the module's source file... */
260 const static __attribute__((unused)) struct ast_module_info *ast_module_info;
262 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...) \
263 static struct ast_module_info __mod_info = { \
264 .name = AST_MODULE, \
265 .flags = flags_to_set | AST_MODFLAG_BUILDSUM, \
266 .description = desc, \
267 .key = keystr, \
268 .buildopt_sum = AST_BUILDOPT_SUM, \
269 fields \
270 }; \
271 static void __attribute__ ((constructor)) __reg_module(void) \
273 ast_module_register(&__mod_info); \
275 static void __attribute__ ((destructor)) __unreg_module(void) \
277 ast_module_unregister(&__mod_info); \
279 const static struct ast_module_info *ast_module_info = &__mod_info
281 #define AST_MODULE_INFO_STANDARD(keystr, desc) \
282 AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
283 .load = load_module, \
284 .unload = unload_module, \
286 #endif
288 #if defined(__cplusplus) || defined(c_plusplus)
290 #endif
292 #endif /* _ASTERISK_MODULE_H */