Revised to support ability to have both SD and USB drives mounted.

Dependents:   Multi-FileSystem Multi-FileSystem

Fork of FATFileSystem by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FATFileHandle.cpp Source File

FATFileHandle.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #include "ff.h"
00023 #include "ffconf.h"
00024 #include "mbed_debug.h"
00025 
00026 #include "FATFileHandle.h"
00027 
00028 //#define DEBUG "FtFH"
00029 // ...
00030 // INFO("Stuff to show %d", var); // new-line is automatically appended
00031 //
00032 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
00033 #include "mbed.h"
00034 #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
00035 #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
00036 #define ERR(x, ...)  std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
00037 static void HexDump(const char * title, const uint8_t * p, int count)
00038 {
00039     int i;
00040     char buf[100] = "0000: ";
00041 
00042     if (*title)
00043         INFO("%s", title);
00044     for (i=0; i<count; ) {
00045         sprintf(buf + strlen(buf), "%02X ", *(p+i));
00046         if ((++i & 0x0F) == 0x00) {
00047             INFO("%s", buf);
00048             if (i < count)
00049                 sprintf(buf, "%04X: ", i);
00050             else
00051                 buf[0] = '\0';
00052         }
00053     }
00054     if (strlen(buf))
00055         INFO("%s", buf);
00056 }
00057 #else
00058 #define INFO(x, ...)
00059 #define WARN(x, ...)
00060 #define ERR(x, ...)
00061 #define HexDump(a, b, c)
00062 #endif
00063 
00064 FATFileHandle::FATFileHandle(FIL fh) {
00065     _fh = fh;
00066 }
00067 
00068 int FATFileHandle::close() {
00069     int retval = f_close(&_fh);
00070     delete this;
00071     return retval;
00072 }
00073 
00074 ssize_t FATFileHandle::write(const void* buffer, size_t length) {
00075     UINT n;
00076     FRESULT res = f_write(&_fh, buffer, length, &n);
00077     if (res) {
00078         INFO("f_write() failed: %d", res);
00079         return -1;
00080     }
00081     return n;
00082 }
00083 
00084 ssize_t FATFileHandle::read(void* buffer, size_t length) {
00085     INFO("read(%d)", length);
00086     UINT n;
00087     FRESULT res = f_read(&_fh, buffer, length, &n);
00088     if (res) {
00089         INFO("f_read() failed: %d", res);
00090         return -1;
00091     }
00092     return n;
00093 }
00094 
00095 int FATFileHandle::isatty() {
00096     return 0;
00097 }
00098 
00099 off_t FATFileHandle::lseek(off_t position, int whence) {
00100     if (whence == SEEK_END) {
00101         position += _fh.fsize;
00102     } else if(whence==SEEK_CUR) {
00103         position += _fh.fptr;
00104     }
00105     FRESULT res = f_lseek(&_fh, position);
00106     if (res) {
00107         INFO("lseek failed: %d", res);
00108         return -1;
00109     } else {
00110         INFO("lseek OK, returning %i", _fh.fptr);
00111         return _fh.fptr;
00112     }
00113 }
00114 
00115 int FATFileHandle::fsync() {
00116     FRESULT res = f_sync(&_fh);
00117     if (res) {
00118         INFO("f_sync() failed: %d", res);
00119         return -1;
00120     }
00121     return 0;
00122 }
00123 
00124 off_t FATFileHandle::flen() {
00125     return _fh.fsize;
00126 }
00127