mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Nov 23 11:57:25 2017 +0000
Revision:
178:79309dc6340a
Parent:
168:9672193075cf
mbed-dev library. Release version 156

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