Wed Apr 6 11:29:40 2011

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: 211539 $")
00031 
00032 #include "asterisk/logger.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/module.h"
00036 
00037 /*** DOCUMENTATION
00038    <application name="WaitUntil" language="en_US">
00039       <synopsis>
00040          Wait (sleep) until the current time is the given epoch.
00041       </synopsis>
00042       <syntax>
00043          <parameter name="epoch" required="true" />
00044       </syntax>
00045       <description>
00046          <para>Waits until the given <replaceable>epoch</replaceable>.</para>
00047          <para>Sets <variable>WAITUNTILSTATUS</variable> to one of the following values:</para>
00048          <variablelist>
00049             <variable name="WAITUNTILSTATUS">
00050                <value name="OK">
00051                   Wait succeeded.
00052                </value>
00053                <value name="FAILURE">
00054                   Invalid argument.
00055                </value>
00056                <value name="HANGUP">
00057                   Channel hungup before time elapsed.
00058                </value>
00059                <value name="PAST">
00060                   Time specified had already past.
00061                </value>
00062             </variable>
00063          </variablelist>
00064       </description>
00065    </application>
00066  ***/
00067 
00068 static char *app = "WaitUntil";
00069 
00070 static int waituntil_exec(struct ast_channel *chan, const char *data)
00071 {
00072    int res;
00073    double fraction;
00074    long seconds;
00075    struct timeval future = { 0, };
00076    struct timeval now = ast_tvnow();
00077    int msec;
00078 
00079    if (ast_strlen_zero(data)) {
00080       ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
00081       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
00082       return 0;
00083    }
00084 
00085    if (sscanf(data, "%30ld%30lf", &seconds, &fraction) == 0) {
00086       ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
00087       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
00088       return 0;
00089    }
00090 
00091    future.tv_sec = seconds;
00092    future.tv_usec = fraction * 1000000;
00093 
00094    if ((msec = ast_tvdiff_ms(future, now)) < 0) {
00095       ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)now.tv_sec, (long)future.tv_sec);
00096       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
00097       return 0;
00098    }
00099 
00100    if ((res = ast_safe_sleep(chan, msec)))
00101       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
00102    else
00103       pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
00104 
00105    return res;
00106 }
00107 
00108 static int unload_module(void)
00109 {
00110    return ast_unregister_application(app);
00111 }
00112 
00113 static int load_module(void)
00114 {
00115    return ast_register_application_xml(app, waituntil_exec);
00116 }
00117 
00118 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");

Generated on Wed Apr 6 11:29:40 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7