Wed Oct 28 11:45:41 2009

Asterisk developer's documentation


res_clioriginate.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005 - 2006, Digium, Inc.
00005  *
00006  * Russell Bryant <russell@digium.com>
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 /*! 
00020  * \file
00021  * \author Russell Bryant <russell@digium.com>
00022  *
00023  * \brief Originate calls via the CLI
00024  * 
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 102379 $");
00030 
00031 #include "asterisk/channel.h"
00032 #include "asterisk/pbx.h"
00033 #include "asterisk/module.h"
00034 #include "asterisk/cli.h"
00035 #include "asterisk/utils.h"
00036 #include "asterisk/frame.h"
00037 
00038 /*! The timeout for originated calls, in seconds */
00039 #define TIMEOUT 30
00040 
00041 /*!
00042  * \brief orginate a call from the CLI
00043  * \param fd file descriptor for cli
00044  * \param chan channel to create type/data
00045  * \param app application you want to run
00046  * \param appdata data for application
00047  * \retval CLI_SUCCESS on success.
00048  * \retval CLI_SHOWUSAGE on failure.
00049 */
00050 static char *orig_app(int fd, const char *chan, const char *app, const char *appdata)
00051 {
00052    char *chantech;
00053    char *chandata;
00054    int reason = 0;
00055    
00056    if (ast_strlen_zero(app))
00057       return CLI_SHOWUSAGE;
00058 
00059    chandata = ast_strdupa(chan);
00060    
00061    chantech = strsep(&chandata, "/");
00062    if (!chandata) {
00063       ast_cli(fd, "*** No data provided after channel type! ***\n");
00064       return CLI_SHOWUSAGE;
00065    }
00066 
00067    ast_pbx_outgoing_app(chantech, AST_FORMAT_SLINEAR, chandata, TIMEOUT * 1000, app, appdata, &reason, 0, NULL, NULL, NULL, NULL, NULL);
00068 
00069    return CLI_SUCCESS;
00070 }
00071 
00072 /*!
00073  * \brief orginate from extension
00074  * \param fd file descriptor for cli
00075  * \param chan channel to create type/data
00076  * \param data contains exten\@context
00077  * \retval CLI_SUCCESS on success.
00078  * \retval CLI_SHOWUSAGE on failure.
00079 */
00080 static char *orig_exten(int fd, const char *chan, const char *data)
00081 {
00082    char *chantech;
00083    char *chandata;
00084    char *exten = NULL;
00085    char *context = NULL;
00086    int reason = 0;
00087 
00088    chandata = ast_strdupa(chan);
00089    
00090    chantech = strsep(&chandata, "/");
00091    if (!chandata) {
00092       ast_cli(fd, "*** No data provided after channel type! ***\n");
00093       return CLI_SHOWUSAGE;
00094    }
00095 
00096    if (!ast_strlen_zero(data)) {
00097       context = ast_strdupa(data);
00098       exten = strsep(&context, "@");
00099    }
00100 
00101    if (ast_strlen_zero(exten))
00102       exten = "s";
00103    if (ast_strlen_zero(context))
00104       context = "default";
00105    
00106    ast_pbx_outgoing_exten(chantech, AST_FORMAT_SLINEAR, chandata, TIMEOUT * 1000, context, exten, 1, &reason, 0, NULL, NULL, NULL, NULL, NULL);
00107 
00108    return CLI_SUCCESS;
00109 }
00110 
00111 /*!
00112  * \brief handle for orgination app or exten.
00113  * \param e pointer to the CLI structure to initialize
00114  * \param cmd operation to execute
00115  * \param a structure that contains either application or extension arguments
00116  * \retval CLI_SUCCESS on success.
00117  * \retval CLI_SHOWUSAGE on failure.
00118 */
00119 static char *handle_orig(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00120 {
00121    static char *choices[] = { "application", "extension", NULL };
00122    char *res;
00123    switch (cmd) {
00124    case CLI_INIT:
00125       e->command = "originate";
00126       e->usage = 
00127          "  There are two ways to use this command. A call can be originated between a\n"
00128          "channel and a specific application, or between a channel and an extension in\n"
00129          "the dialplan. This is similar to call files or the manager originate action.\n"
00130          "Calls originated with this command are given a timeout of 30 seconds.\n\n"
00131 
00132          "Usage1: originate <tech/data> application <appname> [appdata]\n"
00133          "  This will originate a call between the specified channel tech/data and the\n"
00134          "given application. Arguments to the application are optional. If the given\n"
00135          "arguments to the application include spaces, all of the arguments to the\n"
00136          "application need to be placed in quotation marks.\n\n"
00137 
00138          "Usage2: originate <tech/data> extension [exten@][context]\n"
00139          "  This will originate a call between the specified channel tech/data and the\n"
00140          "given extension. If no context is specified, the 'default' context will be\n"
00141          "used. If no extension is given, the 's' extension will be used.\n";
00142       return NULL;
00143    case CLI_GENERATE:
00144       if (a->pos != 2)
00145          return NULL;
00146 
00147       /* ugly, can be removed when CLI entries have ast_module pointers */
00148       ast_module_ref(ast_module_info->self);
00149       res = ast_cli_complete(a->word, choices, a->n);
00150       ast_module_unref(ast_module_info->self);
00151 
00152       return res;
00153    }
00154 
00155    if (ast_strlen_zero(a->argv[1]) || ast_strlen_zero(a->argv[2]))
00156       return CLI_SHOWUSAGE;
00157 
00158    /* ugly, can be removed when CLI entries have ast_module pointers */
00159    ast_module_ref(ast_module_info->self);
00160 
00161    if (!strcasecmp("application", a->argv[2])) {
00162       res = orig_app(a->fd, a->argv[1], a->argv[3], a->argv[4]);  
00163    } else if (!strcasecmp("extension", a->argv[2])) {
00164       res = orig_exten(a->fd, a->argv[1], a->argv[3]);
00165    } else
00166       res = CLI_SHOWUSAGE;
00167 
00168    ast_module_unref(ast_module_info->self);
00169 
00170    return res;
00171 }
00172 
00173 static struct ast_cli_entry cli_cliorig[] = {
00174    AST_CLI_DEFINE(handle_orig, "Originate a call"),
00175 };
00176 
00177 /*! \brief Unload orginate module */
00178 static int unload_module(void)
00179 {
00180    return ast_cli_unregister_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig));
00181 }
00182 
00183 /*! \brief Load orginate module */
00184 static int load_module(void)
00185 {
00186    int res;
00187    res = ast_cli_register_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig));
00188    return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00189 }
00190 
00191 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call origination from the CLI");

Generated on Wed Oct 28 11:45:41 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6