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