Asterisk - The Open Source Telephony Project GIT-master-2de1a68
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_KEEP_TERMINATOR = (1 << 4) , OPT_ANSWER = (1 << 0) ,
  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 }, [ 'e' ] = { .flag = OPT_KEEP_TERMINATOR }, }
 

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

144 {
146 /* note: this entry _MUST_ be the last one in the enum */
148};
@ OPT_ARG_TERMINATOR
Definition: app_read.c:145
@ OPT_ARG_ARRAY_SIZE
Definition: app_read.c:147

◆ 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_KEEP_TERMINATOR 
OPT_ANSWER 
OPT_DELAY 
OPT_MUTE 
OPT_QUELCH 
OPT_RELAXED 
OPT_EXTRAPULSES 

Definition at line 136 of file app_read.c.

136 {
137 OPT_SKIP = (1 << 0),
138 OPT_INDICATION = (1 << 1),
139 OPT_NOANSWER = (1 << 2),
140 OPT_TERMINATOR = (1 << 3),
141 OPT_KEEP_TERMINATOR = (1 << 4),
142};
@ OPT_TERMINATOR
Definition: app_read.c:140
@ OPT_NOANSWER
Definition: app_read.c:139
@ OPT_INDICATION
Definition: app_read.c:138
@ OPT_SKIP
Definition: app_read.c:137
@ OPT_KEEP_TERMINATOR
Definition: app_read.c:141

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 334 of file app_read.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 334 of file app_read.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 334 of file app_read.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 329 of file app_read.c.

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

References app, ast_register_application_xml, and read_exec().

◆ read_exec()

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

Definition at line 160 of file app_read.c.

