Use casts or intermediate variables to remove a number
[asterisk/asterisk.git] / apps / app_waituntil.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Redfish Solutions
5  *
6  * Philip Prindeville <philipp@redfish-solutions.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Sleep until the given epoch
22  *
23  * \author Philip Prindeville <philipp@redfish-solutions.com>
24  *
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/logger.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/module.h"
36
37 static char *app = "WaitUntil";
38 static char *synopsis = "Wait (sleep) until the current time is the given epoch";
39 static char *descrip =
40 "  WaitUntil(<epoch>): Waits until the given time.  Sets WAITUNTILSTATUS to\n"
41 "one of the following values:\n"
42 "  OK       Wait succeeded\n"
43 "  FAILURE  Invalid argument\n"
44 "  HANGUP   Channel hung up before time elapsed\n"
45 "  PAST     The time specified was already past\n";
46
47 static int waituntil_exec(struct ast_channel *chan, void *data)
48 {
49         int res;
50         double fraction;
51         long seconds;
52         struct timeval future = { 0, };
53         struct timeval tv = ast_tvnow();
54         int msec;
55
56         if (ast_strlen_zero(data)) {
57                 ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
58                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
59                 return 0;
60         }
61
62         if (sscanf(data, "%ld%lf", &seconds, &fraction) == 0) {
63                 ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
64                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
65                 return 0;
66         }
67
68         future.tv_sec = seconds;
69         future.tv_usec = fraction * 1000000;
70
71         if ((msec = ast_tvdiff_ms(future, tv)) < 0) {
72                 ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)tv.tv_sec, (long)future.tv_sec);
73                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
74                 return 0;
75         }
76
77         if ((res = ast_safe_sleep(chan, msec)))
78                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
79         else
80                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
81
82         return res;
83 }
84
85 static int unload_module(void)
86 {
87         return ast_unregister_application(app);
88 }
89
90 static int load_module(void)
91 {
92         return ast_register_application(app, waituntil_exec, synopsis, descrip);
93 }
94
95 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");