Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:59 2009

## Asterisk developer's documentation

# app_chanisavail.c File Reference

Check if Channel is Available. More...

`#include <stdlib.h>`
`#include <stdio.h>`
`#include <string.h>`
`#include <unistd.h>`
`#include <errno.h>`
`#include <sys/ioctl.h>`
`#include "asterisk.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/file.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/module.h"`
`#include "asterisk/app.h"`
`#include "asterisk/devicestate.h"`
`#include "asterisk/options.h"`

Include dependency graph for app_chanisavail.c:

Go to the source code of this file.

## Functions

static int | chanavail_exec (struct ast_channel *chan, void *data)
char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static char * | app = "ChanIsAvail"
static char * | descrip
| LOCAL_USER_DECL
| STANDARD_LOCAL_USER
static char * | synopsis = "Check channel availability"
static char * | tdesc = "Check channel availability"

## Detailed Description

Check if Channel is Available.

Definition in file app_chanisavail.c.

## Function Documentation

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

Definition at line 68 of file app_chanisavail.c.

References ast_device_state(), ast_goto_if_exists(), ast_hangup(), ast_log(), ast_request(), ast_strdupa, ast_strlen_zero(), ast_channel::context, ast_channel::exten, inuse, LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_WARNING, ast_channel::name, ast_channel::nativeformats, option_priority_jumping, pbx_builtin_setvar_helper(), peers, ast_channel::priority, and strsep().

Referenced by load_module().

    00069 {
    00070    int res=-1, inuse=-1, option_state=0, priority_jump=0;
    00071    int status;
    00072    struct localuser *u;
    00073    char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur, *options, *stringp;
    00074    struct ast_channel *tempchan;
    00075
    00076    if (ast_strlen_zero(data)) {
    00077       ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
    00078       return -1;
    00079    }
    00080
    00081    LOCAL_USER_ADD(u);
    00082
    00083    info = ast_strdupa(data);
    00084    stringp = info;
    00085    strsep(&stringp, "|");
    00086    options = strsep(&stringp, "|");
    00087    if (options) {
    00088       if (strchr(options, 's'))
    00089          option_state = 1;
    00090       if (strchr(options, 'j'))
    00091          priority_jump = 1;
    00092    }
    00093    peers = info;
    00094    if (peers) {
    00095       cur = peers;
    00096       do {
    00097          /* remember where to start next time */
    00098          rest = strchr(cur, '&');
    00099          if (rest) {
    00100             *rest = 0;
    00101             rest++;
    00102          }
    00103          tech = cur;
    00104          number = strchr(tech, '/');
    00105          if (!number) {
    00106             ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
    00107             LOCAL_USER_REMOVE(u);
    00108             return -1;
    00109          }
    00110          *number = '\0';
    00111          number++;
    00112
    00113          if (option_state) {
    00114             /* If the pbx says in use then don't bother trying further.
    00115                This is to permit testing if someone's on a call, even if the
    00116                channel can permit more calls (ie callwaiting, sip calls, etc).  */
    00117
    00118             snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
    00119             status = inuse = ast_device_state(trychan);
    00120          }
    00121          if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
    00122                pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
    00123                /* Store the originally used channel too */
    00124                snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
    00125                pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
    00126                snprintf(tmp, sizeof(tmp), "%d", status);
    00127                pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
    00128                ast_hangup(tempchan);
    00129                tempchan = NULL;
    00130                res = 1;
    00131                break;
    00132          } else {
    00133             snprintf(tmp, sizeof(tmp), "%d", status);
    00134             pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
    00135          }
    00136          cur = rest;
    00137       } while (cur);
    00138    }
    00139    if (res < 1) {
    00140       pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
    00141       pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
    00142       if (priority_jump || option_priority_jumping) {
    00143          if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
    00144             LOCAL_USER_REMOVE(u);
    00145             return -1;
    00146          }
    00147       }
    00148    }
    00149
    00150    LOCAL_USER_REMOVE(u);
    00151    return 0;
    00152 }

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 170 of file app_chanisavail.c.

    00171 {
    00172    return tdesc;
    00173 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 182 of file app_chanisavail.c.

References ASTERISK_GPL_KEY.

    00183 {
    00184    return ASTERISK_GPL_KEY;
    00185 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 165 of file app_chanisavail.c.

References ast_register_application(), and chanavail_exec().

    00166 {
    00167    return ast_register_application(app, chanavail_exec, synopsis, descrip);
    00168 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 154 of file app_chanisavail.c.

References ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00155 {
    00156    int res = 0;
    00157
    00158    res = ast_unregister_application(app);
    00159
    00160    STANDARD_HANGUP_LOCALUSERS;
    00161
    00162    return res;
    00163 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 175 of file app_chanisavail.c.

References STANDARD_USECOUNT.

    00176 {
    00177    int res;
    00178    STANDARD_USECOUNT(res);
    00179    return res;
    00180 }

## Variable Documentation

char* app = "ChanIsAvail"`[static]`

Definition at line 49 of file app_chanisavail.c.

char* descrip`[static]`

Definition at line 53 of file app_chanisavail.c.

LOCAL_USER_DECL

Definition at line 66 of file app_chanisavail.c.

STANDARD_LOCAL_USER

Definition at line 64 of file app_chanisavail.c.

char* synopsis = "Check channel availability"`[static]`

Definition at line 51 of file app_chanisavail.c.

char* tdesc = "Check channel availability"`[static]`

Definition at line 47 of file app_chanisavail.c.