Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:47 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 * \brief Check if Channel is Available
    00022 *
    00023  * \ingroup applications
    00024  */
    00025
    00026 #include <stdlib.h>
    00027 #include <stdio.h>
    00028 #include <string.h>
    00029 #include <unistd.h>
    00030 #include <errno.h>
    00031 #include <sys/ioctl.h>
    00032
    00033 #include "asterisk.h"
    00034
    00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    00036
    00037 #include "asterisk/lock.h"
    00038 #include "asterisk/file.h"
    00039 #include "asterisk/logger.h"
    00040 #include "asterisk/channel.h"
    00041 #include "asterisk/pbx.h"
    00042 #include "asterisk/module.h"
    00043 #include "asterisk/app.h"
    00044 #include "asterisk/devicestate.h"
    00045 #include "asterisk/options.h"
    00046
    00047 static char *tdesc = "Check channel availability";
    00048
    00049 static char *app = "ChanIsAvail";
    00050
    00051 static char *synopsis = "Check channel availability";
    00052
    00053 static char *descrip =
    00054 "  ChanIsAvail(Technology/resource[&Technology2/resource2...][|options]): \n"
    00055 "This application will check to see if any of the specified channels are\n"
    00056 "available. The following variables will be set by this application:\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 "  Options:\n"
    00061 "    s - Consider the channel unavailable if the channel is in use at all\n"
    00062 "    j - Support jumping to priority n+101 if no channel is available\n";
    00063
    00064 STANDARD_LOCAL_USER;
    00065
    00066 LOCAL_USER_DECL;
    00067
    00068 static int chanavail_exec(struct ast_channel *chan, void *data)
    00069 {
    00070    int res=-1, inuse=-1, option_state=0, priority_jump=0;
    00071    int status;
    00072    struct localuser *u;
    00073    char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur, *options, *stringp;
    00074    struct ast_channel *tempchan;
    00075
    00076    if (ast_strlen_zero(data)) {
    00077       ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
    00078       return -1;
    00079    }
    00080
    00081    LOCAL_USER_ADD(u);
    00082
    00083    info = ast_strdupa(data);
    00084    stringp = info;
    00085    strsep(&stringp, "|");
    00086    options = strsep(&stringp, "|");
    00087    if (options) {
    00088       if (strchr(options, 's'))
    00089          option_state = 1;
    00090       if (strchr(options, 'j'))
    00091          priority_jump = 1;
    00092    }
    00093    peers = info;
    00094    if (peers) {
    00095       cur = peers;
    00096       do {
    00097          /* remember where to start next time */
    00098          rest = strchr(cur, '&');
    00099          if (rest) {
    00100             *rest = 0;
    00101             rest++;
    00102          }
    00103          tech = cur;
    00104          number = strchr(tech, '/');
    00105          if (!number) {
    00106             ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
    00107             LOCAL_USER_REMOVE(u);
    00108             return -1;
    00109          }
    00110          *number = '\0';
    00111          number++;
    00112
    00113          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       if (priority_jump || option_priority_jumping) {
    00143          if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
    00144             LOCAL_USER_REMOVE(u);
    00145             return -1;
    00146          }
    00147       }
    00148    }
    00149
    00150    LOCAL_USER_REMOVE(u);
    00151    return 0;
    00152 }
    00153
    00154 int unload_module(void)
    00155 {
    00156    int res = 0;
    00157
    00158    res = ast_unregister_application(app);
    00159
    00160    STANDARD_HANGUP_LOCALUSERS;
    00161
    00162    return res;
    00163 }
    00164
    00165 int load_module(void)
    00166 {
    00167    return ast_register_application(app, chanavail_exec, synopsis, descrip);
    00168 }
    00169
    00170 char *description(void)
    00171 {
    00172    return tdesc;
    00173 }
    00174
    00175 int usecount(void)
    00176 {
    00177    int res;
    00178    STANDARD_USECOUNT(res);
    00179    return res;
    00180 }
    00181
    00182 char *key()
    00183 {
    00184    return ASTERISK_GPL_KEY;
    00185 }