Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


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 <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 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
35 
36 #include "asterisk/logger.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 
41 /*** DOCUMENTATION
42  <application name="WaitUntil" language="en_US">
43  <synopsis>
44  Wait (sleep) until the current time is the given epoch.
45  </synopsis>
46  <syntax>
47  <parameter name="epoch" required="true" />
48  </syntax>
49  <description>
50  <para>Waits until the given <replaceable>epoch</replaceable>.</para>
51  <para>Sets <variable>WAITUNTILSTATUS</variable> to one of the following values:</para>
52  <variablelist>
53  <variable name="WAITUNTILSTATUS">
54  <value name="OK">
55  Wait succeeded.
56  </value>
57  <value name="FAILURE">
58  Invalid argument.
59  </value>
60  <value name="HANGUP">
61  Channel hungup before time elapsed.
62  </value>
63  <value name="PAST">
64  Time specified had already past.
65  </value>
66  </variable>
67  </variablelist>
68  </description>
69  </application>
70  ***/
71 
72 static char *app = "WaitUntil";
73 
74 static int waituntil_exec(struct ast_channel *chan, const char *data)
75 {
76  int res;
77  double fraction;
78  long seconds;
79  struct timeval future = { 0, };
80  struct timeval now = ast_tvnow();
81  int msec;
82 
83  if (ast_strlen_zero(data)) {
84  ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
85  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
86  return 0;
87  }
88 
89  if (sscanf(data, "%30ld%30lf", &seconds, &fraction) == 0) {
90  ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
91  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
92  return 0;
93  }
94 
95  future.tv_sec = seconds;
96  future.tv_usec = fraction * 1000000;
97 
98  if ((msec = ast_tvdiff_ms(future, now)) < 0) {
99  ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)now.tv_sec, (long)future.tv_sec);
100  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
101  return 0;
102  }
103 
104  if ((res = ast_safe_sleep(chan, msec)))
105  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
106  else
107  pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
108 
109  return res;
110 }
111 
112 static int unload_module(void)
113 {
114  return ast_unregister_application(app);
115 }
116 
117 static int load_module(void)
118 {
120 }
121 
122 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:1916
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
static int unload_module(void)
#define LOG_WARNING
Definition: logger.h:144
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
General Asterisk PBX channel definitions.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Core PBX routines and definitions.
static char * app
Definition: app_waituntil.c:72
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
static int load_module(void)
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...
Definition: pbx.c:10546
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
static int waituntil_exec(struct ast_channel *chan, const char *data)
Definition: app_waituntil.c:74