.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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