mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
Anna Bridge
Date:
Wed Jan 17 15:23:54 2018 +0000
Revision:
180:96ed750bd169
Parent:
178:79309dc6340a
Child:
184:08ed48f1de7f
mbed-dev libray. Release version 158

Who changed what in which revision?

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