(closes issue #12846)
[asterisk-bristuff.git] / apps / app_waitforring.c
bloba4f69ae7746e78622635464d062a72502985b20f
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Wait for Ring Application
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/types.h>
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/options.h"
44 #include "asterisk/lock.h"
46 static char *synopsis = "Wait for Ring Application";
48 static char *desc = " WaitForRing(timeout)\n"
49 "Returns 0 after waiting at least timeout seconds. and\n"
50 "only after the next ring has completed. Returns 0 on\n"
51 "success or -1 on hangup\n";
53 static char *app = "WaitForRing";
56 static int waitforring_exec(struct ast_channel *chan, void *data)
58 struct ast_module_user *u;
59 struct ast_frame *f;
60 int res = 0;
61 int ms;
63 if (!data || (sscanf(data, "%d", &ms) != 1)) {
64 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
65 return 0;
68 u = ast_module_user_add(chan);
70 ms *= 1000;
71 while(ms > 0) {
72 ms = ast_waitfor(chan, ms);
73 if (ms < 0) {
74 res = ms;
75 break;
77 if (ms > 0) {
78 f = ast_read(chan);
79 if (!f) {
80 res = -1;
81 break;
83 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
84 if (option_verbose > 2)
85 ast_verbose(VERBOSE_PREFIX_3 "Got a ring but still waiting for timeout\n");
87 ast_frfree(f);
90 /* Now we're really ready for the ring */
91 if (!res) {
92 ms = 99999999;
93 while(ms > 0) {
94 ms = ast_waitfor(chan, ms);
95 if (ms < 0) {
96 res = ms;
97 break;
99 if (ms > 0) {
100 f = ast_read(chan);
101 if (!f) {
102 res = -1;
103 break;
105 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
106 if (option_verbose > 2)
107 ast_verbose(VERBOSE_PREFIX_3 "Got a ring after the timeout\n");
108 ast_frfree(f);
109 break;
111 ast_frfree(f);
115 ast_module_user_remove(u);
117 return res;
120 static int unload_module(void)
122 int res;
124 res = ast_unregister_application(app);
126 ast_module_user_hangup_all();
128 return res;
131 static int load_module(void)
133 return ast_register_application(app, waitforring_exec, synopsis, desc);
136 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");