Thu Oct 11 06:33:35 2012

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

Generated on Thu Oct 11 06:33:35 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6