Asterisk - The Open Source Telephony Project  18.5.0
app_waituntil.c
Go to the documentation of this file.
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  <synopsis>
42  Wait (sleep) until the current time is the given epoch.
43  </synopsis>
44  <syntax>
45  <parameter name="epoch" required="true" />
46  </syntax>
47  <description>
48  <para>Waits until the given <replaceable>epoch</replaceable>.</para>
49  <para>Sets <variable>WAITUNTILSTATUS</variable> to one of the following values:</para>
50  <variablelist>
51  <variable name="WAITUNTILSTATUS">
52  <value name="OK">
53  Wait succeeded.
54  </value>
55  <value name="FAILURE">
56  Invalid argument.
57  </value>
58  <value name="HANGUP">
59  Channel hungup before time elapsed.
60  </value>
61  <value name="PAST">
62  Time specified had already past.
63  </value>
64  </variable>
65  </variablelist>
66  </description>
67  </application>
68  ***/
69 
70 static char *app = "WaitUntil";
71 
72 static int waituntil_exec(struct ast_channel *chan, const char *data)
73 {
74  int res;
75  double fraction;
76  long seconds;
77  struct timeval future = { 0, };
78  struct timeval now = ast_tvnow();
79  int msec;
80 
81  if (ast_strlen_zero(data)) {
82  ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
83  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
84  return 0;
85  }
86 
87  if (sscanf(data, "%30ld%30lf", &seconds, &fraction) == 0) {
88  ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
89  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
90  return 0;
91  }
92 
93  future.tv_sec = seconds;
94  future.tv_usec = fraction * 1000000;
95 
96  if ((msec = ast_tvdiff_ms(future, now)) < 0) {
97  ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)now.tv_sec, (long)future.tv_sec);
98  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
99  return 0;
100  }
101 
102  if ((res = ast_safe_sleep(chan, msec)))
103  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
104  else
105  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
106 
107  return res;
108 }
109 
110 static int unload_module(void)
111 {
113 }
114 
115 static int load_module(void)
116 {
118 }
119 
120 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
static int unload_module(void)
#define LOG_WARNING
Definition: logger.h:274
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
Core PBX routines and definitions.
static char * app
Definition: app_waituntil.c:70
#define LOG_NOTICE
Definition: logger.h:263
static int load_module(void)
Support for logging to various files, console and syslog Configuration in file logger.conf.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
static int waituntil_exec(struct ast_channel *chan, const char *data)
Definition: app_waituntil.c:72