2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * James Golovich <james@gnuinter.net>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Check if Channel is Available
24 * \author Mark Spencer <markster@digium.com>
25 * \author James Golovich <james@gnuinter.net>
27 * \ingroup applications
32 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
39 #include <sys/ioctl.h>
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/app.h"
48 #include "asterisk/devicestate.h"
49 #include "asterisk/options.h"
51 static char *app
= "ChanIsAvail";
53 static char *synopsis
= "Check channel availability";
55 static char *descrip
=
56 " ChanIsAvail(Technology/resource[&Technology2/resource2...][|options]): \n"
57 "This application will check to see if any of the specified channels are\n"
58 "available. The following variables will be set by this application:\n"
59 " ${AVAILCHAN} - the name of the available channel, if one exists\n"
60 " ${AVAILORIGCHAN} - the canonical channel name that was used to create the channel\n"
61 " ${AVAILSTATUS} - the status code for the available channel\n"
63 " s - Consider the channel unavailable if the channel is in use at all\n"
64 " j - Support jumping to priority n+101 if no channel is available\n";
67 static int chanavail_exec(struct ast_channel
*chan
, void *data
)
69 int res
=-1, inuse
=-1, option_state
=0, priority_jump
=0;
71 struct ast_module_user
*u
;
72 char *info
, tmp
[512], trychan
[512], *peers
, *tech
, *number
, *rest
, *cur
;
73 struct ast_channel
*tempchan
;
74 AST_DECLARE_APP_ARGS(args
,
75 AST_APP_ARG(reqchans
);
79 if (ast_strlen_zero(data
)) {
80 ast_log(LOG_WARNING
, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
84 u
= ast_module_user_add(chan
);
86 info
= ast_strdupa(data
);
88 AST_STANDARD_APP_ARGS(args
, info
);
91 if (strchr(args
.options
, 's'))
93 if (strchr(args
.options
, 'j'))
96 peers
= args
.reqchans
;
100 /* remember where to start next time */
101 rest
= strchr(cur
, '&');
107 number
= strchr(tech
, '/');
109 ast_log(LOG_WARNING
, "ChanIsAvail argument takes format ([technology]/[device])\n");
110 ast_module_user_remove(u
);
117 /* If the pbx says in use then don't bother trying further.
118 This is to permit testing if someone's on a call, even if the
119 channel can permit more calls (ie callwaiting, sip calls, etc). */
121 snprintf(trychan
, sizeof(trychan
), "%s/%s",cur
,number
);
122 status
= inuse
= ast_device_state(trychan
);
124 if ((inuse
<= 1) && (tempchan
= ast_request(tech
, chan
->nativeformats
, number
, &status
))) {
125 pbx_builtin_setvar_helper(chan
, "AVAILCHAN", tempchan
->name
);
126 /* Store the originally used channel too */
127 snprintf(tmp
, sizeof(tmp
), "%s/%s", tech
, number
);
128 pbx_builtin_setvar_helper(chan
, "AVAILORIGCHAN", tmp
);
129 snprintf(tmp
, sizeof(tmp
), "%d", status
);
130 pbx_builtin_setvar_helper(chan
, "AVAILSTATUS", tmp
);
131 ast_hangup(tempchan
);
136 snprintf(tmp
, sizeof(tmp
), "%d", status
);
137 pbx_builtin_setvar_helper(chan
, "AVAILSTATUS", tmp
);
143 pbx_builtin_setvar_helper(chan
, "AVAILCHAN", "");
144 pbx_builtin_setvar_helper(chan
, "AVAILORIGCHAN", "");
145 if (priority_jump
|| ast_opt_priority_jumping
) {
146 if (ast_goto_if_exists(chan
, chan
->context
, chan
->exten
, chan
->priority
+ 101)) {
147 ast_module_user_remove(u
);
153 ast_module_user_remove(u
);
157 static int unload_module(void)
161 res
= ast_unregister_application(app
);
163 ast_module_user_hangup_all();
168 static int load_module(void)
170 return ast_register_application(app
, chanavail_exec
, synopsis
, descrip
);
173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Check channel availability");