Wed Jan 27 20:02:19 2016

Asterisk developer's documentation


app_read.c File Reference

Trivial application to read a variable. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/indications.h"

Go to the source code of this file.

Enumerations

enum  read_option_flags { 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, const char *data)
static int unload_module (void)

Variables

static char * app = "Read"

Detailed Description

Trivial application to read a variable.

Author:
Mark Spencer <markster@digium.com>

Definition in file app_read.c.


Enumeration Type Documentation

Enumerator:
OPT_SKIP 
OPT_INDICATION 
OPT_NOANSWER 

Definition at line 115 of file app_read.c.

00115                        {
00116    OPT_SKIP = (1 << 0),
00117    OPT_INDICATION = (1 << 1),
00118    OPT_NOANSWER = (1 << 2),
00119 };


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 278 of file app_read.c.

References ast_register_application_xml, and read_exec().

00279 {
00280    return ast_register_application_xml(app, read_exec);
00281 }

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

Definition at line 129 of file app_read.c.

References ast_channel::_state, ast_answer(), AST_APP_ARG, ast_app_getdata(), ast_app_parse_options(), ast_check_hangup(), AST_DECLARE_APP_ARGS, ast_get_indication_tone(), AST_GETDATA_COMPLETE, AST_GETDATA_EMPTY_END_TERMINATED, AST_GETDATA_INTERRUPTED, AST_GETDATA_TIMEOUT, ast_log(), ast_playtones_start(), ast_playtones_stop(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_stopstream(), ast_strdupa, ast_strlen_zero(), ast_test_flag, ast_tone_zone_sound_unref(), ast_verb, ast_waitfordigit(), ast_tone_zone_sound::data, LOG_WARNING, OPT_INDICATION, OPT_NOANSWER, OPT_SKIP, ast_channel::pbx, pbx_builtin_setvar_helper(), ast_pbx::rtimeoutms, status, and ast_channel::zone.

Referenced by load_module().

00130 {
00131    int res = 0;
00132    char tmp[256] = "";
00133    int maxdigits = 255;
00134    int tries = 1, to = 0, x = 0;
00135    double tosec;
00136    char *argcopy = NULL;
00137    struct ast_tone_zone_sound *ts = NULL;
00138    struct ast_flags flags = {0};
00139    const char *status = "ERROR";
00140 
00141    AST_DECLARE_APP_ARGS(arglist,
00142       AST_APP_ARG(variable);
00143       AST_APP_ARG(filename);
00144       AST_APP_ARG(maxdigits);
00145       AST_APP_ARG(options);
00146       AST_APP_ARG(attempts);
00147       AST_APP_ARG(timeout);
00148    );
00149    
00150    pbx_builtin_setvar_helper(chan, "READSTATUS", status);
00151    if (ast_strlen_zero(data)) {
00152       ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
00153       return 0;
00154    }
00155    
00156    argcopy = ast_strdupa(data);
00157 
00158    AST_STANDARD_APP_ARGS(arglist, argcopy);
00159 
00160    if (!ast_strlen_zero(arglist.options)) {
00161       ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
00162    }
00163    
00164    if (!ast_strlen_zero(arglist.attempts)) {
00165       tries = atoi(arglist.attempts);
00166       if (tries <= 0)
00167          tries = 1;
00168    }
00169 
00170    if (!ast_strlen_zero(arglist.timeout)) {
00171       tosec = atof(arglist.timeout);
00172       if (tosec <= 0)
00173          to = 0;
00174       else
00175          to = tosec * 1000.0;
00176    }
00177 
00178    if (ast_strlen_zero(arglist.filename)) {
00179       arglist.filename = NULL;
00180    }
00181    if (!ast_strlen_zero(arglist.maxdigits)) {
00182       maxdigits = atoi(arglist.maxdigits);
00183       if ((maxdigits < 1) || (maxdigits > 255)) {
00184          maxdigits = 255;
00185       } else
00186          ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
00187    }
00188    if (ast_strlen_zero(arglist.variable)) {
00189       ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
00190       return 0;
00191    }
00192    if (ast_test_flag(&flags, OPT_INDICATION)) {
00193       if (!ast_strlen_zero(arglist.filename)) {
00194          ts = ast_get_indication_tone(chan->zone, arglist.filename);
00195       }
00196    }
00197    if (chan->_state != AST_STATE_UP) {
00198       if (ast_test_flag(&flags, OPT_SKIP)) {
00199          /* At the user's option, skip if the line is not up */
00200          pbx_builtin_setvar_helper(chan, arglist.variable, "");
00201          pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
00202          return 0;
00203       } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
00204          /* Otherwise answer unless we're supposed to read while on-hook */
00205          res = ast_answer(chan);
00206       }
00207    }
00208    if (!res) {
00209       while (tries && !res) {
00210          ast_stopstream(chan);
00211          if (ts && ts->data[0]) {
00212             if (!to)
00213                to = chan->pbx ? chan->pbx->rtimeoutms : 6000;
00214             res = ast_playtones_start(chan, 0, ts->data, 0);
00215             for (x = 0; x < maxdigits; ) {
00216                res = ast_waitfordigit(chan, to);
00217                ast_playtones_stop(chan);
00218                if (res < 1) {
00219                   if (res == 0)
00220                      status = "TIMEOUT";
00221                   tmp[x]='\0';
00222                   break;
00223                }
00224                tmp[x++] = res;
00225                if (tmp[x-1] == '#') {
00226                   tmp[x-1] = '\0';
00227                   status = "OK";
00228                   break;
00229                }
00230                if (x >= maxdigits) {
00231                   status = "OK";
00232                }
00233             }
00234          } else {
00235             res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
00236             if (res == AST_GETDATA_COMPLETE || res == AST_GETDATA_EMPTY_END_TERMINATED)
00237                status = "OK";
00238             else if (res == AST_GETDATA_TIMEOUT)
00239                status = "TIMEOUT";
00240             else if (res == AST_GETDATA_INTERRUPTED)
00241                status = "INTERRUPTED";
00242          }
00243          if (res > -1) {
00244             pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
00245             if (!ast_strlen_zero(tmp)) {
00246                ast_verb(3, "User entered '%s'\n", tmp);
00247                tries = 0;
00248             } else {
00249                tries--;
00250                if (tries)
00251                   ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
00252                else
00253                   ast_verb(3, "User entered nothing.\n");
00254             }
00255             res = 0;
00256          } else {
00257             pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
00258             ast_verb(3, "User disconnected\n");
00259          }
00260       }
00261    }
00262 
00263    if (ts) {
00264       ts = ast_tone_zone_sound_unref(ts);
00265    }
00266 
00267    if (ast_check_hangup(chan))
00268       status = "HANGUP";
00269    pbx_builtin_setvar_helper(chan, "READSTATUS", status);
00270    return 0;
00271 }

static int unload_module ( void   )  [static]

Definition at line 273 of file app_read.c.

References ast_unregister_application().

00274 {
00275    return ast_unregister_application(app);
00276 }


Variable Documentation

char* app = "Read" [static]

Definition at line 127 of file app_read.c.


Generated on 27 Jan 2016 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1