Asterisk - The Open Source Telephony Project  GIT-master-b7027de
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"

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)
 
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 }, }
 

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

◆ read_option_flags

Enumerator
OPT_SKIP 
OPT_INDICATION 
OPT_NOANSWER 

Definition at line 113 of file app_read.c.

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

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 284 of file app_read.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 284 of file app_read.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 284 of file app_read.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 279 of file app_read.c.

References ast_register_application_xml, and read_exec().

280 {
282 }
static int read_exec(struct ast_channel *chan, const char *data)
Definition: app_read.c:127
static char * app
Definition: app_read.c:125
#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 127 of file app_read.c.

References ast_answer(), AST_APP_ARG, ast_app_getdata(), 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, LOG_WARNING, NULL, OPT_INDICATION, OPT_NOANSWER, OPT_SKIP, options, pbx_builtin_setvar_helper(), read_app_options, ast_pbx::rtimeoutms, status, timeout, and tmp().

Referenced by load_module().

128 {
129  int res = 0;
130  char tmp[256] = "";
131  int maxdigits = 255;
132  int tries = 1, to = 0, x = 0;
133  double tosec;
134  char *argcopy = NULL;
135  struct ast_tone_zone_sound *ts = NULL;
136  struct ast_flags flags = {0};
137  const char *status = "ERROR";
138 
139  AST_DECLARE_APP_ARGS(arglist,
140  AST_APP_ARG(variable);
141  AST_APP_ARG(filename);
142  AST_APP_ARG(maxdigits);
144  AST_APP_ARG(attempts);
146  );
147 
148  pbx_builtin_setvar_helper(chan, "READSTATUS", status);
149  if (ast_strlen_zero(data)) {
150  ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
151  return 0;
152  }
153 
154  argcopy = ast_strdupa(data);
155 
156  AST_STANDARD_APP_ARGS(arglist, argcopy);
157 
158  if (!ast_strlen_zero(arglist.options)) {
159  ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
160  }
161 
162  if (!ast_strlen_zero(arglist.attempts)) {
163  tries = atoi(arglist.attempts);
164  if (tries <= 0)
165  tries = 1;
166  }
167 
168  if (!ast_strlen_zero(arglist.timeout)) {
169  tosec = atof(arglist.timeout);
170  if (tosec <= 0)
171  to = 0;
172  else
173  to = tosec * 1000.0;
174  }
175 
176  if (ast_strlen_zero(arglist.filename)) {
177  arglist.filename = NULL;
178  }
179  if (!ast_strlen_zero(arglist.maxdigits)) {
180  maxdigits = atoi(arglist.maxdigits);
181  if ((maxdigits < 1) || (maxdigits > 255)) {
182  maxdigits = 255;
183  } else
184  ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
185  }
186  if (ast_strlen_zero(arglist.variable)) {
187  ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
188  return 0;
189  }
190  if (ast_test_flag(&flags, OPT_INDICATION)) {
191  if (!ast_strlen_zero(arglist.filename)) {
192  ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
193  }
194  }
195  if (ast_channel_state(chan) != AST_STATE_UP) {
196  if (ast_test_flag(&flags, OPT_SKIP)) {
197  /* At the user's option, skip if the line is not up */
198  if (ts) {
199  ts = ast_tone_zone_sound_unref(ts);
200  }
201  pbx_builtin_setvar_helper(chan, arglist.variable, "");
202  pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
203  return 0;
204  } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
205  /* Otherwise answer unless we're supposed to read while on-hook */
206  res = ast_answer(chan);
207  }
208  }
209  if (!res) {
210  while (tries && !res) {
211  ast_stopstream(chan);
212  if (ts && ts->data[0]) {
213  if (!to)
214  to = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 6000;
215  res = ast_playtones_start(chan, 0, ts->data, 0);
216  for (x = 0; x < maxdigits; ) {
217  res = ast_waitfordigit(chan, to);
218  ast_playtones_stop(chan);
219  if (res < 1) {
220  if (res == 0)
221  status = "TIMEOUT";
222  tmp[x]='\0';
223  break;
224  }
225  tmp[x++] = res;
226  if (tmp[x-1] == '#') {
227  tmp[x-1] = '\0';
228  status = "OK";
229  break;
230  }
231  if (x >= maxdigits) {
232  status = "OK";
233  }
234  }
235  } else {
236  res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
238  status = "OK";
239  else if (res == AST_GETDATA_TIMEOUT)
240  status = "TIMEOUT";
241  else if (res == AST_GETDATA_INTERRUPTED)
242  status = "INTERRUPTED";
243  }
244  if (res > -1) {
245  pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
246  if (!ast_strlen_zero(tmp)) {
247  ast_verb(3, "User entered '%s'\n", tmp);
248  tries = 0;
249  } else {
250  tries--;
251  if (tries)
252  ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
253  else
254  ast_verb(3, "User entered nothing.\n");
255  }
256  res = 0;
257  } else {
258  pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
259  ast_verb(3, "User disconnected\n");
260  }
261  }
262  }
263 
264  if (ts) {
265  ts = ast_tone_zone_sound_unref(ts);
266  }
267 
268  if (ast_check_hangup(chan))
269  status = "HANGUP";
270  pbx_builtin_setvar_helper(chan, "READSTATUS", status);
271  return 0;
272 }
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
static int timeout
Definition: cdr_mysql.c:86
static int tmp()
Definition: bt_open.c:389
unsigned int flags
Definition: utils.h:200
struct ast_tone_zone * ast_channel_zone(const struct ast_channel *chan)
ast_channel_state
ast_channel states
Definition: channelstate.h:35
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:393
#define NULL
Definition: resample.c:96
#define ast_verb(level,...)
Definition: logger.h:455
#define ast_log
Definition: astobj2.c:42
struct ast_pbx * ast_channel_pbx(const struct ast_channel *chan)
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
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
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:2829
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:441
Description of a tone.
Definition: indications.h:35
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
#define ast_strlen_zero(a)
Definition: muted.c:73
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: main/app.c:197
Structure used to handle boolean flags.
Definition: utils.h:199
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...
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3170
static const struct ast_app_option read_app_options[128]
Definition: app_read.c:123
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2800
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
const char * data
Description of a tone.
Definition: indications.h:52
static struct test_options options
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:187
jack_status_t status
Definition: app_jack.c:146
#define AST_APP_ARG(name)
Define an application argument.
int rtimeoutms
Definition: pbx.h:213

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 274 of file app_read.c.

References ast_unregister_application().

275 {
277 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static char * app
Definition: app_read.c:125

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

◆ app

char* app = "Read"
static

Definition at line 125 of file app_read.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 284 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 }, }
static

Definition at line 123 of file app_read.c.

Referenced by read_exec().