Thu Apr 28 2011 17:13:29

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 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 239713 $")
00031 
00032 #include "asterisk/file.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/module.h"
00036 #include "asterisk/lock.h"
00037 
00038 /*** DOCUMENTATION
00039    <application name="WaitForRing" language="en_US">
00040       <synopsis>
00041          Wait for Ring Application.
00042       </synopsis>
00043       <syntax>
00044          <parameter name="timeout" required="true" />
00045       </syntax>
00046       <description>
00047          <para>Returns <literal>0</literal> after waiting at least <replaceable>timeout</replaceable> seconds,
00048          and only after the next ring has completed. Returns <literal>0</literal> on success or
00049          <literal>-1</literal> on hangup.</para>
00050       </description>
00051    </application>
00052  ***/
00053 
00054 static char *app = "WaitForRing";
00055 
00056 static int waitforring_exec(struct ast_channel *chan, void *data)
00057 {
00058    struct ast_frame *f;
00059    struct ast_silence_generator *silgen = NULL;
00060    int res = 0;
00061    double s;
00062    int ms;
00063 
00064    if (!data || (sscanf(data, "%30lg", &s) != 1)) {
00065       ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
00066       return 0;
00067    }
00068 
00069    if (ast_opt_transmit_silence) {
00070       silgen = ast_channel_start_silence_generator(chan);
00071    }
00072 
00073    ms = s * 1000.0;
00074    while (ms > 0) {
00075       ms = ast_waitfor(chan, ms);
00076       if (ms < 0) {
00077          res = ms;
00078          break;
00079       }
00080       if (ms > 0) {
00081          f = ast_read(chan);
00082          if (!f) {
00083             res = -1;
00084             break;
00085          }
00086          if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00087             ast_verb(3, "Got a ring but still waiting for timeout\n");
00088          }
00089          ast_frfree(f);
00090       }
00091    }
00092    /* Now we're really ready for the ring */
00093    if (!res) {
00094       ms = 99999999;
00095       while(ms > 0) {
00096          ms = ast_waitfor(chan, ms);
00097          if (ms < 0) {
00098             res = ms;
00099             break;
00100          }
00101          if (ms > 0) {
00102             f = ast_read(chan);
00103             if (!f) {
00104                res = -1;
00105                break;
00106             }
00107             if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00108                ast_verb(3, "Got a ring after the timeout\n");
00109                ast_frfree(f);
00110                break;
00111             }
00112             ast_frfree(f);
00113          }
00114       }
00115    }
00116 
00117    if (silgen) {
00118       ast_channel_stop_silence_generator(chan, silgen);
00119    }
00120 
00121    return res;
00122 }
00123 
00124 static int unload_module(void)
00125 {
00126    return ast_unregister_application(app);
00127 }
00128 
00129 static int load_module(void)
00130 {
00131    return ast_register_application_xml(app, waitforring_exec);
00132 }
00133 
00134 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");