sleep.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @file sleep.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Module which sleeps a given number of milliseconds on inititalization and
  25. * deinitialization.
  26. *
  27. * Synopsis: sleep(string ms_start, string ms_stop)
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <inttypes.h>
  33. #include <ncd/NCDModule.h>
  34. #include <generated/blog_channel_ncd_sleep.h>
  35. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  36. struct instance {
  37. NCDModuleInst *i;
  38. btime_t ms_start;
  39. btime_t ms_stop;
  40. BTimer timer;
  41. int dying;
  42. };
  43. static void instance_free (struct instance *o);
  44. static void timer_handler (void *vo)
  45. {
  46. struct instance *o = vo;
  47. if (!o->dying) {
  48. // signal up
  49. NCDModuleInst_Backend_Up(o->i);
  50. } else {
  51. // die
  52. instance_free(o);
  53. }
  54. }
  55. static void func_new (NCDModuleInst *i)
  56. {
  57. // allocate instance
  58. struct instance *o = malloc(sizeof(*o));
  59. if (!o) {
  60. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  61. goto fail0;
  62. }
  63. NCDModuleInst_Backend_SetUser(i, o);
  64. // init arguments
  65. o->i = i;
  66. // check arguments
  67. NCDValue *ms_start_arg;
  68. NCDValue *ms_stop_arg;
  69. if (!NCDValue_ListRead(i->args, 2, &ms_start_arg, &ms_stop_arg)) {
  70. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  71. goto fail1;
  72. }
  73. if (NCDValue_Type(ms_start_arg) != NCDVALUE_STRING || NCDValue_Type(ms_stop_arg) != NCDVALUE_STRING) {
  74. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  75. goto fail1;
  76. }
  77. if (sscanf(NCDValue_StringValue(ms_start_arg), "%"SCNi64, &o->ms_start) != 1) {
  78. ModuleLog(o->i, BLOG_ERROR, "wrong time");
  79. goto fail1;
  80. }
  81. if (sscanf(NCDValue_StringValue(ms_stop_arg), "%"SCNi64, &o->ms_stop) != 1) {
  82. ModuleLog(o->i, BLOG_ERROR, "wrong time");
  83. goto fail1;
  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->reactor, &o->timer, o->ms_start);
  91. return;
  92. fail1:
  93. free(o);
  94. fail0:
  95. NCDModuleInst_Backend_SetError(i);
  96. NCDModuleInst_Backend_Dead(i);
  97. }
  98. void instance_free (struct instance *o)
  99. {
  100. NCDModuleInst *i = o->i;
  101. // free timer
  102. BReactor_RemoveTimer(o->i->reactor, &o->timer);
  103. // free instance
  104. free(o);
  105. NCDModuleInst_Backend_Dead(i);
  106. }
  107. static void func_die (void *vo)
  108. {
  109. struct instance *o = vo;
  110. // set dying
  111. o->dying = 1;
  112. // set timer
  113. BReactor_SetTimerAfter(o->i->reactor, &o->timer, o->ms_stop);
  114. }
  115. static const struct NCDModule modules[] = {
  116. {
  117. .type = "sleep",
  118. .func_new = func_new,
  119. .func_die = func_die
  120. }, {
  121. .type = NULL
  122. }
  123. };
  124. const struct NCDModuleGroup ncdmodule_sleep = {
  125. .modules = modules
  126. };