Thu Apr 28 2011 17:13:36

Asterisk developer's documentation


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)
static int chanavail_exec (struct ast_channel *chan, void *data)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_module_info
__MODULE_INFO_SECTION 
__mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Check channel availability" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static char * app = "ChanIsAvail"
static struct ast_module_infoast_module_info = &__mod_info

Detailed Description

Check if Channel is Available.

Author:
Mark Spencer <markster@digium.com>
James Golovich <james@gnuinter.net>

Definition in file app_chanisavail.c.


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 206 of file app_chanisavail.c.

static void __unreg_module ( void  ) [static]

Definition at line 206 of file app_chanisavail.c.

static int chanavail_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 97 of file app_chanisavail.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, 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_strlen_zero(), inuse, LOG_WARNING, ast_channel::name, ast_channel::nativeformats, pbx_builtin_setvar_helper(), peers, and status.

Referenced by load_module().

{
   int inuse=-1, option_state=0, string_compare=0, option_all_avail=0;
   int status;
   char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
   struct ast_str *tmp_availchan = ast_str_alloca(2048);
   struct ast_str *tmp_availorig = ast_str_alloca(2048);
   struct ast_str *tmp_availstat = ast_str_alloca(2048);
   struct ast_channel *tempchan;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(reqchans);
      AST_APP_ARG(options);
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "ChanIsAvail requires an argument (DAHDI/1&DAHDI/2)\n");
      return -1;
   }

   info = ast_strdupa(data);

   AST_STANDARD_APP_ARGS(args, info);

   if (args.options) {
      if (strchr(args.options, 'a')) {
         option_all_avail = 1;
      }
      if (strchr(args.options, 's')) {
         option_state = 1;
      }
      if (strchr(args.options, 't')) {
         string_compare = 1;
      }
   }
   peers = args.reqchans;
   if (peers) {
      cur = peers;
      do {
         /* remember where to start next time */
         rest = strchr(cur, '&');
         if (rest) {
            *rest = 0;
            rest++;
         }
         tech = cur;
         number = strchr(tech, '/');
         if (!number) {
            ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
            return -1;
         }
         *number = '\0';
         number++;
         
         if (string_compare) {
            /* ast_parse_device_state checks for "SIP/1234" as a channel name.
               ast_device_state will ask the SIP driver for the channel state. */

            snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
            status = inuse = ast_parse_device_state(trychan);
         } else if (option_state) {
            /* If the pbx says in use then don't bother trying further.
               This is to permit testing if someone's on a call, even if the
               channel can permit more calls (ie callwaiting, sip calls, etc).  */

            snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
            status = inuse = ast_device_state(trychan);
         }
         if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
               ast_str_append(&tmp_availchan, 0, "%s%s", ast_str_strlen(tmp_availchan) ? "&" : "", tempchan->name);
               
               snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
               ast_str_append(&tmp_availorig, 0, "%s%s", ast_str_strlen(tmp_availorig) ? "&" : "", tmp);

               snprintf(tmp, sizeof(tmp), "%d", status);
               ast_str_append(&tmp_availstat, 0, "%s%s", ast_str_strlen(tmp_availstat) ? "&" : "", tmp);

               ast_hangup(tempchan);
               tempchan = NULL;

               if (!option_all_avail) {
                  break;
               }
         } else {
            snprintf(tmp, sizeof(tmp), "%d", status);
            ast_str_append(&tmp_availstat, 0, "%s%s", ast_str_strlen(tmp_availstat) ? "&" : "", tmp);
         }
         cur = rest;
      } while (cur);
   }

   pbx_builtin_setvar_helper(chan, "AVAILCHAN", ast_str_buffer(tmp_availchan));
   /* Store the originally used channel too */
   pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", ast_str_buffer(tmp_availorig));
   pbx_builtin_setvar_helper(chan, "AVAILSTATUS", ast_str_buffer(tmp_availstat));

   return 0;
}
static int unload_module ( void  ) [static]

Definition at line 195 of file app_chanisavail.c.

References ast_unregister_application().


Variable Documentation

struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Check channel availability" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 206 of file app_chanisavail.c.

char* app = "ChanIsAvail" [static]

Definition at line 44 of file app_chanisavail.c.

Definition at line 206 of file app_chanisavail.c.