app_waituntil.c 3.1 KB

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