git migration: Refactor the ASTERISK_FILE_VERSION macro
[asterisk/asterisk.git] / funcs / func_timeout.c
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 ASTERISK_REGISTER_FILE()
34
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/app.h"
40
41 /*** DOCUMENTATION
42         <function name="TIMEOUT" language="en_US">
43                 <synopsis>
44                         Gets or sets timeouts on the channel. Timeout values are in seconds.
45                 </synopsis>
46                 <syntax>
47                         <parameter name="timeouttype" required="true">
48                                 <para>The timeout that will be manipulated. The possible timeout types
49                                 are: <literal>absolute</literal>, <literal>digit</literal> or 
50                                 <literal>response</literal></para>
51                         </parameter>
52                 </syntax>
53                 <description>
54                         <para>The timeouts that can be manipulated are:</para>
55                         <para><literal>absolute</literal>: The absolute maximum amount of time permitted for a call.
56                         Setting of 0 disables the timeout.</para>
57                         <para><literal>digit</literal>: The maximum amount of time permitted between digits when the
58                         user is typing in an extension.  When this timeout expires,
59                         after the user has started to type in an extension, the
60                         extension will be considered complete, and will be
61                         interpreted.  Note that if an extension typed in is valid,
62                         it will not have to timeout to be tested, so typically at
63                         the expiry of this timeout, the extension will be considered
64                         invalid (and thus control would be passed to the <literal>i</literal>
65                         extension, or if it doesn't exist the call would be
66                         terminated).  The default timeout is 5 seconds.</para>
67                         <para><literal>response</literal>: The maximum amount of time permitted after falling through a
68                         series of priorities for a channel in which the user may
69                         begin typing an extension.  If the user does not type an
70                         extension in this amount of time, control will pass to the
71                         <literal>t</literal> extension if it exists, and if not the call would be
72                         terminated.  The default timeout is 10 seconds.</para>
73                 </description>
74         </function>
75  ***/
76
77 static int timeout_read(struct ast_channel *chan, const char *cmd, char *data,
78                         char *buf, size_t len)
79 {
80         struct timeval myt;
81
82         if (!chan)
83                 return -1;
84
85         if (!data) {
86                 ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
87                 return -1;
88         }
89
90         switch (*data) {
91         case 'a':
92         case 'A':
93                 if (ast_tvzero(*ast_channel_whentohangup(chan))) {
94                         ast_copy_string(buf, "0", len);
95                 } else {
96                         myt = ast_tvnow();
97                         snprintf(buf, len, "%.3f", ast_tvdiff_ms(*ast_channel_whentohangup(chan), myt) / 1000.0);
98                 }
99                 break;
100
101         case 'r':
102         case 'R':
103                 if (ast_channel_pbx(chan)) {
104                         snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
105                 }
106                 break;
107
108         case 'd':
109         case 'D':
110                 if (ast_channel_pbx(chan)) {
111                         snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
112                 }
113                 break;
114
115         default:
116                 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
117                 return -1;
118         }
119
120         return 0;
121 }
122
123 static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
124                          const char *value)
125 {
126         double x = 0.0;
127         long sec = 0L;
128         char timestr[64];
129         struct ast_tm myt;
130         struct timeval when = {0,};
131         int res;
132
133         if (!chan)
134                 return -1;
135
136         if (!data) {
137                 ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
138                 return -1;
139         }
140
141         if (!value)
142                 return -1;
143
144         res = sscanf(value, "%30ld%30lf", &sec, &x);
145         if (res == 0 || sec < 0) {
146                 when.tv_sec = 0;
147                 when.tv_usec = 0;
148         } else if (res == 1) {
149                 when.tv_sec = sec;
150         } else if (res == 2) {
151                 when.tv_sec = sec;
152                 when.tv_usec = x * 1000000;
153         }
154
155         switch (*data) {
156         case 'a':
157         case 'A':
158                 ast_channel_lock(chan);
159                 ast_channel_setwhentohangup_tv(chan, when);
160                 ast_channel_unlock(chan);
161                 if (VERBOSITY_ATLEAST(3)) {
162                         if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
163                                 when = ast_tvadd(when, ast_tvnow());
164                                 ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z",
165                                         ast_localtime(&when, &myt, NULL));
166                                 ast_verb(3, "Channel will hangup at %s.\n", timestr);
167                         } else {
168                                 ast_verb(3, "Channel hangup cancelled.\n");
169                         }
170                 }
171                 break;
172
173         case 'r':
174         case 'R':
175                 if (ast_channel_pbx(chan)) {
176                         ast_channel_pbx(chan)->rtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
177                         ast_verb(3, "Response timeout set to %.3f\n", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
178                 }
179                 break;
180
181         case 'd':
182         case 'D':
183                 if (ast_channel_pbx(chan)) {
184                         ast_channel_pbx(chan)->dtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
185                         ast_verb(3, "Digit timeout set to %.3f\n", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
186                 }
187                 break;
188
189         default:
190                 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
191                 break;
192         }
193
194         return 0;
195 }
196
197 static struct ast_custom_function timeout_function = {
198         .name = "TIMEOUT",
199         .read = timeout_read,
200         .read_max = 22,
201         .write = timeout_write,
202 };
203
204 static int unload_module(void)
205 {
206         return ast_custom_function_unregister(&timeout_function);
207 }
208
209 static int load_module(void)
210 {
211         return ast_custom_function_register(&timeout_function);
212 }
213
214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");