events.cpp 3.5 KB

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