temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

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