Thu Jul 9 13:40:17 2009

Asterisk developer's documentation


app_chanisavail.c

Go to the documentation of this file.
00001 /*
00002 * Asterisk -- An open source telephony toolkit.
00003 *
00004 * Copyright (C) 1999 - 2005, Digium, Inc.
00005 *
00006 * Mark Spencer <markster@digium.com>
00007 * James Golovich <james@gnuinter.net>
00008 *
00009 * See http://www.asterisk.org for more information about
00010 * the Asterisk project. Please do not directly contact
00011 * any of the maintainers of this project for assistance;
00012 * the project provides a web site, mailing lists and IRC
00013 * channels for your use.
00014 *
00015 * This program is free software, distributed under the terms of
00016 * the GNU General Public License Version 2. See the LICENSE file
00017 * at the top of the source tree.
00018 */
00019 
00020 /*! \file
00021  *
00022  * \brief Check if Channel is Available
00023  *
00024  * \author Mark Spencer <markster@digium.com>
00025  * \author James Golovich <james@gnuinter.net>
00026 
00027  * \ingroup applications
00028  */
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 123332 $")
00033 
00034 #include <sys/ioctl.h>
00035 
00036 #include "asterisk/lock.h"
00037 #include "asterisk/file.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/module.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/devicestate.h"
00043 
00044 static char *app = "ChanIsAvail";
00045 
00046 static char *synopsis = "Check channel availability";
00047 
00048 static char *descrip =
00049 "  ChanIsAvail(Technology/resource[&Technology2/resource2...][,options]): \n"
00050 "This application will check to see if any of the specified channels are\n"
00051 "available.\n"
00052 "  Options:\n"
00053 "    s - Consider the channel unavailable if the channel is in use at all.\n"
00054 "    t - Simply checks if specified channels exist in the channel list\n"
00055 "        (implies option s).\n"
00056 "This application sets the following channel variable upon completion:\n"
00057 "  AVAILCHAN     - the name of the available channel, if one exists\n"
00058 "  AVAILORIGCHAN - the canonical channel name that was used to create the channel\n"
00059 "  AVAILSTATUS   - the status code for the available channel\n";
00060 
00061 
00062 static int chanavail_exec(struct ast_channel *chan, void *data)
00063 {
00064    int res=-1, inuse=-1, option_state=0, string_compare=0;
00065    int status;
00066    char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
00067    struct ast_channel *tempchan;
00068    AST_DECLARE_APP_ARGS(args,
00069       AST_APP_ARG(reqchans);
00070       AST_APP_ARG(options);
00071    );
00072 
00073    if (ast_strlen_zero(data)) {
00074       ast_log(LOG_WARNING, "ChanIsAvail requires an argument (DAHDI/1&DAHDI/2)\n");
00075       return -1;
00076    }
00077 
00078    info = ast_strdupa(data);
00079 
00080    AST_STANDARD_APP_ARGS(args, info);
00081 
00082    if (args.options) {
00083       if (strchr(args.options, 's'))
00084          option_state = 1;
00085       if (strchr(args.options, 't'))
00086          string_compare = 1;
00087    }
00088    peers = args.reqchans;
00089    if (peers) {
00090       cur = peers;
00091       do {
00092          /* remember where to start next time */
00093          rest = strchr(cur, '&');
00094          if (rest) {
00095             *rest = 0;
00096             rest++;
00097          }
00098          tech = cur;
00099          number = strchr(tech, '/');
00100          if (!number) {
00101             ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
00102             return -1;
00103          }
00104          *number = '\0';
00105          number++;
00106          
00107          if (string_compare) {
00108             /* ast_parse_device_state checks for "SIP/1234" as a channel name.
00109                ast_device_state will ask the SIP driver for the channel state. */
00110 
00111             snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
00112             status = inuse = ast_parse_device_state(trychan);
00113          } else if (option_state) {
00114             /* If the pbx says in use then don't bother trying further.
00115                This is to permit testing if someone's on a call, even if the
00116                channel can permit more calls (ie callwaiting, sip calls, etc).  */
00117 
00118             snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
00119             status = inuse = ast_device_state(trychan);
00120          }
00121          if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
00122                pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
00123                /* Store the originally used channel too */
00124                snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
00125                pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
00126                snprintf(tmp, sizeof(tmp), "%d", status);
00127                pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
00128                ast_hangup(tempchan);
00129                tempchan = NULL;
00130                res = 1;
00131                break;
00132          } else {
00133             snprintf(tmp, sizeof(tmp), "%d", status);
00134             pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
00135          }
00136          cur = rest;
00137       } while (cur);
00138    }
00139    if (res < 1) {
00140       pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
00141       pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
00142    }
00143 
00144    return 0;
00145 }
00146 
00147 static int unload_module(void)
00148 {
00149    return ast_unregister_application(app);
00150 }
00151 
00152 static int load_module(void)
00153 {
00154    return ast_register_application(app, chanavail_exec, synopsis, descrip);
00155 }
00156 
00157 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");

Generated on Thu Jul 9 13:40:17 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7