hal_files.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_files.h
  15. * @brief Data files.
  16. * @details This header defines abstract interfaces useful to access generic
  17. * data files in a standardized way.
  18. *
  19. * @addtogroup HAL_FILES
  20. * @details This module define an abstract interface for generic data files by
  21. * extending the @p BaseSequentialStream interface. Note that no code
  22. * is present, data files are just abstract interface-like structures,
  23. * you should look at the systems as to a set of abstract C++ classes
  24. * (even if written in C). This system has the advantage to make the
  25. * access to streams independent from the implementation logic.<br>
  26. * The data files interface can be used as base class for high level
  27. * object types such as an API for a File System implementation.
  28. * @{
  29. */
  30. #ifndef HAL_FILES_H
  31. #define HAL_FILES_H
  32. /**
  33. * @name Files return codes
  34. * @{
  35. */
  36. /**
  37. * @brief No error return code.
  38. */
  39. #define FILE_OK STM_OK
  40. /**
  41. * @brief Error code from the file stream methods.
  42. */
  43. #define FILE_ERROR STM_TIMEOUT
  44. /**
  45. * @brief End-of-file condition for file get/put methods.
  46. */
  47. #define FILE_EOF STM_RESET
  48. /** @} */
  49. /**
  50. * @brief File offset type.
  51. */
  52. typedef uint32_t fileoffset_t;
  53. /**
  54. * @brief FileStream specific methods.
  55. */
  56. #define _file_stream_methods \
  57. _base_sequential_stream_methods \
  58. /* File close method.*/ \
  59. msg_t (*close)(void *instance); \
  60. /* Get last error code method.*/ \
  61. msg_t (*geterror)(void *instance); \
  62. /* File get size method.*/ \
  63. msg_t (*getsize)(void *instance, fileoffset_t *offset); \
  64. /* File get current position method.*/ \
  65. msg_t (*getposition)(void *instance, fileoffset_t *offset); \
  66. /* File set current position method.*/ \
  67. msg_t (*setposition)(void *instance, fileoffset_t offset);
  68. /**
  69. * @brief @p FileStream specific data.
  70. * @note It is empty because @p FileStream is only an interface
  71. * without implementation.
  72. */
  73. #define _file_stream_data \
  74. _base_sequential_stream_data
  75. /**
  76. * @extends BaseSequentialStreamVMT
  77. *
  78. * @brief @p FileStream virtual methods table.
  79. */
  80. struct FileStreamVMT {
  81. _file_stream_methods
  82. };
  83. /**
  84. * @extends BaseSequentialStream
  85. *
  86. * @brief Base file stream class.
  87. * @details This class represents a generic file data stream.
  88. */
  89. typedef struct {
  90. /** @brief Virtual Methods Table.*/
  91. const struct FileStreamVMT *vmt;
  92. _file_stream_data
  93. } FileStream;
  94. /**
  95. * @name Macro Functions (FileStream)
  96. * @{
  97. */
  98. /**
  99. * @brief File stream write.
  100. * @details The function writes data from a buffer to a file stream.
  101. *
  102. * @param[in] ip pointer to a @p FileStream or derived class
  103. * @param[in] bp pointer to the data buffer
  104. * @param[in] n the maximum amount of data to be transferred
  105. * @return The number of bytes transferred. The return value can
  106. * be less than the specified number of bytes if an
  107. * end-of-file condition has been met.
  108. * @retval FILE_ERROR operation failed.
  109. *
  110. * @api
  111. */
  112. #define fileStreamWrite(ip, bp, n) streamWrite(ip, bp, n)
  113. /**
  114. * @brief File stream read.
  115. * @details The function reads data from a file stream into a buffer.
  116. *
  117. * @param[in] ip pointer to a @p FileStream or derived class
  118. * @param[out] bp pointer to the data buffer
  119. * @param[in] n the maximum amount of data to be transferred
  120. * @return The number of bytes transferred. The return value can
  121. * be less than the specified number of bytes if an
  122. * end-of-file condition has been met.
  123. * @retval FILE_ERROR operation failed.
  124. *
  125. * @api
  126. */
  127. #define fileStreamRead(ip, bp, n) streamRead(ip, bp, n)
  128. /**
  129. * @brief File stream blocking byte write.
  130. * @details This function writes a byte value to a channel. If the channel
  131. * is not ready to accept data then the calling thread is suspended.
  132. *
  133. * @param[in] ip pointer to a @p FileStream or derived class
  134. * @param[in] b the byte value to be written to the channel
  135. *
  136. * @return The operation status.
  137. * @retval FILE_OK if the operation succeeded.
  138. * @retval FILE_ERROR operation failed.
  139. * @retval FILE_EOF if an end-of-file condition has been met.
  140. *
  141. * @api
  142. */
  143. #define fileStreamPut(ip, b) streamPut(ip, b)
  144. /**
  145. * @brief File stream blocking byte read.
  146. * @details This function reads a byte value from a channel. If the data
  147. * is not available then the calling thread is suspended.
  148. *
  149. * @param[in] ip pointer to a @p FileStream or derived class
  150. *
  151. * @return A byte value from the queue.
  152. * @retval FILE_ERROR operation failed.
  153. * @retval FILE_EOF if an end-of-file condition has been met.
  154. *
  155. * @api
  156. */
  157. #define fileStreamGet(ip) streamGet(ip)
  158. /**
  159. * @brief File Stream close.
  160. * @details The function closes a file stream.
  161. *
  162. * @param[in] ip pointer to a @p FileStream or derived class
  163. * @return The operation status.
  164. * @retval FILE_OK no error.
  165. * @retval FILE_ERROR operation failed.
  166. *
  167. * @api
  168. */
  169. #define fileStreamClose(ip) ((ip)->vmt->close(ip))
  170. /**
  171. * @brief Returns an implementation dependent error code.
  172. * @pre The previously called function must have returned @p FILE_ERROR.
  173. *
  174. * @param[in] ip pointer to a @p FileStream or derived class
  175. * @return Implementation dependent error code.
  176. *
  177. * @api
  178. */
  179. #define fileStreamGetError(ip) ((ip)->vmt->geterror(ip))
  180. /**
  181. * @brief Returns the current file size.
  182. *
  183. * @param[in] ip pointer to a @p FileStream or derived class
  184. * @param[out] offset current size of the file
  185. * @return The file size.
  186. * @retval FILE_ERROR operation failed.
  187. *
  188. * @api
  189. */
  190. #define fileStreamGetSize(ip, offset) ((ip)->vmt->getsize(ip), offset)
  191. /**
  192. * @brief Returns the current file pointer position.
  193. *
  194. * @param[in] ip pointer to a @p FileStream or derived class
  195. * @param[out] offset current position in the file
  196. * @return The current position inside the file.
  197. * @retval FILE_ERROR operation failed.
  198. *
  199. * @api
  200. */
  201. #define fileStreamGetPosition(ip, offset) ((ip)->vmt->getposition(ip, offset))
  202. /**
  203. * @brief Moves the file current pointer to an absolute position.
  204. *
  205. * @param[in] ip pointer to a @p FileStream or derived class
  206. * @param[in] offset new absolute position
  207. * @return The operation status.
  208. * @retval FILE_OK no error.
  209. * @retval FILE_ERROR operation failed.
  210. *
  211. * @api
  212. */
  213. #define fileStreamSetPosition(ip, offset) ((ip)->vmt->setposition(ip, offset))
  214. /** @} */
  215. #endif /* HAL_FILES_H */
  216. /** @} */