Opaquify ast_channel structs and lists
[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_FILE_VERSION(__FILE__, "$Revision$")
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_setwhentohangup_tv(chan, when);
159                 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
160                         when = ast_tvadd(when, ast_tvnow());
161                         ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z",
162                                 ast_localtime(&when, &myt, NULL));
163                         ast_verb(3, "Channel will hangup at %s.\n", timestr);
164                 } else {
165                         ast_verb(3, "Channel hangup cancelled.\n");
166                 }
167                 break;
168
169         case 'r':
170         case 'R':
171                 if (ast_channel_pbx(chan)) {
172                         ast_channel_pbx(chan)->rtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
173                         ast_verb(3, "Response timeout set to %.3f\n", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
174                 }
175                 break;
176
177         case 'd':
178         case 'D':
179                 if (ast_channel_pbx(chan)) {
180                         ast_channel_pbx(chan)->dtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
181                         ast_verb(3, "Digit timeout set to %.3f\n", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
182                 }
183                 break;
184
185         default:
186                 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
187                 break;
188         }
189
190         return 0;
191 }
192
193 static struct ast_custom_function timeout_function = {
194         .name = "TIMEOUT",
195         .read = timeout_read,
196         .read_max = 22,
197         .write = timeout_write,
198 };
199
200 static int unload_module(void)
201 {
202         return ast_custom_function_unregister(&timeout_function);
203 }
204
205 static int load_module(void)
206 {
207         return ast_custom_function_register(&timeout_function);
208 }
209
210 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");