2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #ifndef _ASTERISK_MODULE_H
15 #define _ASTERISK_MODULE_H
17 #if defined(__cplusplus) || defined(c_plusplus)
21 /* Every module must provide these functions */
23 //! Initialize the module
25 * This function is called at module load time. Put all code in here
26 * that needs to set up your module's hardware, software, registrations,
29 int load_module(void);
31 //! Cleanup all module structures, sockets, etc
33 * This is called at exit. Any registrations and memory allocations need
34 * to be unregistered and free'd here. Nothing else will do these for you (until exit).
35 * Return 0 on success, or other than 0 if there is a problem.
37 int unload_module(void);
39 //! Provides a usecount
41 * This function will be called by various parts of asterisk. Basically, all it has
42 * to do is to return a usecount when called. You will need to maintain your usecount
43 * within the module somewhere.
45 int usecount(void); /*! How many channels provided by this module are in use? */
49 * Returns a short description of your module.
51 char *description(void); /*! Description of this module */
53 //! Returns the ASTERISK_GPL_KEY
55 * This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of
56 * the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does
57 * not return the EXACT message, i.e. char *key(void){return ASTERISK_GPL_KEY;}
59 char *key(void); /*! Return the below mentioned key, unmodified */
63 * This function is where any reload routines take place. Re-read config files,
64 * change signalling, whatever is appropriate on a reload.
65 * Return 0 on success, and other than 0 on problem.
67 int reload(void); /*! reload configs */
69 #define ASTERISK_GPL_KEY \
70 "This paragraph is Copyright (C) 2000, Linux Support Services, Inc. \
71 In order for your module to load, it must return this key via a function \
72 called \"key\". Any code which includes this paragraph must be licensed under \
73 the GNU General Public License version 2 or later (at your option). Linux \
74 Support Services, Inc. reserves the right to allow other parties to license \
75 this paragraph under other terms as well."
77 #define AST_MODULE_CONFIG "modules.conf" /*! Module configuration file */
79 #define AST_FORCE_SOFT 0
80 #define AST_FORCE_FIRM 1
81 #define AST_FORCE_HARD 2
85 * \param resource_name the filename of the module to load
86 * This function is ran by the PBX to load the modules. It performs
87 * all loading, setting up of it's module related data structures, etc.
88 * Basically, to load a module, you just give it the name of the module and
89 * it will do the rest.
90 * It returns 0 on success, -1 on error
92 int ast_load_resource(char *resource_name);
96 * \param resourcename the name of the module to unload
97 * \param force the force flag. Setting this to non-zero will force the module to be unloaded
98 * This function unloads a particular module. If the force flag is not set,
99 * it will not unload a module with a usecount > 0. However, if it is set,
100 * it will unload the module regardless of consequences (NOT_RECOMMENDED)
102 int ast_unload_resource(char *resource_name, int force);
104 //! Notify when usecount has been changed
106 * This function goes through and calulates use counts. It also notifies anybody
107 * trying to keep track of them.
109 void ast_update_use_count(void);
111 //! Ask for a list of modules, descriptions, and use counts
113 * \param modentry a callback to an updater function
114 * For each of the modules loaded, modentry will be executed with the resource, description,
115 * and usecount values of each particular module.
117 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt));
119 //! Ask this procedure to be run with modules have been updated
121 * \param updater the function to run when modules have been updated
122 * This function adds the given function to a linked list of functions to be run
123 * when the modules are updated.
124 * It returns 0 on success and -1 on failure.
126 int ast_loader_register(int (*updater)(void));
128 //! No longer run me when modules are updated
130 * \param updater function to unregister
131 * This removes the given function from the updater list.
132 * It returns 0 on success, -1 on failure.
134 int ast_loader_unregister(int (*updater)(void));
136 //! Reload all modules
138 * This reloads all modules set to load in asterisk. It does NOT run the unload
139 * routine and then loads them again, it runs the given reload routine.
141 void ast_module_reload(void);
143 int ast_register_atexit(void (*func)(void));
144 void ast_unregister_atexit(void (*func)(void));
146 /* Local user routines keep track of which channels are using a given module resource.
147 They can help make removing modules safer, particularly if they're in use at the time
148 they have been requested to be removed */
150 #define STANDARD_LOCAL_USER struct localuser { \
151 struct ast_channel *chan; \
152 struct localuser *next; \
155 #define LOCAL_USER_DECL AST_MUTEX_DEFINE_STATIC(localuser_lock); \
156 static struct localuser *localusers = NULL; \
157 static int localusecnt = 0;
159 #define LOCAL_USER_ADD(u) { \
161 if (!(u=(struct localuser *)malloc(sizeof(struct localuser)))) { \
162 ast_log(LOG_WARNING, "Out of memory\n"); \
165 ast_mutex_lock(&localuser_lock); \
167 u->next = localusers; \
170 ast_mutex_unlock(&localuser_lock); \
171 ast_update_use_count(); \
174 #define LOCAL_USER_REMOVE(u) { \
175 struct localuser *uc, *ul = NULL; \
176 ast_mutex_lock(&localuser_lock); \
181 ul->next = uc->next; \
183 localusers = uc->next; \
191 ast_mutex_unlock(&localuser_lock); \
192 ast_update_use_count(); \
195 #define STANDARD_HANGUP_LOCALUSERS { \
196 struct localuser *u, *ul; \
197 ast_mutex_lock(&localuser_lock); \
200 ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD); \
205 ast_mutex_unlock(&localuser_lock); \
209 #define STANDARD_USECOUNT(res) { \
210 ast_mutex_lock(&localuser_lock); \
212 ast_mutex_unlock(&localuser_lock); \
217 #if defined(__cplusplus) || defined(c_plusplus)