Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
MenuClasses/Stats/SDFileSystem/FATFileSystem/FATFileSystem.cpp@104:17040265b7b4, 2019-05-09 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu May 09 14:52:19 2019 +0000
- Revision:
- 104:17040265b7b4
- Parent:
- 51:387249f9b333
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 33:249cf423fb18 | 1 | /* mbed Microcontroller Library |
AhmedPlaymaker | 33:249cf423fb18 | 2 | * Copyright (c) 2006-2012 ARM Limited |
AhmedPlaymaker | 33:249cf423fb18 | 3 | * |
AhmedPlaymaker | 33:249cf423fb18 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
AhmedPlaymaker | 33:249cf423fb18 | 5 | * of this software and associated documentation files (the "Software"), to deal |
AhmedPlaymaker | 33:249cf423fb18 | 6 | * in the Software without restriction, including without limitation the rights |
AhmedPlaymaker | 33:249cf423fb18 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
AhmedPlaymaker | 33:249cf423fb18 | 8 | * copies of the Software, and to permit persons to whom the Software is |
AhmedPlaymaker | 33:249cf423fb18 | 9 | * furnished to do so, subject to the following conditions: |
AhmedPlaymaker | 33:249cf423fb18 | 10 | * |
AhmedPlaymaker | 33:249cf423fb18 | 11 | * The above copyright notice and this permission notice shall be included in |
AhmedPlaymaker | 33:249cf423fb18 | 12 | * all copies or substantial portions of the Software. |
AhmedPlaymaker | 33:249cf423fb18 | 13 | * |
AhmedPlaymaker | 33:249cf423fb18 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
AhmedPlaymaker | 33:249cf423fb18 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
AhmedPlaymaker | 33:249cf423fb18 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
AhmedPlaymaker | 33:249cf423fb18 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
AhmedPlaymaker | 33:249cf423fb18 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AhmedPlaymaker | 33:249cf423fb18 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
AhmedPlaymaker | 33:249cf423fb18 | 20 | * SOFTWARE. |
AhmedPlaymaker | 33:249cf423fb18 | 21 | */ |
AhmedPlaymaker | 33:249cf423fb18 | 22 | #include "mbed.h" |
AhmedPlaymaker | 33:249cf423fb18 | 23 | |
AhmedPlaymaker | 33:249cf423fb18 | 24 | #include "ffconf.h" |
AhmedPlaymaker | 33:249cf423fb18 | 25 | #include "mbed_debug.h" |
AhmedPlaymaker | 33:249cf423fb18 | 26 | |
AhmedPlaymaker | 33:249cf423fb18 | 27 | #include "FATFileSystem.h" |
AhmedPlaymaker | 33:249cf423fb18 | 28 | #include "FATFileHandle.h" |
AhmedPlaymaker | 33:249cf423fb18 | 29 | #include "FATDirHandle.h" |
AhmedPlaymaker | 33:249cf423fb18 | 30 | |
AhmedPlaymaker | 33:249cf423fb18 | 31 | DWORD get_fattime(void) { |
AhmedPlaymaker | 33:249cf423fb18 | 32 | time_t rawtime; |
AhmedPlaymaker | 33:249cf423fb18 | 33 | time(&rawtime); |
AhmedPlaymaker | 33:249cf423fb18 | 34 | struct tm *ptm = localtime(&rawtime); |
AhmedPlaymaker | 33:249cf423fb18 | 35 | return (DWORD)(ptm->tm_year - 80) << 25 |
AhmedPlaymaker | 33:249cf423fb18 | 36 | | (DWORD)(ptm->tm_mon + 1 ) << 21 |
AhmedPlaymaker | 33:249cf423fb18 | 37 | | (DWORD)(ptm->tm_mday ) << 16 |
AhmedPlaymaker | 33:249cf423fb18 | 38 | | (DWORD)(ptm->tm_hour ) << 11 |
AhmedPlaymaker | 33:249cf423fb18 | 39 | | (DWORD)(ptm->tm_min ) << 5 |
AhmedPlaymaker | 33:249cf423fb18 | 40 | | (DWORD)(ptm->tm_sec/2 ); |
AhmedPlaymaker | 33:249cf423fb18 | 41 | } |
AhmedPlaymaker | 33:249cf423fb18 | 42 | |
AhmedPlaymaker | 33:249cf423fb18 | 43 | FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0}; |
AhmedPlaymaker | 33:249cf423fb18 | 44 | |
AhmedPlaymaker | 33:249cf423fb18 | 45 | FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) { |
AhmedPlaymaker | 33:249cf423fb18 | 46 | debug_if(FFS_DBG, "FATFileSystem(%s)\n", n); |
AhmedPlaymaker | 33:249cf423fb18 | 47 | for(int i=0; i<_VOLUMES; i++) { |
AhmedPlaymaker | 33:249cf423fb18 | 48 | if(_ffs[i] == 0) { |
AhmedPlaymaker | 33:249cf423fb18 | 49 | _ffs[i] = this; |
AhmedPlaymaker | 33:249cf423fb18 | 50 | _fsid[0] = '0' + i; |
AhmedPlaymaker | 33:249cf423fb18 | 51 | _fsid[1] = '\0'; |
AhmedPlaymaker | 33:249cf423fb18 | 52 | debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid); |
AhmedPlaymaker | 33:249cf423fb18 | 53 | f_mount(&_fs, _fsid, 0); |
AhmedPlaymaker | 33:249cf423fb18 | 54 | return; |
AhmedPlaymaker | 33:249cf423fb18 | 55 | } |
AhmedPlaymaker | 33:249cf423fb18 | 56 | } |
AhmedPlaymaker | 33:249cf423fb18 | 57 | error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n); |
AhmedPlaymaker | 33:249cf423fb18 | 58 | } |
AhmedPlaymaker | 33:249cf423fb18 | 59 | |
AhmedPlaymaker | 33:249cf423fb18 | 60 | FATFileSystem::~FATFileSystem() { |
AhmedPlaymaker | 33:249cf423fb18 | 61 | for (int i=0; i<_VOLUMES; i++) { |
AhmedPlaymaker | 33:249cf423fb18 | 62 | if (_ffs[i] == this) { |
AhmedPlaymaker | 33:249cf423fb18 | 63 | _ffs[i] = 0; |
AhmedPlaymaker | 33:249cf423fb18 | 64 | f_mount(NULL, _fsid, 0); |
AhmedPlaymaker | 33:249cf423fb18 | 65 | } |
AhmedPlaymaker | 33:249cf423fb18 | 66 | } |
AhmedPlaymaker | 33:249cf423fb18 | 67 | } |
AhmedPlaymaker | 33:249cf423fb18 | 68 | |
AhmedPlaymaker | 33:249cf423fb18 | 69 | FileHandle *FATFileSystem::open(const char* name, int flags) { |
AhmedPlaymaker | 33:249cf423fb18 | 70 | debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid); |
AhmedPlaymaker | 33:249cf423fb18 | 71 | char n[64]; |
AhmedPlaymaker | 33:249cf423fb18 | 72 | sprintf(n, "%s:/%s", _fsid, name); |
AhmedPlaymaker | 33:249cf423fb18 | 73 | |
AhmedPlaymaker | 33:249cf423fb18 | 74 | /* POSIX flags -> FatFS open mode */ |
AhmedPlaymaker | 33:249cf423fb18 | 75 | BYTE openmode; |
AhmedPlaymaker | 33:249cf423fb18 | 76 | if (flags & O_RDWR) { |
AhmedPlaymaker | 33:249cf423fb18 | 77 | openmode = FA_READ|FA_WRITE; |
AhmedPlaymaker | 33:249cf423fb18 | 78 | } else if(flags & O_WRONLY) { |
AhmedPlaymaker | 33:249cf423fb18 | 79 | openmode = FA_WRITE; |
AhmedPlaymaker | 33:249cf423fb18 | 80 | } else { |
AhmedPlaymaker | 33:249cf423fb18 | 81 | openmode = FA_READ; |
AhmedPlaymaker | 33:249cf423fb18 | 82 | } |
AhmedPlaymaker | 33:249cf423fb18 | 83 | if(flags & O_CREAT) { |
AhmedPlaymaker | 33:249cf423fb18 | 84 | if(flags & O_TRUNC) { |
AhmedPlaymaker | 33:249cf423fb18 | 85 | openmode |= FA_CREATE_ALWAYS; |
AhmedPlaymaker | 33:249cf423fb18 | 86 | } else { |
AhmedPlaymaker | 33:249cf423fb18 | 87 | openmode |= FA_OPEN_ALWAYS; |
AhmedPlaymaker | 33:249cf423fb18 | 88 | } |
AhmedPlaymaker | 33:249cf423fb18 | 89 | } |
AhmedPlaymaker | 33:249cf423fb18 | 90 | |
AhmedPlaymaker | 33:249cf423fb18 | 91 | FIL fh; |
AhmedPlaymaker | 33:249cf423fb18 | 92 | FRESULT res = f_open(&fh, n, openmode); |
AhmedPlaymaker | 33:249cf423fb18 | 93 | if (res) { |
AhmedPlaymaker | 33:249cf423fb18 | 94 | debug_if(FFS_DBG, "f_open('w') failed: %d\n", res); |
AhmedPlaymaker | 33:249cf423fb18 | 95 | return NULL; |
AhmedPlaymaker | 33:249cf423fb18 | 96 | } |
AhmedPlaymaker | 33:249cf423fb18 | 97 | if (flags & O_APPEND) { |
AhmedPlaymaker | 33:249cf423fb18 | 98 | f_lseek(&fh, fh.fsize); |
AhmedPlaymaker | 33:249cf423fb18 | 99 | } |
AhmedPlaymaker | 33:249cf423fb18 | 100 | return new FATFileHandle(fh); |
AhmedPlaymaker | 33:249cf423fb18 | 101 | } |
AhmedPlaymaker | 33:249cf423fb18 | 102 | |
AhmedPlaymaker | 33:249cf423fb18 | 103 | int FATFileSystem::open(FileHandle **file, const char *name, int flags) { |
AhmedPlaymaker | 33:249cf423fb18 | 104 | FileHandle *temp = open(name, flags); |
AhmedPlaymaker | 33:249cf423fb18 | 105 | if (!temp) { |
AhmedPlaymaker | 33:249cf423fb18 | 106 | return -1; |
AhmedPlaymaker | 33:249cf423fb18 | 107 | } |
AhmedPlaymaker | 33:249cf423fb18 | 108 | |
AhmedPlaymaker | 33:249cf423fb18 | 109 | *file = temp; |
AhmedPlaymaker | 33:249cf423fb18 | 110 | return 0; |
AhmedPlaymaker | 33:249cf423fb18 | 111 | } |
AhmedPlaymaker | 33:249cf423fb18 | 112 | |
AhmedPlaymaker | 33:249cf423fb18 | 113 | int FATFileSystem::remove(const char *filename) { |
AhmedPlaymaker | 33:249cf423fb18 | 114 | FRESULT res = f_unlink(filename); |
AhmedPlaymaker | 33:249cf423fb18 | 115 | if (res) { |
AhmedPlaymaker | 33:249cf423fb18 | 116 | debug_if(FFS_DBG, "f_unlink() failed: %d\n", res); |
AhmedPlaymaker | 33:249cf423fb18 | 117 | return -1; |
AhmedPlaymaker | 33:249cf423fb18 | 118 | } |
AhmedPlaymaker | 33:249cf423fb18 | 119 | return 0; |
AhmedPlaymaker | 33:249cf423fb18 | 120 | } |
AhmedPlaymaker | 33:249cf423fb18 | 121 | |
AhmedPlaymaker | 33:249cf423fb18 | 122 | int FATFileSystem::rename(const char *oldname, const char *newname) { |
AhmedPlaymaker | 33:249cf423fb18 | 123 | FRESULT res = f_rename(oldname, newname); |
AhmedPlaymaker | 33:249cf423fb18 | 124 | if (res) { |
AhmedPlaymaker | 33:249cf423fb18 | 125 | debug_if(FFS_DBG, "f_rename() failed: %d\n", res); |
AhmedPlaymaker | 33:249cf423fb18 | 126 | return -1; |
AhmedPlaymaker | 33:249cf423fb18 | 127 | } |
AhmedPlaymaker | 33:249cf423fb18 | 128 | return 0; |
AhmedPlaymaker | 33:249cf423fb18 | 129 | } |
AhmedPlaymaker | 33:249cf423fb18 | 130 | |
AhmedPlaymaker | 33:249cf423fb18 | 131 | int FATFileSystem::format() { |
AhmedPlaymaker | 33:249cf423fb18 | 132 | FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) |
AhmedPlaymaker | 33:249cf423fb18 | 133 | if (res) { |
AhmedPlaymaker | 33:249cf423fb18 | 134 | debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res); |
AhmedPlaymaker | 33:249cf423fb18 | 135 | return -1; |
AhmedPlaymaker | 33:249cf423fb18 | 136 | } |
AhmedPlaymaker | 33:249cf423fb18 | 137 | return 0; |
AhmedPlaymaker | 33:249cf423fb18 | 138 | } |
AhmedPlaymaker | 33:249cf423fb18 | 139 | |
AhmedPlaymaker | 33:249cf423fb18 | 140 | DirHandle *FATFileSystem::opendir(const char *name) { |
AhmedPlaymaker | 33:249cf423fb18 | 141 | FATFS_DIR dir; |
AhmedPlaymaker | 33:249cf423fb18 | 142 | FRESULT res = f_opendir(&dir, name); |
AhmedPlaymaker | 33:249cf423fb18 | 143 | if (res != 0) { |
AhmedPlaymaker | 33:249cf423fb18 | 144 | return NULL; |
AhmedPlaymaker | 33:249cf423fb18 | 145 | } |
AhmedPlaymaker | 33:249cf423fb18 | 146 | return new FATDirHandle(dir); |
AhmedPlaymaker | 33:249cf423fb18 | 147 | } |
AhmedPlaymaker | 33:249cf423fb18 | 148 | |
AhmedPlaymaker | 33:249cf423fb18 | 149 | int FATFileSystem::open(DirHandle **dir, const char *name) { |
AhmedPlaymaker | 33:249cf423fb18 | 150 | DirHandle *temp = opendir(name); |
AhmedPlaymaker | 33:249cf423fb18 | 151 | if (!temp) { |
AhmedPlaymaker | 33:249cf423fb18 | 152 | return -1; |
AhmedPlaymaker | 33:249cf423fb18 | 153 | } |
AhmedPlaymaker | 33:249cf423fb18 | 154 | |
AhmedPlaymaker | 33:249cf423fb18 | 155 | *dir = temp; |
AhmedPlaymaker | 33:249cf423fb18 | 156 | return 0; |
AhmedPlaymaker | 33:249cf423fb18 | 157 | } |
AhmedPlaymaker | 33:249cf423fb18 | 158 | |
AhmedPlaymaker | 33:249cf423fb18 | 159 | int FATFileSystem::mkdir(const char *name, mode_t mode) { |
AhmedPlaymaker | 33:249cf423fb18 | 160 | FRESULT res = f_mkdir(name); |
AhmedPlaymaker | 33:249cf423fb18 | 161 | return res == 0 ? 0 : -1; |
AhmedPlaymaker | 33:249cf423fb18 | 162 | } |
AhmedPlaymaker | 33:249cf423fb18 | 163 | |
AhmedPlaymaker | 33:249cf423fb18 | 164 | int FATFileSystem::mount() { |
AhmedPlaymaker | 33:249cf423fb18 | 165 | FRESULT res = f_mount(&_fs, _fsid, 1); |
AhmedPlaymaker | 33:249cf423fb18 | 166 | return res == 0 ? 0 : -1; |
AhmedPlaymaker | 33:249cf423fb18 | 167 | } |
AhmedPlaymaker | 33:249cf423fb18 | 168 | |
AhmedPlaymaker | 33:249cf423fb18 | 169 | int FATFileSystem::unmount() { |
AhmedPlaymaker | 33:249cf423fb18 | 170 | if (disk_sync()) |
AhmedPlaymaker | 33:249cf423fb18 | 171 | return -1; |
AhmedPlaymaker | 33:249cf423fb18 | 172 | FRESULT res = f_mount(NULL, _fsid, 0); |
AhmedPlaymaker | 33:249cf423fb18 | 173 | return res == 0 ? 0 : -1; |
AhmedPlaymaker | 33:249cf423fb18 | 174 | } |