Revert part of previous fix, and heavily comment the logic for object
[asterisk-bristuff.git] / apps / app_chanisavail.c
blobd0b29bdd2c25f88218961747b1e3de16fcbb57de
1 /*
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.
20 /*! \file
22 * \brief Check if Channel is Available
24 * \author Mark Spencer <markster@digium.com>
25 * \author James Golovich <james@gnuinter.net>
27 * \ingroup applications
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <sys/ioctl.h>
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41 #include "asterisk/app.h"
42 #include "asterisk/devicestate.h"
44 static char *app = "ChanIsAvail";
46 static char *synopsis = "Check channel availability";
48 static char *descrip =
49 " ChanIsAvail(Technology/resource[&Technology2/resource2...][,options]): \n"
50 "This application will check to see if any of the specified channels are\n"
51 "available.\n"
52 " Options:\n"
53 " s - Consider the channel unavailable if the channel is in use at all.\n"
54 " t - Simply checks if specified channels exist in the channel list\n"
55 " (implies option s).\n"
56 "This application sets the following channel variable upon completion:\n"
57 " AVAILCHAN - the name of the available channel, if one exists\n"
58 " AVAILORIGCHAN - the canonical channel name that was used to create the channel\n"
59 " AVAILSTATUS - the status code for the available channel\n";
62 static int chanavail_exec(struct ast_channel *chan, void *data)
64 int res=-1, inuse=-1, option_state=0, string_compare=0;
65 int status;
66 char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
67 struct ast_channel *tempchan;
68 AST_DECLARE_APP_ARGS(args,
69 AST_APP_ARG(reqchans);
70 AST_APP_ARG(options);
73 if (ast_strlen_zero(data)) {
74 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
75 return -1;
78 info = ast_strdupa(data);
80 AST_STANDARD_APP_ARGS(args, info);
82 if (args.options) {
83 if (strchr(args.options, 's'))
84 option_state = 1;
85 if (strchr(args.options, 't'))
86 string_compare = 1;
88 peers = args.reqchans;
89 if (peers) {
90 cur = peers;
91 do {
92 /* remember where to start next time */
93 rest = strchr(cur, '&');
94 if (rest) {
95 *rest = 0;
96 rest++;
98 tech = cur;
99 number = strchr(tech, '/');
100 if (!number) {
101 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
102 return -1;
104 *number = '\0';
105 number++;
107 if (string_compare) {
108 /* ast_parse_device_state checks for "SIP/1234" as a channel name.
109 ast_device_state will ask the SIP driver for the channel state. */
111 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
112 status = inuse = ast_parse_device_state(trychan);
113 } else if (option_state) {
114 /* If the pbx says in use then don't bother trying further.
115 This is to permit testing if someone's on a call, even if the
116 channel can permit more calls (ie callwaiting, sip calls, etc). */
118 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
119 status = inuse = ast_device_state(trychan);
121 if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
122 pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
123 /* Store the originally used channel too */
124 snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
125 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
126 snprintf(tmp, sizeof(tmp), "%d", status);
127 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
128 ast_hangup(tempchan);
129 tempchan = NULL;
130 res = 1;
131 break;
132 } else {
133 snprintf(tmp, sizeof(tmp), "%d", status);
134 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
136 cur = rest;
137 } while (cur);
139 if (res < 1) {
140 pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
141 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
144 return 0;
147 static int unload_module(void)
149 return ast_unregister_application(app);
152 static int load_module(void)
154 return ast_register_application(app, chanavail_exec, synopsis, descrip);
157 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");