Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_timeout.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2006, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.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 Channel timeout related dialplan functions
22 *
23 * \author Mark Spencer <markster@digium.com>
24 * \ingroup functions
25 */
26
27/*** MODULEINFO
28 <support_level>core</support_level>
29 ***/
30
31#include "asterisk.h"
32
33#include "asterisk/module.h"
34#include "asterisk/channel.h"
35#include "asterisk/pbx.h"
36#include "asterisk/utils.h"
37#include "asterisk/app.h"
38
39/*** DOCUMENTATION
40 <function name="TIMEOUT" language="en_US">
41 <since>
42 <version>1.2.0</version>
43 </since>
44 <synopsis>
45 Gets or sets timeouts on the channel. Timeout values are in seconds.
46 </synopsis>
47 <syntax>
48 <parameter name="timeouttype" required="true">
49 <para>The timeout that will be manipulated. The possible timeout types
50 are: <literal>absolute</literal>, <literal>digit</literal> or
51 <literal>response</literal></para>
52 </parameter>
53 </syntax>
54 <description>
55 <para>The timeouts that can be manipulated are:</para>
56 <para><literal>absolute</literal>: The absolute maximum amount of time permitted for a call.
57 Setting of 0 disables the timeout.</para>
58 <para><literal>digit</literal>: The maximum amount of time permitted between digits when the
59 user is typing in an extension. When this timeout expires,
60 after the user has started to type in an extension, the
61 extension will be considered complete, and will be
62 interpreted. Note that if an extension typed in is valid,
63 it will not have to timeout to be tested, so typically at
64 the expiry of this timeout, the extension will be considered
65 invalid (and thus control would be passed to the <literal>i</literal>
66 extension, or if it doesn't exist the call would be
67 terminated). The default timeout is 5 seconds.</para>
68 <para><literal>response</literal>: The maximum amount of time permitted after falling through a
69 series of priorities for a channel in which the user may
70 begin typing an extension. If the user does not type an
71 extension in this amount of time, control will pass to the
72 <literal>t</literal> extension if it exists, and if not the call would be
73 terminated. The default timeout is 10 seconds.</para>
74 </description>
75 </function>
76 ***/
77
78static int timeout_read(struct ast_channel *chan, const char *cmd, char *data,
79 char *buf, size_t len)
80{
81 struct timeval myt;
82
83 if (!chan)
84 return -1;
85
86 if (!data) {
87 ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
88 return -1;
89 }
90
91 switch (*data) {
92 case 'a':
93 case 'A':
95 ast_copy_string(buf, "0", len);
96 } else {
97 myt = ast_tvnow();
98 snprintf(buf, len, "%.3f", ast_tvdiff_ms(*ast_channel_whentohangup(chan), myt) / 1000.0);
99 }
100 break;
101
102 case 'r':
103 case 'R':
104 if (ast_channel_pbx(chan)) {
105 snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
106 }
107 break;
108
109 case 'd':
110 case 'D':
111 if (ast_channel_pbx(chan)) {
112 snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
113 }
114 break;
115
116 default:
117 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
118 return -1;
119 }
120
121 return 0;
122}
123
124static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
125 const char *value)
126{
127 double x = 0.0;
128 long sec = 0L;
129 char timestr[64];
130 struct ast_tm myt;
131 struct timeval when = {0,};
132 int res;
133
134 if (!chan)
135 return -1;
136
137 if (!data) {
138 ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
139 return -1;
140 }
141
142 if (!value)
143 return -1;
144
145 res = sscanf(value, "%30ld%30lf", &sec, &x);
146 if (res == 0 || sec < 0) {
147 when.tv_sec = 0;
148 when.tv_usec = 0;
149 } else if (res == 1) {
150 when.tv_sec = sec;
151 } else if (res == 2) {
152 when.tv_sec = sec;
153 when.tv_usec = x * 1000000;
154 }
155
156 switch (*data) {
157 case 'a':
158 case 'A':
159 ast_channel_lock(chan);
161 ast_channel_unlock(chan);
162 if (VERBOSITY_ATLEAST(3)) {
164 when = ast_tvadd(when, ast_tvnow());
165 ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z",
166 ast_localtime(&when, &myt, NULL));
167 ast_verb(3, "Channel will hangup at %s.\n", timestr);
168 } else {
169 ast_verb(3, "Channel hangup cancelled.\n");
170 }
171 }
172 break;
173
174 case 'r':
175 case 'R':
176 if (ast_channel_pbx(chan)) {
177 ast_channel_pbx(chan)->rtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
178 ast_verb(3, "Response timeout set to %.3f\n", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
179 }
180 break;
181
182 case 'd':
183 case 'D':
184 if (ast_channel_pbx(chan)) {
185 ast_channel_pbx(chan)->dtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
186 ast_verb(3, "Digit timeout set to %.3f\n", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
187 }
188 break;
189
190 default:
191 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
192 break;
193 }
194
195 return 0;
196}
197
199 .name = "TIMEOUT",
200 .read = timeout_read,
201 .read_max = 22,
202 .write = timeout_write,
203};
204
205static int unload_module(void)
206{
208}
209
210static int load_module(void)
211{
213}
214
215AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
#define ast_channel_lock(chan)
Definition: channel.h:2970
struct timeval * ast_channel_whentohangup(struct ast_channel *chan)
void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
Set when to hang a channel up.
Definition: channel.c:510
struct ast_pbx * ast_channel_pbx(const struct ast_channel *chan)
#define ast_channel_unlock(chan)
Definition: channel.h:2971
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int timeout_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_timeout.c:78
static int timeout_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_timeout.c:124
static struct ast_custom_function timeout_function
Definition: func_timeout.c:198
static int load_module(void)
Definition: func_timeout.c:210
static int unload_module(void)
Definition: func_timeout.c:205
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define VERBOSITY_ATLEAST(level)
#define LOG_ERROR
#define ast_verb(level,...)
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define NULL
Definition: resample.c:96
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
int rtimeoutms
Definition: pbx.h:217
int dtimeoutms
Definition: pbx.h:216
int value
Definition: syslog.c:37
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:117
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
Utility functions.