app_waitforring.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, 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 Wait for Ring Application
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>extended</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/file.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/lock.h"
  36. /*** DOCUMENTATION
  37. <application name="WaitForRing" language="en_US">
  38. <synopsis>
  39. Wait for Ring Application.
  40. </synopsis>
  41. <syntax>
  42. <parameter name="timeout" required="true" />
  43. </syntax>
  44. <description>
  45. <para>Returns <literal>0</literal> after waiting at least <replaceable>timeout</replaceable> seconds,
  46. and only after the next ring has completed. Returns <literal>0</literal> on success or
  47. <literal>-1</literal> on hangup.</para>
  48. </description>
  49. </application>
  50. ***/
  51. static char *app = "WaitForRing";
  52. static int waitforring_exec(struct ast_channel *chan, const char *data)
  53. {
  54. struct ast_frame *f;
  55. struct ast_silence_generator *silgen = NULL;
  56. int res = 0;
  57. double s;
  58. int timeout_ms;
  59. int ms;
  60. struct timeval start = ast_tvnow();
  61. if (!data || (sscanf(data, "%30lg", &s) != 1)) {
  62. ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
  63. return 0;
  64. }
  65. if (s < 0.0) {
  66. ast_log(LOG_WARNING, "Invalid timeout provided for WaitForRing (%lg)\n", s);
  67. return 0;
  68. }
  69. if (ast_opt_transmit_silence) {
  70. silgen = ast_channel_start_silence_generator(chan);
  71. }
  72. timeout_ms = s * 1000.0;
  73. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  74. ms = ast_waitfor(chan, ms);
  75. if (ms < 0) {
  76. res = -1;
  77. break;
  78. }
  79. if (ms > 0) {
  80. f = ast_read(chan);
  81. if (!f) {
  82. res = -1;
  83. break;
  84. }
  85. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
  86. ast_verb(3, "Got a ring but still waiting for timeout\n");
  87. }
  88. ast_frfree(f);
  89. }
  90. }
  91. /* Now we're really ready for the ring */
  92. if (!res) {
  93. for (;;) {
  94. int wait_res = ast_waitfor(chan, -1);
  95. if (wait_res < 0) {
  96. res = -1;
  97. break;
  98. } else {
  99. f = ast_read(chan);
  100. if (!f) {
  101. res = -1;
  102. break;
  103. }
  104. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
  105. ast_verb(3, "Got a ring after the timeout\n");
  106. ast_frfree(f);
  107. break;
  108. }
  109. ast_frfree(f);
  110. }
  111. }
  112. }
  113. if (silgen) {
  114. ast_channel_stop_silence_generator(chan, silgen);
  115. }
  116. return res;
  117. }
  118. static int unload_module(void)
  119. {
  120. return ast_unregister_application(app);
  121. }
  122. static int load_module(void)
  123. {
  124. return ast_register_application_xml(app, waitforring_exec);
  125. }
  126. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time");