Mon May 14 04:42:58 2007

Asterisk developer's documentation


func_timeout.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Channel timeout related dialplan functions
00022  *
00023  * \author Mark Spencer <markster@digium.com> 
00024  * \ingroup functions
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00030 
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <sys/types.h>
00035 
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/logger.h"
00040 #include "asterisk/utils.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/options.h"
00043 
00044 static int timeout_read(struct ast_channel *chan, char *cmd, char *data,
00045          char *buf, size_t len)
00046 {
00047    time_t myt;
00048 
00049    if (!chan)
00050       return -1;
00051 
00052    if (!data) {
00053       ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
00054       return -1;
00055    }
00056 
00057    switch (*data) {
00058    case 'a':
00059    case 'A':
00060       if (chan->whentohangup == 0) {
00061          ast_copy_string(buf, "0", len);
00062       } else {
00063          time(&myt);
00064          snprintf(buf, len, "%d", (int) (chan->whentohangup - myt));
00065       }
00066       break;
00067 
00068    case 'r':
00069    case 'R':
00070       if (chan->pbx) {
00071          snprintf(buf, len, "%d", chan->pbx->rtimeout);
00072       }
00073       break;
00074 
00075    case 'd':
00076    case 'D':
00077       if (chan->pbx) {
00078          snprintf(buf, len, "%d", chan->pbx->dtimeout);
00079       }
00080       break;
00081 
00082    default:
00083       ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
00084       break;
00085    }
00086 
00087    return 0;
00088 }
00089 
00090 static int timeout_write(struct ast_channel *chan, char *cmd, char *data,
00091           const char *value)
00092 {
00093    int x;
00094    char timestr[64];
00095    struct tm myt;
00096 
00097    if (!chan)
00098       return -1;
00099 
00100    if (!data) {
00101       ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
00102       return -1;
00103    }
00104 
00105    if (!value)
00106       return -1;
00107 
00108    x = atoi(value);
00109 
00110    switch (*data) {
00111    case 'a':
00112    case 'A':
00113       ast_channel_setwhentohangup(chan, x);
00114       if (option_verbose > 2) {
00115          if (chan->whentohangup) {
00116             strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S UTC",
00117                 gmtime_r(&chan->whentohangup, &myt));
00118             ast_verbose(VERBOSE_PREFIX_3 "Channel will hangup at %s.\n",
00119                    timestr);
00120          } else {
00121             ast_verbose(VERBOSE_PREFIX_3 "Channel hangup cancelled.\n");
00122          }
00123       }
00124       break;
00125 
00126    case 'r':
00127    case 'R':
00128       if (chan->pbx) {
00129          chan->pbx->rtimeout = x;
00130          if (option_verbose > 2)
00131             ast_verbose(VERBOSE_PREFIX_3 "Response timeout set to %d\n",
00132                    chan->pbx->rtimeout);
00133       }
00134       break;
00135 
00136    case 'd':
00137    case 'D':
00138       if (chan->pbx) {
00139          chan->pbx->dtimeout = x;
00140          if (option_verbose > 2)
00141             ast_verbose(VERBOSE_PREFIX_3 "Digit timeout set to %d\n",
00142                    chan->pbx->dtimeout);
00143       }
00144       break;
00145 
00146    default:
00147       ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
00148       break;
00149    }
00150 
00151    return 0;
00152 }
00153 
00154 static struct ast_custom_function timeout_function = {
00155    .name = "TIMEOUT",
00156    .synopsis = "Gets or sets timeouts on the channel.",
00157    .syntax = "TIMEOUT(timeouttype)",
00158    .desc =
00159       "Gets or sets various channel timeouts. The timeouts that can be\n"
00160       "manipulated are:\n" "\n"
00161       "absolute: The absolute maximum amount of time permitted for a call.  A\n"
00162       "     setting of 0 disables the timeout.\n" "\n"
00163       "digit:    The maximum amount of time permitted between digits when the\n"
00164       "          user is typing in an extension.  When this timeout expires,\n"
00165       "          after the user has started to type in an extension, the\n"
00166       "          extension will be considered complete, and will be\n"
00167       "          interpreted.  Note that if an extension typed in is valid,\n"
00168       "          it will not have to timeout to be tested, so typically at\n"
00169       "          the expiry of this timeout, the extension will be considered\n"
00170       "          invalid (and thus control would be passed to the 'i'\n"
00171       "          extension, or if it doesn't exist the call would be\n"
00172       "          terminated).  The default timeout is 5 seconds.\n" "\n"
00173       "response: The maximum amount of time permitted after falling through a\n"
00174       "     series of priorities for a channel in which the user may\n"
00175       "     begin typing an extension.  If the user does not type an\n"
00176       "     extension in this amount of time, control will pass to the\n"
00177       "     't' extension if it exists, and if not the call would be\n"
00178       "     terminated.  The default timeout is 10 seconds.\n",
00179    .read = timeout_read,
00180    .write = timeout_write,
00181 };
00182 
00183 static int unload_module(void)
00184 {
00185    return ast_custom_function_unregister(&timeout_function);
00186 }
00187 
00188 static int load_module(void)
00189 {
00190    return ast_custom_function_register(&timeout_function);
00191 }
00192 
00193 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");

Generated on Mon May 14 04:42:59 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1