timer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @file timer.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. * periodic_timer(string down_time, string up_time)
  33. *
  34. * Description:
  35. * Periodically goes up and down. Starting in the down state, waits 'down_time'
  36. * milliseconds. Then it goes up, and after 'up_time' milliseconds, goes back down,
  37. * and the process repeats. When requested to terminate, terminates immedietely.
  38. */
  39. #include <stddef.h>
  40. #include <misc/debug.h>
  41. #include <ncd/NCDModule.h>
  42. #include <ncd/value_utils.h>
  43. #include <generated/blog_channel_ncd_timer.h>
  44. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  45. #define STATE_DOWN 1
  46. #define STATE_UP 2
  47. struct instance {
  48. NCDModuleInst *i;
  49. btime_t down_time;
  50. btime_t up_time;
  51. BTimer timer;
  52. int state;
  53. };
  54. static void timer_handler (void *vo)
  55. {
  56. struct instance *o = vo;
  57. switch (o->state) {
  58. case STATE_DOWN: {
  59. o->state = STATE_UP;
  60. BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->up_time);
  61. NCDModuleInst_Backend_Up(o->i);
  62. } break;
  63. case STATE_UP: {
  64. o->state = STATE_DOWN;
  65. BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->down_time);
  66. NCDModuleInst_Backend_Down(o->i);
  67. } break;
  68. default: ASSERT(0);
  69. }
  70. }
  71. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  72. {
  73. struct instance *o = vo;
  74. o->i = i;
  75. // check arguments
  76. NCDValRef down_time_arg;
  77. NCDValRef up_time_arg;
  78. if (!NCDVal_ListRead(params->args, 2, &down_time_arg, &up_time_arg)) {
  79. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  80. goto fail0;
  81. }
  82. if (!NCDVal_IsString(down_time_arg) || !NCDVal_IsString(up_time_arg)) {
  83. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  84. goto fail0;
  85. }
  86. // read times
  87. if (!ncd_read_time(down_time_arg, &o->down_time)) {
  88. ModuleLog(o->i, BLOG_ERROR, "wrong down time");
  89. goto fail0;
  90. }
  91. if (!ncd_read_time(up_time_arg, &o->up_time)) {
  92. ModuleLog(o->i, BLOG_ERROR, "wrong up time");
  93. goto fail0;
  94. }
  95. // init timer
  96. BTimer_Init(&o->timer, 0, timer_handler, o);
  97. // set state down
  98. o->state = STATE_DOWN;
  99. // set timer
  100. BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->down_time);
  101. return;
  102. fail0:
  103. NCDModuleInst_Backend_SetError(i);
  104. NCDModuleInst_Backend_Dead(i);
  105. }
  106. static void func_die (void *vo)
  107. {
  108. struct instance *o = vo;
  109. // free timer
  110. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  111. NCDModuleInst_Backend_Dead(o->i);
  112. }
  113. static struct NCDModule modules[] = {
  114. {
  115. .type = "periodic_timer",
  116. .func_new2 = func_new,
  117. .func_die = func_die,
  118. .alloc_size = sizeof(struct instance)
  119. }, {
  120. .type = NULL
  121. }
  122. };
  123. const struct NCDModuleGroup ncdmodule_timer = {
  124. .modules = modules
  125. };