Files
asterisk/apps/app_waitforring.c
T

142 lines
3.2 KiB
C
Raw Normal View History

2003-02-23 06:00:11 +00:00
/*
* Asterisk -- An open source telephony toolkit.
2003-02-23 06:00:11 +00:00
*
* Copyright (C) 1999 - 2005, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
2003-02-23 06:00:11 +00:00
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
2003-02-23 06:00:11 +00:00
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Wait for Ring Application
2005-12-30 21:18:06 +00:00
*
* \author Mark Spencer <markster@digium.com>
*
2005-11-06 15:09:47 +00:00
* \ingroup applications
2003-02-23 06:00:11 +00:00
*/
2011-07-14 20:28:54 +00:00
/*** MODULEINFO
<support_level>extended</support_level>
***/
#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
2003-02-23 06:00:11 +00:00
2008-11-03 01:01:25 +00:00
/*** DOCUMENTATION
<application name="WaitForRing" language="en_US">
<synopsis>
Wait for Ring Application.
</synopsis>
<syntax>
<parameter name="timeout" required="true" />
</syntax>
<description>
<para>Returns <literal>0</literal> after waiting at least <replaceable>timeout</replaceable> seconds,
and only after the next ring has completed. Returns <literal>0</literal> on success or
<literal>-1</literal> on hangup.</para>
</description>
</application>
***/
2003-02-23 06:00:11 +00:00
static char *app = "WaitForRing";
static int waitforring_exec(struct ast_channel *chan, const char *data)
2003-02-23 06:00:11 +00:00
{
struct ast_frame *f;
2010-01-13 16:31:14 +00:00
struct ast_silence_generator *silgen = NULL;
2003-02-23 06:00:11 +00:00
int res = 0;
double s;
2012-11-07 19:15:26 +00:00
int timeout_ms;
2003-02-23 06:00:11 +00:00
int ms;
2012-11-07 19:15:26 +00:00
struct timeval start = ast_tvnow();
2009-08-10 19:20:57 +00:00
if (!data || (sscanf(data, "%30lg", &s) != 1)) {
2008-04-25 20:20:10 +00:00
ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
2003-02-23 06:00:11 +00:00
return 0;
}
2012-11-07 19:15:26 +00:00
if (s < 0.0) {
ast_log(LOG_WARNING, "Invalid timeout provided for WaitForRing (%lg)\n", s);
return 0;
}
2010-01-13 16:31:14 +00:00
if (ast_opt_transmit_silence) {
silgen = ast_channel_start_silence_generator(chan);
}
2012-11-07 19:15:26 +00:00
timeout_ms = s * 1000.0;
while ((ms = ast_remaining_ms(start, timeout_ms))) {
2003-02-23 06:00:11 +00:00
ms = ast_waitfor(chan, ms);
if (ms < 0) {
2012-11-07 19:15:26 +00:00
res = -1;
2003-02-23 06:00:11 +00:00
break;
}
if (ms > 0) {
f = ast_read(chan);
if (!f) {
res = -1;
break;
}
if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
ast_verb(3, "Got a ring but still waiting for timeout\n");
2003-02-23 06:00:11 +00:00
}
ast_frfree(f);
}
}
/* Now we're really ready for the ring */
if (!res) {
2012-11-07 19:15:26 +00:00
for (;;) {
int wait_res = ast_waitfor(chan, -1);
if (wait_res < 0) {
res = -1;
2003-02-23 06:00:11 +00:00
break;
2012-11-07 19:15:26 +00:00
} else {
2003-02-23 06:00:11 +00:00
f = ast_read(chan);
if (!f) {
res = -1;
break;
}
if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
ast_verb(3, "Got a ring after the timeout\n");
2003-02-23 06:00:11 +00:00
ast_frfree(f);
break;
}
ast_frfree(f);
}
}
}
2010-01-13 16:31:14 +00:00
if (silgen) {
ast_channel_stop_silence_generator(chan, silgen);
}
2003-02-23 06:00:11 +00:00
return res;
}
static int unload_module(void)
2003-02-23 06:00:11 +00:00
{
return ast_unregister_application(app);
2003-02-23 06:00:11 +00:00
}
static int load_module(void)
2003-02-23 06:00:11 +00:00
{
2008-11-03 01:01:25 +00:00
return ast_register_application_xml(app, waitforring_exec);
2003-02-23 06:00:11 +00:00
}
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time");