chpipes.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
  3. This file is part of ChibiOS.
  4. ChibiOS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. ChibiOS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * @file chpipes.h
  17. * @brief Pipes macros and structures.
  18. *
  19. * @addtogroup oslib_pipes
  20. * @{
  21. */
  22. #ifndef CHPIPES_H
  23. #define CHPIPES_H
  24. #if (CH_CFG_USE_PIPES == TRUE) || defined(__DOXYGEN__)
  25. /*===========================================================================*/
  26. /* Module constants. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Module pre-compile time settings. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Derived constants and error checks. */
  33. /*===========================================================================*/
  34. /*===========================================================================*/
  35. /* Module data structures and types. */
  36. /*===========================================================================*/
  37. /**
  38. * @brief Structure representing a pipe object.
  39. */
  40. typedef struct {
  41. uint8_t *buffer; /**< @brief Pointer to the pipe
  42. buffer. */
  43. uint8_t *top; /**< @brief Pointer to the location
  44. after the buffer. */
  45. uint8_t *wrptr; /**< @brief Write pointer. */
  46. uint8_t *rdptr; /**< @brief Read pointer. */
  47. size_t cnt; /**< @brief Bytes in the pipe. */
  48. bool reset; /**< @brief True if in reset state. */
  49. thread_reference_t wtr; /**< @brief Waiting writer. */
  50. thread_reference_t rtr; /**< @brief Waiting reader. */
  51. #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
  52. mutex_t cmtx; /**< @brief Common access mutex. */
  53. mutex_t wmtx; /**< @brief Write access mutex. */
  54. mutex_t rmtx; /**< @brief Read access mutex. */
  55. #else
  56. semaphore_t csem; /**< @brief Common access semaphore.*/
  57. semaphore_t wsem; /**< @brief Write access semaphore. */
  58. semaphore_t rsem; /**< @brief Read access semaphore. */
  59. #endif
  60. } pipe_t;
  61. /*===========================================================================*/
  62. /* Module macros. */
  63. /*===========================================================================*/
  64. /**
  65. * @brief Data part of a static pipe initializer.
  66. * @details This macro should be used when statically initializing a
  67. * pipe that is part of a bigger structure.
  68. *
  69. * @param[in] name the name of the pipe variable
  70. * @param[in] buffer pointer to the pipe buffer array of @p uint8_t
  71. * @param[in] size number of @p uint8_t elements in the buffer array
  72. */
  73. #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
  74. #define _PIPE_DATA(name, buffer, size) { \
  75. (uint8_t *)(buffer), \
  76. (uint8_t *)(buffer) + size, \
  77. (uint8_t *)(buffer), \
  78. (uint8_t *)(buffer), \
  79. (size_t)0, \
  80. false, \
  81. NULL, \
  82. NULL, \
  83. _MUTEX_DATA(name.cmtx), \
  84. _MUTEX_DATA(name.wmtx), \
  85. _MUTEX_DATA(name.rmtx), \
  86. }
  87. #else /* CH_CFG_USE_MUTEXES == FALSE */
  88. #define _PIPE_DATA(name, buffer, size) { \
  89. (uint8_t *)(buffer), \
  90. (uint8_t *)(buffer) + size, \
  91. (uint8_t *)(buffer), \
  92. (uint8_t *)(buffer), \
  93. (size_t)0, \
  94. false, \
  95. NULL, \
  96. NULL, \
  97. _SEMAPHORE_DATA(name.csem, (cnt_t)1), \
  98. _SEMAPHORE_DATA(name.wsem, (cnt_t)1), \
  99. _SEMAPHORE_DATA(name.rsem, (cnt_t)1), \
  100. }
  101. #endif /* CH_CFG_USE_MUTEXES == FALSE */
  102. /**
  103. * @brief Static pipe initializer.
  104. * @details Statically initialized pipes require no explicit
  105. * initialization using @p chPipeObjectInit().
  106. *
  107. * @param[in] name the name of the pipe variable
  108. * @param[in] buffer pointer to the pipe buffer array of @p uint8_t
  109. * @param[in] size number of @p uint8_t elements in the buffer array
  110. */
  111. #define PIPE_DECL(name, buffer, size) \
  112. pipe_t name = _PIPE_DATA(name, buffer, size)
  113. /*===========================================================================*/
  114. /* External declarations. */
  115. /*===========================================================================*/
  116. #ifdef __cplusplus
  117. extern "C" {
  118. #endif
  119. void chPipeObjectInit(pipe_t *pp, uint8_t *buf, size_t n);
  120. void chPipeReset(pipe_t *pp);
  121. size_t chPipeWriteTimeout(pipe_t *pp, const uint8_t *bp,
  122. size_t n, sysinterval_t timeout);
  123. size_t chPipeReadTimeout(pipe_t *pp, uint8_t *bp,
  124. size_t n, sysinterval_t timeout);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. /*===========================================================================*/
  129. /* Module inline functions. */
  130. /*===========================================================================*/
  131. /**
  132. * @brief Returns the pipe buffer size as number of bytes.
  133. *
  134. * @param[in] pp the pointer to an initialized @p pipe_t object
  135. * @return The size of the pipe.
  136. *
  137. * @api
  138. */
  139. static inline size_t chPipeGetSize(const pipe_t *pp) {
  140. /*lint -save -e9033 [10.8] Perfectly safe pointers
  141. arithmetic.*/
  142. return (size_t)(pp->top - pp->buffer);
  143. /*lint -restore*/
  144. }
  145. /**
  146. * @brief Returns the number of used byte slots into a pipe.
  147. *
  148. * @param[in] pp the pointer to an initialized @p pipe_t object
  149. * @return The number of queued bytes.
  150. *
  151. * @api
  152. */
  153. static inline size_t chPipeGetUsedCount(const pipe_t *pp) {
  154. return pp->cnt;
  155. }
  156. /**
  157. * @brief Returns the number of free byte slots into a pipe.
  158. *
  159. * @param[in] pp the pointer to an initialized @p pipe_t object
  160. * @return The number of empty byte slots.
  161. *
  162. * @api
  163. */
  164. static inline size_t chPipeGetFreeCount(const pipe_t *pp) {
  165. return chPipeGetSize(pp) - chPipeGetUsedCount(pp);
  166. }
  167. /**
  168. * @brief Terminates the reset state.
  169. *
  170. * @param[in] pp the pointer to an initialized @p pipe_t object
  171. *
  172. * @api
  173. */
  174. static inline void chPipeResume(pipe_t *pp) {
  175. pp->reset = false;
  176. }
  177. #endif /* CH_CFG_USE_PIPES == TRUE */
  178. #endif /* CHPIPES_H */
  179. /** @} */