Fri Jun 19 12:09:46 2009

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: 166275 $")
00030 
00031 #include "asterisk/module.h"
00032 #include "asterisk/channel.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/utils.h"
00035 #include "asterisk/app.h"
00036 
00037 static int timeout_read(struct ast_channel *chan, const char *cmd, char *data,
00038          char *buf, size_t len)
00039 {
00040    struct timeval myt;
00041 
00042    if (!chan)
00043       return -1;
00044 
00045    if (!data) {
00046       ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
00047       return -1;
00048    }
00049 
00050    switch (*data) {
00051    case 'a':
00052    case 'A':
00053       if (ast_tvzero(chan->whentohangup)) {
00054          ast_copy_string(buf, "0", len);
00055       } else {
00056          myt = ast_tvnow();
00057          snprintf(buf, len, "%.3f", ast_tvdiff_ms(myt, chan->whentohangup) / 1000.0);
00058       }
00059       break;
00060 
00061    case 'r':
00062    case 'R':
00063       if (chan->pbx) {
00064          snprintf(buf, len, "%.3f", chan->pbx->rtimeoutms / 1000.0);
00065       }
00066       break;
00067 
00068    case 'd':
00069    case 'D':
00070       if (chan->pbx) {
00071          snprintf(buf, len, "%.3f", chan->pbx->dtimeoutms / 1000.0);
00072       }
00073       break;
00074 
00075    default:
00076       ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
00077       return -1;
00078    }
00079 
00080    return 0;
00081 }
00082 
00083 static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
00084           const char *value)
00085 {
00086    double x = 0.0;
00087    long sec = 0L;
00088    char timestr[64];
00089    struct ast_tm myt;
00090    struct timeval when = {0,};
00091    int res;
00092 
00093    if (!chan)
00094       return -1;
00095 
00096    if (!data) {
00097       ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
00098       return -1;
00099    }
00100 
00101    if (!value)
00102       return -1;
00103 
00104    res = sscanf(value, "%ld%lf", &sec, &x);
00105    if (res == 0 || sec < 0) {
00106       when.tv_sec = 0;
00107       when.tv_usec = 0;
00108    } else if (res == 1) {
00109       when.tv_sec = sec;
00110    } else if (res == 2) {
00111       when.tv_sec = sec;
00112       when.tv_usec = x * 1000000;
00113    }
00114 
00115    switch (*data) {
00116    case 'a':
00117    case 'A':
00118       ast_channel_setwhentohangup_tv(chan, when);
00119       if (VERBOSITY_ATLEAST(3)) {
00120          if (!ast_tvzero(chan->whentohangup)) {
00121             when = ast_tvadd(when, ast_tvnow());
00122             ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z",
00123                ast_localtime(&when, &myt, NULL));
00124             ast_verbose("Channel will hangup at %s.\n", timestr);
00125          } else {
00126             ast_verbose("Channel hangup cancelled.\n");
00127          }
00128       }
00129       break;
00130 
00131    case 'r':
00132    case 'R':
00133       if (chan->pbx) {
00134          chan->pbx->rtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000.0;
00135          ast_verb(3, "Response timeout set to %.3f\n", chan->pbx->rtimeoutms / 1000.0);
00136       }
00137       break;
00138 
00139    case 'd':
00140    case 'D':
00141       if (chan->pbx) {
00142          chan->pbx->dtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000.0;
00143          ast_verb(3, "Digit timeout set to %.3f\n", chan->pbx->dtimeoutms / 1000.0);
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. Timeout values are in seconds.",
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 Fri Jun 19 12:09:46 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7