Build console_video support by linking in, as opposed to including,
[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         struct timeval future = { 0, };
52         struct timeval tv = ast_tvnow();
53         int msec;
54
55         if (ast_strlen_zero(data)) {
56                 ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
57                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
58                 return 0;
59         }
60
61         if (sscanf(data, "%ld%lf", (long *)&future.tv_sec, &fraction) == 0) {
62                 ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
63                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
64                 return 0;
65         }
66
67         future.tv_usec = fraction * 1000000;
68
69         if ((msec = ast_tvdiff_ms(future, tv)) < 0) {
70                 ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)tv.tv_sec, (long)future.tv_sec);
71                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
72                 return 0;
73         }
74
75         if ((res = ast_safe_sleep(chan, msec)))
76                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
77         else
78                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
79
80         return res;
81 }
82
83 static int unload_module(void)
84 {
85         return ast_unregister_application(app);
86 }
87
88 static int load_module(void)
89 {
90         return ast_register_application(app, waituntil_exec, synopsis, descrip);
91 }
92
93 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");