Sat Aug 6 00:39:30 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 - 2006, 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_FAILURE = -1, /*!< Module could not be loaded properly */
00065 };
00066 
00067 /*! 
00068  * \brief Load a module.
00069  * \param resource_name The name of the module to load.
00070  *
00071  * This function is run by the PBX to load the modules.  It performs
00072  * all loading and initilization tasks.   Basically, to load a module, just
00073  * give it the name of the module and it will do the rest.
00074  *
00075  * \return See possible enum values for ast_module_load_result.
00076  */
00077 enum ast_module_load_result ast_load_resource(const char *resource_name);
00078 
00079 /*! 
00080  * \brief Unload a module.
00081  * \param resource_name The name of the module to unload.
00082  * \param ast_module_unload_mode The force flag. This should be set using one of the AST_FORCE flags.
00083  *
00084  * This function unloads a module.  It will only unload modules that are not in
00085  * use (usecount not zero), unless #AST_FORCE_FIRM or #AST_FORCE_HARD is 
00086  * specified.  Setting #AST_FORCE_FIRM or #AST_FORCE_HARD will unload the
00087  * module regardless of consequences (NOT RECOMMENDED).
00088  *
00089  * \return Zero on success, -1 on error.
00090  */
00091 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode);
00092 
00093 /*! 
00094  * \brief Notify when usecount has been changed.
00095  *
00096  * This function calulates use counts and notifies anyone trying to keep track
00097  * of them.  It should be called whenever your module's usecount changes.
00098  *
00099  * \note The LOCAL_USER macros take care of calling this function for you.
00100  */
00101 void ast_update_use_count(void);
00102 
00103 /*! 
00104  * \brief Ask for a list of modules, descriptions, and use counts.
00105  * \param modentry A callback to an updater function.
00106  * \param like
00107  *
00108  * For each of the modules loaded, modentry will be executed with the resource,
00109  * description, and usecount values of each particular module.
00110  * 
00111  * \return the number of modules loaded
00112  */
00113 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
00114             const char *like);
00115 
00116 /*! 
00117  * \brief Add a procedure to be run when modules have been updated.
00118  * \param updater The function to run when modules have been updated.
00119  *
00120  * This function adds the given function to a linked list of functions to be
00121  * run when the modules are updated. 
00122  *
00123  * \return Zero on success and -1 on failure.
00124  */
00125 int ast_loader_register(int (*updater)(void));
00126 
00127 /*! 
00128  * \brief Remove a procedure to be run when modules are updated.
00129  * \param updater The updater function to unregister.
00130  *
00131  * This removes the given function from the updater list.
00132  * 
00133  * \return Zero on success, -1 on failure.
00134  */
00135 int ast_loader_unregister(int (*updater)(void));
00136 
00137 /*!
00138  * \brief Run the unload() callback for all loaded modules
00139  *
00140  * This function should be called when Asterisk is shutting down gracefully.
00141  */
00142 void ast_module_shutdown(void);
00143 
00144 /*! 
00145  * \brief Match modules names for the Asterisk cli.
00146  * \param line Unused by this function, but this should be the line we are
00147  *        matching.
00148  * \param word The partial name to match. 
00149  * \param pos The position the word we are completing is in.
00150  * \param state The possible match to return.
00151  * \param rpos The position we should be matching.  This should be the same as
00152  *        pos.
00153  * \param needsreload This should be 1 if we need to reload this module and 0
00154  *        otherwise.  This function will only return modules that are reloadble
00155  *        if this is 1.
00156  *
00157  * \return A possible completion of the partial match, or NULL if no matches
00158  * were found.
00159  */
00160 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload);
00161 
00162 /* Opaque type for module handles generated by the loader */
00163 
00164 struct ast_module;
00165 
00166 /* User count routines keep track of which channels are using a given module
00167    resource.  They can help make removing modules safer, particularly if
00168    they're in use at the time they have been requested to be removed */
00169 
00170 struct ast_module_user;
00171 struct ast_module_user_list;
00172 
00173 /*! \page ModMngmnt The Asterisk Module management interface
00174  *
00175  * All modules must implement the module API (load, unload...)
00176  * whose functions are exported through fields of a "struct module_symbol";
00177  */
00178 
00179 enum ast_module_flags {
00180    AST_MODFLAG_DEFAULT = 0,
00181    AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0),
00182    AST_MODFLAG_BUILDSUM = (1 << 1),
00183    /*!
00184     * \brief Load this module in the first pass on auto loading
00185     *
00186     * When module auto loading is used, modules with this flag set will
00187     * be loaded after preloaded modules, but before all modules being
00188     * automatically loaded without this flag set on them.
00189     */
00190    AST_MODFLAG_LOAD_FIRST = (1 << 2),
00191 };
00192 
00193 struct ast_module_info {
00194 
00195    /*!
00196     * The 'self' pointer for a module; it will be set by the loader before
00197     * it calls the module's load_module() entrypoint, and used by various
00198     * other macros that need to identify the module.
00199     */
00200 
00201    struct ast_module *self;
00202    enum ast_module_load_result (*load)(void);   /* register stuff etc. Optional. */
00203    int (*reload)(void);       /* config etc. Optional. */
00204    int (*unload)(void);       /* unload. called with the module locked */
00205    const char *name;       /* name of the module for loader reference and CLI commands */
00206    const char *description;      /* user friendly description of the module. */
00207 
00208    /*! 
00209     * This holds the ASTERISK_GPL_KEY, signifiying that you agree to the terms of
00210     * the Asterisk license as stated in the ASTERISK_GPL_KEY.  Your module will not
00211     * load if it does not return the EXACT key string.
00212     */
00213 
00214    const char *key;
00215    unsigned int flags;
00216 
00217    /*! The value of AST_BUILDOPT_SUM when this module was compiled */
00218    const char buildopt_sum[33];
00219 };
00220 
00221 void ast_module_register(const struct ast_module_info *);
00222 void ast_module_unregister(const struct ast_module_info *);
00223 
00224 struct ast_module_user *__ast_module_user_add(struct ast_module *, struct ast_channel *);
00225 void __ast_module_user_remove(struct ast_module *, struct ast_module_user *);
00226 void __ast_module_user_hangup_all(struct ast_module *);
00227 
00228 #define ast_module_user_add(chan) __ast_module_user_add(ast_module_info->self, chan)
00229 #define ast_module_user_remove(user) __ast_module_user_remove(ast_module_info->self, user)
00230 #define ast_module_user_hangup_all() __ast_module_user_hangup_all(ast_module_info->self)
00231 
00232 struct ast_module *ast_module_ref(struct ast_module *);
00233 void ast_module_unref(struct ast_module *);
00234 
00235 #if defined(__cplusplus) || defined(c_plusplus)
00236 #define AST_MODULE_INFO(keystr, flags_to_set, desc, load_func, unload_func, reload_func)  \
00237    static struct ast_module_info __mod_info = { \
00238       NULL,             \
00239       load_func,           \
00240       reload_func,            \
00241       unload_func,            \
00242       AST_MODULE,          \
00243       desc,             \
00244       keystr,              \
00245       flags_to_set | AST_MODFLAG_BUILDSUM,   \
00246       AST_BUILDOPT_SUM,       \
00247    };                \
00248    static void  __attribute__((constructor)) __reg_module(void) \
00249    { \
00250       ast_module_register(&__mod_info); \
00251    } \
00252    static void  __attribute__((destructor)) __unreg_module(void) \
00253    { \
00254       ast_module_unregister(&__mod_info); \
00255    } \
00256    const static __attribute__((unused)) struct ast_module_info *ast_module_info = &__mod_info
00257 
00258 #define AST_MODULE_INFO_STANDARD(keystr, desc)     \
00259    AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
00260          load_module,         \
00261          unload_module,    \
00262          NULL        \
00263              )
00264 #else
00265 /* forward declare this pointer in modules, so that macro/function
00266    calls that need it can get it, since it will actually be declared
00267    and populated at the end of the module's source file... */
00268 const static __attribute__((unused)) struct ast_module_info *ast_module_info;
00269 
00270 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...) \
00271    static struct ast_module_info __mod_info = {    \
00272       .name = AST_MODULE,           \
00273       .flags = flags_to_set | AST_MODFLAG_BUILDSUM,   \
00274       .description = desc,          \
00275       .key = keystr,             \
00276       .buildopt_sum = AST_BUILDOPT_SUM,      \
00277       fields                  \
00278    };                   \
00279    static void  __attribute__((constructor)) __reg_module(void) \
00280    { \
00281       ast_module_register(&__mod_info); \
00282    } \
00283    static void  __attribute__((destructor)) __unreg_module(void) \
00284    { \
00285       ast_module_unregister(&__mod_info); \
00286    } \
00287    const static struct ast_module_info *ast_module_info = &__mod_info
00288 
00289 #define AST_MODULE_INFO_STANDARD(keystr, desc)     \
00290    AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
00291          .load = load_module,       \
00292          .unload = unload_module,      \
00293              )
00294 #endif
00295 
00296 #if defined(__cplusplus) || defined(c_plusplus)
00297 }
00298 #endif
00299 
00300 #endif /* _ASTERISK_MODULE_H */

Generated on Sat Aug 6 00:39:30 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7