c1b33b938f61f225c7046d06454530461253b863
[asterisk/asterisk.git] / apps / app_chanisavail.c
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 * James Golovich <james@gnuinter.net>
8 *
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.
14 *
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.
18 */
19
20 /*! \file
21  *
22  * \brief Check if Channel is Available
23  *
24  * \author Mark Spencer <markster@digium.com>
25  * \author James Golovich <james@gnuinter.net>
26
27  * \ingroup applications
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <sys/ioctl.h>
35
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"
43
44 static const char app[] = "ChanIsAvail";
45
46 /*** DOCUMENTATION
47         <application name="ChanIsAvail" language="en_US">
48                 <synopsis>
49                         Check channel availability
50                 </synopsis>
51                 <syntax>
52                         <parameter name="Technology/Resource" required="true" argsep="&amp;">
53                                 <argument name="Technology2/Resource2" multiple="true">
54                                         <para>Optional extra devices to check</para>
55                                         <para>If you need more then one enter them as
56                                         Technology2/Resource2&amp;Technology3/Resourse3&amp;.....</para>
57                                 </argument>
58                                 <para>Specification of the device(s) to check.  These must be in the format of 
59                                 <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
60                                 represents a particular channel driver, and <replaceable>Resource</replaceable>
61                                 represents a resource available to that particular channel driver.</para>
62                         </parameter>
63                         <parameter name="options" required="false">
64                                 <optionlist>
65                                         <option name="a">
66                                                 <para>Check for all available channels, not only the first one</para>
67                                         </option>
68                                         <option name="s">
69                                                 <para>Consider the channel unavailable if the channel is in use at all</para>
70                                         </option>
71                                         <option name="t" implies="s">
72                                                 <para>Simply checks if specified channels exist in the channel list</para>
73                                         </option>
74                                 </optionlist>
75                         </parameter>
76                 </syntax>
77                 <description>
78                         <para>This application will check to see if any of the specified channels are available.</para>
79                         <para>This application sets the following channel variables:</para>
80                         <variablelist>
81                                 <variable name="AVAILCHAN">
82                                         <para>The name of the available channel, if one exists</para>
83                                 </variable>
84                                 <variable name="AVAILORIGCHAN">
85                                         <para>The canonical channel name that was used to create the channel</para>
86                                 </variable>
87                                 <variable name="AVAILSTATUS">
88                                         <para>The status code for the available channel</para>
89                                 </variable>
90                         </variablelist>
91                 </description>
92         </application>
93  ***/
94
95 static int chanavail_exec(struct ast_channel *chan, const char *data)
96 {
97         int inuse=-1, option_state=0, string_compare=0, option_all_avail=0;
98         int status;
99         char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
100         struct ast_str *tmp_availchan = ast_str_alloca(2048);
101         struct ast_str *tmp_availorig = ast_str_alloca(2048);
102         struct ast_str *tmp_availstat = ast_str_alloca(2048);
103         struct ast_channel *tempchan;
104         AST_DECLARE_APP_ARGS(args,
105                 AST_APP_ARG(reqchans);
106                 AST_APP_ARG(options);
107         );
108
109         if (ast_strlen_zero(data)) {
110                 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (DAHDI/1&DAHDI/2)\n");
111                 return -1;
112         }
113
114         info = ast_strdupa(data);
115
116         AST_STANDARD_APP_ARGS(args, info);
117
118         if (args.options) {
119                 if (strchr(args.options, 'a')) {
120                         option_all_avail = 1;
121                 }
122                 if (strchr(args.options, 's')) {
123                         option_state = 1;
124                 }
125                 if (strchr(args.options, 't')) {
126                         string_compare = 1;
127                 }
128         }
129         peers = args.reqchans;
130         if (peers) {
131                 cur = peers;
132                 do {
133                         /* remember where to start next time */
134                         rest = strchr(cur, '&');
135                         if (rest) {
136                                 *rest = 0;
137                                 rest++;
138                         }
139                         tech = cur;
140                         number = strchr(tech, '/');
141                         if (!number) {
142                                 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
143                                 return -1;
144                         }
145                         *number = '\0';
146                         number++;
147                         
148                         if (string_compare) {
149                                 /* ast_parse_device_state checks for "SIP/1234" as a channel name.
150                                    ast_device_state will ask the SIP driver for the channel state. */
151
152                                 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
153                                 status = inuse = ast_parse_device_state(trychan);
154                         } else if (option_state) {
155                                 /* If the pbx says in use then don't bother trying further.
156                                    This is to permit testing if someone's on a call, even if the
157                                    channel can permit more calls (ie callwaiting, sip calls, etc).  */
158
159                                 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
160                                 status = inuse = ast_device_state(trychan);
161                         }
162                         if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, chan, number, &status))) {
163                                         ast_str_append(&tmp_availchan, 0, "%s%s", ast_str_strlen(tmp_availchan) ? "&" : "", tempchan->name);
164                                         
165                                         snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
166                                         ast_str_append(&tmp_availorig, 0, "%s%s", ast_str_strlen(tmp_availorig) ? "&" : "", tmp);
167
168                                         snprintf(tmp, sizeof(tmp), "%d", status);
169                                         ast_str_append(&tmp_availstat, 0, "%s%s", ast_str_strlen(tmp_availstat) ? "&" : "", tmp);
170
171                                         ast_hangup(tempchan);
172                                         tempchan = NULL;
173
174                                         if (!option_all_avail) {
175                                                 break;
176                                         }
177                         } else {
178                                 snprintf(tmp, sizeof(tmp), "%d", status);
179                                 ast_str_append(&tmp_availstat, 0, "%s%s", ast_str_strlen(tmp_availstat) ? "&" : "", tmp);
180                         }
181                         cur = rest;
182                 } while (cur);
183         }
184
185         pbx_builtin_setvar_helper(chan, "AVAILCHAN", ast_str_buffer(tmp_availchan));
186         /* Store the originally used channel too */
187         pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", ast_str_buffer(tmp_availorig));
188         pbx_builtin_setvar_helper(chan, "AVAILSTATUS", ast_str_buffer(tmp_availstat));
189
190         return 0;
191 }
192
193 static int unload_module(void)
194 {
195         return ast_unregister_application(app);
196 }
197
198 static int load_module(void)
199 {
200         return ast_register_application_xml(app, chanavail_exec) ?
201                 AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
202 }
203
204 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");