Tue Jul 14 23:09:39 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: 40722 $")
00031 
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036 #include <sys/types.h>
00037 
00038 #include "asterisk/file.h"
00039 #include "asterisk/logger.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/lock.h"
00045 
00046 static char *synopsis = "Wait for Ring Application";
00047 
00048 static char *desc = "  WaitForRing(timeout)\n"
00049 "Returns 0 after waiting at least timeout seconds. and\n"
00050 "only after the next ring has completed.  Returns 0 on\n"
00051 "success or -1 on hangup\n";
00052 
00053 static char *app = "WaitForRing";
00054 
00055 
00056 static int waitforring_exec(struct ast_channel *chan, void *data)
00057 {
00058    struct ast_module_user *u;
00059    struct ast_frame *f;
00060    int res = 0;
00061    int ms;
00062 
00063    if (!data || (sscanf(data, "%d", &ms) != 1)) {
00064                 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
00065       return 0;
00066    }
00067 
00068    u = ast_module_user_add(chan);
00069 
00070    ms *= 1000;
00071    while(ms > 0) {
00072       ms = ast_waitfor(chan, ms);
00073       if (ms < 0) {
00074          res = ms;
00075          break;
00076       }
00077       if (ms > 0) {
00078          f = ast_read(chan);
00079          if (!f) {
00080             res = -1;
00081             break;
00082          }
00083          if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00084             if (option_verbose > 2)
00085                ast_verbose(VERBOSE_PREFIX_3 "Got a ring but still waiting for timeout\n");
00086          }
00087          ast_frfree(f);
00088       }
00089    }
00090    /* Now we're really ready for the ring */
00091    if (!res) {
00092       ms = 99999999;
00093       while(ms > 0) {
00094          ms = ast_waitfor(chan, ms);
00095          if (ms < 0) {
00096             res = ms;
00097             break;
00098          }
00099          if (ms > 0) {
00100             f = ast_read(chan);
00101             if (!f) {
00102                res = -1;
00103                break;
00104             }
00105             if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00106                if (option_verbose > 2)
00107                   ast_verbose(VERBOSE_PREFIX_3 "Got a ring after the timeout\n");
00108                ast_frfree(f);
00109                break;
00110             }
00111             ast_frfree(f);
00112          }
00113       }
00114    }
00115    ast_module_user_remove(u);
00116 
00117    return res;
00118 }
00119 
00120 static int unload_module(void)
00121 {
00122    int res;
00123 
00124    res = ast_unregister_application(app);
00125 
00126    ast_module_user_hangup_all();
00127 
00128    return res; 
00129 }
00130 
00131 static int load_module(void)
00132 {
00133    return ast_register_application(app, waitforring_exec, synopsis, desc);
00134 }
00135 
00136 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");

Generated on Tue Jul 14 23:09:39 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7