Asterisk - The Open Source Telephony Project  GIT-master-0190e70
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  <synopsis>
43  Wait for Ring Application.
44  </synopsis>
45  <syntax>
46  <parameter name="timeout" required="true" />
47  </syntax>
48  <description>
49  <para>Returns <literal>0</literal> after waiting at least <replaceable>timeout</replaceable> seconds,
50  and only after the next ring has completed. Returns <literal>0</literal> on success or
51  <literal>-1</literal> on hangup.</para>
52  </description>
53  </application>
54  ***/
55 
56 static char *app = "WaitForRing";
57 
58 static int waitforring_exec(struct ast_channel *chan, const char *data)
59 {
60  struct ast_frame *f;
61  struct ast_silence_generator *silgen = NULL;
62  int res = 0;
63  double s;
64  int timeout_ms;
65  int ms;
66  struct timeval start = ast_tvnow();
67 
68  if (!data || (sscanf(data, "%30lg", &s) != 1)) {
69  ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
70  return 0;
71  }
72 
73  if (s < 0.0) {
74  ast_log(LOG_WARNING, "Invalid timeout provided for WaitForRing (%lg)\n", s);
75  return 0;
76  }
77 
80  }
81 
82  timeout_ms = s * 1000.0;
83  while ((ms = ast_remaining_ms(start, timeout_ms))) {
84  ms = ast_waitfor(chan, ms);
85  if (ms < 0) {
86  res = -1;
87  break;
88  }
89  if (ms > 0) {
90  f = ast_read(chan);
91  if (!f) {
92  res = -1;
93  break;
94  }
96  ast_verb(3, "Got a ring but still waiting for timeout\n");
97  }
98  ast_frfree(f);
99  }
100  }
101  /* Now we're really ready for the ring */
102  if (!res) {
103  for (;;) {
104  int wait_res = ast_waitfor(chan, -1);
105  if (wait_res < 0) {
106  res = -1;
107  break;
108  } else {
109  f = ast_read(chan);
110  if (!f) {
111  res = -1;
112  break;
113  }
115  ast_verb(3, "Got a ring after the timeout\n");
116  ast_frfree(f);
117  break;
118  }
119  ast_frfree(f);
120  }
121  }
122  }
123 
124  if (silgen) {
126  }
127 
128  return res;
129 }
130 
131 static int unload_module(void)
132 {
134 }
135 
136 static int load_module(void)
137 {
139 }
140 
141 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time");
static char * app
Main Channel structure associated with a channel.
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time")
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define LOG_WARNING
Definition: logger.h:274
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4271
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#define ast_opt_transmit_silence
Definition: options.h:124
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
#define NULL
Definition: resample.c:96
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_verb(level,...)
Definition: logger.h:455
struct ast_frame_subclass subclass
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
Core PBX routines and 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:8129
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: main/utils.c:1904
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:8175
static int load_module(void)
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3157
static int unload_module(void)
#define ast_frfree(fr)
Data structure associated with a single frame of data.
static int waitforring_exec(struct ast_channel *chan, const char *data)
enum ast_frame_type frametype
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626