sleep.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <ncd/NCDModule.h>
  41. #include <generated/blog_channel_ncd_sleep.h>
  42. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  43. struct instance {
  44. NCDModuleInst *i;
  45. btime_t ms_start;
  46. btime_t ms_stop;
  47. BTimer timer;
  48. int dying;
  49. };
  50. static void instance_free (struct instance *o);
  51. static void timer_handler (void *vo)
  52. {
  53. struct instance *o = vo;
  54. if (!o->dying) {
  55. // signal up
  56. NCDModuleInst_Backend_Up(o->i);
  57. } else {
  58. // die
  59. instance_free(o);
  60. }
  61. }
  62. static void func_new (void *vo, NCDModuleInst *i)
  63. {
  64. struct instance *o = vo;
  65. o->i = i;
  66. // check arguments
  67. NCDValRef ms_start_arg;
  68. NCDValRef ms_stop_arg;
  69. if (!NCDVal_ListRead(i->args, 2, &ms_start_arg, &ms_stop_arg)) {
  70. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  71. goto fail0;
  72. }
  73. if (!NCDVal_IsStringNoNulls(ms_start_arg) || !NCDVal_IsStringNoNulls(ms_stop_arg)) {
  74. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  75. goto fail0;
  76. }
  77. if (sscanf(NCDVal_StringValue(ms_start_arg), "%"SCNi64, &o->ms_start) != 1) {
  78. ModuleLog(o->i, BLOG_ERROR, "wrong time");
  79. goto fail0;
  80. }
  81. if (sscanf(NCDVal_StringValue(ms_stop_arg), "%"SCNi64, &o->ms_stop) != 1) {
  82. ModuleLog(o->i, BLOG_ERROR, "wrong time");
  83. goto fail0;
  84. }
  85. // init timer
  86. BTimer_Init(&o->timer, 0, timer_handler, o);
  87. // set not dying
  88. o->dying = 0;
  89. // set timer
  90. BReactor_SetTimerAfter(o->i->iparams->reactor, &o->timer, o->ms_start);
  91. return;
  92. fail0:
  93. NCDModuleInst_Backend_SetError(i);
  94. NCDModuleInst_Backend_Dead(i);
  95. }
  96. void instance_free (struct instance *o)
  97. {
  98. // free timer
  99. BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
  100. NCDModuleInst_Backend_Dead(o->i);
  101. }
  102. static void func_die (void *vo)
  103. {
  104. struct instance *o = vo;
  105. // set dying
  106. o->dying = 1;
  107. // set timer
  108. BReactor_SetTimerAfter(o->i->iparams->reactor, &o->timer, o->ms_stop);
  109. }
  110. static const struct NCDModule modules[] = {
  111. {
  112. .type = "sleep",
  113. .func_new2 = func_new,
  114. .func_die = func_die,
  115. .alloc_size = sizeof(struct instance)
  116. }, {
  117. .type = NULL
  118. }
  119. };
  120. const struct NCDModuleGroup ncdmodule_sleep = {
  121. .modules = modules
  122. };