Mon Mar 12 2012 21:39:51

Asterisk developer's documentation


func_channel.c File Reference

Channel info dialplan functions. More...

#include "asterisk.h"
#include <regex.h>
#include <ctype.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/indications.h"
#include "asterisk/stringfields.h"
#include "asterisk/global_datastores.h"
Include dependency graph for func_channel.c:

Go to the source code of this file.

Defines

#define locked_copy_string(chan, dest, source, len)
#define locked_string_field_set(chan, field, source)

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int func_channel_read (struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
static int func_channel_write (struct ast_channel *chan, const char *function, char *data, const char *value)
static int func_channel_write_real (struct ast_channel *chan, const char *function, char *data, const char *value)
static int func_channels_read (struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
static int func_mchan_read (struct ast_channel *chan, const char *function, char *data, struct ast_str **buf, ssize_t len)
static int func_mchan_write (struct ast_channel *chan, const char *function, char *data, const char *value)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Channel information dialplan functions" , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_custom_function channel_function
static struct ast_custom_function channels_function
static struct ast_custom_function mchan_function
static const char *const transfercapability_table [0x20]

Detailed Description

Channel info dialplan functions.

Author:
Kevin P. Fleming <kpfleming@digium.com>
Ben Winslow

Definition in file func_channel.c.


Define Documentation

#define locked_copy_string (   chan,
  dest,
  source,
  len 
)
Value:
do { \
      ast_channel_lock(chan); \
      ast_copy_string(dest, source, len); \
      ast_channel_unlock(chan); \
   } while (0)

Definition at line 264 of file func_channel.c.

Referenced by func_channel_read().

#define locked_string_field_set (   chan,
  field,
  source 
)
Value:
do { \
      ast_channel_lock(chan); \
      ast_string_field_set(chan, field, source); \
      ast_channel_unlock(chan); \
   } while (0)

Definition at line 270 of file func_channel.c.

Referenced by func_channel_write_real().


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 652 of file func_channel.c.

static void __unreg_module ( void  ) [static]

Definition at line 652 of file func_channel.c.

