1 // Copyright 2011 Olivier Gillet. 2 // 3 // Author: Olivier Gillet (ol.gillet@gmail.com) 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // You should have received a copy of the GNU General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 // 16 // ----------------------------------------------------------------------------- 17 // 18 // FatFS wrappers. 19 20 #ifndef AVRLIB_FILESYSTEM_FILE_SYSTEM_H_ 21 #define AVRLIB_FILESYSTEM_FILE_SYSTEM_H_ 22 23 #include <string.h> 24 25 #include "avrlib/avrlib.h" 26 27 #include "avrlib/third_party/ff/ff.h" 28 #include "avrlib/third_party/ff/mmc.h" 29 30 namespace avrlib { 31 32 enum FilesystemStatus { 33 FS_OK = 0, 34 FS_DISK_ERROR, 35 FS_EXCEPTION, 36 FS_DRIVE_NOT_READY, 37 FS_FILE_NOT_FOUND, 38 FS_PATH_NOT_FOUND, 39 FS_INVALID_NAME, 40 FS_ACCESS_DENIED, 41 FS_FILE_EXISTS, 42 FS_INVALID_OBJECT, 43 FS_WRITE_PROTECTED, 44 FS_INVALID_DRIVE, 45 FS_VOLUME_NOT_INITIALIZED, 46 FS_NO_FAT_VOLUME, 47 FS_FORMAT_FAILED, 48 FS_TIMEOUT, 49 FS_LOCKED, 50 FS_NOT_ENOUGH_MEMORY, 51 FS_TOO_MANY_FILES, 52 FS_INVALID_PARAMETER, 53 FS_NOT_OPENED, 54 FS_BAD_FILE_FORMAT, 55 FS_COPY_ERROR 56 }; 57 58 enum FileAttribute { 59 FS_ATTRIBUTE_READ_ONLY = 1, 60 FS_ATTRIBUTE_HIDDEN = 2, 61 FS_ATTRIBUTE_SYSTEM = 4, 62 FS_ATTRIBUTE_VOLUME = 8, 63 FS_ATTRIBUTE_LFN = 15, 64 FS_ATTRIBUTE_DIRECTORY = 16, 65 FS_ATTRIBUTE_ARCHIVE = 32, 66 FS_ATTRIBUTE_ATTRIBUTES = 0x3f, 67 }; 68 69 struct FileInfo { sizeFileInfo70 inline uint32_t size() const { 71 return file_info.fsize; 72 } 73 modification_dateFileInfo74 inline uint16_t modification_date() const { 75 return file_info.fdate; 76 } 77 modification_timeFileInfo78 inline uint16_t modification_time() const { 79 return file_info.ftime; 80 } 81 attributesFileInfo82 inline uint8_t attributes() const { 83 return file_info.fattrib; 84 } 85 is_read_onlyFileInfo86 inline uint8_t is_read_only() const { 87 return file_info.fattrib & FS_ATTRIBUTE_READ_ONLY; 88 } 89 is_hiddenFileInfo90 inline uint8_t is_hidden() const { 91 return file_info.fattrib & FS_ATTRIBUTE_HIDDEN; 92 } 93 is_systemFileInfo94 inline uint8_t is_system() const { 95 return file_info.fattrib & FS_ATTRIBUTE_SYSTEM; 96 } 97 is_volumeFileInfo98 inline uint8_t is_volume() const { 99 return file_info.fattrib & FS_ATTRIBUTE_VOLUME; 100 } 101 is_directoryFileInfo102 inline uint8_t is_directory() const { 103 return file_info.fattrib & FS_ATTRIBUTE_DIRECTORY; 104 } 105 is_archiveFileInfo106 inline uint8_t is_archive() const { 107 return file_info.fattrib & FS_ATTRIBUTE_ARCHIVE; 108 } 109 nameFileInfo110 inline const char* name() const { 111 return file_info.fname; 112 } 113 114 FILINFO file_info; 115 }; 116 117 class Filesystem { 118 public: Filesystem()119 Filesystem() { } 120 121 static FilesystemStatus Init(); 122 static FilesystemStatus Init(uint16_t timeout_ms); 123 124 static FilesystemStatus Unlink(const char* file_name); 125 static FilesystemStatus Mkdir(const char* dir_name); 126 static FilesystemStatus Mkdirs(char* path); 127 static FilesystemStatus Chmod( 128 const char* file_name, 129 uint8_t value, 130 uint8_t mask); 131 static FilesystemStatus Rename(const char* old_name, const char* new_name); 132 static FilesystemStatus FileStatus(const char* file_name, FileInfo* info); 133 static FilesystemStatus Utime( 134 const char* file_name, 135 uint16_t date, 136 uint16_t time); 137 138 static FilesystemStatus Mkfs(); 139 140 static uint32_t GetFreeSpace(); GetType()141 static uint16_t GetType() { 142 uint8_t card_type; 143 disk_ioctl(0, MMC_GET_TYPE, &card_type); 144 return fs_.fs_type | (card_type << 8); 145 } 146 Tick()147 static inline void Tick() { 148 disk_timerproc(); 149 } 150 buffer()151 static uint8_t* buffer() { return fs_.win; } 152 153 private: 154 static FATFS fs_; 155 156 DISALLOW_COPY_AND_ASSIGN(Filesystem); 157 }; 158 159 } // namespace avrlib 160 161 #endif // AVRLIB_FILESYSTEM_FILE_SYSTEM_H_ 162