Sun May 20 06:33:54 2012

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 /*** MODULEINFO
00028    <support_level>core</support_level>
00029  ***/
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 357542 $")
00034 
00035 #include "asterisk/module.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/app.h"
00040 
00041 /*** DOCUMENTATION
00042    <function name="TIMEOUT" language="en_US">
00043       <synopsis>
00044          Gets or sets timeouts on the channel. Timeout values are in seconds.
00045       </synopsis>
00046       <syntax>
00047          <parameter name="timeouttype" required="true">
00048             <para>The timeout that will be manipulated. The possible timeout types
00049             are: <literal>absolute</literal>, <literal>digit</literal> or 
00050             <literal>response</literal></para>
00051          </parameter>
00052       </syntax>
00053       <description>
00054          <para>The timeouts that can be manipulated are:</para>
00055          <para><literal>absolute</literal>: The absolute maximum amount of time permitted for a call.
00056          Setting of 0 disables the timeout.</para>
00057          <para><literal>digit</literal>: The maximum amount of time permitted between digits when the
00058          user is typing in an extension.  When this timeout expires,
00059          after the user has started to type in an extension, the
00060          extension will be considered complete, and will be
00061          interpreted.  Note that if an extension typed in is valid,
00062          it will not have to timeout to be tested, so typically at
00063          the expiry of this timeout, the extension will be considered
00064          invalid (and thus control would be passed to the <literal>i</literal>
00065          extension, or if it doesn't exist the call would be
00066          terminated).  The default timeout is 5 seconds.</para>
00067          <para><literal>response</literal>: The maximum amount of time permitted after falling through a
00068          series of priorities for a channel in which the user may
00069          begin typing an extension.  If the user does not type an
00070          extension in this amount of time, control will pass to the
00071          <literal>t</literal> extension if it exists, and if not the call would be
00072          terminated.  The default timeout is 10 seconds.</para>
00073       </description>
00074    </function>
00075  ***/
00076 
00077 static int timeout_read(struct ast_channel *chan, const char *cmd, char *data,
00078          char *buf, size_t len)
00079 {
00080    struct timeval myt;
00081 
00082    if (!chan)
00083       return -1;
00084 
00085    if (!data) {
00086       ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
00087       return -1;
00088    }
00089 
00090    switch (*data) {
00091    case 'a':
00092    case 'A':
00093       if (ast_tvzero(*ast_channel_whentohangup(chan))) {
00094          ast_copy_string(buf, "0", len);
00095       } else {
00096          myt = ast_tvnow();
00097          snprintf(buf, len, "%.3f", ast_tvdiff_ms(*ast_channel_whentohangup(chan), myt) / 1000.0);
00098       }
00099       break;
00100 
00101    case 'r':
00102    case 'R':
00103       if (ast_channel_pbx(chan)) {
00104          snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
00105       }
00106       break;
00107 
00108    case 'd':
00109    case 'D':
00110       if (ast_channel_pbx(chan)) {
00111          snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
00112       }
00113       break;
00114 
00115    default:
00116       ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
00117       return -1;
00118    }
00119 
00120    return 0;
00121 }
00122 
00123 static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
00124           const char *value)
00125 {
00126    double x = 0.0;
00127    long sec = 0L;
00128    char timestr[64];
00129    struct ast_tm myt;
00130    struct timeval when = {0,};
00131    int res;
00132 
00133    if (!chan)
00134       return -1;
00135 
00136    if (!data) {
00137       ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
00138       return -1;
00139    }
00140 
00141    if (!value)
00142       return -1;
00143 
00144    res = sscanf(value, "%30ld%30lf", &sec, &x);
00145    if (res == 0 || sec < 0) {
00146       when.tv_sec = 0;
00147       when.tv_usec = 0;
00148    } else if (res == 1) {
00149       when.tv_sec = sec;
00150    } else if (res == 2) {
00151       when.tv_sec = sec;
00152       when.tv_usec = x * 1000000;
00153    }
00154 
00155    switch (*data) {
00156    case 'a':
00157    case 'A':
00158       ast_channel_setwhentohangup_tv(chan, when);
00159       if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
00160          when = ast_tvadd(when, ast_tvnow());
00161          ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z",
00162             ast_localtime(&when, &myt, NULL));
00163          ast_verb(3, "Channel will hangup at %s.\n", timestr);
00164       } else {
00165          ast_verb(3, "Channel hangup cancelled.\n");
00166       }
00167       break;
00168 
00169    case 'r':
00170    case 'R':
00171       if (ast_channel_pbx(chan)) {
00172          ast_channel_pbx(chan)->rtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
00173          ast_verb(3, "Response timeout set to %.3f\n", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
00174       }
00175       break;
00176 
00177    case 'd':
00178    case 'D':
00179       if (ast_channel_pbx(chan)) {
00180          ast_channel_pbx(chan)->dtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
00181          ast_verb(3, "Digit timeout set to %.3f\n", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
00182       }
00183       break;
00184 
00185    default:
00186       ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
00187       break;
00188    }
00189 
00190    return 0;
00191 }
00192 
00193 static struct ast_custom_function timeout_function = {
00194    .name = "TIMEOUT",
00195    .read = timeout_read,
00196    .read_max = 22,
00197    .write = timeout_write,
00198 };
00199 
00200 static int unload_module(void)
00201 {
00202    return ast_custom_function_unregister(&timeout_function);
00203 }
00204 
00205 static int load_module(void)
00206 {
00207    return ast_custom_function_register(&timeout_function);
00208 }
00209 
00210 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");

Generated on Sun May 20 06:33:54 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6