SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lharoon 0:22612ae617a0 1 /* mbed Microcontroller Library - FileHandler
lharoon 0:22612ae617a0 2 * Copyright (c) 2007-2009 ARM Limited. All rights reserved.
lharoon 0:22612ae617a0 3 */
lharoon 0:22612ae617a0 4
lharoon 0:22612ae617a0 5 #ifndef MBED_FILEHANDLE_H
lharoon 0:22612ae617a0 6 #define MBED_FILEHANDLE_H
lharoon 0:22612ae617a0 7
lharoon 0:22612ae617a0 8 typedef int FILEHANDLE;
lharoon 0:22612ae617a0 9
lharoon 0:22612ae617a0 10 #include <stdio.h>
lharoon 0:22612ae617a0 11 #ifdef __ARMCC_VERSION
lharoon 0:22612ae617a0 12 typedef int ssize_t;
lharoon 0:22612ae617a0 13 typedef long off_t;
lharoon 0:22612ae617a0 14 #else
lharoon 0:22612ae617a0 15 #include <sys/types.h>
lharoon 0:22612ae617a0 16 #endif
lharoon 0:22612ae617a0 17
lharoon 0:22612ae617a0 18 namespace mbed {
lharoon 0:22612ae617a0 19
lharoon 0:22612ae617a0 20 /* Class FileHandle
lharoon 0:22612ae617a0 21 * An OO equivalent of the internal FILEHANDLE variable
lharoon 0:22612ae617a0 22 * and associated _sys_* functions
lharoon 0:22612ae617a0 23 *
lharoon 0:22612ae617a0 24 * FileHandle is an abstract class, needing at least sys_write and
lharoon 0:22612ae617a0 25 * sys_read to be implmented for a simple interactive device
lharoon 0:22612ae617a0 26 *
lharoon 0:22612ae617a0 27 * No one ever directly tals to/instanciates a FileHandle - it gets
lharoon 0:22612ae617a0 28 * created by FileSystem, and wrapped up by stdio
lharoon 0:22612ae617a0 29 */
lharoon 0:22612ae617a0 30 class FileHandle {
lharoon 0:22612ae617a0 31
lharoon 0:22612ae617a0 32 public:
lharoon 0:22612ae617a0 33
lharoon 0:22612ae617a0 34 /* Function write
lharoon 0:22612ae617a0 35 * Write the contents of a buffer to the file
lharoon 0:22612ae617a0 36 *
lharoon 0:22612ae617a0 37 * Parameters
lharoon 0:22612ae617a0 38 * buffer - the buffer to write from
lharoon 0:22612ae617a0 39 * length - the number of characters to write
lharoon 0:22612ae617a0 40 *
lharoon 0:22612ae617a0 41 * Returns
lharoon 0:22612ae617a0 42 * The number of characters written (possibly 0) on success, -1 on error.
lharoon 0:22612ae617a0 43 */
lharoon 0:22612ae617a0 44 virtual ssize_t write(const void* buffer, size_t length) = 0;
lharoon 0:22612ae617a0 45
lharoon 0:22612ae617a0 46 /* Function close
lharoon 0:22612ae617a0 47 * Close the file
lharoon 0:22612ae617a0 48 *
lharoon 0:22612ae617a0 49 * Returns
lharoon 0:22612ae617a0 50 * Zero on success, -1 on error.
lharoon 0:22612ae617a0 51 */
lharoon 0:22612ae617a0 52 virtual int close() = 0;
lharoon 0:22612ae617a0 53
lharoon 0:22612ae617a0 54 /* Function read
lharoon 0:22612ae617a0 55 * Reads the contents of the file into a buffer
lharoon 0:22612ae617a0 56 *
lharoon 0:22612ae617a0 57 * Parameters
lharoon 0:22612ae617a0 58 * buffer - the buffer to read in to
lharoon 0:22612ae617a0 59 * length - the number of characters to read
lharoon 0:22612ae617a0 60 *
lharoon 0:22612ae617a0 61 * Returns
lharoon 0:22612ae617a0 62 * The number of characters read (zero at end of file) on success, -1 on error.
lharoon 0:22612ae617a0 63 */
lharoon 0:22612ae617a0 64 virtual ssize_t read(void* buffer, size_t length) = 0;
lharoon 0:22612ae617a0 65
lharoon 0:22612ae617a0 66 /* Function isatty
lharoon 0:22612ae617a0 67 * Check if the handle is for a interactive terminal device
lharoon 0:22612ae617a0 68 *
lharoon 0:22612ae617a0 69 * If so, line buffered behaviour is used by default
lharoon 0:22612ae617a0 70 *
lharoon 0:22612ae617a0 71 * Returns
lharoon 0:22612ae617a0 72 * 1 if it is a terminal, 0 otherwise
lharoon 0:22612ae617a0 73 */
lharoon 0:22612ae617a0 74 virtual int isatty() = 0 ;
lharoon 0:22612ae617a0 75
lharoon 0:22612ae617a0 76 /* Function lseek
lharoon 0:22612ae617a0 77 * Move the file position to a given offset from a given location.
lharoon 0:22612ae617a0 78 *
lharoon 0:22612ae617a0 79 * Parameters
lharoon 0:22612ae617a0 80 * offset - The offset from whence to move to
lharoon 0:22612ae617a0 81 * whence - SEEK_SET for the start of the file, SEEK_CUR for the
lharoon 0:22612ae617a0 82 * current file position, or SEEK_END for the end of the file.
lharoon 0:22612ae617a0 83 *
lharoon 0:22612ae617a0 84 * Returns
lharoon 0:22612ae617a0 85 * New file position on success, -1 on failure or unsupported
lharoon 0:22612ae617a0 86 */
lharoon 0:22612ae617a0 87 virtual off_t lseek(off_t offset, int whence) = 0;
lharoon 0:22612ae617a0 88
lharoon 0:22612ae617a0 89 /* Function fsync
lharoon 0:22612ae617a0 90 * Flush any buffers associated with the FileHandle, ensuring it
lharoon 0:22612ae617a0 91 * is up to date on disk
lharoon 0:22612ae617a0 92 *
lharoon 0:22612ae617a0 93 * Returns
lharoon 0:22612ae617a0 94 * 0 on success or un-needed, -1 on error
lharoon 0:22612ae617a0 95 */
lharoon 0:22612ae617a0 96 virtual int fsync() = 0;
lharoon 0:22612ae617a0 97
lharoon 0:22612ae617a0 98 virtual off_t flen() {
lharoon 0:22612ae617a0 99 /* remember our current position */
lharoon 0:22612ae617a0 100 off_t pos = lseek(0, SEEK_CUR);
lharoon 0:22612ae617a0 101 if(pos == -1) return -1;
lharoon 0:22612ae617a0 102 /* seek to the end to get the file length */
lharoon 0:22612ae617a0 103 off_t res = lseek(0, SEEK_END);
lharoon 0:22612ae617a0 104 /* return to our old position */
lharoon 0:22612ae617a0 105 lseek(pos, SEEK_SET);
lharoon 0:22612ae617a0 106 return res;
lharoon 0:22612ae617a0 107 }
lharoon 0:22612ae617a0 108
lharoon 0:22612ae617a0 109 };
lharoon 0:22612ae617a0 110
lharoon 0:22612ae617a0 111 } // namespace mbed
lharoon 0:22612ae617a0 112
lharoon 0:22612ae617a0 113 #endif
lharoon 0:22612ae617a0 114