Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:48 2009

## 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
    00019 /*! \file
    00020  *
    00021  * \brief Skeleton application
    00022  *
    00023  * This is a skeleton for development of an Asterisk application
    00024  * \ingroup applications
    00025  */
    00026
    00027 #include <stdio.h>
    00028 #include <stdlib.h>
    00029 #include <unistd.h>
    00030 #include <string.h>
    00031
    00032 #include "asterisk.h"
    00033
    00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 23899 $")
    00035
    00036 #include "asterisk/file.h"
    00037 #include "asterisk/logger.h"
    00038 #include "asterisk/channel.h"
    00039 #include "asterisk/pbx.h"
    00040 #include "asterisk/module.h"
    00041 #include "asterisk/lock.h"
    00042 #include "asterisk/app.h"
    00043
    00044 static char *tdesc = "Trivial skeleton Application";
    00045 static char *app = "Skel";
    00046 static char *synopsis =
    00047 "Skeleton application.";
    00048 static char *descrip = "This application is a template to build other applications from.\n"
    00049  " It shows you the basic structure to create your own Asterisk applications.\n";
    00050
    00051 #define OPTION_A  (1 << 0) /* Option A */
    00052 #define OPTION_B  (1 << 1) /* Option B(n) */
    00053 #define OPTION_C  (1 << 2) /* Option C(str) */
    00054 #define OPTION_NULL  (1 << 3) /* Dummy Termination */
    00055
    00056 AST_APP_OPTIONS(app_opts,{
    00057    ['a'] = { OPTION_A },
    00058    ['b'] = { OPTION_B, 1 },
    00059    ['c'] = { OPTION_C, 2 }
    00060 });
    00061
    00062 STANDARD_LOCAL_USER;
    00063
    00064 LOCAL_USER_DECL;
    00065
    00066 static int app_exec(struct ast_channel *chan, void *data)
    00067 {
    00068    int res = 0;
    00069    struct ast_flags flags;
    00070    struct localuser *u;
    00071    char *options=NULL;
    00072    char *dummy = NULL;
    00073    char *args;
    00074    int argc = 0;
    00075    char *opts[2];
    00076    char *argv[2];
    00077
    00078    if (ast_strlen_zero(data)) {
    00079       ast_log(LOG_WARNING, "%s requires an argument (dummy|[options])\n",app);
    00080       return -1;
    00081    }
    00082
    00083    LOCAL_USER_ADD(u);
    00084
    00085    /* Do our thing here */
    00086
    00087    /* We need to make a copy of the input string if we are going to modify it! */
    00088    args = ast_strdupa(data);
    00089    if (!args) {
    00090       ast_log(LOG_ERROR, "Out of memory!\n");
    00091       LOCAL_USER_REMOVE(u);
    00092       return -1;
    00093    }
    00094
    00095    if ((argc = ast_app_separate_args(args, '|', argv, sizeof(argv) / sizeof(argv[0])))) {
    00096       dummy = argv[0];
    00097       options = argv[1];
    00098       ast_app_parse_options(app_opts, &flags, opts, options);
    00099    }
    00100
    00101    if (!ast_strlen_zero(dummy))
    00102       ast_log(LOG_NOTICE, "Dummy value is : %s\n", dummy);
    00103
    00104    if (ast_test_flag(&flags, OPTION_A))
    00105       ast_log(LOG_NOTICE, "Option A is set\n");
    00106
    00107    if (ast_test_flag(&flags, OPTION_B))
    00108       ast_log(LOG_NOTICE,"Option B is set with : %s\n", opts[0] ? opts[0] : "<unspecified>");
    00109
    00110    if (ast_test_flag(&flags, OPTION_C))
    00111       ast_log(LOG_NOTICE,"Option C is set with : %s\n", opts[1] ? opts[1] : "<unspecified>");
    00112
    00113    LOCAL_USER_REMOVE(u);
    00114
    00115    return res;
    00116 }
    00117
    00118 int unload_module(void)
    00119 {
    00120    int res;
    00121
    00122    res = ast_unregister_application(app);
    00123
    00124    STANDARD_HANGUP_LOCALUSERS;
    00125
    00126    return res;
    00127 }
    00128
    00129 int load_module(void)
    00130 {
    00131    return ast_register_application(app, app_exec, synopsis, descrip);
    00132 }
    00133
    00134 char *description(void)
    00135 {
    00136    return tdesc;
    00137 }
    00138
    00139 int usecount(void)
    00140 {
    00141    int res;
    00142    STANDARD_USECOUNT(res);
    00143    return res;
    00144 }
    00145
    00146 char *key()
    00147 {
    00148    return ASTERISK_GPL_KEY;
    00149 }