events.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * events.cpp
  3. *
  4. * Created on: Jan 7, 2018
  5. * Author: Philipp Hinz
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include "events.h"
  12. #include "logger.h"
  13. /**
  14. * Struct for subscribers, contains the pointer to the handler
  15. */
  16. struct subscriber_t {
  17. void (*handler)(event_t *);
  18. subscriber_t *next = NULL;
  19. };
  20. /**
  21. * Struct for the event list
  22. */
  23. struct events_t {
  24. char event[EVENT_LENGTH];
  25. subscriber_t *subscribers = NULL;
  26. events_t *next = NULL;
  27. };
  28. events_t *firstEvent = NULL; /**< The first element in the event list */
  29. /**
  30. * Finds the specific event in the list and returns it
  31. * @param event Event name
  32. * @return Pointer to event, NULL if not found
  33. */
  34. events_t * event_find(const char *event) {
  35. events_t *tmp = firstEvent;
  36. while (tmp) {
  37. if (!strcmp(tmp->event, event))
  38. return tmp;
  39. tmp = tmp->next;
  40. }
  41. return NULL;
  42. }
  43. /**
  44. * Finds the last event in the list and returns it
  45. * @return Pointer to event, NULL if no event in list
  46. */
  47. events_t * event_findlast() {
  48. events_t *tmp = firstEvent;
  49. while (tmp) {
  50. if (!tmp->next)
  51. return tmp;
  52. tmp = tmp->next;
  53. }
  54. return NULL;
  55. }
  56. /**
  57. * Adds a subscriber to an event
  58. * @param event Target event
  59. * @param handler Event handler
  60. */
  61. void addSubscriber(events_t *event, void (*handler)(event_t *)) {
  62. subscriber_t *newSub = (subscriber_t *) malloc(sizeof(subscriber_t)); // Cast is neccesarry in c++
  63. if (!newSub)
  64. logger_error("Not enough memory\n");
  65. newSub->handler = handler;
  66. newSub->next = NULL;
  67. if (event->subscribers) {
  68. subscriber_t *tmp = event->subscribers;
  69. while (tmp->next) {
  70. tmp = tmp->next;
  71. }
  72. tmp->next = newSub;
  73. } else {
  74. event->subscribers = newSub;
  75. }
  76. }
  77. /**
  78. * Adds a subscriber to an event
  79. * @param event Target event name
  80. * @param handler Event handler
  81. */
  82. void event_subscribe(const char *event, void (*handler)(event_t *)) {
  83. //TODO change event name to a typedef enum or similar
  84. events_t * subscribeEvent = event_find(event);
  85. if(!subscribeEvent){//create new Event
  86. subscribeEvent = (events_t *) malloc(sizeof(events_t));
  87. if (!subscribeEvent)
  88. logger_error("Not enough memory\n");
  89. subscribeEvent->next = NULL;
  90. subscribeEvent->subscribers = NULL;
  91. strcpy(subscribeEvent->event, event);
  92. events_t *tmp = event_findlast();
  93. if (tmp)
  94. tmp->next = subscribeEvent;
  95. else
  96. firstEvent = subscribeEvent;
  97. }
  98. addSubscriber(subscribeEvent, handler);
  99. logger(V_HAL, "New subscriber to event \"%s\"\n", event);
  100. }
  101. /**
  102. * Triggers a specific event
  103. * @param event Event data
  104. */
  105. void event_trigger(event_t *event) {
  106. events_t *tmp = event_find(event->event);
  107. if (!tmp) {
  108. logger(V_HAL, "No subscribers to event \"%s\"\n", event->event);
  109. return;
  110. }
  111. subscriber_t *sub = tmp->subscribers;
  112. while (sub) {
  113. sub->handler(event);
  114. sub = sub->next;
  115. }
  116. logger(V_HAL, "Triggered event \"%s\"\n", event->event);
  117. }
  118. /**
  119. * Triggers a specific event
  120. * @param event Name of event
  121. */
  122. void event_trigger(char *event) {
  123. event_t e;
  124. e.event = (const char *)event;
  125. event_trigger(&e);
  126. }
  127. /**
  128. * Triggers a specific event
  129. * @param event Name of event
  130. */
  131. void event_trigger(const char *event) {
  132. event_t e;
  133. e.event = event;
  134. event_trigger(&e);
  135. }
  136. /**
  137. * Triggers a specific event
  138. * @param event Name of event
  139. * @param data Pointer to some data
  140. * @param len Length of data
  141. */
  142. void event_trigger(char *event, void *data, int len) {
  143. event_trigger((const char *) event, data, len);
  144. }
  145. /**
  146. * Triggers a specific event
  147. * @param event Name of event
  148. * @param data Pointer to some data
  149. * @param len Length of data
  150. */
  151. void event_trigger(const char *event, void *data, int len) {
  152. event_t e;
  153. e.event = event;
  154. e.data = data;
  155. e.len = len;
  156. event_trigger(&e);
  157. }