Asterisk - The Open Source Telephony Project  GIT-master-a24979a
Enumerations | Functions | Variables
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"
Include dependency graph for app_read.c:

Go to the source code of this file.

Enumerations

enum  { OPT_ARG_TERMINATOR , OPT_ARG_ARRAY_SIZE }
 
enum  read_option_flags {
  OPT_DELAY = (1 << 0) , OPT_MUTE = (1 << 1) , OPT_QUELCH = (1 << 2) , OPT_RELAXED = (1 << 3) ,
  OPT_LAX_KP = (1 << 4) , OPT_PROCESS = (1 << 5) , OPT_NO_KP = (1 << 6) , OPT_NO_ST = (1 << 7) ,
  OPT_KP_OVERRIDE = (1 << 8) , OPT_MAXDIGITS = (1 << 9) , OPT_SKIP = (1 << 0) , OPT_INDICATION = (1 << 1) ,
  OPT_NOANSWER = (1 << 2) , OPT_TERMINATOR = (1 << 3) , OPT_DELAY = (1 << 0) , OPT_MUTE = (1 << 1) ,
  OPT_QUELCH = (1 << 2) , OPT_RELAXED = (1 << 3) , OPT_EXTRAPULSES = (1 << 4)
}
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static char * app = "Read"
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const struct ast_app_option read_app_options [128] = { [ 's' ] = { .flag = OPT_SKIP }, [ 'i' ] = { .flag = OPT_INDICATION }, [ 'n' ] = { .flag = OPT_NOANSWER }, [ 't' ] = { .flag = OPT_TERMINATOR , .arg_index = OPT_ARG_TERMINATOR + 1 }, }
 

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

◆ anonymous enum

anonymous enum
Enumerator
OPT_ARG_TERMINATOR 
OPT_ARG_ARRAY_SIZE 

Definition at line 130 of file app_read.c.

130  {
132  /* note: this entry _MUST_ be the last one in the enum */
134 };
@ OPT_ARG_TERMINATOR
Definition: app_read.c:131
@ OPT_ARG_ARRAY_SIZE
Definition: app_read.c:133

◆ read_option_flags

Enumerator
OPT_DELAY 
OPT_MUTE 
OPT_QUELCH 
OPT_RELAXED 
OPT_LAX_KP 
OPT_PROCESS 
OPT_NO_KP 
OPT_NO_ST 
OPT_KP_OVERRIDE 
OPT_MAXDIGITS 
OPT_SKIP 
OPT_INDICATION 
OPT_NOANSWER 
OPT_TERMINATOR 
OPT_DELAY 
OPT_MUTE 
OPT_QUELCH 
OPT_RELAXED 
OPT_EXTRAPULSES 

Definition at line 123 of file app_read.c.

123  {
124  OPT_SKIP = (1 << 0),
125  OPT_INDICATION = (1 << 1),
126  OPT_NOANSWER = (1 << 2),
127  OPT_TERMINATOR = (1 << 3),
128 };
@ OPT_TERMINATOR
Definition: app_read.c:127
@ OPT_NOANSWER
Definition: app_read.c:126
@ OPT_INDICATION
Definition: app_read.c:125
@ OPT_SKIP
Definition: app_read.c:124

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 311 of file app_read.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 311 of file app_read.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 311 of file app_read.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 306 of file app_read.c.

307 {
309 }
static int read_exec(struct ast_channel *chan, const char *data)
Definition: app_read.c:145
static char * app
Definition: app_read.c:143
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

◆ read_exec()

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

Definition at line 145 of file app_read.c.

