Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
app_chanisavail.c File Reference

Check if Channel is Available. More...

#include "asterisk.h"
#include <sys/ioctl.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
#include "asterisk/devicestate.h"
Include dependency graph for app_chanisavail.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int chanavail_exec (struct ast_channel *chan, const char *data)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Check channel availability" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .optional_modules = "func_cdr" }
 
static const char app [] = "ChanIsAvail"
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Check if Channel is Available.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
James Golovich james.nosp@m.@gnu.nosp@m.inter.nosp@m..net

Definition in file app_chanisavail.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 229 of file app_chanisavail.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 229 of file app_chanisavail.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 229 of file app_chanisavail.c.

◆ chanavail_exec()

static int chanavail_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 102 of file app_chanisavail.c.

References args, AST_APP_ARG, ast_channel_name(), ast_channel_nativeformats(), ast_custom_function_find(), AST_DECLARE_APP_ARGS, AST_DEVICE_INVALID, AST_DEVICE_NOT_INUSE, AST_DEVICE_UNKNOWN, ast_func_write(), ast_hangup(), ast_log, ast_parse_device_state(), ast_request(), AST_STANDARD_APP_ARGS, ast_str_alloca, ast_str_append(), ast_str_buffer(), ast_str_strlen(), ast_strdupa, ast_strip(), ast_strlen_zero, sip_to_pjsip::info(), LOG_WARNING, NULL, options, pbx_builtin_setvar_helper(), status, and strsep().

Referenced by load_module().

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 }
Main Channel structure associated with a channel.
ast_device_state
Device States.
Definition: devicestate.h:52
#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
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
const char * args
#define NULL
Definition: resample.c:96
#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
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
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
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
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.
jack_status_t status
Definition: app_jack.c:146
#define AST_APP_ARG(name)
Define an application argument.

◆ load_module()

static int load_module ( void  )
static

Definition at line 218 of file app_chanisavail.c.

References app, AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_EXTENDED, ast_register_application_xml, ASTERISK_GPL_KEY, chanavail_exec(), and unload_module().

219 {
222 }
static const char app[]
static int chanavail_exec(struct ast_channel *chan, const char *data)
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:626

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 213 of file app_chanisavail.c.

References app, and ast_unregister_application().

Referenced by load_module().

214 {
216 }
static const char app[]
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Check channel availability" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .optional_modules = "func_cdr" }
static

Definition at line 229 of file app_chanisavail.c.

◆ app

const char app[] = "ChanIsAvail"
static

Definition at line 46 of file app_chanisavail.c.

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 229 of file app_chanisavail.c.