hal_streams.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_streams.h
  15. * @brief Data streams.
  16. * @details This header defines abstract interfaces useful to access generic
  17. * data streams in a standardized way.
  18. *
  19. * @addtogroup HAL_STREAMS
  20. * @details This module define an abstract interface for generic data streams.
  21. * Note that no code is present, just abstract interfaces-like
  22. * structures, you should look at the system as to a set of
  23. * abstract C++ classes (even if written in C). This system
  24. * has then advantage to make the access to data streams
  25. * independent from the implementation logic.<br>
  26. * The stream interface can be used as base class for high level
  27. * object types such as files, sockets, serial ports, pipes etc.
  28. * @{
  29. */
  30. #ifndef HAL_STREAMS_H
  31. #define HAL_STREAMS_H
  32. /**
  33. * @name Streams return codes
  34. * @{
  35. */
  36. #define STM_OK MSG_OK
  37. #define STM_TIMEOUT MSG_TIMEOUT
  38. #define STM_RESET MSG_RESET
  39. /** @} */
  40. /**
  41. * @brief BaseSequentialStream specific methods.
  42. */
  43. #define _base_sequential_stream_methods \
  44. _base_object_methods \
  45. /* Stream write buffer method.*/ \
  46. size_t (*write)(void *instance, const uint8_t *bp, size_t n); \
  47. /* Stream read buffer method.*/ \
  48. size_t (*read)(void *instance, uint8_t *bp, size_t n); \
  49. /* Channel put method, blocking.*/ \
  50. msg_t (*put)(void *instance, uint8_t b); \
  51. /* Channel get method, blocking.*/ \
  52. msg_t (*get)(void *instance); \
  53. /**
  54. * @brief @p BaseSequentialStream specific data.
  55. * @note It is empty because @p BaseSequentialStream is only an interface
  56. * without implementation.
  57. */
  58. #define _base_sequential_stream_data \
  59. _base_object_data
  60. /**
  61. * @brief @p BaseSequentialStream virtual methods table.
  62. */
  63. struct BaseSequentialStreamVMT {
  64. _base_sequential_stream_methods
  65. };
  66. /**
  67. * @extends BaseObject
  68. *
  69. * @brief Base stream class.
  70. * @details This class represents a generic blocking unbuffered sequential
  71. * data stream.
  72. */
  73. typedef struct {
  74. /** @brief Virtual Methods Table.*/
  75. const struct BaseSequentialStreamVMT *vmt;
  76. _base_sequential_stream_data
  77. } BaseSequentialStream;
  78. /**
  79. * @name Macro Functions (BaseSequentialStream)
  80. * @{
  81. */
  82. /**
  83. * @brief Sequential Stream write.
  84. * @details The function writes data from a buffer to a stream.
  85. *
  86. * @param[in] ip pointer to a @p BaseSequentialStream or derived class
  87. * @param[in] bp pointer to the data buffer
  88. * @param[in] n the maximum amount of data to be transferred
  89. * @return The number of bytes transferred. The return value can
  90. * be less than the specified number of bytes if an
  91. * end-of-file condition has been met.
  92. *
  93. * @api
  94. */
  95. #define streamWrite(ip, bp, n) ((ip)->vmt->write(ip, bp, n))
  96. /**
  97. * @brief Sequential Stream read.
  98. * @details The function reads data from a stream into a buffer.
  99. *
  100. * @param[in] ip pointer to a @p BaseSequentialStream or derived class
  101. * @param[out] bp pointer to the data buffer
  102. * @param[in] n the maximum amount of data to be transferred
  103. * @return The number of bytes transferred. The return value can
  104. * be less than the specified number of bytes if an
  105. * end-of-file condition has been met.
  106. *
  107. * @api
  108. */
  109. #define streamRead(ip, bp, n) ((ip)->vmt->read(ip, bp, n))
  110. /**
  111. * @brief Sequential Stream blocking byte write.
  112. * @details This function writes a byte value to a channel. If the channel
  113. * is not ready to accept data then the calling thread is suspended.
  114. *
  115. * @param[in] ip pointer to a @p BaseChannel or derived class
  116. * @param[in] b the byte value to be written to the channel
  117. *
  118. * @return The operation status.
  119. * @retval STM_OK if the operation succeeded.
  120. * @retval STM_RESET if an end-of-file condition has been met.
  121. *
  122. * @api
  123. */
  124. #define streamPut(ip, b) ((ip)->vmt->put(ip, b))
  125. /**
  126. * @brief Sequential Stream blocking byte read.
  127. * @details This function reads a byte value from a channel. If the data
  128. * is not available then the calling thread is suspended.
  129. *
  130. * @param[in] ip pointer to a @p BaseChannel or derived class
  131. *
  132. * @return A byte value from the queue.
  133. * @retval STM_RESET if an end-of-file condition has been met.
  134. *
  135. * @api
  136. */
  137. #define streamGet(ip) ((ip)->vmt->get(ip))
  138. /** @} */
  139. #endif /* HAL_STREAMS_H */
  140. /** @} */