app_waitforring.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Wait for Ring Application
  5. *
  6. * Copyright (C) 2003, Digium
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <sys/types.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/options.h>
  20. #include <asterisk/lock.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. static char *synopsis = "Wait for Ring Application";
  26. static char *tdesc = "Waits until first ring after time";
  27. static char *desc = " WaitForRing(timeout)\n"
  28. "Returns 0 after waiting at least timeout seconds. and\n"
  29. "only after the next ring has completed. Returns 0 on\n"
  30. "success or -1 on hangup\n";
  31. static char *app = "WaitForRing";
  32. STANDARD_LOCAL_USER;
  33. LOCAL_USER_DECL;
  34. static int waitforring_exec(struct ast_channel *chan, void *data)
  35. {
  36. struct localuser *u;
  37. struct ast_frame *f;
  38. int res = 0;
  39. int ms;
  40. if (!data || (sscanf(data, "%d", &ms) != 1)) {
  41. ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
  42. return 0;
  43. }
  44. ms *= 1000;
  45. LOCAL_USER_ADD(u);
  46. while(ms > 0) {
  47. ms = ast_waitfor(chan, ms);
  48. if (ms < 0) {
  49. res = ms;
  50. break;
  51. }
  52. if (ms > 0) {
  53. f = ast_read(chan);
  54. if (!f) {
  55. res = -1;
  56. break;
  57. }
  58. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
  59. if (option_verbose > 2)
  60. ast_verbose(VERBOSE_PREFIX_3 "Got a ring but still waiting for timeout\n");
  61. }
  62. ast_frfree(f);
  63. }
  64. }
  65. /* Now we're really ready for the ring */
  66. if (!res) {
  67. ms = 99999999;
  68. while(ms > 0) {
  69. ms = ast_waitfor(chan, ms);
  70. if (ms < 0) {
  71. res = ms;
  72. break;
  73. }
  74. if (ms > 0) {
  75. f = ast_read(chan);
  76. if (!f) {
  77. res = -1;
  78. break;
  79. }
  80. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
  81. if (option_verbose > 2)
  82. ast_verbose(VERBOSE_PREFIX_3 "Got a ring after the timeout\n");
  83. ast_frfree(f);
  84. break;
  85. }
  86. ast_frfree(f);
  87. }
  88. }
  89. }
  90. LOCAL_USER_REMOVE(u);
  91. return res;
  92. }
  93. int unload_module(void)
  94. {
  95. STANDARD_HANGUP_LOCALUSERS;
  96. return ast_unregister_application(app);
  97. }
  98. int load_module(void)
  99. {
  100. return ast_register_application(app, waitforring_exec, synopsis, desc);
  101. }
  102. char *description(void)
  103. {
  104. return tdesc;
  105. }
  106. int usecount(void)
  107. {
  108. int res;
  109. STANDARD_USECOUNT(res);
  110. return res;
  111. }
  112. char *key()
  113. {
  114. return ASTERISK_GPL_KEY;
  115. }