Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 <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"
45
46static 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
105static 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;
125 AST_APP_ARG(reqchans);
127 );
128
129 info = ast_strdupa(data ?: "");
130
132
133 ao2_lock(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
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
225static int unload_module(void)
226{
228}
229
230static int load_module(void)
231{
234}
235
236AST_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"
static const char app[]
static int chanavail_exec(struct ast_channel *chan, const char *data)
static int load_module(void)
static int unload_module(void)
jack_status_t status
Definition: app_jack.c:149
char * strsep(char **str, const char *delims)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2570
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
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:6371
Device state management.
enum ast_device_state ast_parse_device_state(const char *device)
Search the Channels by Name.
Definition: devicestate.c:290
ast_device_state
Device States.
Definition: devicestate.h:52
@ AST_DEVICE_UNKNOWN
Definition: devicestate.h:53
@ AST_DEVICE_INVALID
Definition: devicestate.h:57
@ AST_DEVICE_NOT_INUSE
Definition: devicestate.h:54
Generic File Format Support. Should be included by clients of the file handling routines....
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define LOG_WARNING
Asterisk locking-related definitions:
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:329
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODULE_SUPPORT_EXTENDED
Definition: module.h:122
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
def info(msg)
Core PBX routines and definitions.
struct ast_custom_function * ast_custom_function_find(const char *name)
Definition: ael_main.c:173
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.
int ast_func_write(struct ast_channel *chan, const char *function, const char *value)
executes a write operation on a function
#define NULL
Definition: resample.c:96
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:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_alloca(init_len)
Definition: strings.h:848
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:223
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
Support for dynamic strings.
Definition: strings.h:623
Number structure.
Definition: app_followme.c:157
const char * args
static struct test_options options