this hurts
Dependencies: FFT
mbed/drivers/FileHandle.h@0:d6c9b09b4042, 2020-12-02 (annotated)
- Committer:
- annieluo2
- Date:
- Wed Dec 02 18:02:03 2020 +0000
- Revision:
- 0:d6c9b09b4042
boo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
annieluo2 | 0:d6c9b09b4042 | 1 | /* mbed Microcontroller Library |
annieluo2 | 0:d6c9b09b4042 | 2 | * Copyright (c) 2006-2013 ARM Limited |
annieluo2 | 0:d6c9b09b4042 | 3 | * |
annieluo2 | 0:d6c9b09b4042 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
annieluo2 | 0:d6c9b09b4042 | 5 | * you may not use this file except in compliance with the License. |
annieluo2 | 0:d6c9b09b4042 | 6 | * You may obtain a copy of the License at |
annieluo2 | 0:d6c9b09b4042 | 7 | * |
annieluo2 | 0:d6c9b09b4042 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
annieluo2 | 0:d6c9b09b4042 | 9 | * |
annieluo2 | 0:d6c9b09b4042 | 10 | * Unless required by applicable law or agreed to in writing, software |
annieluo2 | 0:d6c9b09b4042 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
annieluo2 | 0:d6c9b09b4042 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
annieluo2 | 0:d6c9b09b4042 | 13 | * See the License for the specific language governing permissions and |
annieluo2 | 0:d6c9b09b4042 | 14 | * limitations under the License. |
annieluo2 | 0:d6c9b09b4042 | 15 | */ |
annieluo2 | 0:d6c9b09b4042 | 16 | #ifndef MBED_FILEHANDLE_H |
annieluo2 | 0:d6c9b09b4042 | 17 | #define MBED_FILEHANDLE_H |
annieluo2 | 0:d6c9b09b4042 | 18 | |
annieluo2 | 0:d6c9b09b4042 | 19 | typedef int FILEHANDLE; |
annieluo2 | 0:d6c9b09b4042 | 20 | |
annieluo2 | 0:d6c9b09b4042 | 21 | #include <stdio.h> |
annieluo2 | 0:d6c9b09b4042 | 22 | |
annieluo2 | 0:d6c9b09b4042 | 23 | #if defined(__ARMCC_VERSION) || defined(__ICCARM__) |
annieluo2 | 0:d6c9b09b4042 | 24 | typedef int ssize_t; |
annieluo2 | 0:d6c9b09b4042 | 25 | typedef long off_t; |
annieluo2 | 0:d6c9b09b4042 | 26 | |
annieluo2 | 0:d6c9b09b4042 | 27 | #else |
annieluo2 | 0:d6c9b09b4042 | 28 | # include <sys/types.h> |
annieluo2 | 0:d6c9b09b4042 | 29 | #endif |
annieluo2 | 0:d6c9b09b4042 | 30 | |
annieluo2 | 0:d6c9b09b4042 | 31 | namespace mbed { |
annieluo2 | 0:d6c9b09b4042 | 32 | /** \addtogroup drivers */ |
annieluo2 | 0:d6c9b09b4042 | 33 | /** @{*/ |
annieluo2 | 0:d6c9b09b4042 | 34 | |
annieluo2 | 0:d6c9b09b4042 | 35 | /** An OO equivalent of the internal FILEHANDLE variable |
annieluo2 | 0:d6c9b09b4042 | 36 | * and associated _sys_* functions. |
annieluo2 | 0:d6c9b09b4042 | 37 | * |
annieluo2 | 0:d6c9b09b4042 | 38 | * FileHandle is an abstract class, needing at least sys_write and |
annieluo2 | 0:d6c9b09b4042 | 39 | * sys_read to be implmented for a simple interactive device. |
annieluo2 | 0:d6c9b09b4042 | 40 | * |
annieluo2 | 0:d6c9b09b4042 | 41 | * No one ever directly tals to/instanciates a FileHandle - it gets |
annieluo2 | 0:d6c9b09b4042 | 42 | * created by FileSystem, and wrapped up by stdio. |
annieluo2 | 0:d6c9b09b4042 | 43 | * |
annieluo2 | 0:d6c9b09b4042 | 44 | * @Note Synchronization level: Set by subclass |
annieluo2 | 0:d6c9b09b4042 | 45 | */ |
annieluo2 | 0:d6c9b09b4042 | 46 | class FileHandle { |
annieluo2 | 0:d6c9b09b4042 | 47 | |
annieluo2 | 0:d6c9b09b4042 | 48 | public: |
annieluo2 | 0:d6c9b09b4042 | 49 | /** Write the contents of a buffer to the file |
annieluo2 | 0:d6c9b09b4042 | 50 | * |
annieluo2 | 0:d6c9b09b4042 | 51 | * @param buffer the buffer to write from |
annieluo2 | 0:d6c9b09b4042 | 52 | * @param length the number of characters to write |
annieluo2 | 0:d6c9b09b4042 | 53 | * |
annieluo2 | 0:d6c9b09b4042 | 54 | * @returns |
annieluo2 | 0:d6c9b09b4042 | 55 | * The number of characters written (possibly 0) on success, -1 on error. |
annieluo2 | 0:d6c9b09b4042 | 56 | */ |
annieluo2 | 0:d6c9b09b4042 | 57 | virtual ssize_t write(const void* buffer, size_t length) = 0; |
annieluo2 | 0:d6c9b09b4042 | 58 | |
annieluo2 | 0:d6c9b09b4042 | 59 | /** Close the file |
annieluo2 | 0:d6c9b09b4042 | 60 | * |
annieluo2 | 0:d6c9b09b4042 | 61 | * @returns |
annieluo2 | 0:d6c9b09b4042 | 62 | * Zero on success, -1 on error. |
annieluo2 | 0:d6c9b09b4042 | 63 | */ |
annieluo2 | 0:d6c9b09b4042 | 64 | virtual int close() = 0; |
annieluo2 | 0:d6c9b09b4042 | 65 | |
annieluo2 | 0:d6c9b09b4042 | 66 | /** Function read |
annieluo2 | 0:d6c9b09b4042 | 67 | * Reads the contents of the file into a buffer |
annieluo2 | 0:d6c9b09b4042 | 68 | * |
annieluo2 | 0:d6c9b09b4042 | 69 | * @param buffer the buffer to read in to |
annieluo2 | 0:d6c9b09b4042 | 70 | * @param length the number of characters to read |
annieluo2 | 0:d6c9b09b4042 | 71 | * |
annieluo2 | 0:d6c9b09b4042 | 72 | * @returns |
annieluo2 | 0:d6c9b09b4042 | 73 | * The number of characters read (zero at end of file) on success, -1 on error. |
annieluo2 | 0:d6c9b09b4042 | 74 | */ |
annieluo2 | 0:d6c9b09b4042 | 75 | virtual ssize_t read(void* buffer, size_t length) = 0; |
annieluo2 | 0:d6c9b09b4042 | 76 | |
annieluo2 | 0:d6c9b09b4042 | 77 | /** Check if the handle is for a interactive terminal device. |
annieluo2 | 0:d6c9b09b4042 | 78 | * If so, line buffered behaviour is used by default |
annieluo2 | 0:d6c9b09b4042 | 79 | * |
annieluo2 | 0:d6c9b09b4042 | 80 | * @returns |
annieluo2 | 0:d6c9b09b4042 | 81 | * 1 if it is a terminal, |
annieluo2 | 0:d6c9b09b4042 | 82 | * 0 otherwise |
annieluo2 | 0:d6c9b09b4042 | 83 | */ |
annieluo2 | 0:d6c9b09b4042 | 84 | virtual int isatty() = 0; |
annieluo2 | 0:d6c9b09b4042 | 85 | |
annieluo2 | 0:d6c9b09b4042 | 86 | /** Move the file position to a given offset from a given location. |
annieluo2 | 0:d6c9b09b4042 | 87 | * |
annieluo2 | 0:d6c9b09b4042 | 88 | * @param offset The offset from whence to move to |
annieluo2 | 0:d6c9b09b4042 | 89 | * @param whence SEEK_SET for the start of the file, SEEK_CUR for the |
annieluo2 | 0:d6c9b09b4042 | 90 | * current file position, or SEEK_END for the end of the file. |
annieluo2 | 0:d6c9b09b4042 | 91 | * |
annieluo2 | 0:d6c9b09b4042 | 92 | * @returns |
annieluo2 | 0:d6c9b09b4042 | 93 | * new file position on success, |
annieluo2 | 0:d6c9b09b4042 | 94 | * -1 on failure or unsupported |
annieluo2 | 0:d6c9b09b4042 | 95 | */ |
annieluo2 | 0:d6c9b09b4042 | 96 | virtual off_t lseek(off_t offset, int whence) = 0; |
annieluo2 | 0:d6c9b09b4042 | 97 | |
annieluo2 | 0:d6c9b09b4042 | 98 | /** Flush any buffers associated with the FileHandle, ensuring it |
annieluo2 | 0:d6c9b09b4042 | 99 | * is up to date on disk |
annieluo2 | 0:d6c9b09b4042 | 100 | * |
annieluo2 | 0:d6c9b09b4042 | 101 | * @returns |
annieluo2 | 0:d6c9b09b4042 | 102 | * 0 on success or un-needed, |
annieluo2 | 0:d6c9b09b4042 | 103 | * -1 on error |
annieluo2 | 0:d6c9b09b4042 | 104 | */ |
annieluo2 | 0:d6c9b09b4042 | 105 | virtual int fsync() = 0; |
annieluo2 | 0:d6c9b09b4042 | 106 | |
annieluo2 | 0:d6c9b09b4042 | 107 | virtual off_t flen() { |
annieluo2 | 0:d6c9b09b4042 | 108 | lock(); |
annieluo2 | 0:d6c9b09b4042 | 109 | /* remember our current position */ |
annieluo2 | 0:d6c9b09b4042 | 110 | off_t pos = lseek(0, SEEK_CUR); |
annieluo2 | 0:d6c9b09b4042 | 111 | if(pos == -1) { |
annieluo2 | 0:d6c9b09b4042 | 112 | unlock(); |
annieluo2 | 0:d6c9b09b4042 | 113 | return -1; |
annieluo2 | 0:d6c9b09b4042 | 114 | } |
annieluo2 | 0:d6c9b09b4042 | 115 | /* seek to the end to get the file length */ |
annieluo2 | 0:d6c9b09b4042 | 116 | off_t res = lseek(0, SEEK_END); |
annieluo2 | 0:d6c9b09b4042 | 117 | /* return to our old position */ |
annieluo2 | 0:d6c9b09b4042 | 118 | lseek(pos, SEEK_SET); |
annieluo2 | 0:d6c9b09b4042 | 119 | unlock(); |
annieluo2 | 0:d6c9b09b4042 | 120 | return res; |
annieluo2 | 0:d6c9b09b4042 | 121 | } |
annieluo2 | 0:d6c9b09b4042 | 122 | |
annieluo2 | 0:d6c9b09b4042 | 123 | virtual ~FileHandle(); |
annieluo2 | 0:d6c9b09b4042 | 124 | |
annieluo2 | 0:d6c9b09b4042 | 125 | protected: |
annieluo2 | 0:d6c9b09b4042 | 126 | |
annieluo2 | 0:d6c9b09b4042 | 127 | /** Acquire exclusive access to this object. |
annieluo2 | 0:d6c9b09b4042 | 128 | */ |
annieluo2 | 0:d6c9b09b4042 | 129 | virtual void lock() { |
annieluo2 | 0:d6c9b09b4042 | 130 | // Stub |
annieluo2 | 0:d6c9b09b4042 | 131 | } |
annieluo2 | 0:d6c9b09b4042 | 132 | |
annieluo2 | 0:d6c9b09b4042 | 133 | /** Release exclusive access to this object. |
annieluo2 | 0:d6c9b09b4042 | 134 | */ |
annieluo2 | 0:d6c9b09b4042 | 135 | virtual void unlock() { |
annieluo2 | 0:d6c9b09b4042 | 136 | // Stub |
annieluo2 | 0:d6c9b09b4042 | 137 | } |
annieluo2 | 0:d6c9b09b4042 | 138 | }; |
annieluo2 | 0:d6c9b09b4042 | 139 | |
annieluo2 | 0:d6c9b09b4042 | 140 | } // namespace mbed |
annieluo2 | 0:d6c9b09b4042 | 141 | |
annieluo2 | 0:d6c9b09b4042 | 142 | #endif |
annieluo2 | 0:d6c9b09b4042 | 143 | |
annieluo2 | 0:d6c9b09b4042 | 144 | /** @}*/ |