event_template.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @file event_template.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. #include <stdlib.h>
  30. #include <misc/offset.h>
  31. #include <misc/debug.h>
  32. #include <misc/balloc.h>
  33. #include <ncd/modules/event_template.h>
  34. #define TemplateLog(o, ...) NCDModuleInst_Backend_Log((o)->i, (o)->blog_channel, __VA_ARGS__)
  35. static void enable_event (event_template *o)
  36. {
  37. ASSERT(!LinkedList1_IsEmpty(&o->events_list))
  38. ASSERT(!o->enabled)
  39. // get event
  40. struct event_template_event *e = UPPER_OBJECT(LinkedList1_GetFirst(&o->events_list), struct event_template_event, events_list_node);
  41. // remove from events list
  42. LinkedList1_Remove(&o->events_list, &e->events_list_node);
  43. // grab enabled map
  44. o->enabled_map = e->map;
  45. // append to free list
  46. LinkedList1_Append(&o->free_list, &e->events_list_node);
  47. // set enabled
  48. o->enabled = 1;
  49. // signal up
  50. NCDModuleInst_Backend_Up(o->i);
  51. }
  52. void event_template_new (event_template *o, NCDModuleInst *i, int blog_channel, int maxevents, void *user,
  53. event_template_func_free func_free)
  54. {
  55. ASSERT(maxevents > 0)
  56. // init arguments
  57. o->i = i;
  58. o->blog_channel = blog_channel;
  59. o->user = user;
  60. o->func_free = func_free;
  61. // allocate events array
  62. if (!(o->events = BAllocArray(maxevents, sizeof(o->events[0])))) {
  63. TemplateLog(o, BLOG_ERROR, "BAllocArray failed");
  64. goto fail0;
  65. }
  66. // init events lists
  67. LinkedList1_Init(&o->events_list);
  68. LinkedList1_Init(&o->free_list);
  69. for (int i = 0; i < maxevents; i++) {
  70. LinkedList1_Append(&o->free_list, &o->events[i].events_list_node);
  71. }
  72. // set not enabled
  73. o->enabled = 0;
  74. return;
  75. fail0:
  76. NCDModuleInst_Backend_SetError(o->i);
  77. o->func_free(o->user);
  78. return;
  79. }
  80. void event_template_die (event_template *o)
  81. {
  82. // free enabled map
  83. if (o->enabled) {
  84. BStringMap_Free(&o->enabled_map);
  85. }
  86. // free event maps
  87. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->events_list);
  88. while (list_node) {
  89. struct event_template_event *e = UPPER_OBJECT(list_node, struct event_template_event, events_list_node);
  90. BStringMap_Free(&e->map);
  91. list_node = LinkedList1Node_Next(list_node);
  92. }
  93. // free events array
  94. BFree(o->events);
  95. o->func_free(o->user);
  96. return;
  97. }
  98. int event_template_getvar (event_template *o, const char *name, NCDValMem *mem, NCDValRef *out)
  99. {
  100. ASSERT(o->enabled)
  101. ASSERT(name)
  102. const char *val = BStringMap_Get(&o->enabled_map, name);
  103. if (!val) {
  104. return 0;
  105. }
  106. *out = NCDVal_NewString(mem, val);
  107. if (NCDVal_IsInvalid(*out)) {
  108. TemplateLog(o, BLOG_ERROR, "NCDVal_NewString failed");
  109. }
  110. return 1;
  111. }
  112. void event_template_queue (event_template *o, BStringMap map, int *out_was_empty)
  113. {
  114. ASSERT(!LinkedList1_IsEmpty(&o->free_list))
  115. // get event
  116. struct event_template_event *e = UPPER_OBJECT(LinkedList1_GetFirst(&o->free_list), struct event_template_event, events_list_node);
  117. // remove from free list
  118. LinkedList1_Remove(&o->free_list, &e->events_list_node);
  119. // set map
  120. e->map = map;
  121. // insert to events list
  122. LinkedList1_Append(&o->events_list, &e->events_list_node);
  123. // enable if not already
  124. if (!o->enabled) {
  125. enable_event(o);
  126. *out_was_empty = 1;
  127. } else {
  128. *out_was_empty = 0;
  129. }
  130. }
  131. void event_template_dequeue (event_template *o, int *out_is_empty)
  132. {
  133. ASSERT(o->enabled)
  134. // free enabled map
  135. BStringMap_Free(&o->enabled_map);
  136. // set not enabled
  137. o->enabled = 0;
  138. // signal down
  139. NCDModuleInst_Backend_Down(o->i);
  140. // enable if there are more events
  141. if (!LinkedList1_IsEmpty(&o->events_list)) {
  142. enable_event(o);
  143. *out_is_empty = 0;
  144. } else {
  145. *out_is_empty = 1;
  146. }
  147. }
  148. int event_template_is_enabled (event_template *o)
  149. {
  150. return o->enabled;
  151. }