2003-03-16 06:00:11 +00:00
|
|
|
/*
|
2005-11-06 15:09:47 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
|
|
|
* James Golovich <james@gnuinter.net>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
2005-09-14 20:46:50 +00:00
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
|
|
|
* \brief Check if Channel is Available
|
|
|
|
*
|
|
|
|
* \author Mark Spencer <markster@digium.com>
|
|
|
|
* \author James Golovich <james@gnuinter.net>
|
|
|
|
|
2005-11-06 15:09:47 +00:00
|
|
|
* \ingroup applications
|
2003-03-16 06:00:11 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2005-06-06 22:39:32 +00:00
|
|
|
#include <stdlib.h>
|
2005-11-08 04:48:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2005-06-06 22:39:32 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/file.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/app.h"
|
2005-07-08 21:14:34 +00:00
|
|
|
#include "asterisk/devicestate.h"
|
2005-11-06 18:11:13 +00:00
|
|
|
#include "asterisk/options.h"
|
2003-03-16 06:00:11 +00:00
|
|
|
|
|
|
|
static char *app = "ChanIsAvail";
|
|
|
|
|
2005-11-10 21:39:01 +00:00
|
|
|
static char *synopsis = "Check channel availability";
|
2003-03-16 06:00:11 +00:00
|
|
|
|
|
|
|
static char *descrip =
|
2005-11-10 20:56:45 +00:00
|
|
|
" ChanIsAvail(Technology/resource[&Technology2/resource2...][|options]): \n"
|
|
|
|
"This application will check to see if any of the specified channels are\n"
|
|
|
|
"available. The following variables will be set by this application:\n"
|
|
|
|
" ${AVAILCHAN} - the name of the available channel, if one exists\n"
|
|
|
|
" ${AVAILORIGCHAN} - the canonical channel name that was used to create the channel\n"
|
|
|
|
" ${AVAILSTATUS} - the status code for the available channel\n"
|
|
|
|
" Options:\n"
|
|
|
|
" s - Consider the channel unavailable if the channel is in use at all\n"
|
|
|
|
" j - Support jumping to priority n+101 if no channel is available\n";
|
2003-03-16 06:00:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int chanavail_exec(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2005-11-06 18:11:13 +00:00
|
|
|
int res=-1, inuse=-1, option_state=0, priority_jump=0;
|
2004-10-26 22:25:43 +00:00
|
|
|
int status;
|
2006-08-21 02:11:39 +00:00
|
|
|
struct ast_module_user *u;
|
2005-11-30 20:15:36 +00:00
|
|
|
char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
|
2003-03-16 06:00:11 +00:00
|
|
|
struct ast_channel *tempchan;
|
2005-11-30 20:15:36 +00:00
|
|
|
AST_DECLARE_APP_ARGS(args,
|
|
|
|
AST_APP_ARG(reqchans);
|
|
|
|
AST_APP_ARG(options);
|
|
|
|
);
|
2003-03-16 06:00:11 +00:00
|
|
|
|
2005-10-26 19:48:14 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2003-03-16 06:00:11 +00:00
|
|
|
ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-10-19 18:19:02 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
u = ast_module_user_add(chan);
|
2003-03-16 06:00:11 +00:00
|
|
|
|
2005-10-19 18:19:02 +00:00
|
|
|
info = ast_strdupa(data);
|
2005-11-30 20:15:36 +00:00
|
|
|
|
|
|
|
AST_STANDARD_APP_ARGS(args, info);
|
|
|
|
|
|
|
|
if (args.options) {
|
|
|
|
if (strchr(args.options, 's'))
|
2005-11-06 18:11:13 +00:00
|
|
|
option_state = 1;
|
2005-11-30 20:15:36 +00:00
|
|
|
if (strchr(args.options, 'j'))
|
2005-11-06 18:11:13 +00:00
|
|
|
priority_jump = 1;
|
|
|
|
}
|
2005-11-30 20:15:36 +00:00
|
|
|
peers = args.reqchans;
|
2003-03-16 06:00:11 +00:00
|
|
|
if (peers) {
|
|
|
|
cur = peers;
|
|
|
|
do {
|
|
|
|
/* remember where to start next time */
|
|
|
|
rest = strchr(cur, '&');
|
|
|
|
if (rest) {
|
|
|
|
*rest = 0;
|
|
|
|
rest++;
|
|
|
|
}
|
|
|
|
tech = cur;
|
|
|
|
number = strchr(tech, '/');
|
|
|
|
if (!number) {
|
2004-02-29 18:30:48 +00:00
|
|
|
ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_module_user_remove(u);
|
2004-02-29 18:30:48 +00:00
|
|
|
return -1;
|
2003-03-16 06:00:11 +00:00
|
|
|
}
|
|
|
|
*number = '\0';
|
|
|
|
number++;
|
2004-11-15 16:27:28 +00:00
|
|
|
|
|
|
|
if (option_state) {
|
|
|
|
/* If the pbx says in use then don't bother trying further.
|
|
|
|
This is to permit testing if someone's on a call, even if the
|
|
|
|
channel can permit more calls (ie callwaiting, sip calls, etc). */
|
|
|
|
|
|
|
|
snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
|
|
|
|
status = inuse = ast_device_state(trychan);
|
|
|
|
}
|
2005-10-18 03:29:12 +00:00
|
|
|
if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
|
2003-03-18 17:03:33 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
|
2004-06-05 18:42:59 +00:00
|
|
|
/* Store the originally used channel too */
|
2004-06-06 17:36:57 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
|
|
|
|
pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
|
2004-10-26 22:25:43 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "%d", status);
|
|
|
|
pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
|
2003-03-16 06:00:11 +00:00
|
|
|
ast_hangup(tempchan);
|
|
|
|
tempchan = NULL;
|
|
|
|
res = 1;
|
|
|
|
break;
|
2004-10-26 22:25:43 +00:00
|
|
|
} else {
|
|
|
|
snprintf(tmp, sizeof(tmp), "%d", status);
|
|
|
|
pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
|
2003-03-16 06:00:11 +00:00
|
|
|
}
|
|
|
|
cur = rest;
|
|
|
|
} while (cur);
|
|
|
|
}
|
|
|
|
if (res < 1) {
|
2003-03-18 17:03:33 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
|
2004-06-05 18:42:59 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
|
2005-12-04 20:40:46 +00:00
|
|
|
if (priority_jump || ast_opt_priority_jumping) {
|
2005-11-06 18:11:13 +00:00
|
|
|
if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_module_user_remove(u);
|
2005-11-06 18:11:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2005-09-28 19:42:36 +00:00
|
|
|
}
|
2003-03-16 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_module_user_remove(u);
|
2003-03-16 06:00:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2003-03-16 06:00:11 +00:00
|
|
|
{
|
2005-10-18 22:52:21 +00:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
res = ast_unregister_application(app);
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_module_user_hangup_all();
|
2005-10-18 22:52:21 +00:00
|
|
|
|
|
|
|
return res;
|
2003-03-16 06:00:11 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2003-03-16 06:00:11 +00:00
|
|
|
{
|
|
|
|
return ast_register_application(app, chanavail_exec, synopsis, descrip);
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");
|