Thu Jul 9 13:40:22 2009

Asterisk developer's documentation


app_waituntil.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007, Redfish Solutions
00005  *
00006  * Philip Prindeville <philipp@redfish-solutions.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 Sleep until the given epoch
00022  *
00023  * \author Philip Prindeville <philipp@redfish-solutions.com>
00024  *
00025  * \ingroup applications
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 94052 $")
00031 
00032 #include "asterisk/logger.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/module.h"
00036 
00037 static char *app = "WaitUntil";
00038 static char *synopsis = "Wait (sleep) until the current time is the given epoch";
00039 static char *descrip =
00040 "  WaitUntil(<epoch>): Waits until the given time.  Sets WAITUNTILSTATUS to\n"
00041 "one of the following values:\n"
00042 "  OK       Wait succeeded\n"
00043 "  FAILURE  Invalid argument\n"
00044 "  HANGUP   Channel hung up before time elapsed\n"
00045 "  PAST     The time specified was already past\n";
00046 
00047 static int waituntil_exec(struct ast_channel *chan, void *data)
00048 {
00049    int res;
00050    double fraction;
00051    struct timeval future = { 0, };
00052    struct timeval tv = ast_tvnow();
00053    int msec;
00054 
00055    if (ast_strlen_zero(data)) {
00056       ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
00057       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
00058       return 0;
00059    }
00060 
00061    if (sscanf(data, "%ld%lf", (long *)&future.tv_sec, &fraction) == 0) {
00062       ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
00063       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
00064       return 0;
00065    }
00066 
00067    future.tv_usec = fraction * 1000000;
00068 
00069    if ((msec = ast_tvdiff_ms(future, tv)) < 0) {
00070       ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)tv.tv_sec, (long)future.tv_sec);
00071       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
00072       return 0;
00073    }
00074 
00075    if ((res = ast_safe_sleep(chan, msec)))
00076       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
00077    else
00078       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
00079 
00080    return res;
00081 }
00082 
00083 static int unload_module(void)
00084 {
00085    return ast_unregister_application(app);
00086 }
00087 
00088 static int load_module(void)
00089 {
00090    return ast_register_application(app, waituntil_exec, synopsis, descrip);
00091 }
00092 
00093 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");

Generated on Thu Jul 9 13:40:22 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7