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