static int func_channel_read ( struct ast_channel chan,
const char *  function,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 283 of file func_channel.c.

References ast_channel::_state, ast_channel::accountcode, ast_channel::amaflags, ast_channel::appl, ast_bridged_channel(), ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_check_hangup(), ast_copy_string(), AST_FORMAT_AUDIO_MASK, AST_FORMAT_VIDEO_MASK, ast_getformatname(), ast_log(), ast_print_group(), ast_state2str(), ast_strlen_zero(), ast_channel::callgroup, ast_channel::cdr, ast_channel::context, ast_tone_zone::country, ast_datastore::data, ast_channel::data, ast_channel::exten, ast_channel_tech::func_channel_read, ast_channel::hangupsource, ast_channel::language, ast_channel::linkedid, locked_copy_string, LOG_WARNING, ast_secure_call_store::media, ast_channel::musicclass, ast_channel::name, ast_channel::nativeformats, ast_channel::parkinglot, pbx_builtin_getvar_helper(), ast_channel::peeraccount, ast_channel::readformat, secure_call_info, ast_secure_call_store::signaling, ast_channel::tech, ast_channel::transfercapability, ast_channel_tech::type, ast_channel::uniqueid, ast_channel::userfield, ast_channel::writeformat, and ast_channel::zone.

{
   int ret = 0;

   if (!strcasecmp(data, "audionativeformat"))
      /* use the _multiple version when chan->nativeformats holds multiple formats */
      /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
      ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
   else if (!strcasecmp(data, "videonativeformat"))
      /* use the _multiple version when chan->nativeformats holds multiple formats */
      /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
      ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
   else if (!strcasecmp(data, "audioreadformat"))
      ast_copy_string(buf, ast_getformatname(chan->readformat), len);
   else if (!strcasecmp(data, "audiowriteformat"))
      ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
#ifdef CHANNEL_TRACE
   else if (!strcasecmp(data, "trace")) {
      ast_channel_lock(chan);
      ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
      ast_channel_unlock(chan);
   }
#endif
   else if (!strcasecmp(data, "tonezone") && chan->zone)
      locked_copy_string(chan, buf, chan->zone->country, len);
   else if (!strcasecmp(data, "language"))
      locked_copy_string(chan, buf, chan->language, len);
   else if (!strcasecmp(data, "musicclass"))
      locked_copy_string(chan, buf, chan->musicclass, len);
   else if (!strcasecmp(data, "name")) {
      locked_copy_string(chan, buf, chan->name, len);
   } else if (!strcasecmp(data, "parkinglot"))
      locked_copy_string(chan, buf, chan->parkinglot, len);
   else if (!strcasecmp(data, "state"))
      locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
   else if (!strcasecmp(data, "channeltype"))
      locked_copy_string(chan, buf, chan->tech->type, len);
   else if (!strcasecmp(data, "accountcode"))
      locked_copy_string(chan, buf, chan->accountcode, len);
   else if (!strcasecmp(data, "checkhangup")) {
      ast_channel_lock(chan);
      ast_copy_string(buf, ast_check_hangup(chan) ? "1" : "0", len);
      ast_channel_unlock(chan);
   } else if (!strcasecmp(data, "peeraccount"))
      locked_copy_string(chan, buf, chan->peeraccount, len);
   else if (!strcasecmp(data, "hangupsource"))
      locked_copy_string(chan, buf, chan->hangupsource, len);
   else if (!strcasecmp(data, "appname") && chan->appl)
      locked_copy_string(chan, buf, chan->appl, len);
   else if (!strcasecmp(data, "appdata") && chan->data)
      locked_copy_string(chan, buf, chan->data, len);
   else if (!strcasecmp(data, "exten") && chan->data)
      locked_copy_string(chan, buf, chan->exten, len);
   else if (!strcasecmp(data, "context") && chan->data)
      locked_copy_string(chan, buf, chan->context, len);
   else if (!strcasecmp(data, "userfield") && chan->data)
      locked_copy_string(chan, buf, chan->userfield, len);
   else if (!strcasecmp(data, "channame") && chan->data)
      locked_copy_string(chan, buf, chan->name, len);
   else if (!strcasecmp(data, "linkedid")) {
      ast_channel_lock(chan);
      if (ast_strlen_zero(chan->linkedid)) {
         /* fall back on the channel's uniqueid if linkedid is unset */
         ast_copy_string(buf, chan->uniqueid, len);
      }
      else {
         ast_copy_string(buf, chan->linkedid, len);
      }
      ast_channel_unlock(chan);
   } else if (!strcasecmp(data, "peer")) {
      struct ast_channel *p;
      ast_channel_lock(chan);
      p = ast_bridged_channel(chan);
      if (p || chan->tech || chan->cdr) /* dummy channel? if so, we hid the peer name in the language */
         ast_copy_string(buf, (p ? p->name : ""), len);
      else {
         /* a dummy channel can still pass along bridged peer info via
                           the BRIDGEPEER variable */
         const char *pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
         if (!ast_strlen_zero(pname))
            ast_copy_string(buf, pname, len); /* a horrible kludge, but... how else? */
         else
            buf[0] = 0;
      }
      ast_channel_unlock(chan);
   } else if (!strcasecmp(data, "uniqueid")) {
      locked_copy_string(chan, buf, chan->uniqueid, len);
   } else if (!strcasecmp(data, "transfercapability"))
      locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
   else if (!strcasecmp(data, "callgroup")) {
      char groupbuf[256];
      locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
   } else if (!strcasecmp(data, "amaflags")) {
      char amabuf[256];
      snprintf(amabuf,sizeof(amabuf), "%d", chan->amaflags);
      locked_copy_string(chan, buf, amabuf, len);
   } else if (!strncasecmp(data, "secure_bridge_", 14)) {
      struct ast_datastore *ds;
      ast_channel_lock(chan);
      if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
         struct ast_secure_call_store *encrypt = ds->data;
         if (!strcasecmp(data, "secure_bridge_signaling")) {
            snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
         } else if (!strcasecmp(data, "secure_bridge_media")) {
            snprintf(buf, len, "%s", encrypt->media ? "1" : "");
         }
      }
      ast_channel_unlock(chan);
   } else if (!chan->tech || !chan->tech->func_channel_read || chan->tech->func_channel_read(chan, function, data, buf, len)) {
      ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
      ret = -1;
   }

   return ret;
}
static int func_channel_write ( struct ast_channel chan,
const char *  function,
char *  data,
const char *  value 
) [static]

Definition at line 518 of file func_channel.c.

References AST_CHAN_WRITE_INFO_T_VERSION, ast_channel_setoption(), AST_OPTION_CHANNEL_WRITE, ast_channel::data, func_channel_write_real(), value, and ast_chan_write_info_t::version.

