Add a function, CHANNELS(), which retrieves a list of all active channels.
[asterisk-bristuff.git] / apps / app_waitforring.c
blobf2d21e5168fe9e90c8cb79984e6d05964a60ab76
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Wait for Ring Application
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/file.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/module.h"
36 #include "asterisk/lock.h"
38 static char *synopsis = "Wait for Ring Application";
40 static char *desc = " WaitForRing(timeout):\n"
41 "Returns 0 after waiting at least timeout seconds. and\n"
42 "only after the next ring has completed. Returns 0 on\n"
43 "success or -1 on hangup\n";
45 static char *app = "WaitForRing";
48 static int waitforring_exec(struct ast_channel *chan, void *data)
50 struct ast_frame *f;
51 int res = 0;
52 double s;
53 int ms;
55 if (!data || (sscanf(data, "%lg", &s) != 1)) {
56 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
57 return 0;
60 ms = s * 1000.0;
61 while (ms > 0) {
62 ms = ast_waitfor(chan, ms);
63 if (ms < 0) {
64 res = ms;
65 break;
67 if (ms > 0) {
68 f = ast_read(chan);
69 if (!f) {
70 res = -1;
71 break;
73 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
74 ast_verb(3, "Got a ring but still waiting for timeout\n");
76 ast_frfree(f);
79 /* Now we're really ready for the ring */
80 if (!res) {
81 ms = 99999999;
82 while(ms > 0) {
83 ms = ast_waitfor(chan, ms);
84 if (ms < 0) {
85 res = ms;
86 break;
88 if (ms > 0) {
89 f = ast_read(chan);
90 if (!f) {
91 res = -1;
92 break;
94 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
95 ast_verb(3, "Got a ring after the timeout\n");
96 ast_frfree(f);
97 break;
99 ast_frfree(f);
104 return res;
107 static int unload_module(void)
109 return ast_unregister_application(app);
112 static int load_module(void)
114 return ast_register_application(app, waitforring_exec, synopsis, desc);
117 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");