events.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * @param subscriber name of the subscribing process
  84. */
  85. void event_subscribe(const char *event, void (*handler)(event_t *), const char *subscriber) {
  86. subscribeMutex.lock();
  87. events_t * subscribeEvent = event_find(event);
  88. if(!subscribeEvent){//create new Event
  89. subscribeEvent = (events_t *) malloc(sizeof(events_t));
  90. if (!subscribeEvent)
  91. logger_error("Not enough memory\n");
  92. subscribeEvent->next = NULL;
  93. subscribeEvent->subscribers = NULL;
  94. strcpy(subscribeEvent->event, event);
  95. events_t *tmp = event_findlast();
  96. if (tmp)
  97. tmp->next = subscribeEvent;
  98. else
  99. firstEvent = subscribeEvent;
  100. }
  101. addSubscriber(subscribeEvent, handler);
  102. logger(V_HAL, "%s subscribed to event \"%s\"\n", subscriber, event);
  103. subscribeMutex.unlock();
  104. }
  105. /**
  106. * Triggers a specific event
  107. * @param event Event data
  108. */
  109. void event_trigger(event_t *event) {
  110. events_t *tmp = event_find(event->event);
  111. if (!tmp) {
  112. logger(V_HAL, "No subscribers to event \"%s\"\n", event->event);
  113. return;
  114. }
  115. subscriber_t *sub = tmp->subscribers;
  116. while (sub) {
  117. sub->handler(event);
  118. sub = sub->next;
  119. }
  120. logger(V_HAL, "Triggered event \"%s\"\n", event->event);
  121. }
  122. /**
  123. * Triggers a specific event
  124. * @param event Name of event
  125. */
  126. void event_trigger(char *event) {
  127. event_t e;
  128. e.event = (const char *)event;
  129. event_trigger(&e);
  130. }
  131. /**
  132. * Triggers a specific event
  133. * @param event Name of event
  134. */
  135. void event_trigger(const char *event) {
  136. event_t e;
  137. e.event = event;
  138. event_trigger(&e);
  139. }
  140. /**
  141. * Triggers a specific event
  142. * @param event Name of event
  143. * @param data Pointer to some data
  144. * @param len Length of data
  145. */
  146. void event_trigger(char *event, void *data, int len) {
  147. event_trigger((const char *) event, data, len);
  148. }
  149. /**
  150. * Triggers a specific event
  151. * @param event Name of event
  152. * @param data Pointer to some data
  153. * @param len Length of data
  154. */
  155. void event_trigger(const char *event, void *data, int len) {
  156. event_t e;
  157. e.event = event;
  158. e.data = data;
  159. e.len = len;
  160. event_trigger(&e);
  161. }