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