{
   int res;
   ast_chan_write_info_t write_info = {
      .version = AST_CHAN_WRITE_INFO_T_VERSION,
      .write_fn = func_channel_write_real,
      .chan = chan,
      .function = function,
      .data = data,
      .value = value,
   };

   res = func_channel_write_real(chan, function, data, value);
   ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);

   return res;
}
static int func_channel_write_real ( struct ast_channel chan,
const char *  function,
char *  data,
const char *  value 
) [static]

Definition at line 400 of file func_channel.c.

References accountcode, ast_channel::amaflags, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_lock, ast_channel_setoption(), ast_channel_unlock, ast_datastore_alloc(), ast_false(), ast_free, ast_get_group(), ast_get_indication_zone(), ast_log(), AST_OPTION_RXGAIN, AST_OPTION_TXGAIN, ast_set_hangupsource(), ast_tone_zone_ref(), ast_tone_zone_unref(), ast_true(), ast_channel::callgroup, ast_datastore::data, ast_channel_tech::func_channel_write, language, locked_string_field_set, LOG_ERROR, LOG_WARNING, ast_secure_call_store::media, musicclass, parkinglot, secure_call_info, ast_secure_call_store::signaling, ast_channel::tech, ast_channel::transfercapability, and ast_channel::zone.

Referenced by func_channel_write().

{
   int ret = 0;
   signed char gainset;

   if (!strcasecmp(data, "language"))
      locked_string_field_set(chan, language, value);
   else if (!strcasecmp(data, "parkinglot"))
      locked_string_field_set(chan, parkinglot, value);
   else if (!strcasecmp(data, "musicclass"))
      locked_string_field_set(chan, musicclass, value);
   else if (!strcasecmp(data, "accountcode"))
      locked_string_field_set(chan, accountcode, value);
   else if (!strcasecmp(data, "userfield"))
      locked_string_field_set(chan, userfield, value);
   else if (!strcasecmp(data, "amaflags")) {
      ast_channel_lock(chan);
      if(isdigit(*value)) {
         sscanf(value, "%30d", &chan->amaflags);
      } else if (!strcasecmp(value,"OMIT")){
         chan->amaflags = 1;
      } else if (!strcasecmp(value,"BILLING")){
         chan->amaflags = 2;
      } else if (!strcasecmp(value,"DOCUMENTATION")){
         chan->amaflags = 3;
      }
      ast_channel_unlock(chan);
   } else if (!strcasecmp(data, "peeraccount"))
      locked_string_field_set(chan, peeraccount, value);
   else if (!strcasecmp(data, "hangupsource"))
      /* XXX - should we be forcing this here? */
      ast_set_hangupsource(chan, value, 0);
#ifdef CHANNEL_TRACE
   else if (!strcasecmp(data, "trace")) {
      ast_channel_lock(chan);
      if (ast_true(value)) 
         ret = ast_channel_trace_enable(chan);
      else if (ast_false(value))
         ret = ast_channel_trace_disable(chan);
      else {
         ret = -1;
         ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
      }
      ast_channel_unlock(chan);
   }
#endif
   else if (!strcasecmp(data, "tonezone")) {
      struct ast_tone_zone *new_zone;
      if (!(new_zone = ast_get_indication_zone(value))) {
         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
         ret = -1;   
      } else {
         ast_channel_lock(chan);
         if (chan->zone) {
            chan->zone = ast_tone_zone_unref(chan->zone);
         }
         chan->zone = ast_tone_zone_ref(new_zone);
         ast_channel_unlock(chan);
         new_zone = ast_tone_zone_unref(new_zone);
      }
   } else if (!strcasecmp(data, "callgroup"))
      chan->callgroup = ast_get_group(value);
   else if (!strcasecmp(data, "txgain")) {
      sscanf(value, "%4hhd", &gainset);
      ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
   } else if (!strcasecmp(data, "rxgain")) {
      sscanf(value, "%4hhd", &gainset);
      ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
   } else if (!strcasecmp(data, "transfercapability")) {
      unsigned short i;
      for (i = 0; i < 0x20; i++) {
         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
            chan->transfercapability = i;
            break;
         }
      }
   } else if (!strncasecmp(data, "secure_bridge_", 14)) {
      struct ast_datastore *ds;
      struct ast_secure_call_store *store;

      if (!chan || !value) {
         return -1;
      }

      ast_channel_lock(chan);
      if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
         if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) {
            ast_channel_unlock(chan);
            return -1;
         }
         if (!(store = ast_calloc(1, sizeof(*store)))) {
            ast_channel_unlock(chan);
            ast_free(ds);
            return -1;
         }
         ds->data = store;
         ast_channel_datastore_add(chan, ds);
      } else {
         store = ds->data;
      }
      ast_channel_unlock(chan);

      if (!strcasecmp(data, "secure_bridge_signaling")) {
         store->signaling = ast_true(value) ? 1 : 0;
      } else if (!strcasecmp(data, "secure_bridge_media")) {
         store->media = ast_true(value) ? 1 : 0;
      }
   } else if (!chan->tech->func_channel_write
       || chan->tech->func_channel_write(chan, function, data, value)) {
      ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
            data);
      ret = -1;
   }

   return ret;
}
static int func_channels_read ( struct ast_channel chan,
const char *  function,
char *  data,
char *  buf,
size_t  maxlen 
) [static]

