Mon Mar 31 07:38:39 2008

Asterisk developer's documentation


app_read.c File Reference

Trivial application to read a variable. More...

#include "asterisk.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/indications.h"

Include dependency graph for app_read.c:

Go to the source code of this file.

Defines

#define ast_next_data(instr, ptr, delim)   if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}

Enumerations

enum  { OPT_SKIP = (1 << 0), OPT_INDICATION = (1 << 1), OPT_NOANSWER = (1 << 2) }

Functions

 AST_APP_OPTIONS (read_app_options,{AST_APP_OPTION('s', OPT_SKIP), AST_APP_OPTION('i', OPT_INDICATION), AST_APP_OPTION('n', OPT_NOANSWER),})
 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Read Variable Application")
static int load_module (void)
static int read_exec (struct ast_channel *chan, void *data)
static int unload_module (void)

Variables

static char * app = "Read"
static char * descrip
enum { ... }  read_option_flags
static char * synopsis = "Read a variable"


Detailed Description

Trivial application to read a variable.

Author:
Mark Spencer <markster@digium.com>

Definition in file app_read.c.


Define Documentation

#define ast_next_data ( instr,
ptr,
delim   )     if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}

Definition at line 85 of file app_read.c.


Enumeration Type Documentation

anonymous enum

Enumerator:
OPT_SKIP 
OPT_INDICATION 
OPT_NOANSWER 

Definition at line 48 of file app_read.c.

00048      {
00049    OPT_SKIP = (1 << 0),
00050    OPT_INDICATION = (1 << 1),
00051    OPT_NOANSWER = (1 << 2),
00052 } read_option_flags;


Function Documentation

AST_APP_OPTIONS ( read_app_options   ) 

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Read Variable Application"   
)

static int load_module ( void   )  [static]

Definition at line 229 of file app_read.c.

References ast_register_application(), and read_exec().

00230 {
00231    return ast_register_application(app, read_exec, synopsis, descrip);
00232 }

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

Definition at line 87 of file app_read.c.

