inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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