Tue Nov 4 13:20:20 2008

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 
00185 struct ast_module_info {
00186 
00187    /*!
00188     * The 'self' pointer for a module; it will be set by the loader before
00189     * it calls the module's load_module() entrypoint, and used by various
00190     * other macros that need to identify the module.
00191     */
00192 
00193    struct ast_module *self;
00194    enum ast_module_load_result (*load)(void);   /* register stuff etc. Optional. */
00195    int (*reload)(void);       /* config etc. Optional. */
00196    int (*unload)(void);       /* unload. called with the module locked */
00197    const char *name;       /* name of the module for loader reference and CLI commands */
00198    const char *description;      /* user friendly description of the module. */
00199 
00200    /*! 
00201     * This holds the ASTERISK_GPL_KEY, signifiying that you agree to the terms of
00202     * the Asterisk license as stated in the ASTERISK_GPL_KEY.  Your module will not
00203     * load if it does not return the EXACT key string.
00204     */
00205 
00206    const char *key;
00207    unsigned int flags;
00208 
00209    /*! The value of AST_BUILDOPT_SUM when this module was compiled */
00210    const char buildopt_sum[33];
00211 };
00212 
00213 void ast_module_register(const struct ast_module_info *);
00214 void ast_module_unregister(const struct ast_module_info *);
00215 
00216 struct ast_module_user *__ast_module_user_add(struct ast_module *, struct ast_channel *);
00217 void __ast_module_user_remove(struct ast_module *, struct ast_module_user *);
00218 void __ast_module_user_hangup_all(struct ast_module *);
00219 
00220 #define ast_module_user_add(chan) __ast_module_user_add(ast_module_info->self, chan)
00221 #define ast_module_user_remove(user) __ast_module_user_remove(ast_module_info->self, user)
00222 #define ast_module_user_hangup_all() __ast_module_user_hangup_all(ast_module_info->self)
00223 
00224 struct ast_module *ast_module_ref(struct ast_module *);
00225 void ast_module_unref(struct ast_module *);
00226 
00227 #if defined(__cplusplus) || defined(c_plusplus)
00228 #define AST_MODULE_INFO(keystr, flags_to_set, desc, load_func, unload_func, reload_func)  \
00229    static struct ast_module_info __mod_info = { \
00230       NULL,             \
00231       load_func,           \
00232       reload_func,            \
00233       unload_func,            \
00234       AST_MODULE,          \
00235       desc,             \
00236       keystr,              \
00237       flags_to_set | AST_MODFLAG_BUILDSUM,   \
00238       AST_BUILDOPT_SUM,       \
00239    };                \
00240    static void  __attribute__ ((constructor)) __reg_module(void) \
00241    { \
00242       ast_module_register(&__mod_info); \
00243    } \
00244    static void  __attribute__ ((destructor)) __unreg_module(void) \
00245    { \
00246       ast_module_unregister(&__mod_info); \
00247    } \
00248    const static __attribute__((unused)) struct ast_module_info *ast_module_info = &__mod_info
00249 
00250 #define AST_MODULE_INFO_STANDARD(keystr, desc)     \
00251    AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
00252          load_module,         \
00253          unload_module,    \
00254          NULL        \
00255              )
00256 #else
00257 /* forward declare this pointer in modules, so that macro/function
00258    calls that need it can get it, since it will actually be declared
00259    and populated at the end of the module's source file... */
00260 const static __attribute__((unused)) struct ast_module_info *ast_module_info;
00261 
00262 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...) \
00263    static struct ast_module_info __mod_info = {    \
00264       .name = AST_MODULE,           \
00265       .flags = flags_to_set | AST_MODFLAG_BUILDSUM,   \
00266       .description = desc,          \
00267       .key = keystr,             \
00268       .buildopt_sum = AST_BUILDOPT_SUM,      \
00269       fields                  \
00270    };                   \
00271    static void  __attribute__ ((constructor)) __reg_module(void) \
00272    { \
00273       ast_module_register(&__mod_info); \
00274    } \
00275    static void  __attribute__ ((destructor)) __unreg_module(void) \
00276    { \
00277       ast_module_unregister(&__mod_info); \
00278    } \
00279    const static struct ast_module_info *ast_module_info = &__mod_info
00280 
00281 #define AST_MODULE_INFO_STANDARD(keystr, desc)     \
00282    AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
00283          .load = load_module,       \
00284          .unload = unload_module,      \
00285              )
00286 #endif
00287 
00288 #if defined(__cplusplus) || defined(c_plusplus)
00289 }
00290 #endif
00291 
00292 #endif /* _ASTERISK_MODULE_H */

Generated on Tue Nov 4 13:20:20 2008 for Asterisk - the Open Source PBX by  doxygen 1.4.7