Thu Apr 28 2011 16:56:41

Asterisk developer's documentation


app_skel.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) <Year>, <Your Name Here>
00005  *
00006  * <Your Name Here> <<Your Email Here>>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  *
00018  * Please follow coding guidelines 
00019  * http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES
00020  */
00021 
00022 /*! \file
00023  *
00024  * \brief Skeleton application
00025  *
00026  * \author\verbatim <Your Name Here> <<Your Email Here>> \endverbatim
00027  * 
00028  * This is a skeleton for development of an Asterisk application 
00029  * \ingroup applications
00030  */
00031 
00032 /*** MODULEINFO
00033    <defaultenabled>no</defaultenabled>
00034  ***/
00035 
00036 #include "asterisk.h"
00037 
00038 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 153365 $")
00039 
00040 #include "asterisk/file.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/lock.h"
00045 #include "asterisk/app.h"
00046 
00047 /*** DOCUMENTATION
00048    <application name="Skel" language="en_US">
00049       <synopsis>
00050          Simple one line explaination.
00051       </synopsis>
00052       <syntax>
00053          <parameter name="dummy" required="true"/>
00054          <parameter name="options">
00055             <optionlist>
00056                <option name="a">
00057                   <para>Option A.</para>
00058                </option>
00059                <option name="b">
00060                   <para>Option B.</para>
00061                </option>
00062                <option name="c">
00063                   <para>Option C.</para>
00064                </option>
00065             </optionlist>
00066          </parameter>
00067       </syntax>
00068       <description>
00069       <para>This application is a template to build other applications from. 
00070       It shows you the basic structure to create your own Asterisk applications.</para>
00071       </description>
00072    </application>
00073  ***/
00074 
00075 static char *app = "Skel";
00076 
00077 enum {
00078    OPTION_A = (1 << 0),
00079    OPTION_B = (1 << 1),
00080    OPTION_C = (1 << 2),
00081 } option_flags;
00082 
00083 enum {
00084    OPTION_ARG_B = 0,
00085    OPTION_ARG_C = 1,
00086    /* This *must* be the last value in this enum! */
00087    OPTION_ARG_ARRAY_SIZE = 2,
00088 } option_args;
00089 
00090 AST_APP_OPTIONS(app_opts,{
00091    AST_APP_OPTION('a', OPTION_A),
00092    AST_APP_OPTION_ARG('b', OPTION_B, OPTION_ARG_B),
00093    AST_APP_OPTION_ARG('c', OPTION_C, OPTION_ARG_C),
00094 });
00095 
00096 
00097 static int app_exec(struct ast_channel *chan, void *data)
00098 {
00099    int res = 0;
00100    struct ast_flags flags;
00101    char *parse, *opts[OPTION_ARG_ARRAY_SIZE];
00102    AST_DECLARE_APP_ARGS(args,
00103       AST_APP_ARG(dummy);
00104       AST_APP_ARG(options);
00105    );
00106 
00107    if (ast_strlen_zero(data)) {
00108       ast_log(LOG_WARNING, "%s requires an argument (dummy[,options])\n", app);
00109       return -1;
00110    }
00111 
00112    /* Do our thing here */
00113 
00114    /* We need to make a copy of the input string if we are going to modify it! */
00115    parse = ast_strdupa(data);
00116 
00117    AST_STANDARD_APP_ARGS(args, parse);
00118 
00119    if (args.argc == 2) {
00120       ast_app_parse_options(app_opts, &flags, opts, args.options);
00121    }
00122 
00123    if (!ast_strlen_zero(args.dummy)) {
00124       ast_log(LOG_NOTICE, "Dummy value is : %s\n", args.dummy);
00125    }
00126 
00127    if (ast_test_flag(&flags, OPTION_A)) {
00128       ast_log(LOG_NOTICE, "Option A is set\n");
00129    }
00130 
00131    if (ast_test_flag(&flags, OPTION_B)) {
00132       ast_log(LOG_NOTICE, "Option B is set with : %s\n", opts[OPTION_ARG_B] ? opts[OPTION_ARG_B] : "<unspecified>");
00133    }
00134 
00135    if (ast_test_flag(&flags, OPTION_C)) {
00136       ast_log(LOG_NOTICE, "Option C is set with : %s\n", opts[OPTION_ARG_C] ? opts[OPTION_ARG_C] : "<unspecified>");
00137    }
00138 
00139    return res;
00140 }
00141 
00142 static int unload_module(void)
00143 {
00144    return ast_unregister_application(app);
00145 }
00146 
00147 static int load_module(void)
00148 {
00149    return ast_register_application_xml(app, app_exec) ? 
00150       AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00151 }
00152 
00153 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Skeleton (sample) Application");