BA / Mbed OS BaBoRo1
Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

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