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

Generated on Thu Apr 16 06:27:08 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6