reb_def.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Change Logs:
  3. * Date Author Notes
  4. * 2023-10-10 RiceChen the first version
  5. */
  6. #ifndef __REB_DEF_H__
  7. #define __REB_DEF_H__
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #include <string.h>
  16. typedef enum {
  17. REB_OK = 0, /**< There is no error */
  18. REB_ERROR, /**< A generic error happens */
  19. REB_FULL, /**< The resource is full */
  20. REB_EMPTY, /**< The resource is empty */
  21. REB_NOMEM, /**< No memory */
  22. REB_INVAL, /**< Invalid argument */
  23. } reb_status;
  24. #define REB_INLINE static __inline
  25. /**
  26. * memory API
  27. */
  28. #ifndef REB_MALLOC
  29. #define REB_MALLOC malloc
  30. #endif
  31. #ifndef REB_FREE
  32. #define REB_FREE free
  33. #endif
  34. /**
  35. * debug API
  36. */
  37. #define REB_LOGE_EN 1
  38. #define REB_LOGD_EN 0
  39. #define REB_LOGI_EN 0
  40. #ifndef REB_PRINT
  41. #ifdef __RTTHREAD__
  42. #include <rtthread.h>
  43. #define REB_PRINT rt_kprintf
  44. #else
  45. #define REB_PRINT printf
  46. #endif
  47. #endif
  48. #ifndef REB_PRINT_TAG
  49. #define REB_PRINT_TAG "REB"
  50. #endif
  51. #if REB_LOGE_EN
  52. #define REB_LOGE(...) REB_PRINT("\033[31;22m[E/%s](%s:%d) ", REB_PRINT_TAG, __FUNCTION__, __LINE__); \
  53. REB_PRINT(__VA_ARGS__); \
  54. REB_PRINT("\033[0m\n")
  55. #else
  56. #define REB_LOGE(...)
  57. #endif
  58. #if REB_LOGI_EN
  59. #define REB_LOGI(...) REB_PRINT("\033[32;22m[I/%s](%s:%d) ", REB_PRINT_TAG, __FUNCTION__, __LINE__); \
  60. REB_PRINT(__VA_ARGS__); \
  61. REB_PRINT("\033[0m\n")
  62. #else
  63. #define REB_LOGI(...)
  64. #endif
  65. #if REB_LOGD_EN
  66. #define REB_LOGD(...) REB_PRINT("[D/%s](%s:%d) ", REB_PRINT_TAG, __FUNCTION__, __LINE__); \
  67. REB_PRINT(__VA_ARGS__); \
  68. REB_PRINT("\n");
  69. #else
  70. #define REB_LOGD(...)
  71. #endif
  72. #define REB_ALL_MINOR_TYPE (0xFFFF)
  73. #define REB_MK_EVENT_TYPE(mojor, minor) \
  74. (((uint32_t)(mojor) << 16) | ((uint32_t)(minor) & 0xFFFF))
  75. #define REB_EVENT_MOJOR_TYPE(event) ((uint16_t)((event) >> 16))
  76. #define REB_EVENT_MINOR_TYPE(event) ((uint32_t)(event) & 0xFFFF)
  77. #define REB_EVENT_TYPE_MOJOR_CMP(pub_type, obs_type) (!((pub_type ^ obs_type) & 0xFFFF0000))
  78. typedef uint32_t reb_time_t;
  79. /**
  80. * Task API
  81. */
  82. typedef void *reb_task_id;
  83. typedef struct reb_task_attr{
  84. char *name; // name of the task
  85. uint32_t stack_size; // size of stack
  86. uint8_t priority; // task priority
  87. } reb_task_attr;
  88. typedef void(*reb_task_func)(void *arg);
  89. reb_task_id reb_task_create(reb_task_func func, void *arg, reb_task_attr *attr);
  90. void reb_task_delete(reb_task_id task);
  91. /**
  92. * Mutex API
  93. */
  94. typedef void *reb_mutex_id;
  95. reb_mutex_id reb_mutex_create(void);
  96. reb_status reb_mutex_lock(reb_mutex_id mutex);
  97. reb_status reb_mutex_unlock(reb_mutex_id mutex);
  98. void reb_mutex_delete(reb_mutex_id mutex);
  99. /**
  100. * Sem API
  101. */
  102. typedef void *reb_sem_id;
  103. reb_sem_id reb_sem_create(uint32_t value);
  104. reb_status reb_sem_lock(reb_sem_id sem, reb_time_t time);
  105. reb_status reb_sem_unlock(reb_sem_id sem);
  106. void reb_sem_delete(reb_sem_id sem);
  107. /**
  108. * Queue API
  109. */
  110. typedef void *reb_queue_id;
  111. reb_queue_id reb_queue_create(int size, int count);
  112. reb_status reb_queue_send(reb_queue_id queue, const void *msg,
  113. uint32_t size, reb_time_t timeout);
  114. reb_status reb_queue_urgent_send(reb_queue_id queue, const void *msg,
  115. uint32_t size, reb_time_t timeout);
  116. reb_status reb_queue_recv(reb_queue_id queue, void *msg, uint32_t size);
  117. void reb_queue_delete(reb_queue_id queue);
  118. /**
  119. * List Api
  120. */
  121. struct reb_list_node {
  122. struct reb_list_node *next;
  123. struct reb_list_node *prev;
  124. };
  125. typedef struct reb_list_node reb_list_t;
  126. REB_INLINE void reb_list_init(reb_list_t *l)
  127. {
  128. l->next = l->prev = l;
  129. }
  130. REB_INLINE void reb_list_insert_after(reb_list_t *l, reb_list_t *n)
  131. {
  132. l->next->prev = n;
  133. n->next = l->next;
  134. l->next = n;
  135. n->prev = l;
  136. }
  137. REB_INLINE void reb_list_insert_before(reb_list_t *l, reb_list_t *n)
  138. {
  139. l->prev->next = n;
  140. n->prev = l->prev;
  141. l->prev = n;
  142. n->next = l;
  143. }
  144. REB_INLINE void reb_list_remove(reb_list_t *n)
  145. {
  146. n->next->prev = n->prev;
  147. n->prev->next = n->next;
  148. n->next = n->prev = n;
  149. }
  150. REB_INLINE int reb_list_is_empty(const reb_list_t *l)
  151. {
  152. return l->next == l;
  153. }
  154. REB_INLINE int reb_list_len(const reb_list_t *l)
  155. {
  156. int len = 0;
  157. const reb_list_t *p = l;
  158. while (p->next != l) {
  159. p = p->next;
  160. len ++;
  161. }
  162. return len;
  163. }
  164. #define reb_container_of(ptr, type, member) \
  165. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  166. #define reb_list_obj_init(obj) {&(obj), &(obj)}
  167. #define reb_list_entry(node, type, member) \
  168. reb_container_of(node, type, member)
  169. #define reb_list_for_each(pos, head) \
  170. for (pos = (head)->next; pos != (head); pos = pos->next)
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif