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