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