sleep.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 (NCDModuleInst *i)
  63. {
  64. // allocate instance
  65. struct instance *o = malloc(sizeof(*o));
  66. if (!o) {
  67. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  68. goto fail0;
  69. }
  70. NCDModuleInst_Backend_SetUser(i, o);
  71. // init arguments
  72. o->i = i;
  73. // check arguments
  74. NCDValue *ms_start_arg;
  75. NCDValue *ms_stop_arg;
  76. if (!NCDValue_ListRead(i->args, 2, &ms_start_arg, &ms_stop_arg)) {
  77. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  78. goto fail1;
  79. }
  80. if (!NCDValue_IsStringNoNulls(ms_start_arg) || !NCDValue_IsStringNoNulls(ms_stop_arg)) {
  81. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  82. goto fail1;
  83. }
  84. if (sscanf(NCDValue_StringValue(ms_start_arg), "%"SCNi64, &o->ms_start) != 1) {
  85. ModuleLog(o->i, BLOG_ERROR, "wrong time");
  86. goto fail1;
  87. }
  88. if (sscanf(NCDValue_StringValue(ms_stop_arg), "%"SCNi64, &o->ms_stop) != 1) {
  89. ModuleLog(o->i, BLOG_ERROR, "wrong time");
  90. goto fail1;
  91. }
  92. // init timer
  93. BTimer_Init(&o->timer, 0, timer_handler, o);
  94. // set not dying
  95. o->dying = 0;
  96. // set timer
  97. BReactor_SetTimerAfter(o->i->params->reactor, &o->timer, o->ms_start);
  98. return;
  99. fail1:
  100. free(o);
  101. fail0:
  102. NCDModuleInst_Backend_SetError(i);
  103. NCDModuleInst_Backend_Dead(i);
  104. }
  105. void instance_free (struct instance *o)
  106. {
  107. NCDModuleInst *i = o->i;
  108. // free timer
  109. BReactor_RemoveTimer(o->i->params->reactor, &o->timer);
  110. // free instance
  111. free(o);
  112. NCDModuleInst_Backend_Dead(i);
  113. }
  114. static void func_die (void *vo)
  115. {
  116. struct instance *o = vo;
  117. // set dying
  118. o->dying = 1;
  119. // set timer
  120. BReactor_SetTimerAfter(o->i->params->reactor, &o->timer, o->ms_stop);
  121. }
  122. static const struct NCDModule modules[] = {
  123. {
  124. .type = "sleep",
  125. .func_new = func_new,
  126. .func_die = func_die
  127. }, {
  128. .type = NULL
  129. }
  130. };
  131. const struct NCDModuleGroup ncdmodule_sleep = {
  132. .modules = modules
  133. };