Wed Jan 8 2020 09:49:55

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 = "ac1f6a56484a8820659555499174e588" , .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 marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

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.

115  {
116  OPT_SKIP = (1 << 0),
117  OPT_INDICATION = (1 << 1),
118  OPT_NOANSWER = (1 << 2),
119 };

Function Documentation

static void __reg_module ( void  )
static

Definition at line 283 of file app_read.c.

static void __unreg_module ( void  )
static

Definition at line 283 of file app_read.c.

static int load_module ( void  )
static

Definition at line 278 of file app_read.c.

References ast_register_application_xml, and read_exec().

279 {
281 }
static int read_exec(struct ast_channel *chan, const char *data)
Definition: app_read.c:129
static char * app
Definition: app_read.c:127
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
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(), read_app_options, ast_pbx::rtimeoutms, status, and ast_channel::zone.

Referenced by load_module().

130 {
131  int res = 0;
132  char tmp[256] = "";
133  int maxdigits = 255;
134  int tries = 1, to = 0, x = 0;
135  double tosec;
136  char *argcopy = NULL;
137  struct ast_tone_zone_sound *ts = NULL;
138  struct ast_flags flags = {0};
139  const char *status = "ERROR";
140 
141  AST_DECLARE_APP_ARGS(arglist,
142  AST_APP_ARG(variable);
143  AST_APP_ARG(filename);
144  AST_APP_ARG(maxdigits);
145  AST_APP_ARG(options);
146  AST_APP_ARG(attempts);
147  AST_APP_ARG(timeout);
148  );
149 
150  pbx_builtin_setvar_helper(chan, "READSTATUS", status);
151  if (ast_strlen_zero(data)) {
152  ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
153  return 0;
154  }
155 
156  argcopy = ast_strdupa(data);
157 
158  AST_STANDARD_APP_ARGS(arglist, argcopy);
159 
160  if (!ast_strlen_zero(arglist.options)) {
161  ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
162  }
163 
164  if (!ast_strlen_zero(arglist.attempts)) {
165  tries = atoi(arglist.attempts);
166  if (tries <= 0)
167  tries = 1;
168  }
169 
170  if (!ast_strlen_zero(arglist.timeout)) {
171  tosec = atof(arglist.timeout);
172  if (tosec <= 0)
173  to = 0;
174  else
175  to = tosec * 1000.0;
176  }
177 
178  if (ast_strlen_zero(arglist.filename)) {
179  arglist.filename = NULL;
180  }
181  if (!ast_strlen_zero(arglist.maxdigits)) {
182  maxdigits = atoi(arglist.maxdigits);
183  if ((maxdigits < 1) || (maxdigits > 255)) {
184  maxdigits = 255;
185  } else
186  ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
187  }
188  if (ast_strlen_zero(arglist.variable)) {
189  ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
190  return 0;
191  }
192  if (ast_test_flag(&flags, OPT_INDICATION)) {
193  if (!ast_strlen_zero(arglist.filename)) {
194  ts = ast_get_indication_tone(chan->zone, arglist.filename);
195  }
196  }
197  if (chan->_state != AST_STATE_UP) {
198  if (ast_test_flag(&flags, OPT_SKIP)) {
199  /* At the user's option, skip if the line is not up */
200  pbx_builtin_setvar_helper(chan, arglist.variable, "");
201  pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
202  return 0;
203  } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
204  /* Otherwise answer unless we're supposed to read while on-hook */
205  res = ast_answer(chan);
206  }
207  }
208  if (!res) {
209  while (tries && !res) {
210  ast_stopstream(chan);
211  if (ts && ts->data[0]) {
212  if (!to)
213  to = chan->pbx ? chan->pbx->rtimeoutms : 6000;
214  res = ast_playtones_start(chan, 0, ts->data, 0);
215  for (x = 0; x < maxdigits; ) {
216  res = ast_waitfordigit(chan, to);
217  ast_playtones_stop(chan);
218  if (res < 1) {
219  if (res == 0)
220  status = "TIMEOUT";
221  tmp[x]='\0';
222  break;
223  }
224  tmp[x++] = res;
225  if (tmp[x-1] == '#') {
226  tmp[x-1] = '\0';
227  status = "OK";
228  break;
229  }
230  if (x >= maxdigits) {
231  status = "OK";
232  }
233  }
234  } else {
235  res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
237  status = "OK";
238  else if (res == AST_GETDATA_TIMEOUT)
239  status = "TIMEOUT";
240  else if (res == AST_GETDATA_INTERRUPTED)
241  status = "INTERRUPTED";
242  }
243  if (res > -1) {
244  pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
245  if (!ast_strlen_zero(tmp)) {
246  ast_verb(3, "User entered '%s'\n", tmp);
247  tries = 0;
248  } else {
249  tries--;
250  if (tries)
251  ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
252  else
253  ast_verb(3, "User entered nothing.\n");
254  }
255  res = 0;
256  } else {
257  pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
258  ast_verb(3, "User disconnected\n");
259  }
260  }
261  }
262 
263  if (ts) {
264  ts = ast_tone_zone_sound_unref(ts);
265  }
266 
267  if (ast_check_hangup(chan))
268  status = "HANGUP";
269  pbx_builtin_setvar_helper(chan, "READSTATUS", status);
270  return 0;
271 }
int ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout)
Plays a stream and gets DTMF data from a channel.
Definition: app.c:178
struct ast_tone_zone * zone
Definition: channel.h:767
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define LOG_WARNING
Definition: logger.h:144
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: app.c:2101
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
unsigned int flags
Definition: utils.h:201
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:411
#define ast_verb(level,...)
Definition: logger.h:243
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static struct ast_tone_zone_sound * ast_tone_zone_sound_unref(struct ast_tone_zone_sound *ts)
Release a reference to an ast_tone_zone_sound.
Definition: indications.h:226
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:806
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
enum ast_channel_state _state
Definition: channel.h:839
Description of a tone.
Definition: indications.h:36
struct ast_tone_zone_sound * ast_get_indication_tone(const struct ast_tone_zone *zone, const char *indication)
Locate a tone zone sound.
Definition: indications.c:473
static struct ast_app_option read_app_options[128]
Definition: app_read.c:125
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
Structure used to handle boolean flags.
Definition: utils.h:200
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
Definition: pbx.c:10546
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3552
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:3086
int ast_playtones_start(struct ast_channel *chan, int vol, const char *tonelist, int interruptible)
Start playing a list of tones on a channel.
Definition: indications.c:319
const char * data
Description of a tone.
Definition: indications.h:53
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:128
jack_status_t status
Definition: app_jack.c:143
struct ast_pbx * pbx
Definition: channel.h:761
int rtimeoutms
Definition: pbx.h:181
static int unload_module ( void  )
static

Definition at line 273 of file app_read.c.

References ast_unregister_application().

274 {
276 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
static char * app
Definition: app_read.c:127

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 283 of file app_read.c.

char* app = "Read"
static

Definition at line 127 of file app_read.c.

Definition at line 283 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 125 of file app_read.c.

Referenced by read_exec().