Definition at line 542 of file func_channel.c.

References ast_channel_iterator_all_new(), ast_channel_iterator_destroy(), ast_channel_iterator_next(), ast_channel_lock, ast_channel_unlock, ast_channel_unref, ast_log(), ast_strlen_zero(), LOG_WARNING, and ast_channel::name.

{
   struct ast_channel *c = NULL;
   regex_t re;
   int res;
   size_t buflen = 0;
   struct ast_channel_iterator *iter;

   buf[0] = '\0';

   if (!ast_strlen_zero(data)) {
      if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
         regerror(res, &re, buf, maxlen);
         ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
         return -1;
      }
   }

   if (!(iter = ast_channel_iterator_all_new())) {
      if (!ast_strlen_zero(data)) {
         regfree(&re);
      }
      return -1;
   }

   while ((c = ast_channel_iterator_next(iter))) {
      ast_channel_lock(c);
      if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
         size_t namelen = strlen(c->name);
         if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
            if (!ast_strlen_zero(buf)) {
               strcat(buf, " ");
               buflen++;
            }
            strcat(buf, c->name);
            buflen += namelen;
         } else {
            ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space.  Output will be truncated!\n");
         }
      }
      ast_channel_unlock(c);
      c = ast_channel_unref(c);
   }

   ast_channel_iterator_destroy(iter);

   if (!ast_strlen_zero(data)) {
      regfree(&re);
   }

   return 0;
}
static int func_mchan_read ( struct ast_channel chan,
const char *  function,
char *  data,
struct ast_str **  buf,
ssize_t  len 
) [static]

Definition at line 600 of file func_channel.c.

References ast_channel_get_by_name(), ast_channel_unref, ast_str_substitute_variables(), and ast_channel::linkedid.

{
   struct ast_channel *mchan = ast_channel_get_by_name(chan->linkedid);
   char *template = alloca(4 + strlen(data));
   sprintf(template, "${%s}", data); /* SAFE */
   ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
   if (mchan) {
      ast_channel_unref(mchan);
   }
   return 0;
}
static int func_mchan_write ( struct ast_channel chan,
const char *  function,
char *  data,
const char *  value 
) [static]

Definition at line 613 of file func_channel.c.

References ast_channel_get_by_name(), ast_channel_unref, ast_channel::linkedid, and pbx_builtin_setvar_helper().

{
   struct ast_channel *mchan = ast_channel_get_by_name(chan->linkedid);
   pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
   if (mchan) {
      ast_channel_unref(mchan);
   }
   return 0;
}
static int load_module ( void  ) [static]
static int unload_module ( void  ) [static]

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Channel information dialplan functions" , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 652 of file func_channel.c.

Definition at line 652 of file func_channel.c.

Initial value:
 {
   .name = "CHANNEL",
   .read = func_channel_read,
   .write = func_channel_write,
}

Definition at line 536 of file func_channel.c.

Initial value:
 {
   .name = "CHANNELS",
   .read = func_channels_read,
}

Definition at line 595 of file func_channel.c.

Initial value:
 {
   .name = "MASTER_CHANNEL",
   .read2 = func_mchan_read,
   .write = func_mchan_write,
}

Definition at line 624 of file func_channel.c.

const char* const transfercapability_table[0x20] [static]
Initial value:
 {
   "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
   "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
   "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
   "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", }

Definition at line 277 of file func_channel.c.