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