events.cpp 3.4 KB

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