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