2 * Asterisk -- A telephony toolkit for Linux.
4 * Check if Channel is Available
6 * Copyright (C) 2003, Digium
8 * Mark Spencer <markster@digium.com>
9 * James Golovich <james@gnuinter.net>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
16 #include <asterisk/lock.h>
17 #include <asterisk/file.h>
18 #include <asterisk/logger.h>
19 #include <asterisk/channel.h>
20 #include <asterisk/pbx.h>
21 #include <asterisk/module.h>
22 #include <asterisk/app.h>
28 #include <sys/ioctl.h>
32 static char *tdesc = "Check if channel is available";
34 static char *app = "ChanIsAvail";
36 static char *synopsis = "Check if channel is available";
38 static char *descrip =
39 " ChanIsAvail(Technology/resource[&Technology2/resource2...]): \n"
40 "Checks is any of the requested channels are available. If none\n"
41 "of the requested channels are available the new priority will be\n"
42 "n+101 (unless such a priority does not exist or on error, in which\n"
43 "case ChanIsAvail will return -1). If any of the requested channels\n"
44 "are available, the next priority will be n+1, the channel variable\n"
45 "${AVAILCHAN} will be set to the name of the available channel and\n"
46 "the ChanIsAvail app will return 0. ${AVAILORIGCHAN} is\n"
47 "the canonical channel name that was used to create the channel.\n";
53 static int chanavail_exec(struct ast_channel *chan, void *data)
57 char info[512], tmp[512], *peers, *tech, *number, *rest, *cur;
58 struct ast_channel *tempchan;
61 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
66 strncpy(info, (char *)data, sizeof(info)-1);
71 /* remember where to start next time */
72 rest = strchr(cur, '&');
78 number = strchr(tech, '/');
80 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
85 if ((tempchan = ast_request(tech, chan->nativeformats, number))) {
86 pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
87 /* Store the originally used channel too */
88 snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
89 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
100 pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
101 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
102 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
108 LOCAL_USER_REMOVE(u);
112 int unload_module(void)
114 STANDARD_HANGUP_LOCALUSERS;
115 return ast_unregister_application(app);
118 int load_module(void)
120 return ast_register_application(app, chanavail_exec, synopsis, descrip);
123 char *description(void)
131 STANDARD_USECOUNT(res);
137 return ASTERISK_GPL_KEY;