Sat Aug 6 00:39:29 2011

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

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7