References ast_channel::_state, ast_answer(), AST_APP_ARG, ast_app_getdata(), ast_app_parse_options(), AST_DECLARE_APP_ARGS, ast_get_indication_tone(), ast_log(), ast_module_user_add, ast_module_user_remove, ast_playtones_start(), ast_playtones_stop(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_stopstream(), ast_strdupa, ast_strlen_zero(), ast_test_flag, ast_verbose(), ast_waitfordigit(), tone_zone_sound::data, ast_flags::flags, LOG_WARNING, OPT_INDICATION, OPT_NOANSWER, OPT_SKIP, option_verbose, pbx_builtin_setvar_helper(), timeout, VERBOSE_PREFIX_3, and ast_channel::zone.

Referenced by load_module().

00088 {
00089    int res = 0;
00090    struct ast_module_user *u;
00091    char tmp[256] = "";
00092    int maxdigits = 255;
00093    int tries = 1, to = 0, x = 0;
00094    char *argcopy = NULL;
00095    struct tone_zone_sound *ts;
00096    struct ast_flags flags = {0};
00097 
00098     AST_DECLARE_APP_ARGS(arglist,
00099       AST_APP_ARG(variable);
00100       AST_APP_ARG(filename);
00101       AST_APP_ARG(maxdigits);
00102       AST_APP_ARG(options);
00103       AST_APP_ARG(attempts);
00104       AST_APP_ARG(timeout);
00105    );
00106    
00107    if (ast_strlen_zero(data)) {
00108       ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
00109       return -1;
00110    }
00111 
00112    u = ast_module_user_add(chan);
00113    
00114    argcopy = ast_strdupa(data);
00115 
00116    AST_STANDARD_APP_ARGS(arglist, argcopy);
00117 
00118    if (!ast_strlen_zero(arglist.options)) {
00119       ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
00120    }
00121    
00122    if (!ast_strlen_zero(arglist.attempts)) {
00123       tries = atoi(arglist.attempts);
00124       if (tries <= 0)
00125          tries = 1;
00126    }
00127 
00128    if (!ast_strlen_zero(arglist.timeout)) {
00129       to = atoi(arglist.timeout);
00130       if (to <= 0)
00131          to = 0;
00132       else
00133          to *= 1000;
00134    }
00135 
00136    if (ast_strlen_zero(arglist.filename)) {
00137       arglist.filename = NULL;
00138    }
00139    if (!ast_strlen_zero(arglist.maxdigits)) {
00140       maxdigits = atoi(arglist.maxdigits);
00141       if ((maxdigits<1) || (maxdigits>255)) {
00142             maxdigits = 255;
00143       } else if (option_verbose > 2)
00144          ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %d digits.\n", maxdigits);
00145    }
00146    if (ast_strlen_zero(arglist.variable)) {
00147       ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n");
00148       ast_module_user_remove(u);
00149       return -1;
00150    }
00151    ts=NULL;
00152    if (ast_test_flag(&flags,OPT_INDICATION)) {
00153       if (!ast_strlen_zero(arglist.filename)) {
00154          ts = ast_get_indication_tone(chan->zone,arglist.filename);
00155       }
00156    }
00157    if (chan->_state != AST_STATE_UP) {
00158       if (ast_test_flag(&flags,OPT_SKIP)) {
00159          /* At the user's option, skip if the line is not up */
00160          pbx_builtin_setvar_helper(chan, arglist.variable, "\0");
00161          ast_module_user_remove(u);
00162          return 0;
00163       } else if (!ast_test_flag(&flags,OPT_NOANSWER)) {
00164          /* Otherwise answer unless we're supposed to read while on-hook */
00165          res = ast_answer(chan);
00166       }
00167    }
00168    if (!res) {
00169       while (tries && !res) {
00170          ast_stopstream(chan);
00171          if (ts && ts->data[0]) {
00172             if (!to)
00173                to = chan->pbx ? chan->pbx->rtimeout * 1000 : 6000;
00174             res = ast_playtones_start(chan, 0, ts->data, 0);
00175             for (x = 0; x < maxdigits; ) {
00176                res = ast_waitfordigit(chan, to);
00177                ast_playtones_stop(chan);
00178                if (res < 1) {
00179                   tmp[x]='\0';
00180                   break;
00181                }
00182                tmp[x++] = res;
00183                if (tmp[x-1] == '#') {
00184                   tmp[x-1] = '\0';
00185                   break;
00186                }
00187             }
00188          } else {
00189             res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
00190          }
00191          if (res > -1) {
00192             pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
00193             if (!ast_strlen_zero(tmp)) {
00194                if (option_verbose > 2)
00195                   ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
00196                tries = 0;
00197             } else {
00198                tries--;
00199                if (option_verbose > 2) {
00200                   if (tries)
00201                      ast_verbose(VERBOSE_PREFIX_3 "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
00202                   else
00203                      ast_verbose(VERBOSE_PREFIX_3 "User entered nothing.\n");
00204                }
00205             }
00206             res = 0;
00207          } else {
00208             pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
00209             if (option_verbose > 2)
00210                ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
00211          }
00212       }
00213    }
00214    ast_module_user_remove(u);
00215    return res;
00216 }

static int unload_module ( void   )  [static]

Definition at line 218 of file app_read.c.

References ast_module_user_hangup_all, and ast_unregister_application().

00219 {
00220    int res;
00221 
00222    res = ast_unregister_application(app);
00223    
00224    ast_module_user_hangup_all();
00225 
00226    return res; 
00227 }


Variable Documentation

char* app = "Read" [static]

Definition at line 60 of file app_read.c.

char* descrip [static]

Definition at line 64 of file app_read.c.

enum { ... } read_option_flags

char* synopsis = "Read a variable" [static]

Definition at line 62 of file app_read.c.


Generated on Mon Mar 31 07:38:40 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1