Debian Codegraph @

apps/app_waituntil.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2007, Redfish Solutions
5 *
6 * Philip Prindeville <[email protected]>
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 <[email protected]>
24 *
25 * \ingroup applications
26 */
27
28 /*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38
39 /*** DOCUMENTATION
40 <application name="WaitUntil" language="en_US">
41 <since>
42 <version>1.6.0</version>
43 </since>
44 <synopsis>
45 Wait (sleep) until the current time is the given epoch.
46 </synopsis>
47 <syntax>
48 <parameter name="epoch" required="true" />
49 </syntax>
50 <description>
51 <para>Waits until the given <replaceable>epoch</replaceable>.</para>
52 <para>Sets <variable>WAITUNTILSTATUS</variable> to one of the following values:</para>
53 <variablelist>
54 <variable name="WAITUNTILSTATUS">
55 <value name="OK">
56 Wait succeeded.
57 </value>
58 <value name="FAILURE">
59 Invalid argument.
60 </value>
61 <value name="HANGUP">
62 Channel hungup before time elapsed.
63 </value>
64 <value name="PAST">
65 Time specified had already past.
66 </value>
67 </variable>
68 </variablelist>
69 </description>
70 </application>
71 ***/
72
73 static char *app = "WaitUntil";
74
75 static int waituntil_exec(struct ast_channel *chan, const char *data)
76 {
77 int res;
78 double fraction;
79 long seconds;
80 struct timeval future = { 0, };
81 struct timeval now = ast_tvnow();
82 int msec;
83
84 if (ast_strlen_zero(data)) {
85 ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
86 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
87 return 0;
88 }
89
90 if (sscanf(data, "%30ld%30lf", &seconds, &fraction) == 0) {
91 ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
92 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
93 return 0;
94 }
95
96 future.tv_sec = seconds;
97 future.tv_usec = fraction * 1000000;
98
99 if ((msec = ast_tvdiff_ms(future, now)) < 0) {
100 ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)now.tv_sec, (long)future.tv_sec);
101 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
102 return 0;
103 }
104
105 if ((res = ast_safe_sleep(chan, msec)))
106 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
107 else
108 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
109
110 return res;
111 }
112
113 static int unload_module(void)
114 {
115 return ast_unregister_application(app);
116 }
117
118 static int load_module(void)
119 {
120 return ast_register_application_xml(app, waituntil_exec);
121 }
122
123 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");