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