mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1 /* mbed Microcontroller Library
kenjiArai 0:5b88d5760320 2 * Copyright (c) 2006-2012 ARM Limited
kenjiArai 0:5b88d5760320 3 *
kenjiArai 0:5b88d5760320 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
kenjiArai 0:5b88d5760320 5 * of this software and associated documentation files (the "Software"), to deal
kenjiArai 0:5b88d5760320 6 * in the Software without restriction, including without limitation the rights
kenjiArai 0:5b88d5760320 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kenjiArai 0:5b88d5760320 8 * copies of the Software, and to permit persons to whom the Software is
kenjiArai 0:5b88d5760320 9 * furnished to do so, subject to the following conditions:
kenjiArai 0:5b88d5760320 10 *
kenjiArai 0:5b88d5760320 11 * The above copyright notice and this permission notice shall be included in
kenjiArai 0:5b88d5760320 12 * all copies or substantial portions of the Software.
kenjiArai 0:5b88d5760320 13 *
kenjiArai 0:5b88d5760320 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kenjiArai 0:5b88d5760320 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kenjiArai 0:5b88d5760320 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kenjiArai 0:5b88d5760320 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kenjiArai 0:5b88d5760320 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenjiArai 0:5b88d5760320 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
kenjiArai 0:5b88d5760320 20 * SOFTWARE.
kenjiArai 0:5b88d5760320 21 */
kenjiArai 0:5b88d5760320 22
kenjiArai 0:5b88d5760320 23 /** \addtogroup storage */
kenjiArai 0:5b88d5760320 24 /** @{*/
kenjiArai 0:5b88d5760320 25
kenjiArai 0:5b88d5760320 26 #ifndef MBED_FATFILESYSTEM_H
kenjiArai 0:5b88d5760320 27 #define MBED_FATFILESYSTEM_H
kenjiArai 0:5b88d5760320 28
kenjiArai 1:9db0e321a9f4 29 #include "features/storage/filesystem/FileSystem.h"
kenjiArai 1:9db0e321a9f4 30 #include "features/storage/blockdevice/BlockDevice.h"
kenjiArai 0:5b88d5760320 31 #include "FileHandle.h"
kenjiArai 0:5b88d5760320 32 #include <stdint.h>
kenjiArai 0:5b88d5760320 33 #include "PlatformMutex.h"
kenjiArai 1:9db0e321a9f4 34 #include "features/storage/filesystem/fat/ChaN/ff.h"
kenjiArai 0:5b88d5760320 35
kenjiArai 0:5b88d5760320 36 namespace mbed {
kenjiArai 0:5b88d5760320 37
kenjiArai 0:5b88d5760320 38 /**
kenjiArai 0:5b88d5760320 39 * FAT file system based on ChaN's FAT file system library v0.8
kenjiArai 0:5b88d5760320 40 *
kenjiArai 0:5b88d5760320 41 * Synchronization level: Thread safe
kenjiArai 0:5b88d5760320 42 */
kenjiArai 0:5b88d5760320 43 class FATFileSystem : public FileSystem {
kenjiArai 0:5b88d5760320 44 public:
kenjiArai 0:5b88d5760320 45 /** Lifetime of the FAT file system.
kenjiArai 0:5b88d5760320 46 *
kenjiArai 0:5b88d5760320 47 * @param name Name of the file system in the tree.
kenjiArai 0:5b88d5760320 48 * @param bd Block device to mount. Mounted immediately if not NULL.
kenjiArai 0:5b88d5760320 49 */
kenjiArai 0:5b88d5760320 50 FATFileSystem(const char *name = NULL, BlockDevice *bd = NULL);
kenjiArai 0:5b88d5760320 51 virtual ~FATFileSystem();
kenjiArai 0:5b88d5760320 52
kenjiArai 0:5b88d5760320 53 /** Format a logical drive, FDISK partitioning rule.
kenjiArai 0:5b88d5760320 54 *
kenjiArai 0:5b88d5760320 55 * The block device to format should be mounted when this function is called.
kenjiArai 0:5b88d5760320 56 *
kenjiArai 0:5b88d5760320 57 * @param bd
kenjiArai 0:5b88d5760320 58 * This is the block device that will be formatted.
kenjiArai 0:5b88d5760320 59 *
kenjiArai 0:5b88d5760320 60 * @param cluster_size
kenjiArai 0:5b88d5760320 61 * This is the number of bytes per cluster. A larger cluster size decreases
kenjiArai 0:5b88d5760320 62 * the overhead of the FAT table, but also increases the minimum file size. The
kenjiArai 0:5b88d5760320 63 * cluster size must be a multiple of the underlying device's allocation unit
kenjiArai 0:5b88d5760320 64 * and is currently limited to a max of 32,768 bytes. If the cluster size is set to zero, a cluster size
kenjiArai 0:5b88d5760320 65 * is determined from the device's allocation unit. Defaults to zero.
kenjiArai 0:5b88d5760320 66 *
kenjiArai 0:5b88d5760320 67 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 68 */
kenjiArai 0:5b88d5760320 69 static int format(BlockDevice *bd, bd_size_t cluster_size = 0);
kenjiArai 0:5b88d5760320 70
kenjiArai 0:5b88d5760320 71 /** Mount a file system to a block device.
kenjiArai 0:5b88d5760320 72 *
kenjiArai 0:5b88d5760320 73 * @param bd Block device to mount to.
kenjiArai 0:5b88d5760320 74 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 75 */
kenjiArai 0:5b88d5760320 76 virtual int mount(BlockDevice *bd);
kenjiArai 0:5b88d5760320 77
kenjiArai 0:5b88d5760320 78 /** Unmount a file system from the underlying block device.
kenjiArai 0:5b88d5760320 79 *
kenjiArai 0:5b88d5760320 80 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 81 */
kenjiArai 0:5b88d5760320 82 virtual int unmount();
kenjiArai 0:5b88d5760320 83
kenjiArai 0:5b88d5760320 84 /** Reformat a file system, results in an empty and mounted file system.
kenjiArai 0:5b88d5760320 85 *
kenjiArai 0:5b88d5760320 86 * @param bd
kenjiArai 0:5b88d5760320 87 * Block device to reformat and mount. If NULL, the mounted
kenjiArai 0:5b88d5760320 88 * Block device is used.
kenjiArai 0:5b88d5760320 89 * Note: If mount fails, bd must be provided.
kenjiArai 0:5b88d5760320 90 * Default: NULL
kenjiArai 0:5b88d5760320 91 *
kenjiArai 0:5b88d5760320 92 * @param allocation_unit
kenjiArai 0:5b88d5760320 93 * This is the number of bytes per cluster size. The valid value is N
kenjiArai 0:5b88d5760320 94 * times the sector size. N is a power of 2, from 1 to 128, for the FAT
kenjiArai 0:5b88d5760320 95 * volume, and up to 16MiB for the exFAT volume. If zero is given,
kenjiArai 0:5b88d5760320 96 * the default allocation unit size is selected by the underlying
kenjiArai 0:5b88d5760320 97 * file system, which depends on the volume size.
kenjiArai 0:5b88d5760320 98 *
kenjiArai 0:5b88d5760320 99 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 100 */
kenjiArai 0:5b88d5760320 101 virtual int reformat(BlockDevice *bd, int allocation_unit);
kenjiArai 0:5b88d5760320 102
kenjiArai 0:5b88d5760320 103 /** Reformat a file system, results in an empty and mounted file system.
kenjiArai 0:5b88d5760320 104 *
kenjiArai 0:5b88d5760320 105 * @param bd Block device to reformat and mount. If NULL, the mounted
kenjiArai 0:5b88d5760320 106 * Block device is used.
kenjiArai 0:5b88d5760320 107 * Note: If mount fails, bd must be provided.
kenjiArai 0:5b88d5760320 108 * Default: NULL
kenjiArai 0:5b88d5760320 109 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 110 */
kenjiArai 0:5b88d5760320 111 virtual int reformat(BlockDevice *bd = NULL)
kenjiArai 0:5b88d5760320 112 {
kenjiArai 0:5b88d5760320 113 // Required for virtual inheritance shenanigans.
kenjiArai 0:5b88d5760320 114 return reformat(bd, 0);
kenjiArai 0:5b88d5760320 115 }
kenjiArai 0:5b88d5760320 116
kenjiArai 0:5b88d5760320 117 /** Remove a file from the file system.
kenjiArai 0:5b88d5760320 118 *
kenjiArai 0:5b88d5760320 119 * @param path The name of the file to remove.
kenjiArai 0:5b88d5760320 120 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 121 */
kenjiArai 0:5b88d5760320 122 virtual int remove(const char *path);
kenjiArai 0:5b88d5760320 123
kenjiArai 0:5b88d5760320 124 /** Rename a file in the file system.
kenjiArai 0:5b88d5760320 125 *
kenjiArai 0:5b88d5760320 126 * @param path The current name of the file to rename.
kenjiArai 0:5b88d5760320 127 * @param newpath The new name of the file.
kenjiArai 0:5b88d5760320 128 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 129 */
kenjiArai 0:5b88d5760320 130 virtual int rename(const char *path, const char *newpath);
kenjiArai 0:5b88d5760320 131
kenjiArai 0:5b88d5760320 132 /** Store information about the file in a stat structure.
kenjiArai 0:5b88d5760320 133 *
kenjiArai 0:5b88d5760320 134 * @param path The name of the file to store information about.
kenjiArai 0:5b88d5760320 135 * @param st The stat buffer to write to.
kenjiArai 0:5b88d5760320 136 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 137 */
kenjiArai 0:5b88d5760320 138 virtual int stat(const char *path, struct stat *st);
kenjiArai 0:5b88d5760320 139
kenjiArai 0:5b88d5760320 140 /** Create a directory in the file system.
kenjiArai 0:5b88d5760320 141 *
kenjiArai 0:5b88d5760320 142 * @param path The name of the directory to create.
kenjiArai 0:5b88d5760320 143 * @param mode The permissions with which to create the directory.
kenjiArai 0:5b88d5760320 144 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 145 */
kenjiArai 0:5b88d5760320 146 virtual int mkdir(const char *path, mode_t mode);
kenjiArai 0:5b88d5760320 147
kenjiArai 0:5b88d5760320 148 /** Store information about the mounted file system in a statvfs structure.
kenjiArai 0:5b88d5760320 149 *
kenjiArai 0:5b88d5760320 150 * @param path The name of the file to store information about.
kenjiArai 0:5b88d5760320 151 * @param buf The stat buffer to write to.
kenjiArai 0:5b88d5760320 152 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 153 */
kenjiArai 0:5b88d5760320 154 virtual int statvfs(const char *path, struct statvfs *buf);
kenjiArai 0:5b88d5760320 155
kenjiArai 0:5b88d5760320 156 protected:
kenjiArai 0:5b88d5760320 157 #if !(DOXYGEN_ONLY)
kenjiArai 0:5b88d5760320 158 /** Open a file on the file system.
kenjiArai 0:5b88d5760320 159 *
kenjiArai 0:5b88d5760320 160 * @param file Destination for the handle to a newly created file.
kenjiArai 0:5b88d5760320 161 * @param path The name of the file to open.
kenjiArai 0:5b88d5760320 162 * @param flags The flags that trigger opening of the file. These flags are O_RDONLY, O_WRONLY, and O_RDWR,
kenjiArai 0:5b88d5760320 163 * with an O_CREAT, O_TRUNC, or O_APPEND bitwise OR operator.
kenjiArai 0:5b88d5760320 164 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 165 */
kenjiArai 0:5b88d5760320 166 virtual int file_open(fs_file_t *file, const char *path, int flags);
kenjiArai 0:5b88d5760320 167
kenjiArai 0:5b88d5760320 168 /** Close a file
kenjiArai 0:5b88d5760320 169 *
kenjiArai 0:5b88d5760320 170 * @param file File handle.
kenjiArai 0:5b88d5760320 171 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 172 */
kenjiArai 0:5b88d5760320 173 virtual int file_close(fs_file_t file);
kenjiArai 0:5b88d5760320 174
kenjiArai 0:5b88d5760320 175 /** Read the contents of a file into a buffer.
kenjiArai 0:5b88d5760320 176 *
kenjiArai 0:5b88d5760320 177 * @param file File handle.
kenjiArai 0:5b88d5760320 178 * @param buffer The buffer to read into.
kenjiArai 0:5b88d5760320 179 * @param len The number of bytes to read.
kenjiArai 0:5b88d5760320 180 * @return The number of bytes read; 0 at the end of the file, negative error on failure.
kenjiArai 0:5b88d5760320 181 */
kenjiArai 0:5b88d5760320 182 virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len);
kenjiArai 0:5b88d5760320 183
kenjiArai 0:5b88d5760320 184 /** Write the contents of a buffer to a file.
kenjiArai 0:5b88d5760320 185 *
kenjiArai 0:5b88d5760320 186 * @param file File handle.
kenjiArai 0:5b88d5760320 187 * @param buffer The buffer to write from.
kenjiArai 0:5b88d5760320 188 * @param len The number of bytes to write.
kenjiArai 0:5b88d5760320 189 * @return The number of bytes written, negative error on failure.
kenjiArai 0:5b88d5760320 190 */
kenjiArai 0:5b88d5760320 191 virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len);
kenjiArai 0:5b88d5760320 192
kenjiArai 0:5b88d5760320 193 /** Flush any buffers associated with the file.
kenjiArai 0:5b88d5760320 194 *
kenjiArai 0:5b88d5760320 195 * @param file File handle.
kenjiArai 0:5b88d5760320 196 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 197 */
kenjiArai 0:5b88d5760320 198 virtual int file_sync(fs_file_t file);
kenjiArai 0:5b88d5760320 199
kenjiArai 0:5b88d5760320 200 /** Move the file position to a given offset from a given location
kenjiArai 0:5b88d5760320 201 *
kenjiArai 0:5b88d5760320 202 * @param file File handle.
kenjiArai 0:5b88d5760320 203 * @param offset The offset from whence to move to.
kenjiArai 0:5b88d5760320 204 * @param whence The start of where to seek.
kenjiArai 0:5b88d5760320 205 * SEEK_SET to start from the beginning of the file,
kenjiArai 0:5b88d5760320 206 * SEEK_CUR to start from the current position in the file,
kenjiArai 0:5b88d5760320 207 * SEEK_END to start from the end of the file.
kenjiArai 0:5b88d5760320 208 * @return The new offset of the file.
kenjiArai 0:5b88d5760320 209 */
kenjiArai 0:5b88d5760320 210 virtual off_t file_seek(fs_file_t file, off_t offset, int whence);
kenjiArai 0:5b88d5760320 211
kenjiArai 0:5b88d5760320 212 /** Get the file position of the file.
kenjiArai 0:5b88d5760320 213 *
kenjiArai 0:5b88d5760320 214 * @param file File handle.
kenjiArai 0:5b88d5760320 215 * @return The current offset in the file.
kenjiArai 0:5b88d5760320 216 */
kenjiArai 0:5b88d5760320 217 virtual off_t file_tell(fs_file_t file);
kenjiArai 0:5b88d5760320 218
kenjiArai 0:5b88d5760320 219 /** Get the size of the file.
kenjiArai 0:5b88d5760320 220 *
kenjiArai 0:5b88d5760320 221 * @param file File handle.
kenjiArai 0:5b88d5760320 222 * @return Size of the file in bytes.
kenjiArai 0:5b88d5760320 223 */
kenjiArai 0:5b88d5760320 224 virtual off_t file_size(fs_file_t file);
kenjiArai 0:5b88d5760320 225
kenjiArai 0:5b88d5760320 226 /** Truncate or extend a file.
kenjiArai 0:5b88d5760320 227 *
kenjiArai 0:5b88d5760320 228 * The file's length is set to the specified value. The seek pointer is
kenjiArai 0:5b88d5760320 229 * not changed. If the file is extended, the extended area appears as if
kenjiArai 0:5b88d5760320 230 * it were zero-filled.
kenjiArai 0:5b88d5760320 231 *
kenjiArai 0:5b88d5760320 232 * @param file File handle.
kenjiArai 0:5b88d5760320 233 * @param length The requested new length for the file.
kenjiArai 0:5b88d5760320 234 *
kenjiArai 0:5b88d5760320 235 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 236 */
kenjiArai 0:5b88d5760320 237 virtual int file_truncate(mbed::fs_file_t file, off_t length);
kenjiArai 0:5b88d5760320 238
kenjiArai 0:5b88d5760320 239 /** Open a directory on the file system.
kenjiArai 0:5b88d5760320 240 *
kenjiArai 0:5b88d5760320 241 * @param dir Destination for the handle to the directory.
kenjiArai 0:5b88d5760320 242 * @param path Name of the directory to open.
kenjiArai 0:5b88d5760320 243 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 244 */
kenjiArai 0:5b88d5760320 245 virtual int dir_open(fs_dir_t *dir, const char *path);
kenjiArai 0:5b88d5760320 246
kenjiArai 0:5b88d5760320 247 /** Close a directory
kenjiArai 0:5b88d5760320 248 *
kenjiArai 0:5b88d5760320 249 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 250 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 251 */
kenjiArai 0:5b88d5760320 252 virtual int dir_close(fs_dir_t dir);
kenjiArai 0:5b88d5760320 253
kenjiArai 0:5b88d5760320 254 /** Read the next directory entry
kenjiArai 0:5b88d5760320 255 *
kenjiArai 0:5b88d5760320 256 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 257 * @param ent The directory entry to fill out.
kenjiArai 0:5b88d5760320 258 * @return 1 on reading a filename, 0 at the end of the directory, negative error on failure.
kenjiArai 0:5b88d5760320 259 */
kenjiArai 0:5b88d5760320 260 virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
kenjiArai 0:5b88d5760320 261
kenjiArai 0:5b88d5760320 262 /** Set the current position of the directory.
kenjiArai 0:5b88d5760320 263 *
kenjiArai 0:5b88d5760320 264 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 265 * @param offset Offset of the location to seek to.
kenjiArai 0:5b88d5760320 266 * Must be a value returned by dir_tell.
kenjiArai 0:5b88d5760320 267 */
kenjiArai 0:5b88d5760320 268 virtual void dir_seek(fs_dir_t dir, off_t offset);
kenjiArai 0:5b88d5760320 269
kenjiArai 0:5b88d5760320 270 /** Get the current position of the directory.
kenjiArai 0:5b88d5760320 271 *
kenjiArai 0:5b88d5760320 272 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 273 * @return Directory position, which can be passed to dir_rewind.
kenjiArai 0:5b88d5760320 274 */
kenjiArai 0:5b88d5760320 275 virtual off_t dir_tell(fs_dir_t dir);
kenjiArai 0:5b88d5760320 276
kenjiArai 0:5b88d5760320 277 /** Rewind the current position to the beginning of the directory.
kenjiArai 0:5b88d5760320 278 *
kenjiArai 0:5b88d5760320 279 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 280 */
kenjiArai 0:5b88d5760320 281 virtual void dir_rewind(fs_dir_t dir);
kenjiArai 0:5b88d5760320 282 #endif //!(DOXYGEN_ONLY)
kenjiArai 0:5b88d5760320 283
kenjiArai 0:5b88d5760320 284 private:
kenjiArai 0:5b88d5760320 285 FATFS _fs; // Work area (file system object) for logical drive.
kenjiArai 0:5b88d5760320 286 char _fsid[sizeof("0:")];
kenjiArai 0:5b88d5760320 287 int _id;
kenjiArai 0:5b88d5760320 288
kenjiArai 0:5b88d5760320 289 protected:
kenjiArai 0:5b88d5760320 290 virtual void lock();
kenjiArai 0:5b88d5760320 291 virtual void unlock();
kenjiArai 0:5b88d5760320 292 virtual int mount(BlockDevice *bd, bool mount);
kenjiArai 0:5b88d5760320 293 };
kenjiArai 0:5b88d5760320 294
kenjiArai 0:5b88d5760320 295 } // namespace mbed
kenjiArai 0:5b88d5760320 296
kenjiArai 0:5b88d5760320 297 // Added "using" for backwards compatibility.
kenjiArai 0:5b88d5760320 298 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
kenjiArai 0:5b88d5760320 299 using mbed::FATFileSystem;
kenjiArai 0:5b88d5760320 300 #endif
kenjiArai 0:5b88d5760320 301
kenjiArai 0:5b88d5760320 302 #endif
kenjiArai 0:5b88d5760320 303
kenjiArai 0:5b88d5760320 304 /** @}*/