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