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