mFS file system library for EEPROM memory chips.

Committer:
HBP
Date:
Tue Feb 22 21:09:04 2011 +0000
Revision:
10:211cb54339a0
Parent:
9:52c01cb100ac
Child:
11:6c4fcb9d6193
Code optimizations and improvements in documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HBP 5:a0fe74dce80d 1 /** @file mfs.h */
HBP 0:cbf45dde2b49 2 /*H****************************************************************************
HBP 7:5ac5121bb4e0 3 * FILENAME : mfs.h *
HBP 7:5ac5121bb4e0 4 * *
HBP 7:5ac5121bb4e0 5 * DESCRIPTION : *
HBP 7:5ac5121bb4e0 6 * mFS file system implementation for mBED with external I2C EEEPROM. *
HBP 8:e67733ad4427 7 * *
HBP 8:e67733ad4427 8 * ---------------------------------------------------------------------------*
HBP 8:e67733ad4427 9 * "THE BEER-WARE LICENSE" (Revision 42): *
HBP 8:e67733ad4427 10 * <olli.vanhoja@gmail.com> wrote this file. As long as you retain this notice*
HBP 8:e67733ad4427 11 * you can do whatever you want with this stuff. If we meet some day, and you *
HBP 8:e67733ad4427 12 * think this stuff is worth it, you can buy me a beer in return Olli Vanhoja *
HBP 8:e67733ad4427 13 * ---------------------------------------------------------------------------*
HBP 8:e67733ad4427 14 * *
HBP 8:e67733ad4427 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
HBP 8:e67733ad4427 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
HBP 8:e67733ad4427 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
HBP 8:e67733ad4427 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
HBP 8:e67733ad4427 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
HBP 8:e67733ad4427 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
HBP 8:e67733ad4427 21 * DEALINGS IN THE SOFTWARE. *
HBP 8:e67733ad4427 22 * *
HBP 8:e67733ad4427 23 * *
HBP 8:e67733ad4427 24 * Block Flags: *
HBP 8:e67733ad4427 25 * 7:FBOF Begining of file *
HBP 8:e67733ad4427 26 * 6:LBOF Last block of file *
HBP 8:e67733ad4427 27 * 5:RO Read only file (Used only with FBOF) *
HBP 8:e67733ad4427 28 * 4:HIDDEN Hidden file (Used only with FBOF) *
HBP 8:e67733ad4427 29 * 3:INUSE Block in use *
HBP 8:e67733ad4427 30 * 2:NBAD Bad block (INV) *
HBP 8:e67733ad4427 31 * 1:VOL Volume label (Used only with FBOF) *
HBP 8:e67733ad4427 32 * 0:LOCK Locked file (Used only with FBOF) *
HBP 8:e67733ad4427 33 * *
HBP 8:e67733ad4427 34 * AUTHOR : Olli Vanhoja START DATE : 2011-02-18 *
HBP 8:e67733ad4427 35 ******************************************************************************
HBP 7:5ac5121bb4e0 36 *
HBP 7:5ac5121bb4e0 37 * CHANGES :
HBP 7:5ac5121bb4e0 38 *
HBP 7:5ac5121bb4e0 39 * VERSION DATE WHO DETAIL
HBP 7:5ac5121bb4e0 40 * 0.1 2011-02-21 Olli Vanhoja Initial release version
HBP 7:5ac5121bb4e0 41 * 0.2 2011-02-21 Olli Vanhoja Documentational comments added
HBP 10:211cb54339a0 42 * 0.3 2011-02-21 Olli Vanhoja * File::read issues fixed, rewind/forward
HBP 10:211cb54339a0 43 * functions improved
HBP 10:211cb54339a0 44 * * Added possibility change I2C speed
HBP 10:211cb54339a0 45 * * I2C autoreset on failure
HBP 10:211cb54339a0 46 * 0.4 2011-02-22 Olli Vanhoja * mfs::renameFile(char [20], char [20] function added
HBP 10:211cb54339a0 47 * * Incresed fault tolerance by first allocating new
HBP 10:211cb54339a0 48 * block and then linking to it from previous block
HBP 10:211cb54339a0 49 * * Reconstructed initialization and use of some variables
HBP 10:211cb54339a0 50 * 0.5 2011-02-22 Olli Vanhoja * Improved documentation
HBP 10:211cb54339a0 51 * * Block variables changed from char to uint32_t for
HBP 10:211cb54339a0 52 * code optimization (and for possible 16bit update which
HBP 10:211cb54339a0 53 would technically allow 256 TB volume sizes)
HBP 10:211cb54339a0 54 * * Optimized file searching algorithms
HBP 7:5ac5121bb4e0 55 *
HBP 7:5ac5121bb4e0 56 * TODO :
HBP 10:211cb54339a0 57 * * Directory support (VOL blocks?)
HBP 10:211cb54339a0 58 * * Support for >256 blocks
HBP 7:5ac5121bb4e0 59 *H*/
HBP 0:cbf45dde2b49 60
HBP 0:cbf45dde2b49 61 #ifndef MFS_H
HBP 0:cbf45dde2b49 62 #define MFS_H
HBP 0:cbf45dde2b49 63
HBP 0:cbf45dde2b49 64 #include "i2c_eeprom.h"
HBP 0:cbf45dde2b49 65
HBP 6:dd3346914d42 66 const unsigned int VOL_SIZE=65536; /**< EEPROM chip size in bytes */
HBP 6:dd3346914d42 67 const unsigned int BS=1024; /**< How many bytes per block (default: 4096 bytes) */
HBP 10:211cb54339a0 68 const unsigned int BC=VOL_SIZE / BS; // block count
HBP 0:cbf45dde2b49 69 const char mEOF='\x01'; // End Of File/Section marked
HBP 6:dd3346914d42 70 const unsigned int BUF=400; /**< File buffer length */
HBP 0:cbf45dde2b49 71
HBP 3:1cbc15648de1 72 /** mFS File System class
HBP 3:1cbc15648de1 73 *
HBP 3:1cbc15648de1 74 * This class is used as a handle for the fs in use.
HBP 3:1cbc15648de1 75 */
HBP 0:cbf45dde2b49 76 class mfs {
HBP 0:cbf45dde2b49 77 private:
HBP 0:cbf45dde2b49 78 i2c_eeprom *mem; // Only 512 kB I2C EEPROM is supported ATM
HBP 0:cbf45dde2b49 79 public:
HBP 3:1cbc15648de1 80 /** Create a new file system object
HBP 3:1cbc15648de1 81 *
HBP 3:1cbc15648de1 82 * @param xi2c_address a Physical I2C address of the EEPROM chip
HBP 3:1cbc15648de1 83 */
HBP 0:cbf45dde2b49 84 mfs(int i2c_address);
HBP 5:a0fe74dce80d 85
HBP 10:211cb54339a0 86 /** Read data from specified fs block
HBP 3:1cbc15648de1 87 *
HBP 3:1cbc15648de1 88 * @param *data Pointer for readed data
HBP 3:1cbc15648de1 89 * @param block Block number.
HBP 3:1cbc15648de1 90 * @param byte Selected byte.
HBP 3:1cbc15648de1 91 * @param n Bytes to be read.
HBP 10:211cb54339a0 92 * @returns Error code: 0 = OK, 1 = Incorrect input
HBP 3:1cbc15648de1 93 */
HBP 10:211cb54339a0 94 char read(char *data, uint32_t block, uint32_t byte, uint32_t n);
HBP 5:a0fe74dce80d 95
HBP 10:211cb54339a0 96 /** Write data to specified fs block
HBP 3:1cbc15648de1 97 *
HBP 3:1cbc15648de1 98 * @param *data Pointer for readed data
HBP 3:1cbc15648de1 99 * @param block Block number.
HBP 3:1cbc15648de1 100 * @param byte Selected byte.
HBP 3:1cbc15648de1 101 * @param n Bytes to be read.
HBP 10:211cb54339a0 102 * @returns Error code: 0 = OK, 1 = Incorrect input
HBP 3:1cbc15648de1 103 */
HBP 10:211cb54339a0 104 char write(char *data, uint32_t block, uint32_t byte, uint32_t n);
HBP 5:a0fe74dce80d 105
HBP 3:1cbc15648de1 106 /** Locate next free block
HBP 3:1cbc15648de1 107 *
HBP 3:1cbc15648de1 108 * @param *blockOut Returns next free block from begining of the fs.
HBP 10:211cb54339a0 109 * @returns Error code: 0 = OK, 1 = Out of space
HBP 3:1cbc15648de1 110 */
HBP 10:211cb54339a0 111 char getNextFreeBlock(uint32_t *blockOut);
HBP 5:a0fe74dce80d 112
HBP 3:1cbc15648de1 113 /** Locates next starting file from parameter block
HBP 3:1cbc15648de1 114 *
HBP 3:1cbc15648de1 115 * @param block Start scanning from this block.
HBP 3:1cbc15648de1 116 * @param *filenameOut Return name of the file found.
HBP 10:211cb54339a0 117 * @param Returns block number of the file found.
HBP 10:211cb54339a0 118 * @returns Error code: 0 = OK, 1 = Empty fs
HBP 3:1cbc15648de1 119 */
HBP 10:211cb54339a0 120 char findNextFile(uint32_t block, char *filenameOut, uint32_t *blockOut);
HBP 5:a0fe74dce80d 121
HBP 10:211cb54339a0 122 /** Get block number of the given file
HBP 10:211cb54339a0 123 *
HBP 10:211cb54339a0 124 * Returns block number of the block flaged with FBOF flag.
HBP 3:1cbc15648de1 125 *
HBP 3:1cbc15648de1 126 * @param filename[20] Filename input.
HBP 10:211cb54339a0 127 * @param Returns block number of the first block of the given file.
HBP 10:211cb54339a0 128 * @returns Error code: 0 = OK, 1 = File not found
HBP 3:1cbc15648de1 129 */
HBP 10:211cb54339a0 130 char getFirstBlockOfFile(char filename[20], uint32_t *blockOut);
HBP 5:a0fe74dce80d 131
HBP 4:c77812997c9c 132 /** Create a new empty file
HBP 3:1cbc15648de1 133 *
HBP 3:1cbc15648de1 134 * Reserves one block for the file created.
HBP 3:1cbc15648de1 135 *
HBP 3:1cbc15648de1 136 * @param filename[20] Filename input.
HBP 10:211cb54339a0 137 * @returns Error code: 0 = OK, 1 = File exists already, 2 = Out of space
HBP 3:1cbc15648de1 138 */
HBP 0:cbf45dde2b49 139 char createFile(char filename[20]);
HBP 5:a0fe74dce80d 140
HBP 10:211cb54339a0 141 /** Remove a file from the file system
HBP 3:1cbc15648de1 142 *
HBP 3:1cbc15648de1 143 * @param filename[20] Filename input.
HBP 10:211cb54339a0 144 * @returns Error code: 0 = OK, 1 = File doesn't exists
HBP 3:1cbc15648de1 145 */
HBP 0:cbf45dde2b49 146 char removeFile(char filename[20]);
HBP 5:a0fe74dce80d 147
HBP 10:211cb54339a0 148 /** Rename a file
HBP 7:5ac5121bb4e0 149 *
HBP 10:211cb54339a0 150 * @param oldFilename[20] Old filename.
HBP 10:211cb54339a0 151 * @param newFilename[20] New file name.
HBP 10:211cb54339a0 152 * @returns Error code: 0 = OK, 1 = File doesn't exists
HBP 7:5ac5121bb4e0 153 */
HBP 7:5ac5121bb4e0 154 char renameFile(char oldFilename[20], char newFilename[20]);
HBP 7:5ac5121bb4e0 155
HBP 3:1cbc15648de1 156 /** Set user modifiable flags.
HBP 3:1cbc15648de1 157 *
HBP 10:211cb54339a0 158 * \code
HBP 3:1cbc15648de1 159 * desc RO|HIDDEN|LOCK
HBP 3:1cbc15648de1 160 * bit 3 2 1
HBP 10:211cb54339a0 161 * \endcode
HBP 3:1cbc15648de1 162 *
HBP 3:1cbc15648de1 163 * @param *flags Flag input
HBP 3:1cbc15648de1 164 * @param filename[20] Filename input.
HBP 10:211cb54339a0 165 * @returns Error code: 0 = OK, 1 = File doesn't exists, 2 = File system is corrupted
HBP 3:1cbc15648de1 166 */
HBP 0:cbf45dde2b49 167 char setFileFlags(char *flags, char filename[20]);
HBP 5:a0fe74dce80d 168
HBP 3:1cbc15648de1 169 /** Read user modifiable flags.
HBP 3:1cbc15648de1 170 *
HBP 10:211cb54339a0 171 * \code
HBP 3:1cbc15648de1 172 * desc RO|HIDDEN|LOCK
HBP 3:1cbc15648de1 173 * bit 3 2 1
HBP 10:211cb54339a0 174 * \endcode
HBP 3:1cbc15648de1 175 *
HBP 3:1cbc15648de1 176 * @param *flags Flag output
HBP 3:1cbc15648de1 177 * @param filename[20] Filename input.
HBP 10:211cb54339a0 178 * @returns Error code: 0 = OK, 1 = File doesn't exists
HBP 3:1cbc15648de1 179 */
HBP 0:cbf45dde2b49 180 char getFileFlags(char *flags, char filename[20]);
HBP 5:a0fe74dce80d 181
HBP 3:1cbc15648de1 182 /** Get number of free blocks
HBP 3:1cbc15648de1 183 *
HBP 3:1cbc15648de1 184 * @returns Number of free blocks.
HBP 3:1cbc15648de1 185 */
HBP 10:211cb54339a0 186 uint32_t free();
HBP 5:a0fe74dce80d 187
HBP 3:1cbc15648de1 188 /** Format new file system
HBP 3:1cbc15648de1 189 *
HBP 10:211cb54339a0 190 * \note Keep in mind that only first byte is checked for functionality and
HBP 10:211cb54339a0 191 * if it's broken the who file system is useless.
HBP 3:1cbc15648de1 192 *
HBP 3:1cbc15648de1 193 * @param createLabel Create volume label at the begining of the file system. (there is no specified use for volume labels atm).
HBP 10:211cb54339a0 194 * @returns Number of bad block headers.
HBP 3:1cbc15648de1 195 */
HBP 10:211cb54339a0 196 uint32_t mkfs(bool createLabel);
HBP 0:cbf45dde2b49 197 };
HBP 0:cbf45dde2b49 198
HBP 4:c77812997c9c 199 /** mFS File handle class
HBP 4:c77812997c9c 200 *
HBP 10:211cb54339a0 201 * This class provides a file handle and data manipulation methods to be
HBP 10:211cb54339a0 202 * used for files stored in mFS files system.
HBP 4:c77812997c9c 203 */
HBP 0:cbf45dde2b49 204 class file {
HBP 0:cbf45dde2b49 205 private:
HBP 0:cbf45dde2b49 206 mfs *fs; // Reference to the file system in use
HBP 0:cbf45dde2b49 207 char attr; // RW = 1; RO = 0
HBP 0:cbf45dde2b49 208 char buffer[BUF]; // Write buffer
HBP 9:52c01cb100ac 209 uint32_t bufPos; // "Cursor" position in buffer
HBP 0:cbf45dde2b49 210 char firstBlock; // First block of the file
HBP 0:cbf45dde2b49 211 char currBlock; // Current block in use
HBP 9:52c01cb100ac 212 uint32_t blockPos; // "head" position on the current block
HBP 0:cbf45dde2b49 213 public:
HBP 10:211cb54339a0 214 /** Create file handle
HBP 3:1cbc15648de1 215 *
HBP 10:211cb54339a0 216 * \warning File must be created before it can be opened!
HBP 10:211cb54339a0 217 * Opening non-existing file will trip the system to error();
HBP 10:211cb54339a0 218 * If read only file is opened in rw mode system will trip to error().
HBP 3:1cbc15648de1 219 *
HBP 3:1cbc15648de1 220 * @param filename[20] Filename input.
HBP 10:211cb54339a0 221 * @param operation 0 = read only, 1 = read and write.
HBP 3:1cbc15648de1 222 */
HBP 3:1cbc15648de1 223 file(mfs *fs_ref, char filename[20], char operation);
HBP 5:a0fe74dce80d 224
HBP 3:1cbc15648de1 225 /** Close file handle
HBP 3:1cbc15648de1 226 *
HBP 3:1cbc15648de1 227 * Flushes the file and closes the handle.
HBP 3:1cbc15648de1 228 */
HBP 0:cbf45dde2b49 229 ~file(); // Close file handle and flush
HBP 5:a0fe74dce80d 230
HBP 3:1cbc15648de1 231 /** Rewind to the start postion of the file
HBP 5:a0fe74dce80d 232 *
HBP 3:1cbc15648de1 233 */
HBP 3:1cbc15648de1 234 void rewind();
HBP 5:a0fe74dce80d 235
HBP 5:a0fe74dce80d 236 /** Rewind n bytes back
HBP 5:a0fe74dce80d 237 *
HBP 5:a0fe74dce80d 238 * @param n Number of blocks to be rewinded.
HBP 10:211cb54339a0 239 * @returns Error code: 0 = OK, 1 = First block of file.
HBP 5:a0fe74dce80d 240 */
HBP 9:52c01cb100ac 241 char rewind(uint32_t n);
HBP 5:a0fe74dce80d 242
HBP 5:a0fe74dce80d 243 /** Forward one byte
HBP 5:a0fe74dce80d 244 *
HBP 10:211cb54339a0 245 * @returns Error code: 0 = OK, 1 = End of file.
HBP 3:1cbc15648de1 246 */
HBP 3:1cbc15648de1 247 char forward();
HBP 5:a0fe74dce80d 248
HBP 5:a0fe74dce80d 249 /** Forward n bytes
HBP 5:a0fe74dce80d 250 *
HBP 5:a0fe74dce80d 251 * @param n Number of blocks.
HBP 10:211cb54339a0 252 * @returns Error code: 0 = OK, 1 = End of file.
HBP 5:a0fe74dce80d 253 */
HBP 9:52c01cb100ac 254 char forward(uint32_t n);
HBP 5:a0fe74dce80d 255
HBP 3:1cbc15648de1 256 /** Reads a string of bytes
HBP 3:1cbc15648de1 257 *
HBP 3:1cbc15648de1 258 * Always places '\0' at the end of string.
HBP 3:1cbc15648de1 259 *
HBP 3:1cbc15648de1 260 * @param data Output buffer.
HBP 3:1cbc15648de1 261 * @param n Number of bytes to be read.
HBP 5:a0fe74dce80d 262 * @returns Error code. 0 = OK, 1 = Last block of the file
HBP 3:1cbc15648de1 263 */
HBP 9:52c01cb100ac 264 void read(char *data, uint32_t n);
HBP 3:1cbc15648de1 265 /** Reads a binary array of bytes
HBP 3:1cbc15648de1 266 *
HBP 3:1cbc15648de1 267 * Doesn't add '\0' at the end of data array and doesn't respect mEOF byte.
HBP 3:1cbc15648de1 268 *
HBP 3:1cbc15648de1 269 * @param data Output buffer.
HBP 3:1cbc15648de1 270 * @param n Number of bytes to be read.
HBP 3:1cbc15648de1 271 */
HBP 5:a0fe74dce80d 272
HBP 9:52c01cb100ac 273 void readBin(char *data, uint32_t n);
HBP 3:1cbc15648de1 274 /** Write byte array to a file (buffer)
HBP 3:1cbc15648de1 275 *
HBP 3:1cbc15648de1 276 * @param data Input data.
HBP 3:1cbc15648de1 277 * @param n Number of bytes to be read.
HBP 10:211cb54339a0 278 * @returns Error code: 0 = OK, 1 = Flush failed.
HBP 3:1cbc15648de1 279 */
HBP 5:a0fe74dce80d 280
HBP 9:52c01cb100ac 281 char write(char *data, uint32_t n);
HBP 5:a0fe74dce80d 282
HBP 3:1cbc15648de1 283 /** Flush file buffer
HBP 3:1cbc15648de1 284 * Writes buffer to the EEPROM chip in use.
HBP 3:1cbc15648de1 285 */
HBP 0:cbf45dde2b49 286 char flush();
HBP 0:cbf45dde2b49 287 };
HBP 0:cbf45dde2b49 288
HBP 0:cbf45dde2b49 289 #endif