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