Debian Codegraph @

apps/app_waitforring.c

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