Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:09 2009

## Asterisk developer's documentation

# app_page.c File Reference

page() - Paging application More...

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

Include dependency graph for app_page.c:

Go to the source code of this file.

## Data Structures

struct | calloutdata

## Enumerations

enum | { PAGE_DUPLEX = (1 << 0), PAGE_QUIET = (1 << 1) }

## Functions

| AST_APP_OPTIONS (page_opts,{AST_APP_OPTION('d', PAGE_DUPLEX), AST_APP_OPTION('q', PAGE_QUIET),})
char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
static void | launch_page (struct ast_channel *chan, const char *meetmeopts, const char *tech, const char *resource)
int | load_module (void)
| Initialize the module.
static int | page_exec (struct ast_channel *chan, void *data)
static void * | page_thread (void *data)
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static const char * | app_page = "Page"
| LOCAL_USER_DECL
static const char * | page_descrip
enum { ... } | page_opt_flags
static const char * | page_synopsis = "Pages phones"
| STANDARD_LOCAL_USER
static const char * | tdesc = "Page Multiple Phones"

## Detailed Description

page() - Paging application

Definition in file app_page.c.

## Enumeration Type Documentation

anonymous enum

Enumerator:

  PAGE_DUPLEX |
  PAGE_QUIET |

Definition at line 65 of file app_page.c.

    00065      {
    00066    PAGE_DUPLEX = (1 << 0),
    00067    PAGE_QUIET = (1 << 1),
    00068 } page_opt_flags;

## Function Documentation

AST_APP_OPTIONS | ( | page_opts |  | ) |

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 229 of file app_page.c.

    00230 {
    00231    return (char *) tdesc;
    00232 }

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 243 of file app_page.c.

References ASTERISK_GPL_KEY.

    00244 {
    00245    return ASTERISK_GPL_KEY;
    00246 }

static void launch_page | ( | struct ast_channel * | chan,
|  | const char * | meetmeopts,
|  | const char * | tech,
|  | const char * | resource |
| ) |  |  | `[static]`

Definition at line 93 of file app_page.c.

References AST_LIST_TRAVERSE, ast_log(), ast_pthread_create, ast_var_full_name(), ast_var_value(), ast_variable_new(), ast_channel::cid, ast_callerid::cid_name, ast_callerid::cid_num, calloutdata::cidname, calloutdata::cidnum, free, LOG_WARNING, malloc, calloutdata::meetmeopts, ast_variable::next, page_thread(), calloutdata::resource, t, calloutdata::tech, calloutdata::variables, and ast_channel::varshead.

Referenced by page_exec().

    00094 {
    00095    struct calloutdata *cd;
    00096    const char *varname;
    00097    struct ast_variable *lastvar = NULL;
    00098    struct ast_var_t *varptr;
    00099    pthread_t t;
    00100    pthread_attr_t attr;
    00101    cd = malloc(sizeof(struct calloutdata));
    00102    if (cd) {
    00103       memset(cd, 0, sizeof(struct calloutdata));
    00104       ast_copy_string(cd->cidnum, chan->cid.cid_num ? chan->cid.cid_num : "", sizeof(cd->cidnum));
    00105       ast_copy_string(cd->cidname, chan->cid.cid_name ? chan->cid.cid_name : "", sizeof(cd->cidname));
    00106       ast_copy_string(cd->tech, tech, sizeof(cd->tech));
    00107       ast_copy_string(cd->resource, resource, sizeof(cd->resource));
    00108       ast_copy_string(cd->meetmeopts, meetmeopts, sizeof(cd->meetmeopts));
    00109
    00110       AST_LIST_TRAVERSE(&chan->varshead, varptr, entries) {
    00111          if (!(varname = ast_var_full_name(varptr)))
    00112             continue;
    00113          if (varname[0] == '_') {
    00114             struct ast_variable *newvar = NULL;
    00115
    00116             if (varname[1] == '_') {
    00117                newvar = ast_variable_new(varname, ast_var_value(varptr));
    00118             } else {
    00119                newvar = ast_variable_new(&varname[1], ast_var_value(varptr));
    00120             }
    00121
    00122             if (newvar) {
    00123                if (lastvar)
    00124                   lastvar->next = newvar;
    00125                else
    00126                   cd->variables = newvar;
    00127                lastvar = newvar;
    00128             }
    00129          }
    00130       }
    00131
    00132       pthread_attr_init(&attr);
    00133       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    00134       if (ast_pthread_create(&t, &attr, page_thread, cd)) {
    00135          ast_log(LOG_WARNING, "Unable to create paging thread: %s\n", strerror(errno));
    00136          free(cd);
    00137       }
    00138       pthread_attr_destroy(&attr);
    00139    }
    00140 }

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 224 of file app_page.c.

References ast_register_application(), and page_exec().

    00225 {
    00226    return ast_register_application(app_page, page_exec, page_synopsis, page_descrip);
    00227 }

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

