00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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