Wed Aug 18 22:33:43 2010

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

Generated on Wed Aug 18 22:33:43 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7