app_waituntil.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007, Redfish Solutions
  5. *
  6. * Philip Prindeville <philipp@redfish-solutions.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 Sleep until the given epoch
  21. *
  22. * \author Philip Prindeville <philipp@redfish-solutions.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. #include "asterisk/logger.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. /*** DOCUMENTATION
  35. <application name="WaitUntil" language="en_US">
  36. <synopsis>
  37. Wait (sleep) until the current time is the given epoch.
  38. </synopsis>
  39. <syntax>
  40. <parameter name="epoch" required="true" />
  41. </syntax>
  42. <description>
  43. <para>Waits until the given <replaceable>epoch</replaceable>.</para>
  44. <para>Sets <variable>WAITUNTILSTATUS</variable> to one of the following values:</para>
  45. <variablelist>
  46. <variable name="WAITUNTILSTATUS">
  47. <value name="OK">
  48. Wait succeeded.
  49. </value>
  50. <value name="FAILURE">
  51. Invalid argument.
  52. </value>
  53. <value name="HANGUP">
  54. Channel hungup before time elapsed.
  55. </value>
  56. <value name="PAST">
  57. Time specified had already past.
  58. </value>
  59. </variable>
  60. </variablelist>
  61. </description>
  62. </application>
  63. ***/
  64. static char *app = "WaitUntil";
  65. static int waituntil_exec(struct ast_channel *chan, const char *data)
  66. {
  67. int res;
  68. double fraction;
  69. long seconds;
  70. struct timeval future = { 0, };
  71. struct timeval now = ast_tvnow();
  72. int msec;
  73. if (ast_strlen_zero(data)) {
  74. ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
  75. pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
  76. return 0;
  77. }
  78. if (sscanf(data, "%30ld%30lf", &seconds, &fraction) == 0) {
  79. ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
  80. pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
  81. return 0;
  82. }
  83. future.tv_sec = seconds;
  84. future.tv_usec = fraction * 1000000;
  85. if ((msec = ast_tvdiff_ms(future, now)) < 0) {
  86. ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)now.tv_sec, (long)future.tv_sec);
  87. pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
  88. return 0;
  89. }
  90. if ((res = ast_safe_sleep(chan, msec)))
  91. pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
  92. else
  93. pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
  94. return res;
  95. }
  96. static int unload_module(void)
  97. {
  98. return ast_unregister_application(app);
  99. }
  100. static int load_module(void)
  101. {
  102. return ast_register_application_xml(app, waituntil_exec);
  103. }
  104. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");