FileHandle.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2017 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_FILEHANDLE_H
  17. #define MBED_FILEHANDLE_H
  18. typedef int FILEHANDLE;
  19. #include <cstdio>
  20. #include "Callback.h"
  21. #include "platform/mbed_poll.h"
  22. #include "platform/platform.h"
  23. #include "platform/NonCopyable.h"
  24. namespace mbed {
  25. /** \addtogroup platform */
  26. /** @{*/
  27. /**
  28. * \defgroup platform_FileHandle FileHandle functions
  29. * @{
  30. */
  31. /** Class FileHandle
  32. *
  33. * An abstract interface that represents operations on a file-like
  34. * object. The core functions are read, write, and seek, but only
  35. * a subset of these operations can be provided.
  36. *
  37. * @note to create a file, @see File
  38. * @note Synchronization level: Set by subclass
  39. */
  40. class FileHandle : private NonCopyable<FileHandle> {
  41. public:
  42. virtual ~FileHandle() {}
  43. /** Read the contents of a file into a buffer
  44. *
  45. * Devices acting as FileHandles should follow POSIX semantics:
  46. *
  47. * * if no data is available, and non-blocking set return -EAGAIN
  48. * * if no data is available, and blocking set, wait until some data is available
  49. * * If any data is available, call returns immediately
  50. *
  51. * @param buffer The buffer to read in to
  52. * @param size The number of bytes to read
  53. * @return The number of bytes read, 0 at end of file, negative error on failure
  54. */
  55. virtual ssize_t read(void *buffer, size_t size) = 0;
  56. /** Write the contents of a buffer to a file
  57. *
  58. * Devices acting as FileHandles should follow POSIX semantics:
  59. *
  60. * * if blocking, block until all data is written
  61. * * if no data can be written, and non-blocking set, return -EAGAIN
  62. * * if some data can be written, and non-blocking set, write partial
  63. *
  64. * @param buffer The buffer to write from
  65. * @param size The number of bytes to write
  66. * @return The number of bytes written, negative error on failure
  67. */
  68. virtual ssize_t write(const void *buffer, size_t size) = 0;
  69. /** Move the file position to a given offset from from a given location
  70. *
  71. * @param offset The offset from whence to move to
  72. * @param whence The start of where to seek
  73. * SEEK_SET to start from beginning of file,
  74. * SEEK_CUR to start from current position in file,
  75. * SEEK_END to start from end of file
  76. * @return The new offset of the file, negative error code on failure
  77. */
  78. virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0;
  79. /** Close a file
  80. *
  81. * @return 0 on success, negative error code on failure
  82. */
  83. virtual int close() = 0;
  84. /** Flush any buffers associated with the file
  85. *
  86. * @return 0 on success, negative error code on failure
  87. */
  88. virtual int sync()
  89. {
  90. return 0;
  91. }
  92. /** Check if the file in an interactive terminal device
  93. *
  94. * @return True if the file is a terminal
  95. * @return False if the file is not a terminal
  96. * @return Negative error code on failure
  97. */
  98. virtual int isatty()
  99. {
  100. return false;
  101. }
  102. /** Get the file position of the file
  103. *
  104. * @note This is equivalent to seek(0, SEEK_CUR)
  105. *
  106. * @return The current offset in the file, negative error code on failure
  107. */
  108. virtual off_t tell()
  109. {
  110. return seek(0, SEEK_CUR);
  111. }
  112. /** Rewind the file position to the beginning of the file
  113. *
  114. * @note This is equivalent to seek(0, SEEK_SET)
  115. */
  116. virtual void rewind()
  117. {
  118. seek(0, SEEK_SET);
  119. }
  120. /** Get the size of the file
  121. *
  122. * @return Size of the file in bytes
  123. */
  124. virtual off_t size();
  125. /** Move the file position to a given offset from a given location.
  126. *
  127. * @param offset The offset from whence to move to
  128. * @param whence SEEK_SET for the start of the file, SEEK_CUR for the
  129. * current file position, or SEEK_END for the end of the file.
  130. *
  131. * @returns
  132. * new file position on success,
  133. * -1 on failure or unsupported
  134. * @deprecated Replaced by `off_t FileHandle::seek(off_t offset, int whence = SEEK_SET)'
  135. *
  136. */
  137. MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek")
  138. virtual off_t lseek(off_t offset, int whence)
  139. {
  140. return seek(offset, whence);
  141. }
  142. /** Flush any buffers associated with the FileHandle, ensuring it
  143. * is up to date on disk
  144. *
  145. * @returns
  146. * 0 on success or un-needed,
  147. * -1 on error
  148. * @deprecated Replaced by `int FileHandle::sync()'
  149. */
  150. MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync")
  151. virtual int fsync()
  152. {
  153. return sync();
  154. }
  155. /** Find the length of the file
  156. *
  157. * @returns
  158. * Length of the file
  159. * @deprecated Replaced by `off_t FileHandle::size()'
  160. */
  161. MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size")
  162. virtual off_t flen()
  163. {
  164. return size();
  165. }
  166. /** Set blocking or non-blocking mode of the file operation like read/write.
  167. * Definition depends upon the subclass implementing FileHandle.
  168. * The default is blocking.
  169. *
  170. * @param blocking true for blocking mode, false for non-blocking mode.
  171. *
  172. * @return 0 on success
  173. * @return Negative error code on failure
  174. */
  175. virtual int set_blocking(bool blocking)
  176. {
  177. return blocking ? 0 : -ENOTTY;
  178. }
  179. /** Check current blocking or non-blocking mode for file operations.
  180. *
  181. * @return true for blocking mode, false for non-blocking mode.
  182. */
  183. virtual bool is_blocking() const
  184. {
  185. return true;
  186. }
  187. /** Check for poll event flags
  188. * The input parameter can be used or ignored - the could always return all events,
  189. * or could check just the events listed in events.
  190. * Call is non-blocking - returns instantaneous state of events.
  191. * Whenever an event occurs, the derived class should call the sigio() callback).
  192. *
  193. * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
  194. *
  195. * @returns bitmask of poll events that have occurred.
  196. */
  197. virtual short poll(short events) const
  198. {
  199. // Possible default for real files
  200. return POLLIN | POLLOUT;
  201. }
  202. /** Definition depends upon the subclass implementing FileHandle.
  203. * For example, if the FileHandle is of type Stream, writable() could return
  204. * true when there is ample buffer space available for write() calls.
  205. *
  206. * @returns true if the FileHandle is writable.
  207. */
  208. bool writable() const
  209. {
  210. return poll(POLLOUT) & POLLOUT;
  211. }
  212. /** Definition depends upon the subclass implementing FileHandle.
  213. * For example, if the FileHandle is of type Stream, readable() could return
  214. * true when there is something available to read.
  215. *
  216. * @returns true when there is something available to read.
  217. */
  218. bool readable() const
  219. {
  220. return poll(POLLIN) & POLLIN;
  221. }
  222. /** Register a callback on state change of the file.
  223. *
  224. * The specified callback will be called on state changes such as when
  225. * the file can be written to or read from.
  226. *
  227. * The callback may be called in an interrupt context and should not
  228. * perform expensive operations.
  229. *
  230. * Note! This is not intended as an attach-like asynchronous api, but rather
  231. * as a building block for constructing such functionality.
  232. *
  233. * The exact timing of when the registered function
  234. * is called is not guaranteed and susceptible to change. It should be used
  235. * as a cue to make read/write/poll calls to find the current state.
  236. *
  237. * @param func Function to call on state change
  238. */
  239. virtual void sigio(Callback<void()> func)
  240. {
  241. //Default for real files. Do nothing for real files.
  242. }
  243. };
  244. /**@}*/
  245. /**@}*/
  246. } // namespace mbed
  247. #endif