Fri Jun 19 12:09:30 2009

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: 114667 $")
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    int res = 0;
00052    double s;
00053    int ms;
00054 
00055    if (!data || (sscanf(data, "%lg", &s) != 1)) {
00056       ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
00057       return 0;
00058    }
00059 
00060    ms = s * 1000.0;
00061    while (ms > 0) {
00062       ms = ast_waitfor(chan, ms);
00063       if (ms < 0) {
00064          res = ms;
00065          break;
00066       }
00067       if (ms > 0) {
00068          f = ast_read(chan);
00069          if (!f) {
00070             res = -1;
00071             break;
00072          }
00073          if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00074             ast_verb(3, "Got a ring but still waiting for timeout\n");
00075          }
00076          ast_frfree(f);
00077       }
00078    }
00079    /* Now we're really ready for the ring */
00080    if (!res) {
00081       ms = 99999999;
00082       while(ms > 0) {
00083          ms = ast_waitfor(chan, ms);
00084          if (ms < 0) {
00085             res = ms;
00086             break;
00087          }
00088          if (ms > 0) {
00089             f = ast_read(chan);
00090             if (!f) {
00091                res = -1;
00092                break;
00093             }
00094             if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00095                ast_verb(3, "Got a ring after the timeout\n");
00096                ast_frfree(f);
00097                break;
00098             }
00099             ast_frfree(f);
00100          }
00101       }
00102    }
00103 
00104    return res;
00105 }
00106 
00107 static int unload_module(void)
00108 {
00109    return ast_unregister_application(app);
00110 }
00111 
00112 static int load_module(void)
00113 {
00114    return ast_register_application(app, waitforring_exec, synopsis, desc);
00115 }
00116 
00117 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");

Generated on Fri Jun 19 12:09:30 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7