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