Asterisk - The Open Source Telephony Project  18.5.0
app_chanisavail.c
Go to the documentation of this file.
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  <synopsis>
51  Check channel availability
52  </synopsis>
53  <syntax>
54  <parameter name="Technology/Resource" required="false" argsep="&amp;">
55  <argument name="Technology/Resource" required="true">
56  <para>Specification of the device(s) to check. These must be in the format of
57  <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
58  represents a particular channel driver, and <replaceable>Resource</replaceable>
59  represents a resource available to that particular channel driver.</para>
60  </argument>
61  <argument name="Technology2/Resource2" multiple="true">
62  <para>Optional extra devices to check</para>
63  <para>If you need more than one enter them as
64  Technology2/Resource2&amp;Technology3/Resource3&amp;.....</para>
65  </argument>
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;
105  int option_state = 0;
106  int string_compare = 0;
107  int option_all_avail = 0;
108  int status;
109  char *info;
110  char trychan[512];
111  char *rest;
112  char *tech;
113  char *number;
114  struct ast_str *tmp_availchan = ast_str_alloca(2048);
115  struct ast_str *tmp_availorig = ast_str_alloca(2048);
116  struct ast_str *tmp_availstat = ast_str_alloca(2048);
117  struct ast_str *tmp_availcause = ast_str_alloca(2048);
118  struct ast_channel *tempchan;
119  struct ast_custom_function *cdr_prop_func = ast_custom_function_find("CDR_PROP");
121  AST_APP_ARG(reqchans);
123  );
124 
125  info = ast_strdupa(data ?: "");
126 
128 
129  if (args.options) {
130  if (strchr(args.options, 'a')) {
131  option_all_avail = 1;
132  }
133  if (strchr(args.options, 's')) {
134  option_state = 1;
135  }
136  if (strchr(args.options, 't')) {
137  string_compare = 1;
138  }
139  }
140 
141  rest = args.reqchans;
142  if (!rest) {
143  rest = "";
144  }
145  while ((tech = strsep(&rest, "&"))) {
146  tech = ast_strip(tech);
147 
148  number = strchr(tech, '/');
149  if (!number) {
150  if (!ast_strlen_zero(tech)) {
151  ast_log(LOG_WARNING, "Invalid ChanIsAvail technology/resource argument: '%s'\n",
152  tech);
153  }
154 
155  ast_str_append(&tmp_availstat, 0, "%s%d",
156  ast_str_strlen(tmp_availstat) ? "&" : "", AST_DEVICE_INVALID);
157  continue;
158  }
159  *number++ = '\0';
160 
161  status = AST_DEVICE_UNKNOWN;
162 
163  if (string_compare) {
164  /* ast_parse_device_state checks for "SIP/1234" as a channel name.
165  ast_device_state will ask the SIP driver for the channel state. */
166 
167  snprintf(trychan, sizeof(trychan), "%s/%s", tech, number);
168  status = inuse = ast_parse_device_state(trychan);
169  } else if (option_state) {
170  /* If the pbx says in use then don't bother trying further.
171  This is to permit testing if someone's on a call, even if the
172  channel can permit more calls (ie callwaiting, sip calls, etc). */
173 
174  snprintf(trychan, sizeof(trychan), "%s/%s", tech, number);
175  status = inuse = ast_device_state(trychan);
176  }
177  ast_str_append(&tmp_availstat, 0, "%s%d",
178  ast_str_strlen(tmp_availstat) ? "&" : "", status);
179  if ((inuse <= (int) AST_DEVICE_NOT_INUSE)
180  && (tempchan = ast_request(tech, ast_channel_nativeformats(chan), NULL, chan, number, &status))) {
181  ast_str_append(&tmp_availchan, 0, "%s%s",
182  ast_str_strlen(tmp_availchan) ? "&" : "", ast_channel_name(tempchan));
183 
184  ast_str_append(&tmp_availorig, 0, "%s%s/%s",
185  ast_str_strlen(tmp_availorig) ? "&" : "", tech, number);
186 
187  ast_str_append(&tmp_availcause, 0, "%s%d",
188  ast_str_strlen(tmp_availcause) ? "&" : "", status);
189 
190  /* Disable CDR for this temporary channel. */
191  if (cdr_prop_func) {
192  ast_func_write(tempchan, "CDR_PROP(disable)", "1");
193  }
194 
195  ast_hangup(tempchan);
196  tempchan = NULL;
197 
198  if (!option_all_avail) {
199  break;
200  }
201  }
202  }
203 
204  pbx_builtin_setvar_helper(chan, "AVAILCHAN", ast_str_buffer(tmp_availchan));
205  /* Store the originally used channel too */
206  pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", ast_str_buffer(tmp_availorig));
207  pbx_builtin_setvar_helper(chan, "AVAILSTATUS", ast_str_buffer(tmp_availstat));
208  pbx_builtin_setvar_helper(chan, "AVAILCAUSECODE", ast_str_buffer(tmp_availcause));
209 
210  return 0;
211 }
212 
213 static int unload_module(void)
214 {
216 }
217 
218 static int load_module(void)
219 {
222 }
223 
224 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Check channel availability",
225  .support_level = AST_MODULE_SUPPORT_EXTENDED,
226  .load = load_module,
227  .unload = unload_module,
228  .optional_modules = "func_cdr"
229 );
static const char app[]
Main Channel structure associated with a channel.
ast_device_state
Device States.
Definition: devicestate.h:52
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
Device state management.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
static int unload_module(void)
enum ast_device_state ast_parse_device_state(const char *device)
Search the Channels by Name.
Definition: devicestate.c:287
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define ast_str_alloca(init_len)
Definition: strings.h:800
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
const char * args
#define NULL
Definition: resample.c:96
static int load_module(void)
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_strlen_zero(foo)
Definition: strings.h:52
struct ast_channel * ast_request(const char *type, struct ast_format_cap *request_cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int *cause)
Requests a channel.
Definition: channel.c:6444
Number structure.
Definition: app_followme.c:154
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
static int chanavail_exec(struct ast_channel *chan, const char *data)
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
Core PBX routines and definitions.
struct ast_custom_function * ast_custom_function_find(const char *name)
Definition: ael_main.c:173
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
def info(msg)
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2548
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:688
char * strsep(char **str, const char *delims)
const char * ast_channel_name(const struct ast_channel *chan)
static struct test_options options
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
int ast_func_write(struct ast_channel *chan, const char *function, const char *value)
executes a write operation on a function
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
jack_status_t status
Definition: app_jack.c:146
#define AST_APP_ARG(name)
Define an application argument.