Debian Codegraph @

apps/app_chanisavail.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
7 * James Golovich <[email protected]>
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 <[email protected]>
25 * \author James Golovich <[email protected]>
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 <since>
51 <version>0.4.0</version>
52 </since>
53 <synopsis>
54 Check channel availability
55 </synopsis>
56 <syntax>
57 <parameter name="Technology/Resource" required="false" argsep="&amp;">
58 <argument name="Technology/Resource" required="true">
59 <para>Specification of the device(s) to check. These must be in the format of
60 <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
61 represents a particular channel driver, and <replaceable>Resource</replaceable>
62 represents a resource available to that particular channel driver.</para>
63 </argument>
64 <argument name="Technology2/Resource2" multiple="true">
65 <para>Optional extra devices to check</para>
66 <para>If you need more than one enter them as
67 Technology2/Resource2&amp;Technology3/Resource3&amp;.....</para>
68 </argument>
69 </parameter>
70 <parameter name="options" required="false">
71 <optionlist>
72 <option name="a">
73 <para>Check for all available channels, not only the first one</para>
74 </option>
75 <option name="s">
76 <para>Consider the channel unavailable if the channel is in use at all</para>
77 </option>
78 <option name="t" implies="s">
79 <para>Simply checks if specified channels exist in the channel list</para>
80 </option>
81 </optionlist>
82 </parameter>
83 </syntax>
84 <description>
85 <para>This application will check to see if any of the specified channels are available.</para>
86 <para>This application sets the following channel variables:</para>
87 <variablelist>
88 <variable name="AVAILCHAN">
89 <para>The name of the available channel, if one exists</para>
90 </variable>
91 <variable name="AVAILORIGCHAN">
92 <para>The canonical channel name that was used to create the channel</para>
93 </variable>
94 <variable name="AVAILSTATUS">
95 <para>The device state for the device</para>
96 </variable>
97 <variable name="AVAILCAUSECODE">
98 <para>The cause code returned when requesting the channel</para>
99 </variable>
100 </variablelist>
101 </description>
102 </application>
103 ***/
104
105 static int chanavail_exec(struct ast_channel *chan, const char *data)
106 {
107 int inuse = -1;
108 int option_state = 0;
109 int string_compare = 0;
110 int option_all_avail = 0;
111 int status;
112 char *info;
113 char trychan[512];
114 char *rest;
115 char *tech;
116 char *number;
117 struct ast_str *tmp_availchan = ast_str_alloca(2048);
118 struct ast_str *tmp_availorig = ast_str_alloca(2048);
119 struct ast_str *tmp_availstat = ast_str_alloca(2048);
120 struct ast_str *tmp_availcause = ast_str_alloca(2048);
121 struct ast_channel *tempchan;
122 struct ast_custom_function *cdr_prop_func = ast_custom_function_find("CDR_PROP");
123 struct ast_format_cap *caps = NULL;
124 AST_DECLARE_APP_ARGS(args,
125 AST_APP_ARG(reqchans);
126 AST_APP_ARG(options);
127 );
128
129 info = ast_strdupa(data ?: "");
130
131 AST_STANDARD_APP_ARGS(args, info);
132
133 ao2_lock(chan);
134 caps = ao2_bump(ast_channel_nativeformats(chan));
135 ao2_unlock(chan);
136
137 if (args.options) {
138 if (strchr(args.options, 'a')) {
139 option_all_avail = 1;
140 }
141 if (strchr(args.options, 's')) {
142 option_state = 1;
143 }
144 if (strchr(args.options, 't')) {
145 string_compare = 1;
146 }
147 }
148
149 rest = args.reqchans;
150 if (!rest) {
151 rest = "";
152 }
153 while ((tech = strsep(&rest, "&"))) {
154 tech = ast_strip(tech);
155
156 number = strchr(tech, '/');
157 if (!number) {
158 if (!ast_strlen_zero(tech)) {
159 ast_log(LOG_WARNING, "Invalid ChanIsAvail technology/resource argument: '%s'\n",
160 tech);
161 }
162
163 ast_str_append(&tmp_availstat, 0, "%s%d",
164 ast_str_strlen(tmp_availstat) ? "&" : "", AST_DEVICE_INVALID);
165 continue;
166 }
167 *number++ = '\0';
168
169 status = AST_DEVICE_UNKNOWN;
170
171 if (string_compare) {
172 /* ast_parse_device_state checks for "SIP/1234" as a channel name.
173 ast_device_state will ask the SIP driver for the channel state. */
174
175 snprintf(trychan, sizeof(trychan), "%s/%s", tech, number);
176 status = inuse = ast_parse_device_state(trychan);
177 } else if (option_state) {
178 /* If the pbx says in use then don't bother trying further.
179 This is to permit testing if someone's on a call, even if the
180 channel can permit more calls (ie callwaiting, sip calls, etc). */
181
182 snprintf(trychan, sizeof(trychan), "%s/%s", tech, number);
183 status = inuse = ast_device_state(trychan);
184 }
185 ast_str_append(&tmp_availstat, 0, "%s%d", ast_str_strlen(tmp_availstat) ? "&" : "", status);
186
187 if ((inuse <= (int) AST_DEVICE_NOT_INUSE)
188 && (tempchan = ast_request(tech, caps, NULL, chan, number, &status))) {
189
190 ast_str_append(&tmp_availchan, 0, "%s%s",
191 ast_str_strlen(tmp_availchan) ? "&" : "", ast_channel_name(tempchan));
192
193 ast_str_append(&tmp_availorig, 0, "%s%s/%s",
194 ast_str_strlen(tmp_availorig) ? "&" : "", tech, number);
195
196 ast_str_append(&tmp_availcause, 0, "%s%d",
197 ast_str_strlen(tmp_availcause) ? "&" : "", status);
198
199 /* Disable CDR for this temporary channel. */
200 if (cdr_prop_func) {
201 ast_func_write(tempchan, "CDR_PROP(disable)", "1");
202 }
203
204 ast_hangup(tempchan);
205 tempchan = NULL;
206
207 if (!option_all_avail) {
208 break;
209 }
210 }
211
212 }
213
214 ao2_cleanup(caps);
215
216 pbx_builtin_setvar_helper(chan, "AVAILCHAN", ast_str_buffer(tmp_availchan));
217 /* Store the originally used channel too */
218 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", ast_str_buffer(tmp_availorig));
219 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", ast_str_buffer(tmp_availstat));
220 pbx_builtin_setvar_helper(chan, "AVAILCAUSECODE", ast_str_buffer(tmp_availcause));
221
222 return 0;
223 }
224
225 static int unload_module(void)
226 {
227 return ast_unregister_application(app);
228 }
229
230 static int load_module(void)
231 {
232 return ast_register_application_xml(app, chanavail_exec) ?
233 AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
234 }
235
236 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Check channel availability",
237 .support_level = AST_MODULE_SUPPORT_EXTENDED,
238 .load = load_module,
239 .unload = unload_module,
240 .optional_modules = "func_cdr"
241 );