sleep.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @file sleep.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Module which sleeps a given number of milliseconds on inititalization and
  32. * deinitialization.
  33. *
  34. * Synopsis: sleep(string ms_start, string ms_stop)
  35. */
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39. #include <inttypes.h>
  40. #include <limits.h>
  41. #include <ncd/NCDModule.h>
  42. #include <ncd/value_utils.h>
  43. #include <generated/blog_channel_ncd_sleep.h>
  44. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  45. struct instance {
  46. NCDModuleInst *i;
  47. btime_t ms_start;
  48. btime_t ms_stop;
  49. BTimer timer;
  50. int dying;
  51. };
  52. static void instance_free (struct instance *o);
  53. static void timer_handler (void *vo)
  54. {
  55. struct instance *o = vo;
  56. if (!o->dying) {
  57. // signal up
  58. NCDModuleInst_Backend_Up(o->i);
  59. } else {
  60. // die
  61. instance_free(o);
  62. }
  63. }
  64. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  65. {
  66. struct instance *o = vo;
  67. o->i = i;
  68. // check arguments
  69. NCDValRef ms_start_arg;
  70. NCDValRef ms_stop_arg;
  71. if (!NCDVal_ListRead(params->args, 2, &ms_start_arg, &ms_stop_arg)) {
  72. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  73. goto fail0;
  74. }
  75. if (!NCDVal_IsString(ms_start_arg) || !NCDVal_IsString(ms_stop_arg)) {
  76. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  77. goto fail0;
  78. }
  79. uintmax_t ms_start;
  80. uintmax_t ms_stop;
  81. if (!ncd_read_uintmax(ms_start_arg, &ms_start) || ms_start > INT64_MAX) {
  82. ModuleLog(o->i, BLOG_ERROR, "wrong start time");
  83. goto fail0;
  84. }
  85. if (!ncd_read_uintmax(ms_stop_arg, &ms_stop) || ms_stop > INT64_MAX) {
  86. ModuleLog(o->i, BLOG_ERROR, "wrong stop time");
  87. goto fail0;
  88. }
  89. o->ms_start = ms_start;
  90. o->ms_stop = ms_stop;
  91. // init timer
  92. BTimer_Init(&o->timer, 0, timer_handler, o);
  93. // set not dying
  94. o->dying = 0;
  95. // set timer
  96. BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->ms_start);
  97. return;
  98. fail0:
  99. NCDModuleInst_Backend_SetError(i);
  100. NCDModuleInst_Backend_Dead(i);
  101. }
  102. void instance_free (struct instance *o)
  103. {
  104. // free timer
  105. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  106. NCDModuleInst_Backend_Dead(o->i);
  107. }
  108. static void func_die (void *vo)
  109. {
  110. struct instance *o = vo;
  111. // set dying
  112. o->dying = 1;
  113. // set timer
  114. BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->ms_stop);
  115. }
  116. static struct NCDModule modules[] = {
  117. {
  118. .type = "sleep",
  119. .func_new2 = func_new,
  120. .func_die = func_die,
  121. .alloc_size = sizeof(struct instance)
  122. }, {
  123. .type = NULL
  124. }
  125. };
  126. const struct NCDModuleGroup ncdmodule_sleep = {
  127. .modules = modules
  128. };