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