Rahul Dahiya / Mbed OS STM32F7 Ethernet
Committer:
rahul_dahiya
Date:
Wed Jan 15 15:57:15 2020 +0530
Revision:
0:fb8047b156bb
STM32F7 LWIP

Who changed what in which revision?

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