schev.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /** @brief Mechanism to shedule events.
  2. */
  3. #include <sys/time.h>
  4. #include <time.h>
  5. #include <errno.h>
  6. #include <stdint.h>
  7. #include <assert.h>
  8. #include "private.h"
  9. #include "faux/faux.h"
  10. #include "faux/time.h"
  11. #include "faux/list.h"
  12. #include "faux/schev.h"
  13. /** @brief Allocates new schev (SCHedule EVent) object.
  14. *
  15. * Before working with schev object it must be allocated and initialized.
  16. *
  17. * @return Allocated and initialized schev object or NULL on error.
  18. */
  19. faux_schev_t *faux_schev_new(void)
  20. {
  21. faux_schev_t *schev = NULL;
  22. schev = faux_zmalloc(sizeof(*schev));
  23. if (!schev)
  24. return NULL;
  25. // Init
  26. schev->list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
  27. faux_ev_compare, NULL, faux_ev_free);
  28. return schev;
  29. }
  30. /** @brief Frees the schev object.
  31. *
  32. * After using the schev object must be freed. Function frees object itself
  33. * and all events stored within schev object.
  34. */
  35. void faux_schev_free(faux_schev_t *schev)
  36. {
  37. assert(schev);
  38. if (!schev)
  39. return;
  40. faux_list_free(schev->list);
  41. faux_free(schev);
  42. }
  43. /** @brief Adds event to scheduling list using absolute time.
  44. *
  45. * @param [in] sched Allocated and initialized sched object.
  46. * @param [in] time Absolute time of future event.
  47. * @param [in] Event ID.
  48. * @param [in] Pointer to arbitrary data linked to event.
  49. * @return 0 - success, < 0 on error.
  50. */
  51. int faux_schev_schedule(
  52. faux_schev_t *schev, const struct timespec *time, int ev_id, void *data)
  53. {
  54. faux_ev_t *ev = NULL;
  55. faux_list_node_t *node = NULL;
  56. assert(schev);
  57. if (!schev)
  58. return -1;
  59. ev = faux_ev_new(time, ev_id, data);
  60. assert(ev);
  61. if (!ev)
  62. return -1;
  63. node = faux_list_add(schev->list, ev);
  64. if (!node) { // Something went wrong
  65. faux_ev_free(ev);
  66. return -1;
  67. }
  68. return 0;
  69. }
  70. /** @brief Adds event to scheduling list using interval.
  71. *
  72. * Add interval to the list. The absolute time is calculated by
  73. * adding specified interval to the current absolute time.
  74. *
  75. * @param [in] sched Allocated and initialized sched object.
  76. * @param [in] interval Interval (NULL means "now").
  77. * @param [in] Event ID.
  78. * @param [in] Pointer to arbitrary data linked to event.
  79. * @return 0 - success, < 0 on error.
  80. */
  81. int faux_schev_schedule_interval(faux_schev_t *schev,
  82. const struct timespec *interval, int ev_id, void *data)
  83. {
  84. struct timespec t = {};
  85. struct timespec plan = {};
  86. assert(schev);
  87. if (!schev)
  88. return -1;
  89. if (!interval)
  90. return faux_schev_schedule(schev, FAUX_SCHEV_NOW, ev_id, data);
  91. clock_gettime(FAUX_SCHEV_CLOCK_SOURCE, &t);
  92. faux_timespec_sum(&plan, &t, interval);
  93. return faux_schev_schedule(schev, &plan, ev_id, data);
  94. }