app_waituntil.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211569 $")
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 long seconds;
00052 struct timeval future = { 0, };
00053 struct timeval now = ast_tvnow();
00054 int msec;
00055
00056 if (ast_strlen_zero(data)) {
00057 ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
00058 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
00059 return 0;
00060 }
00061
00062 if (sscanf(data, "%30ld%30lf", &seconds, &fraction) == 0) {
00063 ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
00064 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
00065 return 0;
00066 }
00067
00068 future.tv_sec = seconds;
00069 future.tv_usec = fraction * 1000000;
00070
00071 if ((msec = ast_tvdiff_ms(future, now)) < 0) {
00072 ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)now.tv_sec, (long)future.tv_sec);
00073 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
00074 return 0;
00075 }
00076
00077 if ((res = ast_safe_sleep(chan, msec)))
00078 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
00079 else
00080 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
00081
00082 return res;
00083 }
00084
00085 static int unload_module(void)
00086 {
00087 return ast_unregister_application(app);
00088 }
00089
00090 static int load_module(void)
00091 {
00092 return ast_register_application(app, waituntil_exec, synopsis, descrip);
00093 }
00094
00095 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");