Definition at line 142 of file app_page.c.

References app, ast_app_parse_options(), AST_CHANNEL_NAME, ast_log(), ast_strdupa, ast_streamfile(), ast_strlen_zero(), ast_test_flag, ast_waitstream(), ast_channel::language, launch_page(), LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_ERROR, LOG_WARNING, ast_channel::name, PAGE_DUPLEX, PAGE_QUIET, pbx_exec(), pbx_findapp(), and strsep().

Referenced by load_module().

    00143 {
    00144    struct localuser *u;
    00145    char *options;
    00146    char *tech, *resource;
    00147    char meetmeopts[80];
    00148    struct ast_flags flags = { 0 };
    00149    unsigned int confid = rand();
    00150    struct ast_app *app;
    00151    char *tmp;
    00152    int res=0;
    00153    char originator[AST_CHANNEL_NAME];
    00154
    00155    if (ast_strlen_zero(data)) {
    00156       ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
    00157       return -1;
    00158    }
    00159
    00160    LOCAL_USER_ADD(u);
    00161
    00162    if (!(app = pbx_findapp("MeetMe"))) {
    00163       ast_log(LOG_WARNING, "There is no MeetMe application available!\n");
    00164       LOCAL_USER_REMOVE(u);
    00165       return -1;
    00166    };
    00167
    00168    options = ast_strdupa(data);
    00169    if (!options) {
    00170       ast_log(LOG_ERROR, "Out of memory\n");
    00171       LOCAL_USER_REMOVE(u);
    00172       return -1;
    00173    }
    00174
    00175    ast_copy_string(originator, chan->name, sizeof(originator));
    00176    if ((tmp = strchr(originator, '-')))
    00177       *tmp = '\0';
    00178
    00179    tmp = strsep(&options, "|");
    00180    if (options)
    00181       ast_app_parse_options(page_opts, &flags, NULL, options);
    00182
    00183    snprintf(meetmeopts, sizeof(meetmeopts), "%ud|%sqxdw(5)", confid, ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m");
    00184
    00185    while ((tech = strsep(&tmp, "&"))) {
    00186       /* don't call the originating device */
    00187       if (!strcasecmp(tech, originator))
    00188          continue;
    00189
    00190       if ((resource = strchr(tech, '/'))) {
    00191          *resource++ = '\0';
    00192          launch_page(chan, meetmeopts, tech, resource);
    00193       } else {
    00194          ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
    00195       }
    00196    }
    00197
    00198    if (!ast_test_flag(&flags, PAGE_QUIET)) {
    00199       res = ast_streamfile(chan, "beep", chan->language);
    00200       if (!res)
    00201          res = ast_waitstream(chan, "");
    00202    }
    00203    if (!res) {
    00204       snprintf(meetmeopts, sizeof(meetmeopts), "%ud|A%sqxd", confid, ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t");
    00205       pbx_exec(chan, app, meetmeopts, 1);
    00206    }
    00207
    00208    LOCAL_USER_REMOVE(u);
    00209
    00210    return -1;
    00211 }

static void* page_thread | ( | void * | data | ) | `[static]`

Definition at line 84 of file app_page.c.

References AST_FORMAT_SLINEAR, ast_pbx_outgoing_app(), calloutdata::cidname, calloutdata::cidnum, free, calloutdata::meetmeopts, calloutdata::resource, calloutdata::tech, and calloutdata::variables.

Referenced by launch_page().

    00085 {
    00086    struct calloutdata *cd = data;
    00087    ast_pbx_outgoing_app(cd->tech, AST_FORMAT_SLINEAR, cd->resource, 30000,
    00088       "MeetMe", cd->meetmeopts, NULL, 0, cd->cidnum, cd->cidname, cd->variables, NULL, NULL);
    00089    free(cd);
    00090    return NULL;
    00091 }

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 213 of file app_page.c.

References ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00214 {
    00215    int res;
    00216
    00217    res =  ast_unregister_application(app_page);
    00218
    00219    STANDARD_HANGUP_LOCALUSERS;
    00220
    00221    return res;
    00222 }

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 234 of file app_page.c.

References STANDARD_USECOUNT.

    00235 {
    00236    int res;
    00237
    00238    STANDARD_USECOUNT(res);
    00239
    00240    return res;
    00241 }

## Variable Documentation

const char* app_page = "Page"`[static]`

Definition at line 48 of file app_page.c.

LOCAL_USER_DECL

Definition at line 63 of file app_page.c.

const char* page_descrip`[static]`

Definition at line 52 of file app_page.c.

enum { ... } page_opt_flags

const char* page_synopsis = "Pages phones"`[static]`

Definition at line 50 of file app_page.c.

STANDARD_LOCAL_USER

Definition at line 61 of file app_page.c.

const char* tdesc = "Page Multiple Phones"`[static]`

Definition at line 46 of file app_page.c.