Merged revisions 277182 via svnmerge from
[asterisk/asterisk.git] / apps / app_waitforring.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Wait for Ring Application
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/file.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/module.h"
36 #include "asterisk/lock.h"
37
38 /*** DOCUMENTATION
39         <application name="WaitForRing" language="en_US">
40                 <synopsis>
41                         Wait for Ring Application.
42                 </synopsis>
43                 <syntax>
44                         <parameter name="timeout" required="true" />
45                 </syntax>
46                 <description>
47                         <para>Returns <literal>0</literal> after waiting at least <replaceable>timeout</replaceable> seconds,
48                         and only after the next ring has completed. Returns <literal>0</literal> on success or
49                         <literal>-1</literal> on hangup.</para>
50                 </description>
51         </application>
52  ***/
53
54 static char *app = "WaitForRing";
55
56 static int waitforring_exec(struct ast_channel *chan, const char *data)
57 {
58         struct ast_frame *f;
59         struct ast_silence_generator *silgen = NULL;
60         int res = 0;
61         double s;
62         int ms;
63
64         if (!data || (sscanf(data, "%30lg", &s) != 1)) {
65                 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
66                 return 0;
67         }
68
69         if (ast_opt_transmit_silence) {
70                 silgen = ast_channel_start_silence_generator(chan);
71         }
72
73         ms = s * 1000.0;
74         while (ms > 0) {
75                 ms = ast_waitfor(chan, ms);
76                 if (ms < 0) {
77                         res = ms;
78                         break;
79                 }
80                 if (ms > 0) {
81                         f = ast_read(chan);
82                         if (!f) {
83                                 res = -1;
84                                 break;
85                         }
86                         if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
87                                 ast_verb(3, "Got a ring but still waiting for timeout\n");
88                         }
89                         ast_frfree(f);
90                 }
91         }
92         /* Now we're really ready for the ring */
93         if (!res) {
94                 ms = 99999999;
95                 while(ms > 0) {
96                         ms = ast_waitfor(chan, ms);
97                         if (ms < 0) {
98                                 res = ms;
99                                 break;
100                         }
101                         if (ms > 0) {
102                                 f = ast_read(chan);
103                                 if (!f) {
104                                         res = -1;
105                                         break;
106                                 }
107                                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
108                                         ast_verb(3, "Got a ring after the timeout\n");
109                                         ast_frfree(f);
110                                         break;
111                                 }
112                                 ast_frfree(f);
113                         }
114                 }
115         }
116
117         if (silgen) {
118                 ast_channel_stop_silence_generator(chan, silgen);
119         }
120
121         return res;
122 }
123
124 static int unload_module(void)
125 {
126         return ast_unregister_application(app);
127 }
128
129 static int load_module(void)
130 {
131         return ast_register_application_xml(app, waitforring_exec);
132 }
133
134 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");