Mon Jun 27 16:50:54 2011

Asterisk developer's documentation


module.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * Kevin P. Fleming <kpfleming@digium.com>
00008  * Luigi Rizzo <rizzo@icir.org>
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  */
00020 
00021 /*! \file
00022  * \brief Asterisk module definitions.
00023  *
00024  * This file contains the definitons for functions Asterisk modules should
00025  * provide and some other module related functions.
00026  */
00027 
00028 #ifndef _ASTERISK_MODULE_H
00029 #define _ASTERISK_MODULE_H
00030 
00031 #include "asterisk/utils.h"
00032 
00033 #if defined(__cplusplus) || defined(c_plusplus)
00034 extern "C" {
00035 #endif
00036 
00037 /*! \brief The text the key() function should return. */
00038 #define ASTERISK_GPL_KEY \
00039 "This paragraph is copyright (c) 2006 by Digium, Inc. \
00040 In order for your module to load, it must return this \
00041 key via a function called \"key\".  Any code which \
00042 includes this paragraph must be licensed under the GNU \
00043 General Public License version 2 or later (at your \
00044 option).  In addition to Digium's general reservations \
00045 of rights, Digium expressly reserves the right to \
00046 allow other parties to license this paragraph under \
00047 different terms. Any use of Digium, Inc. trademarks or \
00048 logos (including \"Asterisk\" or \"Digium\") without \
00049 express written permission of Digium, Inc. is prohibited.\n"
00050 
00051 #define AST_MODULE_CONFIG "modules.conf" /*!< \brief Module configuration file */
00052 
00053 enum ast_module_unload_mode {
00054    AST_FORCE_SOFT = 0, /*!< Softly unload a module, only if not in use */
00055    AST_FORCE_FIRM = 1, /*!< Firmly unload a module, even if in use */
00056    AST_FORCE_HARD = 2, /*!< as FIRM, plus dlclose() on the module. Not recommended
00057             as it may cause crashes */
00058 };
00059 
00060 enum ast_module_load_result {
00061    AST_MODULE_LOAD_SUCCESS = 0,    /*!< Module loaded and configured */
00062    AST_MODULE_LOAD_DECLINE = 1,    /*!< Module is not configured */
00063    AST_MODULE_LOAD_SKIP = 2,       /*!< Module was skipped for some reason */
00064    AST_MODULE_LOAD_PRIORITY = 3,   /*!< Module is not loaded yet, but is added to prioity heap */
00065    AST_MODULE_LOAD_FAILURE = -1,   /*!< Module could not be loaded properly */
00066 };
00067 
00068 /*! 
00069  * \brief Load a module.
00070  * \param resource_name The name of the module to load.
00071  *
00072  * This function is run by the PBX to load the modules.  It performs
00073  * all loading and initialization tasks.   Basically, to load a module, just
00074  * give it the name of the module and it will do the rest.
00075  *
00076  * \return See possible enum values for ast_module_load_result.
00077  */
00078 enum ast_module_load_result ast_load_resource(const char *resource_name);
00079 
00080 /*! 
00081  * \brief Unload a module.
00082  * \param resource_name The name of the module to unload.
00083  * \param ast_module_unload_mode The force flag. This should be set using one of the AST_FORCE flags.
00084  *
00085  * This function unloads a module.  It will only unload modules that are not in
00086  * use (usecount not zero), unless #AST_FORCE_FIRM or #AST_FORCE_HARD is 
00087  * specified.  Setting #AST_FORCE_FIRM or #AST_FORCE_HARD will unload the
00088  * module regardless of consequences (NOT RECOMMENDED).
00089  *
00090  * \retval 0 on success.
00091  * \retval -1 on error.
00092  */
00093 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode);
00094 
00095 /*! 
00096  * \brief Notify when usecount has been changed.
00097  *
00098  * This function calulates use counts and notifies anyone trying to keep track
00099  * of them.  It should be called whenever your module's usecount changes.
00100  *
00101  * \note The ast_module_user_* functions take care of calling this function for you.
00102  */
00103 void ast_update_use_count(void);
00104 
00105 /*! 
00106  * \brief Ask for a list of modules, descriptions, and use counts.
00107  * \param modentry A callback to an updater function.
00108  * \param like
00109  *
00110  * For each of the modules loaded, modentry will be executed with the resource,
00111  * description, and usecount values of each particular module.
00112  * 
00113  * \return the number of modules loaded
00114  */
00115 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
00116             const char *like);
00117 
00118 /*!
00119  * \brief Check if module with the name given is loaded
00120  * \param name Module name, like "chan_sip.so"
00121  * \retval 1 if true 
00122  * \retval 0 if false
00123  */
00124 int ast_module_check(const char *name);
00125 
00126 /*! 
00127  * \brief Add a procedure to be run when modules have been updated.
00128  * \param updater The function to run when modules have been updated.
00129  *
00130  * This function adds the given function to a linked list of functions to be
00131  * run when the modules are updated. 
00132  *
00133  * \retval 0 on success 
00134  * \retval -1 on failure.
00135  */
00136 int ast_loader_register(int (*updater)(void));
00137 
00138 /*! 
00139  * \brief Remove a procedure to be run when modules are updated.
00140  * \param updater The updater function to unregister.
00141  *
00142  * This removes the given function from the updater list.
00143  * 
00144  * \retval 0 on success
00145  * \retval -1 on failure.
00146  */
00147 int ast_loader_unregister(int (*updater)(void));
00148 
00149 /*!
00150  * \brief Run the unload() callback for all loaded modules
00151  *
00152  * This function should be called when Asterisk is shutting down gracefully.
00153  */
00154 void ast_module_shutdown(void);
00155 
00156 /*! 
00157  * \brief Match modules names for the Asterisk cli.
00158  * \param line Unused by this function, but this should be the line we are
00159  *        matching.
00160  * \param word The partial name to match. 
00161  * \param pos The position the word we are completing is in.
00162  * \param state The possible match to return.
00163  * \param rpos The position we should be matching.  This should be the same as
00164  *        pos.
00165  * \param needsreload This should be 1 if we need to reload this module and 0
00166  *        otherwise.  This function will only return modules that are reloadble
00167  *        if this is 1.
00168  *
00169  * \retval A possible completion of the partial match.
00170  * \retval NULL if no matches were found.
00171  */
00172 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload);
00173 
00174 /* Opaque type for module handles generated by the loader */
00175 
00176 struct ast_module;
00177 
00178 /* User count routines keep track of which channels are using a given module
00179    resource.  They can help make removing modules safer, particularly if
00180    they're in use at the time they have been requested to be removed */
00181 
00182 struct ast_module_user;
00183 struct ast_module_user_list;
00184 
00185 /*! \page ModMngmnt The Asterisk Module management interface
00186  *
00187  * All modules must implement the module API (load, unload...)
00188  */
00189 
00190 enum ast_module_flags {
00191    AST_MODFLAG_DEFAULT = 0,
00192    AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0),
00193    AST_MODFLAG_LOAD_ORDER = (1 << 1),
00194 };
00195 
00196 enum ast_module_load_priority {
00197    AST_MODPRI_REALTIME_DEPEND =    10,  /*!< Dependency for a realtime driver */
00198    AST_MODPRI_REALTIME_DEPEND2 =   20,  /*!< Second level dependency for a realtime driver (func_curl needs res_curl, but is needed by res_config_curl) */
00199    AST_MODPRI_REALTIME_DRIVER =    30,  /*!< A realtime driver, which provides configuration services for other modules */
00200    AST_MODPRI_CHANNEL_DEPEND =     50,  /*!< Channel driver dependency (may depend upon realtime, e.g. MOH) */
00201    AST_MODPRI_CHANNEL_DRIVER =     60,  /*!< Channel drivers (provide devicestate) */
00202    AST_MODPRI_APP_DEPEND =         70,  /*!< Dependency for an application */
00203    AST_MODPRI_DEVSTATE_PROVIDER =  80,  /*!< Applications and other modules that _provide_ devicestate (e.g. meetme) */
00204    AST_MODPRI_DEVSTATE_PLUGIN =    90,  /*!< Plugin for a module that provides devstate (e.g. res_calendar_*) */
00205    AST_MODPRI_CDR_DRIVER =        100,  /*!< CDR or CEL backend */
00206    AST_MODPRI_DEFAULT =           128,  /*!< Modules not otherwise defined (such as most apps) will load here */
00207    AST_MODPRI_DEVSTATE_CONSUMER = 150,  /*!< Certain modules, which consume devstate, need to load after all others (e.g. app_queue) */
00208 };
00209 
00210 struct ast_module_info {
00211 
00212    /*!
00213     * The 'self' pointer for a module; it will be set by the loader before
00214     * it calls the module's load_module() entrypoint, and used by various
00215     * other macros that need to identify the module.
00216     */
00217 
00218    struct ast_module *self;
00219    enum ast_module_load_result (*load)(void);   /*!< register stuff etc. Optional. */
00220    int (*reload)(void);       /*!< config etc. Optional. */
00221    int (*unload)(void);       /*!< unload. called with the module locked */
00222    int (*backup_globals)(void);     /*!< for embedded modules, backup global data */
00223    void (*restore_globals)(void);      /*!< for embedded modules, restore global data */
00224    const char *name;       /*!< name of the module for loader reference and CLI commands */
00225    const char *description;      /*!< user friendly description of the module. */
00226 
00227    /*! 
00228     * This holds the ASTERISK_GPL_KEY, signifiying that you agree to the terms of
00229     * the Asterisk license as stated in the ASTERISK_GPL_KEY.  Your module will not
00230     * load if it does not return the EXACT key string.
00231     */
00232 
00233    const char *key;
00234    unsigned int flags;
00235 
00236    /*! The value of AST_BUILDOPT_SUM when this module was compiled */
00237    const char buildopt_sum[33];
00238 
00239    /*! This value represents the order in which a module's load() function is initialized.
00240     *  The lower this value, the higher the priority.  The value is only checked if the
00241     *  AST_MODFLAG_LOAD_ORDER flag is set.  If the AST_MODFLAG_LOAD_ORDER flag is not set,
00242     *  this value will never be read and the module will be given the lowest possible priority
00243     *  on load. */
00244    unsigned char load_pri;
00245 
00246    /*! Modules which should be loaded first, in comma-separated string format.
00247     * These are only required for loading, when the optional_api header file
00248     * detects that the compiler does not support the optional API featureset. */
00249    const char *nonoptreq;
00250 };
00251 
00252 void ast_module_register(const struct ast_module_info *);
00253 void ast_module_unregister(const struct ast_module_info *);
00254 
00255 struct ast_module_user *__ast_module_user_add(struct ast_module *, struct ast_channel *);
00256 void __ast_module_user_remove(struct ast_module *, struct ast_module_user *);
00257 void __ast_module_user_hangup_all(struct ast_module *);
00258 
00259 #define ast_module_user_add(chan) __ast_module_user_add(ast_module_info->self, chan)
00260 #define ast_module_user_remove(user) __ast_module_user_remove(ast_module_info->self, user)
00261 #define ast_module_user_hangup_all() __ast_module_user_hangup_all(ast_module_info->self)
00262 
00263 struct ast_module *ast_module_ref(struct ast_module *);
00264 void ast_module_unref(struct ast_module *);
00265 
00266 #if defined(__cplusplus) || defined(c_plusplus)
00267 #define AST_MODULE_INFO(keystr, flags_to_set, desc, load_func, unload_func, reload_func, load_pri) \
00268    static struct ast_module_info __mod_info = { \
00269       NULL,             \
00270       load_func,           \
00271       reload_func,            \
00272       unload_func,            \
00273       NULL,             \
00274       NULL,             \
00275       AST_MODULE,          \
00276       desc,             \
00277       keystr,              \
00278       flags_to_set,           \
00279       AST_BUILDOPT_SUM,       \
00280       load_pri,           \
00281    };                \
00282    static void  __attribute__((constructor)) __reg_module(void) \
00283    { \
00284       ast_module_register(&__mod_info); \
00285    } \
00286    static void  __attribute__((destructor)) __unreg_module(void) \
00287    { \
00288       ast_module_unregister(&__mod_info); \
00289    } \
00290    static const __attribute__((unused)) struct ast_module_info *ast_module_info = &__mod_info
00291 
00292 #define AST_MODULE_INFO_STANDARD(keystr, desc)     \
00293    AST_MODULE_INFO(keystr, AST_MODFLAG_LOAD_ORDER, desc, \
00294          load_module,         \
00295          unload_module,    \
00296          NULL,       \
00297          AST_MODPRI_DEFAULT \
00298              )
00299 #else /* plain C */
00300 
00301 /* forward declare this pointer in modules, so that macro/function
00302    calls that need it can get it, since it will actually be declared
00303    and populated at the end of the module's source file... */
00304 static const __attribute__((unused)) struct ast_module_info *ast_module_info;
00305 
00306 #if !defined(EMBEDDED_MODULE)
00307 #define __MODULE_INFO_SECTION
00308 #define __MODULE_INFO_GLOBALS
00309 #else
00310 /*
00311  * For embedded modules we need additional information to backup and
00312  * restore the global variables in the module itself, so we can unload
00313  * reload the module.
00314  * EMBEDDED_MODULE is defined as the module name, so the calls to make_var()
00315  * below will actually define different symbols for each module.
00316  */
00317 #define __MODULE_INFO_SECTION __attribute__((section(".embed_module")))
00318 #define __MODULE_INFO_GLOBALS .backup_globals = __backup_globals, .restore_globals = __restore_globals,
00319 
00320 #define make_var_sub(mod, type) __ ## mod ## _ ## type
00321 #define make_var(mod, type) make_var_sub(mod, type)
00322 
00323 extern void make_var(EMBEDDED_MODULE, bss_start);
00324 extern void make_var(EMBEDDED_MODULE, bss_end);
00325 extern void make_var(EMBEDDED_MODULE, data_start);
00326 extern void make_var(EMBEDDED_MODULE, data_end);
00327 
00328 static void * __attribute__((section(".embed_module"))) __global_backup;
00329 
00330 static int __backup_globals(void)
00331 {
00332    size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
00333 
00334    if (__global_backup)
00335       return 0;
00336 
00337    if (!data_size)
00338       return 0;
00339 
00340    if (!(__global_backup = ast_malloc(data_size)))
00341       return -1;
00342 
00343    memcpy(__global_backup, & make_var(EMBEDDED_MODULE, data_start), data_size);
00344 
00345    return 0;
00346 }
00347 
00348 static void __restore_globals(void)
00349 {
00350    size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
00351    size_t bss_size = & make_var(EMBEDDED_MODULE, bss_end) - & make_var(EMBEDDED_MODULE, bss_start);
00352 
00353    if (bss_size)
00354       memset(& make_var(EMBEDDED_MODULE, bss_start), 0, bss_size);
00355 
00356    if (!data_size || !__global_backup)
00357       return;
00358 
00359    memcpy(& make_var(EMBEDDED_MODULE, data_start), __global_backup, data_size);
00360 }
00361 #undef make_var
00362 #undef make_var_sub
00363 #endif /* EMBEDDED_MODULE */
00364 
00365 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...) \
00366    static struct ast_module_info             \
00367       __MODULE_INFO_SECTION            \
00368       __mod_info = {             \
00369       __MODULE_INFO_GLOBALS            \
00370       .name = AST_MODULE,           \
00371       .flags = flags_to_set,           \
00372       .description = desc,          \
00373       .key = keystr,             \
00374       .buildopt_sum = AST_BUILDOPT_SUM,      \
00375       fields                  \
00376    };                   \
00377    static void  __attribute__((constructor)) __reg_module(void) \
00378    { \
00379       ast_module_register(&__mod_info); \
00380    } \
00381    static void  __attribute__((destructor)) __unreg_module(void) \
00382    { \
00383       ast_module_unregister(&__mod_info); \
00384    } \
00385    static const struct ast_module_info *ast_module_info = &__mod_info
00386 
00387 #define AST_MODULE_INFO_STANDARD(keystr, desc)     \
00388    AST_MODULE_INFO(keystr, AST_MODFLAG_LOAD_ORDER, desc, \
00389          .load = load_module,       \
00390          .unload = unload_module,      \
00391          .load_pri = AST_MODPRI_DEFAULT, \
00392              )
00393 #endif   /* plain C */
00394 
00395 /*! 
00396  * \brief Register an application.
00397  *
00398  * \param app Short name of the application
00399  * \param execute a function callback to execute the application. It should return
00400  *                non-zero if the channel needs to be hung up.
00401  * \param synopsis a short description (one line synopsis) of the application
00402  * \param description long description with all of the details about the use of 
00403  *                    the application
00404  * 
00405  * This registers an application with Asterisk's internal application list. 
00406  * \note The individual applications themselves are responsible for registering and unregistering
00407  *       and unregistering their own CLI commands.
00408  * 
00409  * \retval 0 success 
00410  * \retval -1 failure.
00411  */
00412 #define ast_register_application(app, execute, synopsis, description) ast_register_application2(app, execute, synopsis, description, ast_module_info->self)
00413 
00414 /*! 
00415  * \brief Register an application using XML documentation.
00416  *
00417  * \param app Short name of the application
00418  * \param execute a function callback to execute the application. It should return
00419  *                non-zero if the channel needs to be hung up.
00420  * 
00421  * This registers an application with Asterisk's internal application list. 
00422  * \note The individual applications themselves are responsible for registering and unregistering
00423  *       and unregistering their own CLI commands.
00424  * 
00425  * \retval 0 success 
00426  * \retval -1 failure.
00427  */
00428 #define ast_register_application_xml(app, execute) ast_register_application(app, execute, NULL, NULL)
00429 
00430 
00431 /*!
00432  * \brief Register an application.
00433  *
00434  * \param app Short name of the application
00435  * \param execute a function callback to execute the application. It should return
00436  *                non-zero if the channel needs to be hung up.
00437  * \param synopsis a short description (one line synopsis) of the application
00438  * \param description long description with all of the details about the use of
00439  *                    the application
00440  * \param mod module this application belongs to
00441  *
00442  * This registers an application with Asterisk's internal application list.
00443  * \note The individual applications themselves are responsible for registering and unregistering
00444  *       and unregistering their own CLI commands.
00445  *
00446  * \retval 0 success
00447  * \retval -1 failure.
00448  */
00449 int ast_register_application2(const char *app, int (*execute)(struct ast_channel *, const char *),
00450                  const char *synopsis, const char *description, void *mod);
00451 
00452 /*! 
00453  * \brief Unregister an application
00454  * 
00455  * \param app name of the application (does not have to be the same string as the one that was registered)
00456  * 
00457  * This unregisters an application from Asterisk's internal application list.
00458  * 
00459  * \retval 0 success 
00460  * \retval -1 failure
00461  */
00462 int ast_unregister_application(const char *app);
00463 
00464 
00465 #if defined(__cplusplus) || defined(c_plusplus)
00466 }
00467 #endif
00468 
00469 #endif /* _ASTERISK_MODULE_H */

Generated on Mon Jun 27 16:50:54 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7