rtp_def.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Change Logs:
  3. * Date Author Notes
  4. * 2023-05-12 RiceChen the first version
  5. */
  6. #ifndef __RTP_DEF_H__
  7. #define __RTP_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. RTP_EOK = 0, /**< There is no error */
  18. RTP_ERROR, /**< A generic error happens */
  19. RTP_EFULL, /**< The resource is full */
  20. RTP_EEMPTY, /**< The resource is empty */
  21. RTP_ENOMEM, /**< No memory */
  22. RTP_EINVAL, /**< Invalid argument */
  23. }rtp_err_t;
  24. #define RTP_INLINE static __inline
  25. /**
  26. * memory API
  27. */
  28. #ifndef RTP_MALLOC
  29. #define RTP_MALLOC malloc
  30. #endif
  31. #ifndef RTP_FREE
  32. #define RTP_FREE free
  33. #endif
  34. #ifndef RTP_PRINT
  35. #define RTP_PRINT printf
  36. #endif
  37. #ifndef RTP_PRINT_TAG
  38. #define RTP_PRINT_TAG "RTP"
  39. #endif
  40. #define RTP_LOGE(...) RTP_PRINT("\033[31;22m[E/%s](%s:%d) ", RTP_PRINT_TAG, __FUNCTION__, __LINE__); \
  41. RTP_PRINT(__VA_ARGS__); \
  42. RTP_PRINT("\033[0m\n")
  43. #define RTP_LOGI(...) RTP_PRINT("\033[32;22m[I/%s](%s:%d) ", RTP_PRINT_TAG, __FUNCTION__, __LINE__); \
  44. RTP_PRINT(__VA_ARGS__); \
  45. RTP_PRINT("\033[0m\n")
  46. #define RTP_LOGD(...) RTP_PRINT("[D/%s](%s:%d) ", RTP_PRINT_TAG, __FUNCTION__, __LINE__); \
  47. RTP_PRINT(__VA_ARGS__); \
  48. RTP_PRINT("\n")
  49. typedef void *rtp_task_id;
  50. typedef void *rtp_mutex_id;
  51. typedef void *rtp_sem_id;
  52. /**
  53. * Task API
  54. */
  55. struct rtp_task_attr{
  56. char *name; // name of the task
  57. uint32_t stack_size; // size of stack
  58. uint8_t priority; // initial task priority
  59. };
  60. typedef void(*rtp_task_func)(void *arg);
  61. rtp_task_id rtp_task_create(rtp_task_func func, void *arg, const struct rtp_task_attr *attr);
  62. void rtp_task_delete(rtp_task_id task);
  63. rtp_err_t rtp_task_suspend(rtp_task_id task);
  64. rtp_err_t rtp_task_resume(rtp_task_id task);
  65. /**
  66. * Mutex API
  67. */
  68. rtp_mutex_id rtp_mutex_create(void);
  69. rtp_err_t rtp_mutex_lock(rtp_mutex_id mutex);
  70. rtp_err_t rtp_mutex_unlock(rtp_mutex_id mutex);
  71. void rtp_mutex_delete(rtp_mutex_id mutex);
  72. /**
  73. * Sem API
  74. */
  75. rtp_sem_id rtp_sem_create(uint32_t value);
  76. rtp_err_t rtp_sem_lock(rtp_sem_id sem);
  77. rtp_err_t rtp_sem_unlock(rtp_sem_id sem);
  78. void rtp_sem_delete(rtp_sem_id sem);
  79. struct rtp_list_node {
  80. struct rtp_list_node *next;
  81. struct rtp_list_node *prev;
  82. };
  83. typedef struct rtp_list_node rtp_list_t;
  84. RTP_INLINE void rtp_list_init(rtp_list_t *l)
  85. {
  86. l->next = l->prev = l;
  87. }
  88. RTP_INLINE void rtp_list_insert_after(rtp_list_t *l, rtp_list_t *n)
  89. {
  90. l->next->prev = n;
  91. n->next = l->next;
  92. l->next = n;
  93. n->prev = l;
  94. }
  95. RTP_INLINE void rtp_list_insert_before(rtp_list_t *l, rtp_list_t *n)
  96. {
  97. l->prev->next = n;
  98. n->prev = l->prev;
  99. l->prev = n;
  100. n->next = l;
  101. }
  102. RTP_INLINE void rtp_list_remove(rtp_list_t *n)
  103. {
  104. n->next->prev = n->prev;
  105. n->prev->next = n->next;
  106. n->next = n->prev = n;
  107. }
  108. RTP_INLINE int rtp_list_is_empty(const rtp_list_t *l)
  109. {
  110. return l->next == l;
  111. }
  112. RTP_INLINE int rtp_list_len(const rtp_list_t *l)
  113. {
  114. int len = 0;
  115. const rtp_list_t *p = l;
  116. while (p->next != l) {
  117. p = p->next;
  118. len ++;
  119. }
  120. return len;
  121. }
  122. #define rtp_container_of(ptr, type, member) \
  123. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  124. #define rtp_list_obj_init(obj) {&(obj), &(obj)}
  125. #define rtp_list_entry(node, type, member) \
  126. rtp_container_of(node, type, member)
  127. #define rtp_list_for_each(pos, head) \
  128. for (pos = (head)->next; pos != (head); pos = pos->next)
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif