mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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