mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

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