Mon Mar 19 11:30:22 2012

Asterisk developer's documentation


app_waitforring.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Wait for Ring Application
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  * 
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>extended</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00035 
00036 #include "asterisk/file.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/lock.h"
00041 
00042 /*** DOCUMENTATION
00043    <application name="WaitForRing" language="en_US">
00044       <synopsis>
00045          Wait for Ring Application.
00046       </synopsis>
00047       <syntax>
00048          <parameter name="timeout" required="true" />
00049       </syntax>
00050       <description>
00051          <para>Returns <literal>0</literal> after waiting at least <replaceable>timeout</replaceable> seconds,
00052          and only after the next ring has completed. Returns <literal>0</literal> on success or
00053          <literal>-1</literal> on hangup.</para>
00054       </description>
00055    </application>
00056  ***/
00057 
00058 static char *app = "WaitForRing";
00059 
00060 static int waitforring_exec(struct ast_channel *chan, const char *data)
00061 {
00062    struct ast_frame *f;
00063    struct ast_silence_generator *silgen = NULL;
00064    int res = 0;
00065    double s;
00066    int ms;
00067 
00068    if (!data || (sscanf(data, "%30lg", &s) != 1)) {
00069       ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
00070       return 0;
00071    }
00072 
00073    if (ast_opt_transmit_silence) {
00074       silgen = ast_channel_start_silence_generator(chan);
00075    }
00076 
00077    ms = s * 1000.0;
00078    while (ms > 0) {
00079       ms = ast_waitfor(chan, ms);
00080       if (ms < 0) {
00081          res = ms;
00082          break;
00083       }
00084       if (ms > 0) {
00085          f = ast_read(chan);
00086          if (!f) {
00087             res = -1;
00088             break;
00089          }
00090          if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
00091             ast_verb(3, "Got a ring but still waiting for timeout\n");
00092          }
00093          ast_frfree(f);
00094       }
00095    }
00096    /* Now we're really ready for the ring */
00097    if (!res) {
00098       ms = 99999999;
00099       while(ms > 0) {
00100          ms = ast_waitfor(chan, ms);
00101          if (ms < 0) {
00102             res = ms;
00103             break;
00104          }
00105          if (ms > 0) {
00106             f = ast_read(chan);
00107             if (!f) {
00108                res = -1;
00109                break;
00110             }
00111             if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
00112                ast_verb(3, "Got a ring after the timeout\n");
00113                ast_frfree(f);
00114                break;
00115             }
00116             ast_frfree(f);
00117          }
00118       }
00119    }
00120 
00121    if (silgen) {
00122       ast_channel_stop_silence_generator(chan, silgen);
00123    }
00124 
00125    return res;
00126 }
00127 
00128 static int unload_module(void)
00129 {
00130    return ast_unregister_application(app);
00131 }
00132 
00133 static int load_module(void)
00134 {
00135    return ast_register_application_xml(app, waitforring_exec);
00136 }
00137 
00138 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");

Generated on Mon Mar 19 11:30:22 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7