mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Mon Feb 18 09:41:56 2013 +0000
Revision:
9:663789d7729f
Parent:
8:c14af7958ef5
Update mbed-KL25Z to latest build

Who changed what in which revision?

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