Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_waitforring.c
Go to the documentation of this file.
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#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
59static char *app = "WaitForRing";
60
61static 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
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 }
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 }
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) {
129 }
130
131 return res;
132}
133
134static int unload_module(void)
135{
137}
138
139static int load_module(void)
140{
142}
143
144AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time");
static int waitforring_exec(struct ast_channel *chan, const char *data)
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time")
static char * app
static int load_module(void)
static int unload_module(void)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
struct ast_silence_generator * ast_channel_start_silence_generator(struct ast_channel *chan)
Starts a silence generator on the given channel.
Definition: channel.c:8190
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3190
void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
Stops a previously-started silence generator on the given channel.
Definition: channel.c:8236
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4274
Generic File Format Support. Should be included by clients of the file handling routines....
#define ast_frfree(fr)
@ AST_FRAME_CONTROL
@ AST_CONTROL_RING
#define ast_verb(level,...)
#define LOG_WARNING
Asterisk locking-related definitions:
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
#define ast_opt_transmit_silence
Definition: options.h:124
Core PBX routines and definitions.
#define NULL
Definition: resample.c:96
Main Channel structure associated with a channel.
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
enum ast_frame_type frametype
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: utils.c:2281
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159