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