Mon Jun 27 16:50:59 2011

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

static void __reg_module (void)
static void __unreg_module (void)
static int load_module (void)
static int read_exec (struct ast_channel *chan, const char *data)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Read Variable Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static char * app = "Read"
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_app_option read_app_options [128] = { [ 's' ] = { .flag = OPT_SKIP }, [ 'i' ] = { .flag = OPT_INDICATION }, [ 'n' ] = { .flag = OPT_NOANSWER },}


Detailed Description

Trivial application to read a variable.

Author:
Mark Spencer <markster@digium.com>

Definition in file app_read.c.


Enumeration Type Documentation

enum read_option_flags

Enumerator:
OPT_SKIP 
OPT_INDICATION 
OPT_NOANSWER 

Definition at line 111 of file app_read.c.

00111                        {
00112    OPT_SKIP = (1 << 0),
00113    OPT_INDICATION = (1 << 1),
00114    OPT_NOANSWER = (1 << 2),
00115 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 279 of file app_read.c.

static void __unreg_module ( void   )  [static]

Definition at line 279 of file app_read.c.

static int load_module ( void   )  [static]

Definition at line 274 of file app_read.c.

References ast_register_application_xml, and read_exec().

00275 {
00276    return ast_register_application_xml(app, read_exec);
00277 }

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

Definition at line 125 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, ast_flags::flags, LOG_WARNING, OPT_INDICATION, OPT_NOANSWER, OPT_SKIP, pbx_builtin_setvar_helper(), read_app_options, status, and ast_channel::zone.

Referenced by load_module().

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

static int unload_module ( void   )  [static]

Definition at line 269 of file app_read.c.

References ast_unregister_application().

00270 {
00271    return ast_unregister_application(app);
00272 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Read Variable Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 279 of file app_read.c.

char* app = "Read" [static]

Definition at line 123 of file app_read.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 279 of file app_read.c.

struct ast_app_option read_app_options[128] = { [ 's' ] = { .flag = OPT_SKIP }, [ 'i' ] = { .flag = OPT_INDICATION }, [ 'n' ] = { .flag = OPT_NOANSWER },} [static]

Definition at line 121 of file app_read.c.

Referenced by read_exec().


Generated on Mon Jun 27 16:50:59 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7