7ed906bfaff058ee255794d56a30a20f5e8a9870
[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 /*** MODULEINFO
29         <support_level>extended</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/lock.h"
41
42 /*** DOCUMENTATION
43         <application name="WaitForRing" language="en_US">
44                 <synopsis>
45                         Wait for Ring Application.
46                 </synopsis>
47                 <syntax>
48                         <parameter name="timeout" required="true" />
49                 </syntax>
50                 <description>
51                         <para>Returns <literal>0</literal> after waiting at least <replaceable>timeout</replaceable> seconds,
52                         and only after the next ring has completed. Returns <literal>0</literal> on success or
53                         <literal>-1</literal> on hangup.</para>
54                 </description>
55         </application>
56  ***/
57
58 static char *app = "WaitForRing";
59
60 static int waitforring_exec(struct ast_channel *chan, const char *data)
61 {
62         struct ast_frame *f;
63         struct ast_silence_generator *silgen = NULL;
64         int res = 0;
65         double s;
66         int timeout_ms;
67         int ms;
68         struct timeval start = ast_tvnow();
69
70         if (!data || (sscanf(data, "%30lg", &s) != 1)) {
71                 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
72                 return 0;
73         }
74
75         if (s < 0.0) {
76                 ast_log(LOG_WARNING, "Invalid timeout provided for WaitForRing (%lg)\n", s);
77                 return 0;
78         }
79
80         if (ast_opt_transmit_silence) {
81                 silgen = ast_channel_start_silence_generator(chan);
82         }
83
84         timeout_ms = s * 1000.0;
85         while ((ms = ast_remaining_ms(start, timeout_ms))) {
86                 ms = ast_waitfor(chan, ms);
87                 if (ms < 0) {
88                         res = -1;
89                         break;
90                 }
91                 if (ms > 0) {
92                         f = ast_read(chan);
93                         if (!f) {
94                                 res = -1;
95                                 break;
96                         }
97                         if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
98                                 ast_verb(3, "Got a ring but still waiting for timeout\n");
99                         }
100                         ast_frfree(f);
101                 }
102         }
103         /* Now we're really ready for the ring */
104         if (!res) {
105                 for (;;) {
106                         int wait_res = ast_waitfor(chan, -1);
107                         if (wait_res < 0) {
108                                 res = -1;
109                                 break;
110                         } else {
111                                 f = ast_read(chan);
112                                 if (!f) {
113                                         res = -1;
114                                         break;
115                                 }
116                                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
117                                         ast_verb(3, "Got a ring after the timeout\n");
118                                         ast_frfree(f);
119                                         break;
120                                 }
121                                 ast_frfree(f);
122                         }
123                 }
124         }
125
126         if (silgen) {
127                 ast_channel_stop_silence_generator(chan, silgen);
128         }
129
130         return res;
131 }
132
133 static int unload_module(void)
134 {
135         return ast_unregister_application(app);
136 }
137
138 static int load_module(void)
139 {
140         return ast_register_application_xml(app, waitforring_exec);
141 }
142
143 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time");
144