146 {
147  int res = 0;
148  char tmp[256] = "";
149  int maxdigits = 255;
150  int tries = 1, to = 0, x = 0;
151  double tosec;
152  char *argcopy = NULL;
153  char *opt_args[OPT_ARG_ARRAY_SIZE];
154  struct ast_tone_zone_sound *ts = NULL;
155  struct ast_flags flags = {0};
156  const char *status = "ERROR";
157  char *terminator = "#"; /* use default terminator # by default */
158 
159  AST_DECLARE_APP_ARGS(arglist,
160  AST_APP_ARG(variable);
161  AST_APP_ARG(filename);
162  AST_APP_ARG(maxdigits);
164  AST_APP_ARG(attempts);
165  AST_APP_ARG(timeout);
166  );
167 
168  pbx_builtin_setvar_helper(chan, "READSTATUS", status);
169  if (ast_strlen_zero(data)) {
170  ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
171  return 0;
172  }
173 
174  argcopy = ast_strdupa(data);
175 
176  AST_STANDARD_APP_ARGS(arglist, argcopy);
177 
178  if (!ast_strlen_zero(arglist.options)) {
179  ast_app_parse_options(read_app_options, &flags, opt_args, arglist.options);
180  }
181 
182  if (!ast_strlen_zero(arglist.attempts)) {
183  tries = atoi(arglist.attempts);
184  if (tries <= 0)
185  tries = 1;
186  }
187 
188  if (!ast_strlen_zero(arglist.timeout)) {
189  tosec = atof(arglist.timeout);
190  if (tosec <= 0)
191  to = 0;
192  else
193  to = tosec * 1000.0;
194  }
195 
196  if (ast_strlen_zero(arglist.filename)) {
197  arglist.filename = NULL;
198  }
199  if (!ast_strlen_zero(arglist.maxdigits)) {
200  maxdigits = atoi(arglist.maxdigits);
201  if ((maxdigits < 1) || (maxdigits > 255)) {
202  maxdigits = 255;
203  } else
204  ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
205  }
206  if (ast_strlen_zero(arglist.variable)) {
207  ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
208  return 0;
209  }
211  if (!ast_strlen_zero(arglist.filename)) {
212  ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
213  }
214  }
216  if (!ast_strlen_zero(opt_args[OPT_ARG_TERMINATOR])) {
217  terminator = opt_args[OPT_ARG_TERMINATOR];
218  } else {
219  terminator = ""; /* no digit inherently will terminate input */
220  }
221  }
222  if (ast_channel_state(chan) != AST_STATE_UP) {
223  if (ast_test_flag(&flags, OPT_SKIP)) {
224  /* At the user's option, skip if the line is not up */
225  if (ts) {
226  ts = ast_tone_zone_sound_unref(ts);
227  }
228  pbx_builtin_setvar_helper(chan, arglist.variable, "");
229  pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
230  return 0;
231  } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
232  /* Otherwise answer unless we're supposed to read while on-hook */
233  res = ast_answer(chan);
234  }
235  }
236  if (!res) {
237  while (tries && !res) {
238  ast_stopstream(chan);
239  if (ts && ts->data[0]) {
240  if (!to)
241  to = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 6000;
242  res = ast_playtones_start(chan, 0, ts->data, 0);
243  for (x = 0; x < maxdigits; ) {
244  res = ast_waitfordigit(chan, to);
245  ast_playtones_stop(chan);
246  if (res < 1) {
247  if (res == 0)
248  status = "TIMEOUT";
249  tmp[x]='\0';
250  break;
251  }
252  tmp[x++] = res;
253  if (terminator && strchr(terminator, tmp[x-1])) {
254  tmp[x-1] = '\0';
255  status = "OK";
256  break;
257  }
258  if (x >= maxdigits) {
259  status = "OK";
260  }
261  }
262  } else {
263  res = ast_app_getdata_terminator(chan, arglist.filename, tmp, maxdigits, to, terminator);
265  status = "OK";
266  else if (res == AST_GETDATA_TIMEOUT)
267  status = "TIMEOUT";
268  else if (res == AST_GETDATA_INTERRUPTED)
269  status = "INTERRUPTED";
270  }
271  if (res > -1) {
272  pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
273  if (!ast_strlen_zero(tmp)) {
274  ast_verb(3, "User entered '%s'\n", tmp);
275  tries = 0;
276  } else {
277  tries--;
278  if (tries)
279  ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
280  else
281  ast_verb(3, "User entered nothing.\n");
282  }
283  res = 0;
284  } else {
285  pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
286  ast_verb(3, "User disconnected\n");
287  }
288  }
289  }
290 
291  if (ts) {
292  ts = ast_tone_zone_sound_unref(ts);
293  }
294 
295  if (ast_check_hangup(chan))
296  status = "HANGUP";
297  pbx_builtin_setvar_helper(chan, "READSTATUS", status);
298  return 0;
299 }
jack_status_t status
Definition: app_jack.c:146
static const struct ast_app_option read_app_options[128]
Definition: app_read.c:141
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
static int tmp()
Definition: bt_open.c:389
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3176
struct ast_pbx * ast_channel_pbx(const struct ast_channel *chan)
struct ast_tone_zone * ast_channel_zone(const struct ast_channel *chan)
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:445
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2806
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:222
#define AST_APP_ARG(name)
Define an application argument.
@ AST_GETDATA_INTERRUPTED
@ AST_GETDATA_COMPLETE
@ AST_GETDATA_TIMEOUT
@ AST_GETDATA_EMPTY_END_TERMINATED
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
int ast_app_getdata_terminator(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout, char *terminator)
Plays a stream and gets DTMF data from a channel.
Definition: main/app.c:193
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: main/app.c:3126
#define ast_verb(level,...)
#define LOG_WARNING
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:455
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:302
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:227
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:393
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.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
int rtimeoutms
Definition: pbx.h:216
Description of a tone.
Definition: indications.h:35
const char * data
Description of a tone.
Definition: indications.h:52
static struct test_options options
#define ast_test_flag(p, flag)
Definition: utils.h:63

References ast_answer(), AST_APP_ARG, ast_app_getdata_terminator(), ast_app_parse_options(), ast_channel_pbx(), ast_channel_zone(), 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, NULL, OPT_ARG_ARRAY_SIZE, OPT_ARG_TERMINATOR, OPT_INDICATION, OPT_NOANSWER, OPT_SKIP, OPT_TERMINATOR, options, pbx_builtin_setvar_helper(), read_app_options, ast_pbx::rtimeoutms, status, and tmp().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 301 of file app_read.c.

302 {
304 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

References app, and ast_unregister_application().

Variable Documentation

◆ __mod_info

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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 306 of file app_read.c.

◆ app

char* app = "Read"
static

Definition at line 143 of file app_read.c.

Referenced by unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 311 of file app_read.c.

◆ read_app_options

const struct ast_app_option read_app_options[128] = { [ 's' ] = { .flag = OPT_SKIP }, [ 'i' ] = { .flag = OPT_INDICATION }, [ 'n' ] = { .flag = OPT_NOANSWER }, [ 't' ] = { .flag = OPT_TERMINATOR , .arg_index = OPT_ARG_TERMINATOR + 1 }, }
static

Definition at line 1 of file app_read.c.

Referenced by read_exec().