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