161{
162 int res = 0;
163 char tmp[256] = "";
164 int maxdigits = 255;
165 int tries = 1, to = 0, x = 0;
166 double tosec;
167 char *argcopy = NULL;
168 char *opt_args[OPT_ARG_ARRAY_SIZE];
169 struct ast_tone_zone_sound *ts = NULL;
170 struct ast_flags flags = {0};
171 const char *status = "ERROR";
172 char *terminator = "#"; /* use default terminator # by default */
173
174 AST_DECLARE_APP_ARGS(arglist,
175 AST_APP_ARG(variable);
176 AST_APP_ARG(filename);
177 AST_APP_ARG(maxdigits);
179 AST_APP_ARG(attempts);
180 AST_APP_ARG(timeout);
181 );
182
183 pbx_builtin_setvar_helper(chan, "READSTATUS", status);
184 if (ast_strlen_zero(data)) {
185 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
186 return 0;
187 }
188
189 argcopy = ast_strdupa(data);
190
191 AST_STANDARD_APP_ARGS(arglist, argcopy);
192
193 if (!ast_strlen_zero(arglist.options)) {
194 ast_app_parse_options(read_app_options, &flags, opt_args, arglist.options);
195 }
196
197 if (!ast_strlen_zero(arglist.attempts)) {
198 tries = atoi(arglist.attempts);
199 if (tries <= 0)
200 tries = 1;
201 }
202
203 if (!ast_strlen_zero(arglist.timeout)) {
204 tosec = atof(arglist.timeout);
205 if (tosec <= 0)
206 to = 0;
207 else
208 to = tosec * 1000.0;
209 }
210
211 if (ast_strlen_zero(arglist.filename)) {
212 arglist.filename = NULL;
213 }
214 if (!ast_strlen_zero(arglist.maxdigits)) {
215 maxdigits = atoi(arglist.maxdigits);
216 if ((maxdigits < 1) || (maxdigits > 255)) {
217 maxdigits = 255;
218 } else
219 ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
220 }
221 if (ast_strlen_zero(arglist.variable)) {
222 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
223 return 0;
224 }
226 if (!ast_strlen_zero(arglist.filename)) {
227 ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
228 }
229 }
231 if (!ast_strlen_zero(opt_args[OPT_ARG_TERMINATOR])) {
232 terminator = opt_args[OPT_ARG_TERMINATOR];
233 } else {
234 terminator = ""; /* no digit inherently will terminate input */
235 }
236 }
237 if (ast_channel_state(chan) != AST_STATE_UP) {
239 /* At the user's option, skip if the line is not up */
240 if (ts) {
242 }
243 pbx_builtin_setvar_helper(chan, arglist.variable, "");
244 pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
245 return 0;
246 } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
247 /* Otherwise answer unless we're supposed to read while on-hook */
248 res = ast_answer(chan);
249 }
250 }
251 if (!res) {
252 while (tries && !res) {
253 ast_stopstream(chan);
254 if (ts && ts->data[0]) {
255 if (!to)
256 to = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 6000;
257 res = ast_playtones_start(chan, 0, ts->data, 0);
258 for (x = 0; x < maxdigits; ) {
259 res = ast_waitfordigit(chan, to);
260 ast_playtones_stop(chan);
261 if (res < 1) {
262 if (res == 0)
263 status = "TIMEOUT";
264 tmp[x]='\0';
265 break;
266 }
267 tmp[x++] = res;
268 if (terminator && strchr(terminator, tmp[x-1])) {
269 tmp[x-1] = '\0';
270 status = "OK";
271 break;
272 }
273 if (x >= maxdigits) {
274 status = "OK";
275 }
276 }
277 } else {
278 res = ast_app_getdata_terminator(chan, arglist.filename, tmp, maxdigits, to, terminator);
279 if (res == AST_GETDATA_COMPLETE) {
280 status = "OK";
281 } else if (res == AST_GETDATA_EMPTY_END_TERMINATED) {
283 /* if the option is set to do so, read the
284 returned string as the terminator string */
285 ast_copy_string(tmp, terminator, sizeof(tmp));
286 }
287 status = "OK";
288 } else if (res == AST_GETDATA_TIMEOUT) {
289 status = "TIMEOUT";
290 } else if (res == AST_GETDATA_INTERRUPTED) {
291 status = "INTERRUPTED";
292 }
293 }
294 if (res > -1) {
295 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
296 if (!ast_strlen_zero(tmp)) {
297 ast_verb(3, "User entered '%s'\n", tmp);
298 tries = 0;
299 } else {
300 tries--;
301 if (tries)
302 ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
303 else
304 ast_verb(3, "User entered nothing.\n");
305 }
306 res = 0;
307 } else {
308 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
309 ast_verb(3, "User disconnected\n");
310 }
311 }
312 }
313
314 if (ts) {
316 }
317
318 if (ast_check_hangup(chan))
319 status = "HANGUP";
320 pbx_builtin_setvar_helper(chan, "READSTATUS", status);
321 return 0;
322}
jack_status_t status
Definition: app_jack.c:146
static const struct ast_app_option read_app_options[128]
Definition: app_read.c:156
#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:3175
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:445
struct ast_tone_zone * ast_channel_zone(const struct ast_channel *chan)
struct ast_pbx * ast_channel_pbx(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2805
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.
enum ast_getdata_result 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
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
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:3056
#define ast_verb(level,...)
#define LOG_WARNING
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_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
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:393
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:461
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
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
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_copy_string(), 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_KEEP_TERMINATOR, OPT_NOANSWER, OPT_SKIP, OPT_TERMINATOR, options, pbx_builtin_setvar_helper(), read_app_options, ast_pbx::rtimeoutms, status, and tmp().

Referenced by load_module().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 324 of file app_read.c.

325{
327}
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 334 of file app_read.c.

◆ app

char* app = "Read"
static

Definition at line 158 of file app_read.c.

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 334 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 }, [ 'e' ] = { .flag = OPT_KEEP_TERMINATOR }, }
static

Definition at line 156 of file app_read.c.

Referenced by read_exec().