app_waitforring.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 ms;
  59. if (!data || (sscanf(data, "%30lg", &s) != 1)) {
  60. ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
  61. return 0;
  62. }
  63. if (ast_opt_transmit_silence) {
  64. silgen = ast_channel_start_silence_generator(chan);
  65. }
  66. ms = s * 1000.0;
  67. while (ms > 0) {
  68. ms = ast_waitfor(chan, ms);
  69. if (ms < 0) {
  70. res = ms;
  71. break;
  72. }
  73. if (ms > 0) {
  74. f = ast_read(chan);
  75. if (!f) {
  76. res = -1;
  77. break;
  78. }
  79. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
  80. ast_verb(3, "Got a ring but still waiting for timeout\n");
  81. }
  82. ast_frfree(f);
  83. }
  84. }
  85. /* Now we're really ready for the ring */
  86. if (!res) {
  87. ms = 99999999;
  88. while(ms > 0) {
  89. ms = ast_waitfor(chan, ms);
  90. if (ms < 0) {
  91. res = ms;
  92. break;
  93. }
  94. if (ms > 0) {
  95. f = ast_read(chan);
  96. if (!f) {
  97. res = -1;
  98. break;
  99. }
  100. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) {
  101. ast_verb(3, "Got a ring after the timeout\n");
  102. ast_frfree(f);
  103. break;
  104. }
  105. ast_frfree(f);
  106. }
  107. }
  108. }
  109. if (silgen) {
  110. ast_channel_stop_silence_generator(chan, silgen);
  111. }
  112. return res;
  113. }
  114. static int unload_module(void)
  115. {
  116. return ast_unregister_application(app);
  117. }
  118. static int load_module(void)
  119. {
  120. return ast_register_application_xml(app, waitforring_exec);
  121. }
  122. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");