The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Jun 21 17:31:38 2017 +0100
Revision:
145:64910690c574
Child:
146:22da6e220af6
Release 145 of the mbed library.

Who changed what in which revision?

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