nkjnm
Dependencies: MAX44000 nexpaq_mdk
Fork of LED_Demo by
mbd_os/libraries/fs/fat/FATFileHandle.cpp@1:55a6170b404f, 2016-09-17 (annotated)
- Committer:
- nexpaq
- Date:
- Sat Sep 17 16:32:05 2016 +0000
- Revision:
- 1:55a6170b404f
checking in for sharing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | /* mbed Microcontroller Library |
nexpaq | 1:55a6170b404f | 2 | * Copyright (c) 2006-2012 ARM Limited |
nexpaq | 1:55a6170b404f | 3 | * |
nexpaq | 1:55a6170b404f | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
nexpaq | 1:55a6170b404f | 5 | * of this software and associated documentation files (the "Software"), to deal |
nexpaq | 1:55a6170b404f | 6 | * in the Software without restriction, including without limitation the rights |
nexpaq | 1:55a6170b404f | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
nexpaq | 1:55a6170b404f | 8 | * copies of the Software, and to permit persons to whom the Software is |
nexpaq | 1:55a6170b404f | 9 | * furnished to do so, subject to the following conditions: |
nexpaq | 1:55a6170b404f | 10 | * |
nexpaq | 1:55a6170b404f | 11 | * The above copyright notice and this permission notice shall be included in |
nexpaq | 1:55a6170b404f | 12 | * all copies or substantial portions of the Software. |
nexpaq | 1:55a6170b404f | 13 | * |
nexpaq | 1:55a6170b404f | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
nexpaq | 1:55a6170b404f | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
nexpaq | 1:55a6170b404f | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
nexpaq | 1:55a6170b404f | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
nexpaq | 1:55a6170b404f | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
nexpaq | 1:55a6170b404f | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
nexpaq | 1:55a6170b404f | 20 | * SOFTWARE. |
nexpaq | 1:55a6170b404f | 21 | */ |
nexpaq | 1:55a6170b404f | 22 | #include "ff.h" |
nexpaq | 1:55a6170b404f | 23 | #include "ffconf.h" |
nexpaq | 1:55a6170b404f | 24 | #include "mbed_debug.h" |
nexpaq | 1:55a6170b404f | 25 | |
nexpaq | 1:55a6170b404f | 26 | #include "FATFileHandle.h" |
nexpaq | 1:55a6170b404f | 27 | |
nexpaq | 1:55a6170b404f | 28 | FATFileHandle::FATFileHandle(FIL fh, PlatformMutex * mutex): _mutex(mutex) { |
nexpaq | 1:55a6170b404f | 29 | _fh = fh; |
nexpaq | 1:55a6170b404f | 30 | } |
nexpaq | 1:55a6170b404f | 31 | |
nexpaq | 1:55a6170b404f | 32 | int FATFileHandle::close() { |
nexpaq | 1:55a6170b404f | 33 | lock(); |
nexpaq | 1:55a6170b404f | 34 | int retval = f_close(&_fh); |
nexpaq | 1:55a6170b404f | 35 | unlock(); |
nexpaq | 1:55a6170b404f | 36 | delete this; |
nexpaq | 1:55a6170b404f | 37 | return retval; |
nexpaq | 1:55a6170b404f | 38 | } |
nexpaq | 1:55a6170b404f | 39 | |
nexpaq | 1:55a6170b404f | 40 | ssize_t FATFileHandle::write(const void* buffer, size_t length) { |
nexpaq | 1:55a6170b404f | 41 | lock(); |
nexpaq | 1:55a6170b404f | 42 | UINT n; |
nexpaq | 1:55a6170b404f | 43 | FRESULT res = f_write(&_fh, buffer, length, &n); |
nexpaq | 1:55a6170b404f | 44 | if (res) { |
nexpaq | 1:55a6170b404f | 45 | debug_if(FFS_DBG, "f_write() failed: %d", res); |
nexpaq | 1:55a6170b404f | 46 | unlock(); |
nexpaq | 1:55a6170b404f | 47 | return -1; |
nexpaq | 1:55a6170b404f | 48 | } |
nexpaq | 1:55a6170b404f | 49 | unlock(); |
nexpaq | 1:55a6170b404f | 50 | return n; |
nexpaq | 1:55a6170b404f | 51 | } |
nexpaq | 1:55a6170b404f | 52 | |
nexpaq | 1:55a6170b404f | 53 | ssize_t FATFileHandle::read(void* buffer, size_t length) { |
nexpaq | 1:55a6170b404f | 54 | lock(); |
nexpaq | 1:55a6170b404f | 55 | debug_if(FFS_DBG, "read(%d)\n", length); |
nexpaq | 1:55a6170b404f | 56 | UINT n; |
nexpaq | 1:55a6170b404f | 57 | FRESULT res = f_read(&_fh, buffer, length, &n); |
nexpaq | 1:55a6170b404f | 58 | if (res) { |
nexpaq | 1:55a6170b404f | 59 | debug_if(FFS_DBG, "f_read() failed: %d\n", res); |
nexpaq | 1:55a6170b404f | 60 | unlock(); |
nexpaq | 1:55a6170b404f | 61 | return -1; |
nexpaq | 1:55a6170b404f | 62 | } |
nexpaq | 1:55a6170b404f | 63 | unlock(); |
nexpaq | 1:55a6170b404f | 64 | return n; |
nexpaq | 1:55a6170b404f | 65 | } |
nexpaq | 1:55a6170b404f | 66 | |
nexpaq | 1:55a6170b404f | 67 | int FATFileHandle::isatty() { |
nexpaq | 1:55a6170b404f | 68 | return 0; |
nexpaq | 1:55a6170b404f | 69 | } |
nexpaq | 1:55a6170b404f | 70 | |
nexpaq | 1:55a6170b404f | 71 | off_t FATFileHandle::lseek(off_t position, int whence) { |
nexpaq | 1:55a6170b404f | 72 | lock(); |
nexpaq | 1:55a6170b404f | 73 | if (whence == SEEK_END) { |
nexpaq | 1:55a6170b404f | 74 | position += _fh.fsize; |
nexpaq | 1:55a6170b404f | 75 | } else if(whence==SEEK_CUR) { |
nexpaq | 1:55a6170b404f | 76 | position += _fh.fptr; |
nexpaq | 1:55a6170b404f | 77 | } |
nexpaq | 1:55a6170b404f | 78 | FRESULT res = f_lseek(&_fh, position); |
nexpaq | 1:55a6170b404f | 79 | if (res) { |
nexpaq | 1:55a6170b404f | 80 | debug_if(FFS_DBG, "lseek failed: %d\n", res); |
nexpaq | 1:55a6170b404f | 81 | unlock(); |
nexpaq | 1:55a6170b404f | 82 | return -1; |
nexpaq | 1:55a6170b404f | 83 | } else { |
nexpaq | 1:55a6170b404f | 84 | debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr); |
nexpaq | 1:55a6170b404f | 85 | unlock(); |
nexpaq | 1:55a6170b404f | 86 | return _fh.fptr; |
nexpaq | 1:55a6170b404f | 87 | } |
nexpaq | 1:55a6170b404f | 88 | } |
nexpaq | 1:55a6170b404f | 89 | |
nexpaq | 1:55a6170b404f | 90 | int FATFileHandle::fsync() { |
nexpaq | 1:55a6170b404f | 91 | lock(); |
nexpaq | 1:55a6170b404f | 92 | FRESULT res = f_sync(&_fh); |
nexpaq | 1:55a6170b404f | 93 | if (res) { |
nexpaq | 1:55a6170b404f | 94 | debug_if(FFS_DBG, "f_sync() failed: %d\n", res); |
nexpaq | 1:55a6170b404f | 95 | unlock(); |
nexpaq | 1:55a6170b404f | 96 | return -1; |
nexpaq | 1:55a6170b404f | 97 | } |
nexpaq | 1:55a6170b404f | 98 | unlock(); |
nexpaq | 1:55a6170b404f | 99 | return 0; |
nexpaq | 1:55a6170b404f | 100 | } |
nexpaq | 1:55a6170b404f | 101 | |
nexpaq | 1:55a6170b404f | 102 | off_t FATFileHandle::flen() { |
nexpaq | 1:55a6170b404f | 103 | lock(); |
nexpaq | 1:55a6170b404f | 104 | off_t size = _fh.fsize; |
nexpaq | 1:55a6170b404f | 105 | unlock(); |
nexpaq | 1:55a6170b404f | 106 | return size; |
nexpaq | 1:55a6170b404f | 107 | } |
nexpaq | 1:55a6170b404f | 108 | |
nexpaq | 1:55a6170b404f | 109 | void FATFileHandle::lock() { |
nexpaq | 1:55a6170b404f | 110 | _mutex->lock(); |
nexpaq | 1:55a6170b404f | 111 | } |
nexpaq | 1:55a6170b404f | 112 | |
nexpaq | 1:55a6170b404f | 113 | void FATFileHandle::unlock() { |
nexpaq | 1:55a6170b404f | 114 | _mutex->unlock(); |
nexpaq | 1:55a6170b404f | 115 | } |