func_timeout.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Channel timeout related dialplan functions
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. * \ingroup functions
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/module.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/utils.h"
  34. #include "asterisk/app.h"
  35. /*** DOCUMENTATION
  36. <function name="TIMEOUT" language="en_US">
  37. <synopsis>
  38. Gets or sets timeouts on the channel. Timeout values are in seconds.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="timeouttype" required="true">
  42. <para>The timeout that will be manipulated. The possible timeout types
  43. are: <literal>absolute</literal>, <literal>digit</literal> or
  44. <literal>response</literal></para>
  45. </parameter>
  46. </syntax>
  47. <description>
  48. <para>The timeouts that can be manipulated are:</para>
  49. <para><literal>absolute</literal>: The absolute maximum amount of time permitted for a call.
  50. Setting of 0 disables the timeout.</para>
  51. <para><literal>digit</literal>: The maximum amount of time permitted between digits when the
  52. user is typing in an extension. When this timeout expires,
  53. after the user has started to type in an extension, the
  54. extension will be considered complete, and will be
  55. interpreted. Note that if an extension typed in is valid,
  56. it will not have to timeout to be tested, so typically at
  57. the expiry of this timeout, the extension will be considered
  58. invalid (and thus control would be passed to the <literal>i</literal>
  59. extension, or if it doesn't exist the call would be
  60. terminated). The default timeout is 5 seconds.</para>
  61. <para><literal>response</literal>: The maximum amount of time permitted after falling through a
  62. series of priorities for a channel in which the user may
  63. begin typing an extension. If the user does not type an
  64. extension in this amount of time, control will pass to the
  65. <literal>t</literal> extension if it exists, and if not the call would be
  66. terminated. The default timeout is 10 seconds.</para>
  67. </description>
  68. </function>
  69. ***/
  70. static int timeout_read(struct ast_channel *chan, const char *cmd, char *data,
  71. char *buf, size_t len)
  72. {
  73. struct timeval myt;
  74. if (!chan)
  75. return -1;
  76. if (!data) {
  77. ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
  78. return -1;
  79. }
  80. switch (*data) {
  81. case 'a':
  82. case 'A':
  83. if (ast_tvzero(*ast_channel_whentohangup(chan))) {
  84. ast_copy_string(buf, "0", len);
  85. } else {
  86. myt = ast_tvnow();
  87. snprintf(buf, len, "%.3f", ast_tvdiff_ms(*ast_channel_whentohangup(chan), myt) / 1000.0);
  88. }
  89. break;
  90. case 'r':
  91. case 'R':
  92. if (ast_channel_pbx(chan)) {
  93. snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
  94. }
  95. break;
  96. case 'd':
  97. case 'D':
  98. if (ast_channel_pbx(chan)) {
  99. snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
  100. }
  101. break;
  102. default:
  103. ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
  109. const char *value)
  110. {
  111. double x = 0.0;
  112. long sec = 0L;
  113. char timestr[64];
  114. struct ast_tm myt;
  115. struct timeval when = {0,};
  116. int res;
  117. if (!chan)
  118. return -1;
  119. if (!data) {
  120. ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
  121. return -1;
  122. }
  123. if (!value)
  124. return -1;
  125. res = sscanf(value, "%30ld%30lf", &sec, &x);
  126. if (res == 0 || sec < 0) {
  127. when.tv_sec = 0;
  128. when.tv_usec = 0;
  129. } else if (res == 1) {
  130. when.tv_sec = sec;
  131. } else if (res == 2) {
  132. when.tv_sec = sec;
  133. when.tv_usec = x * 1000000;
  134. }
  135. switch (*data) {
  136. case 'a':
  137. case 'A':
  138. ast_channel_lock(chan);
  139. ast_channel_setwhentohangup_tv(chan, when);
  140. ast_channel_unlock(chan);
  141. if (VERBOSITY_ATLEAST(3)) {
  142. if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
  143. when = ast_tvadd(when, ast_tvnow());
  144. ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z",
  145. ast_localtime(&when, &myt, NULL));
  146. ast_verb(3, "Channel will hangup at %s.\n", timestr);
  147. } else {
  148. ast_verb(3, "Channel hangup cancelled.\n");
  149. }
  150. }
  151. break;
  152. case 'r':
  153. case 'R':
  154. if (ast_channel_pbx(chan)) {
  155. ast_channel_pbx(chan)->rtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
  156. ast_verb(3, "Response timeout set to %.3f\n", ast_channel_pbx(chan)->rtimeoutms / 1000.0);
  157. }
  158. break;
  159. case 'd':
  160. case 'D':
  161. if (ast_channel_pbx(chan)) {
  162. ast_channel_pbx(chan)->dtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000;
  163. ast_verb(3, "Digit timeout set to %.3f\n", ast_channel_pbx(chan)->dtimeoutms / 1000.0);
  164. }
  165. break;
  166. default:
  167. ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
  168. break;
  169. }
  170. return 0;
  171. }
  172. static struct ast_custom_function timeout_function = {
  173. .name = "TIMEOUT",
  174. .read = timeout_read,
  175. .read_max = 22,
  176. .write = timeout_write,
  177. };
  178. static int unload_module(void)
  179. {
  180. return ast_custom_function_unregister(&timeout_function);
  181. }
  182. static int load_module(void)
  183. {
  184. return ast_custom_function_register(&timeout_function);
  185. }
  186. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");