Thu Apr 28 2011 16:56:39

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: 229969 $")
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 /*** DOCUMENTATION
00047    <application name="ChanIsAvail" language="en_US">
00048       <synopsis>
00049          Check channel availability
00050       </synopsis>
00051       <syntax>
00052          <parameter name="Technology/Resource" required="true" argsep="&amp;">
00053             <argument name="Technology2/Resource2" multiple="true">
00054                <para>Optional extra devices to check</para>
00055                <para>If you need more then one enter them as
00056                Technology2/Resource2&amp;Technology3/Resourse3&amp;.....</para>
00057             </argument>
00058             <para>Specification of the device(s) to check.  These must be in the format of 
00059             <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
00060             represents a particular channel driver, and <replaceable>Resource</replaceable>
00061             represents a resource available to that particular channel driver.</para>
00062          </parameter>
00063          <parameter name="options" required="false">
00064             <optionlist>
00065                <option name="a">
00066                   <para>Check for all available channels, not only the first one</para>
00067                </option>
00068                <option name="s">
00069                   <para>Consider the channel unavailable if the channel is in use at all</para>
00070                </option>
00071                <option name="t" implies="s">
00072                   <para>Simply checks if specified channels exist in the channel list</para>
00073                </option>
00074             </optionlist>
00075          </parameter>
00076       </syntax>
00077       <description>
00078          <para>This application will check to see if any of the specified channels are available.</para>
00079          <para>This application sets the following channel variables:</para>
00080          <variablelist>
00081             <variable name="AVAILCHAN">
00082                <para>The name of the available channel, if one exists</para>
00083             </variable>
00084             <variable name="AVAILORIGCHAN">
00085                <para>The canonical channel name that was used to create the channel</para>
00086             </variable>
00087             <variable name="AVAILSTATUS">
00088                <para>The status code for the available channel. This is used for both
00089                device state and cause code. It is recommended that you use AVAILORIGCHAN
00090                instead to see if a device is available or not.</para>
00091             </variable>
00092          </variablelist>
00093       </description>
00094    </application>
00095  ***/
00096 
00097 static int chanavail_exec(struct ast_channel *chan, void *data)
00098 {
00099    int inuse=-1, option_state=0, string_compare=0, option_all_avail=0;
00100    int status;
00101    char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
00102    struct ast_str *tmp_availchan = ast_str_alloca(2048);
00103    struct ast_str *tmp_availorig = ast_str_alloca(2048);
00104    struct ast_str *tmp_availstat = ast_str_alloca(2048);
00105    struct ast_channel *tempchan;
00106    AST_DECLARE_APP_ARGS(args,
00107       AST_APP_ARG(reqchans);
00108       AST_APP_ARG(options);
00109    );
00110 
00111    if (ast_strlen_zero(data)) {
00112       ast_log(LOG_WARNING, "ChanIsAvail requires an argument (DAHDI/1&DAHDI/2)\n");
00113       return -1;
00114    }
00115 
00116    info = ast_strdupa(data);
00117 
00118    AST_STANDARD_APP_ARGS(args, info);
00119 
00120    if (args.options) {
00121       if (strchr(args.options, 'a')) {
00122          option_all_avail = 1;
00123       }
00124       if (strchr(args.options, 's')) {
00125          option_state = 1;
00126       }
00127       if (strchr(args.options, 't')) {
00128          string_compare = 1;
00129       }
00130    }
00131    peers = args.reqchans;
00132    if (peers) {
00133       cur = peers;
00134       do {
00135          /* remember where to start next time */
00136          rest = strchr(cur, '&');
00137          if (rest) {
00138             *rest = 0;
00139             rest++;
00140          }
00141          tech = cur;
00142          number = strchr(tech, '/');
00143          if (!number) {
00144             ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
00145             return -1;
00146          }
00147          *number = '\0';
00148          number++;
00149          
00150          if (string_compare) {
00151             /* ast_parse_device_state checks for "SIP/1234" as a channel name.
00152                ast_device_state will ask the SIP driver for the channel state. */
00153 
00154             snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
00155             status = inuse = ast_parse_device_state(trychan);
00156          } else if (option_state) {
00157             /* If the pbx says in use then don't bother trying further.
00158                This is to permit testing if someone's on a call, even if the
00159                channel can permit more calls (ie callwaiting, sip calls, etc).  */
00160 
00161             snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
00162             status = inuse = ast_device_state(trychan);
00163          }
00164          if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
00165                ast_str_append(&tmp_availchan, 0, "%s%s", ast_str_strlen(tmp_availchan) ? "&" : "", tempchan->name);
00166                
00167                snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
00168                ast_str_append(&tmp_availorig, 0, "%s%s", ast_str_strlen(tmp_availorig) ? "&" : "", tmp);
00169 
00170                snprintf(tmp, sizeof(tmp), "%d", status);
00171                ast_str_append(&tmp_availstat, 0, "%s%s", ast_str_strlen(tmp_availstat) ? "&" : "", tmp);
00172 
00173                ast_hangup(tempchan);
00174                tempchan = NULL;
00175 
00176                if (!option_all_avail) {
00177                   break;
00178                }
00179          } else {
00180             snprintf(tmp, sizeof(tmp), "%d", status);
00181             ast_str_append(&tmp_availstat, 0, "%s%s", ast_str_strlen(tmp_availstat) ? "&" : "", tmp);
00182          }
00183          cur = rest;
00184       } while (cur);
00185    }
00186 
00187    pbx_builtin_setvar_helper(chan, "AVAILCHAN", ast_str_buffer(tmp_availchan));
00188    /* Store the originally used channel too */
00189    pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", ast_str_buffer(tmp_availorig));
00190    pbx_builtin_setvar_helper(chan, "AVAILSTATUS", ast_str_buffer(tmp_availstat));
00191 
00192    return 0;
00193 }
00194 
00195 static int unload_module(void)
00196 {
00197    return ast_unregister_application(app);
00198 }
00199 
00200 static int load_module(void)
00201 {
00202    return ast_register_application_xml(app, chanavail_exec) ?
00203       AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00204 }
00205 
00206 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");