Asterisk.org: Developer Documentation (28 Oct 2009)

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