Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 375993 $")
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 
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  }
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  }
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) {
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 {
141 }
142 
143 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");
static char * app
union ast_frame_subclass subclass
Definition: frame.h:146
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define LOG_WARNING
Definition: logger.h:144
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4383
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
#define ast_opt_transmit_silence
Definition: options.h:120
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
#define ast_verb(level,...)
Definition: logger.h:243
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:8309
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: utils.c:1615
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
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:8355
static int load_module(void)
static struct ast_format f[]
Definition: format_g726.c:181
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3539
static int unload_module(void)
Data structure associated with a single frame of data.
Definition: frame.h:142
static int waitforring_exec(struct ast_channel *chan, const char *data)
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_frfree(fr)
Definition: frame.h:583
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180