Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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