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