events.cpp 3.7 KB

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