takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FATFileSystem.h Source File

FATFileSystem.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MBED_FATFILESYSTEM_H
00023 #define MBED_FATFILESYSTEM_H
00024 
00025 #include "FileSystem.h"
00026 #include "BlockDevice.h"
00027 #include "FileHandle.h"
00028 #include "ff.h"
00029 #include <stdint.h>
00030 #include "PlatformMutex.h"
00031 
00032 
00033 /**
00034  * FATFileSystem based on ChaN's Fat Filesystem library v0.8
00035  *
00036  * Synchronization level: Thread safe
00037  */
00038 class FATFileSystem : public mbed::FileSystem {
00039 public:
00040     /** Lifetime of the FATFileSystem
00041      *
00042      *  @param name     Name to add filesystem to tree as
00043      *  @param bd       BlockDevice to mount, may be passed instead to mount call
00044      */
00045     FATFileSystem(const char *name = NULL, BlockDevice *bd = NULL);
00046     virtual ~FATFileSystem();
00047 
00048     /** Formats a logical drive, FDISK partitioning rule.
00049      *
00050      *  The block device to format should be mounted when this function is called.
00051      *
00052      *  @param bd
00053      *    This is the block device that will be formatted.
00054      *
00055      *  @param cluster_size
00056      *    This is the number of bytes per cluster. A larger cluster size will decrease
00057      *    the overhead of the FAT table, but also increase the minimum file size. The
00058      *    cluster_size must be a multiple of the underlying device's allocation unit
00059      *    and is currently limited to a max of 32,768 bytes. If zero, a cluster size
00060      *    will be determined from the device's allocation unit. Defaults to zero.
00061      *
00062      *  @return         0 on success, negative error code on failure
00063      */
00064     static int format(BlockDevice *bd, bd_size_t cluster_size = 0);
00065 
00066     /** Mounts a filesystem to a block device
00067      *
00068      *  @param bd       BlockDevice to mount to
00069      *  @return         0 on success, negative error code on failure
00070      */
00071     virtual int mount(BlockDevice *bd);
00072 
00073     /** Unmounts a filesystem from the underlying block device
00074      *
00075      *  @return         0 on success, negative error code on failure
00076      */
00077     virtual int unmount();
00078 
00079     /** Reformats a filesystem, results in an empty and mounted filesystem
00080      *
00081      *  @param bd
00082      *      BlockDevice to reformat and mount. If NULL, the mounted
00083      *      block device will be used.
00084      *      Note: if mount fails, bd must be provided.
00085      *      Default: NULL
00086      *
00087      *  @param allocation_unit
00088      *      This is the number of bytes per cluster size. The valid value is N
00089      *      times the sector size. N is a power of 2 from 1 to 128 for FAT
00090      *      volume and upto 16MiB for exFAT volume. If zero is given,
00091      *      the default allocation unit size is selected by the underlying
00092      *      filesystem, which depends on the volume size.
00093      *
00094      *  @return         0 on success, negative error code on failure
00095      */
00096     virtual int reformat(BlockDevice *bd, int allocation_unit);
00097 
00098     /** Reformats a filesystem, results in an empty and mounted filesystem
00099      *
00100      *  @param bd       BlockDevice to reformat and mount. If NULL, the mounted
00101      *                  block device will be used.
00102      *                  Note: if mount fails, bd must be provided.
00103      *                  Default: NULL
00104      *  @return         0 on success, negative error code on failure
00105      */
00106     virtual int reformat(BlockDevice *bd = NULL)
00107     {
00108         // required for virtual inheritance shenanigans
00109         return reformat(bd, 0);
00110     }
00111 
00112     /** Remove a file from the filesystem.
00113      *
00114      *  @param path     The name of the file to remove.
00115      *  @return         0 on success, negative error code on failure
00116      */
00117     virtual int remove(const char *path);
00118 
00119     /** Rename a file in the filesystem.
00120      *
00121      *  @param path     The name of the file to rename.
00122      *  @param newpath  The name to rename it to
00123      *  @return         0 on success, negative error code on failure
00124      */
00125     virtual int rename(const char *path, const char *newpath);
00126 
00127     /** Store information about the file in a stat structure
00128      *
00129      *  @param path     The name of the file to find information about
00130      *  @param st       The stat buffer to write to
00131      *  @return         0 on success, negative error code on failure
00132      */
00133     virtual int stat(const char *path, struct stat *st);
00134 
00135     /** Create a directory in the filesystem.
00136      *
00137      *  @param path     The name of the directory to create.
00138      *  @param mode     The permissions with which to create the directory
00139      *  @return         0 on success, negative error code on failure
00140      */
00141     virtual int mkdir(const char *path, mode_t mode);
00142 
00143     /** Store information about the mounted filesystem in a statvfs structure
00144      *
00145      *  @param path     The name of the file to find information about
00146      *  @param buf      The stat buffer to write to
00147      *  @return         0 on success, negative error code on failure
00148      */
00149      virtual int statvfs(const char *path, struct statvfs *buf);
00150 
00151 protected:
00152     /** Open a file on the filesystem
00153      *
00154      *  @param file     Destination for the handle to a newly created file
00155      *  @param path     The name of the file to open
00156      *  @param flags    The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
00157      *                  bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
00158      *  @return         0 on success, negative error code on failure
00159      */
00160     virtual int file_open(mbed::fs_file_t *file, const char *path, int flags);
00161 
00162     /** Close a file
00163      *
00164      *  @param file     File handle
00165      *  @return         0 on success, negative error code on failure
00166      */
00167     virtual int file_close(mbed::fs_file_t file);
00168 
00169     /** Read the contents of a file into a buffer
00170      *
00171      *  @param file     File handle
00172      *  @param buffer   The buffer to read in to
00173      *  @param len      The number of bytes to read
00174      *  @return         The number of bytes read, 0 at end of file, negative error on failure
00175      */
00176     virtual ssize_t file_read(mbed::fs_file_t file, void *buffer, size_t len);
00177 
00178     /** Write the contents of a buffer to a file
00179      *
00180      *  @param file     File handle
00181      *  @param buffer   The buffer to write from
00182      *  @param len      The number of bytes to write
00183      *  @return         The number of bytes written, negative error on failure
00184      */
00185     virtual ssize_t file_write(mbed::fs_file_t file, const void *buffer, size_t len);
00186 
00187     /** Flush any buffers associated with the file
00188      *
00189      *  @param file     File handle
00190      *  @return         0 on success, negative error code on failure
00191      */
00192     virtual int file_sync(mbed::fs_file_t file);
00193 
00194     /** Move the file position to a given offset from from a given location
00195      *
00196      *  @param file     File handle
00197      *  @param offset   The offset from whence to move to
00198      *  @param whence   The start of where to seek
00199      *      SEEK_SET to start from beginning of file,
00200      *      SEEK_CUR to start from current position in file,
00201      *      SEEK_END to start from end of file
00202      *  @return         The new offset of the file
00203      */
00204     virtual off_t file_seek(mbed::fs_file_t file, off_t offset, int whence);
00205 
00206     /** Get the file position of the file
00207      *
00208      *  @param file     File handle
00209      *  @return         The current offset in the file
00210      */
00211     virtual off_t file_tell(mbed::fs_file_t file);
00212 
00213     /** Get the size of the file
00214      *
00215      *  @param file     File handle
00216      *  @return         Size of the file in bytes
00217      */
00218     virtual off_t file_size(mbed::fs_file_t file);
00219 
00220     /** Open a directory on the filesystem
00221      *
00222      *  @param dir      Destination for the handle to the directory
00223      *  @param path     Name of the directory to open
00224      *  @return         0 on success, negative error code on failure
00225      */
00226     virtual int dir_open(mbed::fs_dir_t *dir, const char *path);
00227 
00228     /** Close a directory
00229      *
00230      *  @param dir      Dir handle
00231      *  @return         0 on success, negative error code on failure
00232      */
00233     virtual int dir_close(mbed::fs_dir_t dir);
00234 
00235     /** Read the next directory entry
00236      *
00237      *  @param dir      Dir handle
00238      *  @param ent      The directory entry to fill out
00239      *  @return         1 on reading a filename, 0 at end of directory, negative error on failure
00240      */
00241     virtual ssize_t dir_read(mbed::fs_dir_t dir, struct dirent *ent);
00242 
00243     /** Set the current position of the directory
00244      *
00245      *  @param dir      Dir handle
00246      *  @param offset   Offset of the location to seek to,
00247      *                  must be a value returned from dir_tell
00248      */
00249     virtual void dir_seek(mbed::fs_dir_t dir, off_t offset);
00250 
00251     /** Get the current position of the directory
00252      *
00253      *  @param dir      Dir handle
00254      *  @return         Position of the directory that can be passed to dir_rewind
00255      */
00256     virtual off_t dir_tell(mbed::fs_dir_t dir);
00257 
00258     /** Rewind the current position to the beginning of the directory
00259      *
00260      *  @param dir      Dir handle
00261      */
00262     virtual void dir_rewind(mbed::fs_dir_t dir);
00263 
00264 private:
00265     FATFS _fs; // Work area (file system object) for logical drive
00266     char _fsid[sizeof("0:")];
00267     int _id;
00268 
00269 protected:
00270     virtual void lock();
00271     virtual void unlock();
00272     virtual int mount(BlockDevice *bd, bool mount);
00273 };
00274 
00275 #endif