2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief Channel info dialplan functions
21 * \author Kevin P. Fleming <kpfleming@digium.com>
28 <support_level>core</support_level>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/module.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43 #include "asterisk/indications.h"
44 #include "asterisk/stringfields.h"
45 #include "asterisk/global_datastores.h"
48 <function name="CHANNELS" language="en_US">
50 Gets the list of channels, optionally filtering by a regular expression.
53 <parameter name="regular_expression" />
56 <para>Gets the list of channels, optionally filtering by a <replaceable>regular_expression</replaceable>. If
57 no argument is provided, all known channels are returned. The
58 <replaceable>regular_expression</replaceable> must correspond to
59 the POSIX.2 specification, as shown in <emphasis>regex(7)</emphasis>. The list returned
60 will be space-delimited.</para>
63 <function name="MASTER_CHANNEL" language="en_US">
65 Gets or sets variables on the master channel
68 <para>Allows access to the channel which created the current channel, if any. If the channel is already
69 a master channel, then accesses local channel variables.</para>
72 <function name="CHANNEL" language="en_US">
74 Gets/sets various pieces of information about the channel.
77 <parameter name="item" required="true">
78 <para>Standard items (provided by all channel technologies) are:</para>
80 <enum name="audioreadformat">
81 <para>R/O format currently being read.</para>
83 <enum name="audionativeformat">
84 <para>R/O format used natively for audio.</para>
86 <enum name="audiowriteformat">
87 <para>R/O format currently being written.</para>
89 <enum name="callgroup">
90 <para>R/W call groups for call pickup.</para>
92 <enum name="pickupgroup">
93 <para>R/W call groups for call pickup.</para>
95 <enum name="channeltype">
96 <para>R/O technology used for channel.</para>
98 <enum name="checkhangup">
99 <para>R/O Whether the channel is hanging up (1/0)</para>
101 <enum name="language">
102 <para>R/W language for sounds played.</para>
104 <enum name="musicclass">
105 <para>R/W class (from musiconhold.conf) for hold music.</para>
108 <para>The name of the channel</para>
110 <enum name="parkinglot">
111 <para>R/W parkinglot for parking.</para>
114 <para>R/W set rxgain level on channel drivers that support it.</para>
116 <enum name="secure_bridge_signaling">
117 <para>Whether or not channels bridged to this channel require secure signaling</para>
119 <enum name="secure_bridge_media">
120 <para>Whether or not channels bridged to this channel require secure media</para>
123 <para>R/O state for channel</para>
125 <enum name="tonezone">
126 <para>R/W zone for indications played</para>
128 <enum name="transfercapability">
129 <para>R/W ISDN Transfer Capability, one of:</para>
131 <enum name="SPEECH" />
132 <enum name="DIGITAL" />
133 <enum name="RESTRICTED_DIGITAL" />
134 <enum name="3K1AUDIO" />
135 <enum name="DIGITAL_W_TONES" />
136 <enum name="VIDEO" />
140 <para>R/W set txgain level on channel drivers that support it.</para>
142 <enum name="videonativeformat">
143 <para>R/O format used natively for video</para>
146 <para>R/W whether or not context tracing is enabled, only available
147 <emphasis>if CHANNEL_TRACE is defined</emphasis>.</para>
150 <para><emphasis>chan_sip</emphasis> provides the following additional options:</para>
153 <para>R/O Get the IP address of the peer.</para>
156 <para>R/O Get the source IP address of the peer.</para>
159 <para>R/O Get the URI from the From: header.</para>
162 <para>R/O Get the URI from the Contact: header.</para>
164 <enum name="useragent">
165 <para>R/O Get the useragent.</para>
167 <enum name="peername">
168 <para>R/O Get the name of the peer.</para>
170 <enum name="t38passthrough">
171 <para>R/O <literal>1</literal> if T38 is offered or enabled in this channel,
172 otherwise <literal>0</literal></para>
175 <para>R/O Get QOS information about the RTP stream</para>
176 <para> This option takes two additional arguments:</para>
177 <para> Argument 1:</para>
178 <para> <literal>audio</literal> Get data about the audio stream</para>
179 <para> <literal>video</literal> Get data about the video stream</para>
180 <para> <literal>text</literal> Get data about the text stream</para>
181 <para> Argument 2:</para>
182 <para> <literal>local_ssrc</literal> Local SSRC (stream ID)</para>
183 <para> <literal>local_lostpackets</literal> Local lost packets</para>
184 <para> <literal>local_jitter</literal> Local calculated jitter</para>
185 <para> <literal>local_maxjitter</literal> Local calculated jitter (maximum)</para>
186 <para> <literal>local_minjitter</literal> Local calculated jitter (minimum)</para>
187 <para> <literal>local_normdevjitter</literal>Local calculated jitter (normal deviation)</para>
188 <para> <literal>local_stdevjitter</literal> Local calculated jitter (standard deviation)</para>
189 <para> <literal>local_count</literal> Number of received packets</para>
190 <para> <literal>remote_ssrc</literal> Remote SSRC (stream ID)</para>
191 <para> <literal>remote_lostpackets</literal>Remote lost packets</para>
192 <para> <literal>remote_jitter</literal> Remote reported jitter</para>
193 <para> <literal>remote_maxjitter</literal> Remote calculated jitter (maximum)</para>
194 <para> <literal>remote_minjitter</literal> Remote calculated jitter (minimum)</para>
195 <para> <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
196 <para> <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
197 <para> <literal>remote_count</literal> Number of transmitted packets</para>
198 <para> <literal>rtt</literal> Round trip time</para>
199 <para> <literal>maxrtt</literal> Round trip time (maximum)</para>
200 <para> <literal>minrtt</literal> Round trip time (minimum)</para>
201 <para> <literal>normdevrtt</literal> Round trip time (normal deviation)</para>
202 <para> <literal>stdevrtt</literal> Round trip time (standard deviation)</para>
203 <para> <literal>all</literal> All statistics (in a form suited to logging,
204 but not for parsing)</para>
206 <enum name="rtpdest">
207 <para>R/O Get remote RTP destination information.</para>
208 <para> This option takes one additional argument:</para>
209 <para> Argument 1:</para>
210 <para> <literal>audio</literal> Get audio destination</para>
211 <para> <literal>video</literal> Get video destination</para>
212 <para> <literal>text</literal> Get text destination</para>
215 <para><emphasis>chan_iax2</emphasis> provides the following additional options:</para>
218 <para>R/O Get the peer's ip address.</para>
220 <enum name="peername">
221 <para>R/O Get the peer's username.</para>
224 <para><emphasis>chan_dahdi</emphasis> provides the following additional options:</para>
226 <enum name="dahdi_channel">
227 <para>R/O DAHDI channel related to this channel.</para>
229 <enum name="dahdi_span">
230 <para>R/O DAHDI span related to this channel.</para>
232 <enum name="dahdi_type">
233 <para>R/O DAHDI channel type, one of:</para>
235 <enum name="analog" />
236 <enum name="mfc/r2" />
238 <enum name="pseudo" />
242 <enum name="keypad_digits">
243 <para>R/O PRI Keypad digits that came in with the SETUP message.</para>
245 <enum name="reversecharge">
246 <para>R/O PRI Reverse Charging Indication, one of:</para>
248 <enum name="-1"> <para>None</para></enum>
249 <enum name=" 1"> <para>Reverse Charging Requested</para></enum>
252 <enum name="no_media_path">
253 <para>R/O PRI Nonzero if the channel has no B channel.
254 The channel is either on hold or a call waiting call.</para>
260 <para>Gets/sets various pieces of information about the channel, additional <replaceable>item</replaceable> may
261 be available from the channel driver; see its documentation for details. Any <replaceable>item</replaceable>
262 requested that is not available on the current channel will return an empty string.</para>
267 #define locked_copy_string(chan, dest, source, len) \
269 ast_channel_lock(chan); \
270 ast_copy_string(dest, source, len); \
271 ast_channel_unlock(chan); \
273 #define locked_string_field_set(chan, field, source) \
275 ast_channel_lock(chan); \
276 ast_string_field_set(chan, field, source); \
277 ast_channel_unlock(chan); \
280 static const char * const transfercapability_table[0x20] = {
281 "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
282 "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
283 "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
284 "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
286 static int func_channel_read(struct ast_channel *chan, const char *function,
287 char *data, char *buf, size_t len)
291 struct ast_format_cap *tmpcap;
293 if (!strcasecmp(data, "audionativeformat")) {
294 if ((tmpcap = ast_format_cap_get_type(chan->nativeformats, AST_FORMAT_TYPE_AUDIO))) {
295 ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
296 tmpcap = ast_format_cap_destroy(tmpcap);
298 } else if (!strcasecmp(data, "videonativeformat")) {
299 if ((tmpcap = ast_format_cap_get_type(chan->nativeformats, AST_FORMAT_TYPE_VIDEO))) {
300 ast_copy_string(buf, ast_getformatname_multiple(tmp, sizeof(tmp), tmpcap), len);
301 tmpcap = ast_format_cap_destroy(tmpcap);
303 } else if (!strcasecmp(data, "audioreadformat")) {
304 ast_copy_string(buf, ast_getformatname(&chan->readformat), len);
305 } else if (!strcasecmp(data, "audiowriteformat")) {
306 ast_copy_string(buf, ast_getformatname(&chan->writeformat), len);
308 } else if (!strcasecmp(data, "trace")) {
309 ast_channel_lock(chan);
310 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
311 ast_channel_unlock(chan);
313 } else if (!strcasecmp(data, "tonezone") && chan->zone)
314 locked_copy_string(chan, buf, chan->zone->country, len);
315 else if (!strcasecmp(data, "language"))
316 locked_copy_string(chan, buf, chan->language, len);
317 else if (!strcasecmp(data, "musicclass"))
318 locked_copy_string(chan, buf, chan->musicclass, len);
319 else if (!strcasecmp(data, "name")) {
320 locked_copy_string(chan, buf, chan->name, len);
321 } else if (!strcasecmp(data, "parkinglot"))
322 locked_copy_string(chan, buf, chan->parkinglot, len);
323 else if (!strcasecmp(data, "state"))
324 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
325 else if (!strcasecmp(data, "channeltype"))
326 locked_copy_string(chan, buf, chan->tech->type, len);
327 else if (!strcasecmp(data, "accountcode"))
328 locked_copy_string(chan, buf, chan->accountcode, len);
329 else if (!strcasecmp(data, "checkhangup")) {
330 ast_channel_lock(chan);
331 ast_copy_string(buf, ast_check_hangup(chan) ? "1" : "0", len);
332 ast_channel_unlock(chan);
333 } else if (!strcasecmp(data, "peeraccount"))
334 locked_copy_string(chan, buf, chan->peeraccount, len);
335 else if (!strcasecmp(data, "hangupsource"))
336 locked_copy_string(chan, buf, chan->hangupsource, len);
337 else if (!strcasecmp(data, "appname") && chan->appl)
338 locked_copy_string(chan, buf, chan->appl, len);
339 else if (!strcasecmp(data, "appdata") && chan->data)
340 locked_copy_string(chan, buf, chan->data, len);
341 else if (!strcasecmp(data, "exten") && chan->data)
342 locked_copy_string(chan, buf, chan->exten, len);
343 else if (!strcasecmp(data, "context") && chan->data)
344 locked_copy_string(chan, buf, chan->context, len);
345 else if (!strcasecmp(data, "userfield") && chan->data)
346 locked_copy_string(chan, buf, chan->userfield, len);
347 else if (!strcasecmp(data, "channame") && chan->data)
348 locked_copy_string(chan, buf, chan->name, len);
349 else if (!strcasecmp(data, "linkedid")) {
350 ast_channel_lock(chan);
351 if (ast_strlen_zero(chan->linkedid)) {
352 /* fall back on the channel's uniqueid if linkedid is unset */
353 ast_copy_string(buf, chan->uniqueid, len);
356 ast_copy_string(buf, chan->linkedid, len);
358 ast_channel_unlock(chan);
359 } else if (!strcasecmp(data, "peer")) {
360 struct ast_channel *p;
361 ast_channel_lock(chan);
362 p = ast_bridged_channel(chan);
363 if (p || chan->tech || chan->cdr) /* dummy channel? if so, we hid the peer name in the language */
364 ast_copy_string(buf, (p ? p->name : ""), len);
366 /* a dummy channel can still pass along bridged peer info via
367 the BRIDGEPEER variable */
368 const char *pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
369 if (!ast_strlen_zero(pname))
370 ast_copy_string(buf, pname, len); /* a horrible kludge, but... how else? */
374 ast_channel_unlock(chan);
375 } else if (!strcasecmp(data, "uniqueid")) {
376 locked_copy_string(chan, buf, chan->uniqueid, len);
377 } else if (!strcasecmp(data, "transfercapability")) {
378 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
379 } else if (!strcasecmp(data, "callgroup")) {
381 locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
382 } else if (!strcasecmp(data, "pickupgroup")) {
384 locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), chan->pickupgroup), len);
385 } else if (!strcasecmp(data, "amaflags")) {
387 snprintf(amabuf,sizeof(amabuf), "%d", chan->amaflags);
388 locked_copy_string(chan, buf, amabuf, len);
389 } else if (!strncasecmp(data, "secure_bridge_", 14)) {
390 struct ast_datastore *ds;
391 ast_channel_lock(chan);
392 if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
393 struct ast_secure_call_store *encrypt = ds->data;
394 if (!strcasecmp(data, "secure_bridge_signaling")) {
395 snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
396 } else if (!strcasecmp(data, "secure_bridge_media")) {
397 snprintf(buf, len, "%s", encrypt->media ? "1" : "");
400 ast_channel_unlock(chan);
401 } else if (!chan->tech || !chan->tech->func_channel_read || chan->tech->func_channel_read(chan, function, data, buf, len)) {
402 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
409 static int func_channel_write_real(struct ast_channel *chan, const char *function,
410 char *data, const char *value)
415 if (!strcasecmp(data, "language"))
416 locked_string_field_set(chan, language, value);
417 else if (!strcasecmp(data, "parkinglot"))
418 locked_string_field_set(chan, parkinglot, value);
419 else if (!strcasecmp(data, "musicclass"))
420 locked_string_field_set(chan, musicclass, value);
421 else if (!strcasecmp(data, "accountcode"))
422 locked_string_field_set(chan, accountcode, value);
423 else if (!strcasecmp(data, "userfield"))
424 locked_string_field_set(chan, userfield, value);
425 else if (!strcasecmp(data, "amaflags")) {
426 ast_channel_lock(chan);
427 if(isdigit(*value)) {
428 sscanf(value, "%30d", &chan->amaflags);
429 } else if (!strcasecmp(value,"OMIT")){
431 } else if (!strcasecmp(value,"BILLING")){
433 } else if (!strcasecmp(value,"DOCUMENTATION")){
436 ast_channel_unlock(chan);
437 } else if (!strcasecmp(data, "peeraccount"))
438 locked_string_field_set(chan, peeraccount, value);
439 else if (!strcasecmp(data, "hangupsource"))
440 /* XXX - should we be forcing this here? */
441 ast_set_hangupsource(chan, value, 0);
443 else if (!strcasecmp(data, "trace")) {
444 ast_channel_lock(chan);
446 ret = ast_channel_trace_enable(chan);
447 else if (ast_false(value))
448 ret = ast_channel_trace_disable(chan);
451 ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
453 ast_channel_unlock(chan);
456 else if (!strcasecmp(data, "tonezone")) {
457 struct ast_tone_zone *new_zone;
458 if (!(new_zone = ast_get_indication_zone(value))) {
459 ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
462 ast_channel_lock(chan);
464 chan->zone = ast_tone_zone_unref(chan->zone);
466 chan->zone = ast_tone_zone_ref(new_zone);
467 ast_channel_unlock(chan);
468 new_zone = ast_tone_zone_unref(new_zone);
470 } else if (!strcasecmp(data, "callgroup")) {
471 chan->callgroup = ast_get_group(value);
472 } else if (!strcasecmp(data, "pickupgroup")) {
473 chan->pickupgroup = ast_get_group(value);
474 } else if (!strcasecmp(data, "txgain")) {
475 sscanf(value, "%4hhd", &gainset);
476 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
477 } else if (!strcasecmp(data, "rxgain")) {
478 sscanf(value, "%4hhd", &gainset);
479 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
480 } else if (!strcasecmp(data, "transfercapability")) {
482 for (i = 0; i < 0x20; i++) {
483 if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
484 chan->transfercapability = i;
488 } else if (!strncasecmp(data, "secure_bridge_", 14)) {
489 struct ast_datastore *ds;
490 struct ast_secure_call_store *store;
492 if (!chan || !value) {
496 ast_channel_lock(chan);
497 if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
498 if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) {
499 ast_channel_unlock(chan);
502 if (!(store = ast_calloc(1, sizeof(*store)))) {
503 ast_channel_unlock(chan);
508 ast_channel_datastore_add(chan, ds);
512 ast_channel_unlock(chan);
514 if (!strcasecmp(data, "secure_bridge_signaling")) {
515 store->signaling = ast_true(value) ? 1 : 0;
516 } else if (!strcasecmp(data, "secure_bridge_media")) {
517 store->media = ast_true(value) ? 1 : 0;
519 } else if (!chan->tech->func_channel_write
520 || chan->tech->func_channel_write(chan, function, data, value)) {
521 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
529 static int func_channel_write(struct ast_channel *chan, const char *function, char *data, const char *value)
532 ast_chan_write_info_t write_info = {
533 .version = AST_CHAN_WRITE_INFO_T_VERSION,
534 .write_fn = func_channel_write_real,
536 .function = function,
541 res = func_channel_write_real(chan, function, data, value);
542 ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);
547 static struct ast_custom_function channel_function = {
549 .read = func_channel_read,
550 .write = func_channel_write,
553 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
555 struct ast_channel *c = NULL;
559 struct ast_channel_iterator *iter;
563 if (!ast_strlen_zero(data)) {
564 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
565 regerror(res, &re, buf, maxlen);
566 ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
571 if (!(iter = ast_channel_iterator_all_new())) {
572 if (!ast_strlen_zero(data)) {
578 while ((c = ast_channel_iterator_next(iter))) {
580 if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
581 size_t namelen = strlen(c->name);
582 if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
583 if (!ast_strlen_zero(buf)) {
587 strcat(buf, c->name);
590 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space. Output will be truncated!\n");
593 ast_channel_unlock(c);
594 c = ast_channel_unref(c);
597 ast_channel_iterator_destroy(iter);
599 if (!ast_strlen_zero(data)) {
606 static struct ast_custom_function channels_function = {
608 .read = func_channels_read,
611 static int func_mchan_read(struct ast_channel *chan, const char *function,
612 char *data, struct ast_str **buf, ssize_t len)
614 struct ast_channel *mchan = ast_channel_get_by_name(chan->linkedid);
615 char *template = alloca(4 + strlen(data));
616 sprintf(template, "${%s}", data); /* SAFE */
617 ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
619 ast_channel_unref(mchan);
624 static int func_mchan_write(struct ast_channel *chan, const char *function,
625 char *data, const char *value)
627 struct ast_channel *mchan = ast_channel_get_by_name(chan->linkedid);
628 pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
630 ast_channel_unref(mchan);
635 static struct ast_custom_function mchan_function = {
636 .name = "MASTER_CHANNEL",
637 .read2 = func_mchan_read,
638 .write = func_mchan_write,
641 static int unload_module(void)
645 res |= ast_custom_function_unregister(&channel_function);
646 res |= ast_custom_function_unregister(&channels_function);
647 res |= ast_custom_function_unregister(&mchan_function);
652 static int load_module(void)
656 res |= ast_custom_function_register(&channel_function);
657 res |= ast_custom_function_register(&channels_function);
658 res |= ast_custom_function_register(&mchan_function);
663 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");