Class that contain only FATFileSystem

Fork of USBFileSystem by Erik -

Committer:
Sissors
Date:
Wed Jul 31 19:15:55 2013 +0000
Revision:
0:dabe3383ef23
Child:
1:4ba08d11e36e
v0.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:dabe3383ef23 1 #ifndef USBFILESYSTEM_H
Sissors 0:dabe3383ef23 2 #define USBFILESYSTEM_H
Sissors 0:dabe3383ef23 3
Sissors 0:dabe3383ef23 4 #include "FATFileSystem.h"
Sissors 0:dabe3383ef23 5 #include "USBMSD.h"
Sissors 0:dabe3383ef23 6 #include "FATFileHandle.h"
Sissors 0:dabe3383ef23 7
Sissors 0:dabe3383ef23 8 class FATFileSystem_ds : public FATFileSystem {
Sissors 0:dabe3383ef23 9 public:
Sissors 0:dabe3383ef23 10 FATFileSystem_ds(const char* n) : FATFileSystem(n) {};
Sissors 0:dabe3383ef23 11
Sissors 0:dabe3383ef23 12 protected:
Sissors 0:dabe3383ef23 13 virtual int disk_status_fat( void ) = 0;
Sissors 0:dabe3383ef23 14 virtual int disk_status( void ) {return disk_status_fat();}
Sissors 0:dabe3383ef23 15 };
Sissors 0:dabe3383ef23 16
Sissors 0:dabe3383ef23 17 class USBMSD_ds : public USBMSD {
Sissors 0:dabe3383ef23 18 public:
Sissors 0:dabe3383ef23 19 USBMSD_ds() {};
Sissors 0:dabe3383ef23 20
Sissors 0:dabe3383ef23 21 protected:
Sissors 0:dabe3383ef23 22 virtual int disk_status_msd( void ) = 0;
Sissors 0:dabe3383ef23 23 virtual int disk_status( void ) {return disk_status_msd();}
Sissors 0:dabe3383ef23 24 };
Sissors 0:dabe3383ef23 25
Sissors 0:dabe3383ef23 26 /** Class which combines FATFileSystem with USBMSD device
Sissors 0:dabe3383ef23 27 *
Sissors 0:dabe3383ef23 28 * The functions to be implemented by the child class are the same as
Sissors 0:dabe3383ef23 29 * those that have to be implemented when using FATFileSystem/USBMSD.
Sissors 0:dabe3383ef23 30 * However there are some caveats: _disk_status() and _disk_write MUST
Sissors 0:dabe3383ef23 31 * be named with the '_' in front of it. You are not allowed to have a
Sissors 0:dabe3383ef23 32 * function without the '_'. Those functions may not be inherited, but
Sissors 0:dabe3383ef23 33 * with this version of C++ I cannot prevent the child from doing so.
Sissors 0:dabe3383ef23 34 *
Sissors 0:dabe3383ef23 35 * Next the size of a disk sector must be 512 bytes. This is because
Sissors 0:dabe3383ef23 36 * FATFileSystem only accepts that. If disk_size is not implemented
Sissors 0:dabe3383ef23 37 * this is done automatically.
Sissors 0:dabe3383ef23 38 *
Sissors 0:dabe3383ef23 39 * What the library does is block write access locally when USB writes,
Sissors 0:dabe3383ef23 40 * and USB write access is blocked when with fopen a file is opened for
Sissors 0:dabe3383ef23 41 * write locally. This way they can't both write at the same time. Read
Sissors 0:dabe3383ef23 42 * access is not blocked.
Sissors 0:dabe3383ef23 43 *
Sissors 0:dabe3383ef23 44 * The final issue to take into account is caching: FATFileSystem will not
Sissors 0:dabe3383ef23 45 * re-open a file if you read twice the same data from it. So if USBMSD
Sissors 0:dabe3383ef23 46 * changed it, it will not show up different. If you close the file and
Sissors 0:dabe3383ef23 47 * call fopen again it will work. The USBMSD part has the
Sissors 0:dabe3383ef23 48 * same issue, but worse. Windows at least will only load everything once!
Sissors 0:dabe3383ef23 49 * So if you open a file, and then change it manually, you will never find
Sissors 0:dabe3383ef23 50 * out until the device is disconnected. If the disk is write protected
Sissors 0:dabe3383ef23 51 * it will not tell you so. It will write/delete what you want, only when
Sissors 0:dabe3383ef23 52 * you reconnect the device will it show it didn't actually do anything!
Sissors 0:dabe3383ef23 53 *
Sissors 0:dabe3383ef23 54 * See the program's wiki for more documentation!
Sissors 0:dabe3383ef23 55 */
Sissors 0:dabe3383ef23 56 class USBFileSystem : public FATFileSystem_ds, public USBMSD_ds {
Sissors 0:dabe3383ef23 57 public:
Sissors 0:dabe3383ef23 58 /**
Sissors 0:dabe3383ef23 59 * Constructor for USBFileSystem
Sissors 0:dabe3383ef23 60 *
Sissors 0:dabe3383ef23 61 * @param n - Name for the file system
Sissors 0:dabe3383ef23 62 */
Sissors 0:dabe3383ef23 63 USBFileSystem(const char* n);
Sissors 0:dabe3383ef23 64
Sissors 0:dabe3383ef23 65 /** Attach a function to be called when locally the filesystem is occupied/freed
Sissors 0:dabe3383ef23 66 *
Sissors 0:dabe3383ef23 67 * The function is called with 'true' as argument when it is freed, false when it is occupied
Sissors 0:dabe3383ef23 68 *
Sissors 0:dabe3383ef23 69 * @param function - Function to be called, must have void as return type, and a single boolean argument
Sissors 0:dabe3383ef23 70 */
Sissors 0:dabe3383ef23 71 void attachLocal(void (*function)(bool));
Sissors 0:dabe3383ef23 72
Sissors 0:dabe3383ef23 73 /** Attach a function to be called when USB occupies/frees the filesystem
Sissors 0:dabe3383ef23 74 *
Sissors 0:dabe3383ef23 75 * The function is called with 'true' as argument when it is freed, false when it is occupied
Sissors 0:dabe3383ef23 76 *
Sissors 0:dabe3383ef23 77 * @param function - Function to be called, must have void as return type, and a single boolean argument
Sissors 0:dabe3383ef23 78 */
Sissors 0:dabe3383ef23 79 void attachUSB(void (*function)(bool));
Sissors 0:dabe3383ef23 80
Sissors 0:dabe3383ef23 81 /** Check if locally files are opened for write
Sissors 0:dabe3383ef23 82 *
Sissors 0:dabe3383ef23 83 * @return - true if none are opened for write, false otherwise
Sissors 0:dabe3383ef23 84 */
Sissors 0:dabe3383ef23 85 bool localSafe( void );
Sissors 0:dabe3383ef23 86
Sissors 0:dabe3383ef23 87 /** Check if via USB files files are being written
Sissors 0:dabe3383ef23 88 *
Sissors 0:dabe3383ef23 89 * @return - true if none are being written, false otherwise
Sissors 0:dabe3383ef23 90 */
Sissors 0:dabe3383ef23 91 bool usbSafe( void );
Sissors 0:dabe3383ef23 92
Sissors 0:dabe3383ef23 93
Sissors 0:dabe3383ef23 94 protected:
Sissors 0:dabe3383ef23 95 //Functions to be implemented by child:
Sissors 0:dabe3383ef23 96 virtual int disk_initialize() { return 0; }
Sissors 0:dabe3383ef23 97 virtual int _disk_status() { return 0; }
Sissors 0:dabe3383ef23 98 virtual int disk_read(uint8_t * buffer, uint64_t sector) { return 0;}
Sissors 0:dabe3383ef23 99 virtual int _disk_write(const uint8_t * buffer, uint64_t sector) = 0;
Sissors 0:dabe3383ef23 100 virtual int disk_sync() { return 0; }
Sissors 0:dabe3383ef23 101 virtual uint64_t disk_sectors() = 0;
Sissors 0:dabe3383ef23 102 virtual uint64_t disk_size() {return 512 * disk_sectors();}
Sissors 0:dabe3383ef23 103
Sissors 0:dabe3383ef23 104 //Not to be implemented by child:
Sissors 0:dabe3383ef23 105 virtual FileHandle* open(const char* name, int flags);
Sissors 0:dabe3383ef23 106 void localOpen( bool open );
Sissors 0:dabe3383ef23 107 void usbFree( void );
Sissors 0:dabe3383ef23 108
Sissors 0:dabe3383ef23 109 private:
Sissors 0:dabe3383ef23 110 virtual int disk_status_fat(void);
Sissors 0:dabe3383ef23 111 virtual int disk_status_msd(void);
Sissors 0:dabe3383ef23 112 virtual int disk_write(const uint8_t * buffer, uint64_t sector);
Sissors 0:dabe3383ef23 113
Sissors 0:dabe3383ef23 114 int local_count;
Sissors 0:dabe3383ef23 115 Timeout usb_write;
Sissors 0:dabe3383ef23 116 bool usbfree;
Sissors 0:dabe3383ef23 117
Sissors 0:dabe3383ef23 118 void (*localFunction)(bool);
Sissors 0:dabe3383ef23 119 void (*usbFunction)(bool);
Sissors 0:dabe3383ef23 120
Sissors 0:dabe3383ef23 121 friend class USBFileHandle;
Sissors 0:dabe3383ef23 122
Sissors 0:dabe3383ef23 123 };
Sissors 0:dabe3383ef23 124
Sissors 0:dabe3383ef23 125
Sissors 0:dabe3383ef23 126 class USBFileHandle : public FATFileHandle {
Sissors 0:dabe3383ef23 127 public:
Sissors 0:dabe3383ef23 128 USBFileHandle(FIL fh, USBFileSystem *p, bool write) : FATFileHandle(fh) {
Sissors 0:dabe3383ef23 129 _p = p;
Sissors 0:dabe3383ef23 130 _write = write;
Sissors 0:dabe3383ef23 131 }
Sissors 0:dabe3383ef23 132
Sissors 0:dabe3383ef23 133 protected:
Sissors 0:dabe3383ef23 134 USBFileSystem *_p;
Sissors 0:dabe3383ef23 135 bool _write;
Sissors 0:dabe3383ef23 136
Sissors 0:dabe3383ef23 137 virtual int close() {
Sissors 0:dabe3383ef23 138 int retval = FATFileHandle::close();
Sissors 0:dabe3383ef23 139 if (_write)
Sissors 0:dabe3383ef23 140 _p->localOpen(false);
Sissors 0:dabe3383ef23 141 return retval;
Sissors 0:dabe3383ef23 142 }
Sissors 0:dabe3383ef23 143 };
Sissors 0:dabe3383ef23 144
Sissors 0:dabe3383ef23 145
Sissors 0:dabe3383ef23 146
Sissors 0:dabe3383ef23 147
Sissors 0:dabe3383ef23 148
Sissors 0:dabe3383ef23 149 #endif