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