sleep.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * Synopsis:
  32. * sleep(string ms_start [, string ms_stop])
  33. *
  34. * Description:
  35. * On init, sleeps 'ms_start' milliseconds then goes up, or goes up immediately
  36. * if 'ms_start' is an empty string.
  37. * On deinit, sleeps 'ms_stop' milliseconds then dies, or dies immediately if
  38. * 'ms_stop' is an empty string or is not provided. If a deinit is requested while
  39. * the init sleep is still in progress, the init sleep is aborted and the deinit
  40. * sleep is started immediately (if any).
  41. */
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <stdio.h>
  45. #include <inttypes.h>
  46. #include <limits.h>
  47. #include <ncd/NCDModule.h>
  48. #include <ncd/static_strings.h>
  49. #include <ncd/extra/value_utils.h>
  50. #include <generated/blog_channel_ncd_sleep.h>
  51. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  52. struct instance {
  53. NCDModuleInst *i;
  54. btime_t ms_stop;
  55. BTimer timer;
  56. int dying;
  57. };
  58. static void instance_free (struct instance *o);
  59. static void timer_handler (void *vo)
  60. {
  61. struct instance *o = vo;
  62. if (!o->dying) {
  63. // signal up
  64. NCDModuleInst_Backend_Up(o->i);
  65. } else {
  66. // die
  67. instance_free(o);
  68. }
  69. }
  70. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  71. {
  72. struct instance *o = vo;
  73. o->i = i;
  74. // check arguments
  75. NCDValRef ms_start_arg;
  76. NCDValRef ms_stop_arg = NCDVal_NewInvalid();
  77. if (!NCDVal_ListRead(params->args, 1, &ms_start_arg) &&
  78. !NCDVal_ListRead(params->args, 2, &ms_start_arg, &ms_stop_arg)) {
  79. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  80. goto fail0;
  81. }
  82. if (!NCDVal_IsString(ms_start_arg) || (!NCDVal_IsInvalid(ms_stop_arg) && !NCDVal_IsString(ms_stop_arg))) {
  83. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  84. goto fail0;
  85. }
  86. uintmax_t ms;
  87. btime_t ms_start;
  88. if (NCDVal_StringEqualsId(ms_start_arg, NCD_STRING_EMPTY, i->params->iparams->string_index)) {
  89. ms_start = -1;
  90. } else {
  91. if (!ncd_read_uintmax(ms_start_arg, &ms) || ms > INT64_MAX) {
  92. ModuleLog(o->i, BLOG_ERROR, "wrong start time");
  93. goto fail0;
  94. }
  95. ms_start = ms;
  96. }
  97. if (NCDVal_IsInvalid(ms_stop_arg) || NCDVal_StringEqualsId(ms_stop_arg, NCD_STRING_EMPTY, i->params->iparams->string_index)) {
  98. o->ms_stop = -1;
  99. } else {
  100. if (!ncd_read_uintmax(ms_stop_arg, &ms) || ms > INT64_MAX) {
  101. ModuleLog(o->i, BLOG_ERROR, "wrong stop time");
  102. goto fail0;
  103. }
  104. o->ms_stop = ms;
  105. }
  106. // init timer
  107. BTimer_Init(&o->timer, 0, timer_handler, o);
  108. // set not dying
  109. o->dying = 0;
  110. if (ms_start < 0) {
  111. // go up
  112. NCDModuleInst_Backend_Up(i);
  113. } else {
  114. // set timer
  115. BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, ms_start);
  116. }
  117. return;
  118. fail0:
  119. NCDModuleInst_Backend_DeadError(i);
  120. }
  121. void instance_free (struct instance *o)
  122. {
  123. // free timer
  124. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  125. NCDModuleInst_Backend_Dead(o->i);
  126. }
  127. static void func_die (void *vo)
  128. {
  129. struct instance *o = vo;
  130. if (o->ms_stop < 0) {
  131. // die immediately
  132. instance_free(o);
  133. return;
  134. }
  135. // set dying
  136. o->dying = 1;
  137. // set timer
  138. BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->ms_stop);
  139. }
  140. static struct NCDModule modules[] = {
  141. {
  142. .type = "sleep",
  143. .func_new2 = func_new,
  144. .func_die = func_die,
  145. .alloc_size = sizeof(struct instance)
  146. }, {
  147. .type = NULL
  148. }
  149. };
  150. const struct NCDModuleGroup ncdmodule_sleep = {
  151. .modules = modules
  152. };