Asterisk - The Open Source Telephony Project  GIT-master-9ed6387
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 281 of file app_read.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 281 of file app_read.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 281 of file app_read.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 276 of file app_read.c.

References ast_register_application_xml, and read_exec().

277 {
279 }
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  pbx_builtin_setvar_helper(chan, arglist.variable, "");
199  pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
200  return 0;
201  } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
202  /* Otherwise answer unless we're supposed to read while on-hook */
203  res = ast_answer(chan);
204  }
205  }
206  if (!res) {
207  while (tries && !res) {
208  ast_stopstream(chan);
209  if (ts && ts->data[0]) {
210  if (!to)
211  to = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 6000;
212  res = ast_playtones_start(chan, 0, ts->data, 0);
213  for (x = 0; x < maxdigits; ) {
214  res = ast_waitfordigit(chan, to);
215  ast_playtones_stop(chan);
216  if (res < 1) {
217  if (res == 0)
218  status = "TIMEOUT";
219  tmp[x]='\0';
220  break;
221  }
222  tmp[x++] = res;
223  if (tmp[x-1] == '#') {
224  tmp[x-1] = '\0';
225  status = "OK";
226  break;
227  }
228  if (x >= maxdigits) {
229  status = "OK";
230  }
231  }
232  } else {
233  res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
235  status = "OK";
236  else if (res == AST_GETDATA_TIMEOUT)
237  status = "TIMEOUT";
238  else if (res == AST_GETDATA_INTERRUPTED)
239  status = "INTERRUPTED";
240  }
241  if (res > -1) {
242  pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
243  if (!ast_strlen_zero(tmp)) {
244  ast_verb(3, "User entered '%s'\n", tmp);
245  tries = 0;
246  } else {
247  tries--;
248  if (tries)
249  ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
250  else
251  ast_verb(3, "User entered nothing.\n");
252  }
253  res = 0;
254  } else {
255  pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
256  ast_verb(3, "User disconnected\n");
257  }
258  }
259  }
260 
261  if (ts) {
262  ts = ast_tone_zone_sound_unref(ts);
263  }
264 
265  if (ast_check_hangup(chan))
266  status = "HANGUP";
267  pbx_builtin_setvar_helper(chan, "READSTATUS", status);
268  return 0;
269 }
#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:3150
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:2780
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 271 of file app_read.c.

References ast_unregister_application().

272 {
274 }
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 281 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 281 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().