mFS file system library for EEPROM memory chips.

Committer:
HBP
Date:
Tue Feb 22 21:39:41 2011 +0000
Revision:
11:6c4fcb9d6193
Parent:
10:211cb54339a0
Child:
12:928346513c87
Oops there was a bug on mkfs and removefile :P

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 11:6c4fcb9d6193 53 * would technically allow 256 TB volume sizes)
HBP 10:211cb54339a0 54 * * Optimized file searching algorithms
HBP 11:6c4fcb9d6193 55 * 0.6 2011-02-22 Olli Vanhoja * Fixed file remove issue
HBP 11:6c4fcb9d6193 56 * * Fixed mkfs bug
HBP 7:5ac5121bb4e0 57 *
HBP 7:5ac5121bb4e0 58 * TODO :
HBP 10:211cb54339a0 59 * * Directory support (VOL blocks?)
HBP 10:211cb54339a0 60 * * Support for >256 blocks
HBP 7:5ac5121bb4e0 61 *H*/
HBP 0:cbf45dde2b49 62
HBP 0:cbf45dde2b49 63 #ifndef MFS_H
HBP 0:cbf45dde2b49 64 #define MFS_H
HBP 0:cbf45dde2b49 65
HBP 0:cbf45dde2b49 66 #include "i2c_eeprom.h"
HBP 0:cbf45dde2b49 67
HBP 6:dd3346914d42 68 const unsigned int VOL_SIZE=65536; /**< EEPROM chip size in bytes */
HBP 6:dd3346914d42 69 const unsigned int BS=1024; /**< How many bytes per block (default: 4096 bytes) */
HBP 10:211cb54339a0 70 const unsigned int BC=VOL_SIZE / BS; // block count
HBP 0:cbf45dde2b49 71 const char mEOF='\x01'; // End Of File/Section marked
HBP 6:dd3346914d42 72 const unsigned int BUF=400; /**< File buffer length */
HBP 0:cbf45dde2b49 73
HBP 3:1cbc15648de1 74 /** mFS File System class
HBP 3:1cbc15648de1 75 *
HBP 3:1cbc15648de1 76 * This class is used as a handle for the fs in use.
HBP 3:1cbc15648de1 77 */
HBP 0:cbf45dde2b49 78 class mfs {
HBP 0:cbf45dde2b49 79 private:
HBP 0:cbf45dde2b49 80 i2c_eeprom *mem; // Only 512 kB I2C EEPROM is supported ATM
HBP 0:cbf45dde2b49 81 public:
HBP 3:1cbc15648de1 82 /** Create a new file system object
HBP 3:1cbc15648de1 83 *
HBP 3:1cbc15648de1 84 * @param xi2c_address a Physical I2C address of the EEPROM chip
HBP 3:1cbc15648de1 85 */
HBP 0:cbf45dde2b49 86 mfs(int i2c_address);
HBP 5:a0fe74dce80d 87
HBP 10:211cb54339a0 88 /** Read data from specified fs block
HBP 3:1cbc15648de1 89 *
HBP 3:1cbc15648de1 90 * @param *data Pointer for readed data
HBP 3:1cbc15648de1 91 * @param block Block number.
HBP 3:1cbc15648de1 92 * @param byte Selected byte.
HBP 3:1cbc15648de1 93 * @param n Bytes to be read.
HBP 10:211cb54339a0 94 * @returns Error code: 0 = OK, 1 = Incorrect input
HBP 3:1cbc15648de1 95 */
HBP 10:211cb54339a0 96 char read(char *data, uint32_t block, uint32_t byte, uint32_t n);
HBP 5:a0fe74dce80d 97
HBP 10:211cb54339a0 98 /** Write data to specified fs block
HBP 3:1cbc15648de1 99 *
HBP 3:1cbc15648de1 100 * @param *data Pointer for readed data
HBP 3:1cbc15648de1 101 * @param block Block number.
HBP 3:1cbc15648de1 102 * @param byte Selected byte.
HBP 3:1cbc15648de1 103 * @param n Bytes to be read.
HBP 10:211cb54339a0 104 * @returns Error code: 0 = OK, 1 = Incorrect input
HBP 3:1cbc15648de1 105 */
HBP 10:211cb54339a0 106 char write(char *data, uint32_t block, uint32_t byte, uint32_t n);
HBP 5:a0fe74dce80d 107
HBP 3:1cbc15648de1 108 /** Locate next free block
HBP 3:1cbc15648de1 109 *
HBP 3:1cbc15648de1 110 * @param *blockOut Returns next free block from begining of the fs.
HBP 10:211cb54339a0 111 * @returns Error code: 0 = OK, 1 = Out of space
HBP 3:1cbc15648de1 112 */
HBP 10:211cb54339a0 113 char getNextFreeBlock(uint32_t *blockOut);
HBP 5:a0fe74dce80d 114
HBP 3:1cbc15648de1 115 /** Locates next starting file from parameter block
HBP 3:1cbc15648de1 116 *
HBP 3:1cbc15648de1 117 * @param block Start scanning from this block.
HBP 3:1cbc15648de1 118 * @param *filenameOut Return name of the file found.
HBP 10:211cb54339a0 119 * @param Returns block number of the file found.
HBP 10:211cb54339a0 120 * @returns Error code: 0 = OK, 1 = Empty fs
HBP 3:1cbc15648de1 121 */
HBP 10:211cb54339a0 122 char findNextFile(uint32_t block, char *filenameOut, uint32_t *blockOut);
HBP 5:a0fe74dce80d 123
HBP 10:211cb54339a0 124 /** Get block number of the given file
HBP 10:211cb54339a0 125 *
HBP 10:211cb54339a0 126 * Returns block number of the block flaged with FBOF flag.
HBP 3:1cbc15648de1 127 *
HBP 3:1cbc15648de1 128 * @param filename[20] Filename input.
HBP 10:211cb54339a0 129 * @param Returns block number of the first block of the given file.
HBP 10:211cb54339a0 130 * @returns Error code: 0 = OK, 1 = File not found
HBP 3:1cbc15648de1 131 */
HBP 10:211cb54339a0 132 char getFirstBlockOfFile(char filename[20], uint32_t *blockOut);
HBP 5:a0fe74dce80d 133
HBP 4:c77812997c9c 134 /** Create a new empty file
HBP 3:1cbc15648de1 135 *
HBP 3:1cbc15648de1 136 * Reserves one block for the file created.
HBP 3:1cbc15648de1 137 *
HBP 3:1cbc15648de1 138 * @param filename[20] Filename input.
HBP 10:211cb54339a0 139 * @returns Error code: 0 = OK, 1 = File exists already, 2 = Out of space
HBP 3:1cbc15648de1 140 */
HBP 0:cbf45dde2b49 141 char createFile(char filename[20]);
HBP 5:a0fe74dce80d 142
HBP 10:211cb54339a0 143 /** Remove a file from the file system
HBP 3:1cbc15648de1 144 *
HBP 3:1cbc15648de1 145 * @param filename[20] Filename input.
HBP 10:211cb54339a0 146 * @returns Error code: 0 = OK, 1 = File doesn't exists
HBP 3:1cbc15648de1 147 */
HBP 0:cbf45dde2b49 148 char removeFile(char filename[20]);
HBP 5:a0fe74dce80d 149
HBP 10:211cb54339a0 150 /** Rename a file
HBP 7:5ac5121bb4e0 151 *
HBP 10:211cb54339a0 152 * @param oldFilename[20] Old filename.
HBP 10:211cb54339a0 153 * @param newFilename[20] New file name.
HBP 10:211cb54339a0 154 * @returns Error code: 0 = OK, 1 = File doesn't exists
HBP 7:5ac5121bb4e0 155 */
HBP 7:5ac5121bb4e0 156 char renameFile(char oldFilename[20], char newFilename[20]);
HBP 7:5ac5121bb4e0 157
HBP 3:1cbc15648de1 158 /** Set user modifiable flags.
HBP 3:1cbc15648de1 159 *
HBP 10:211cb54339a0 160 * \code
HBP 3:1cbc15648de1 161 * desc RO|HIDDEN|LOCK
HBP 3:1cbc15648de1 162 * bit 3 2 1
HBP 10:211cb54339a0 163 * \endcode
HBP 3:1cbc15648de1 164 *
HBP 3:1cbc15648de1 165 * @param *flags Flag input
HBP 3:1cbc15648de1 166 * @param filename[20] Filename input.
HBP 10:211cb54339a0 167 * @returns Error code: 0 = OK, 1 = File doesn't exists, 2 = File system is corrupted
HBP 3:1cbc15648de1 168 */
HBP 0:cbf45dde2b49 169 char setFileFlags(char *flags, char filename[20]);
HBP 5:a0fe74dce80d 170
HBP 3:1cbc15648de1 171 /** Read user modifiable flags.
HBP 3:1cbc15648de1 172 *
HBP 10:211cb54339a0 173 * \code
HBP 3:1cbc15648de1 174 * desc RO|HIDDEN|LOCK
HBP 3:1cbc15648de1 175 * bit 3 2 1
HBP 10:211cb54339a0 176 * \endcode
HBP 3:1cbc15648de1 177 *
HBP 3:1cbc15648de1 178 * @param *flags Flag output
HBP 3:1cbc15648de1 179 * @param filename[20] Filename input.
HBP 10:211cb54339a0 180 * @returns Error code: 0 = OK, 1 = File doesn't exists
HBP 3:1cbc15648de1 181 */
HBP 0:cbf45dde2b49 182 char getFileFlags(char *flags, char filename[20]);
HBP 5:a0fe74dce80d 183
HBP 3:1cbc15648de1 184 /** Get number of free blocks
HBP 3:1cbc15648de1 185 *
HBP 3:1cbc15648de1 186 * @returns Number of free blocks.
HBP 3:1cbc15648de1 187 */
HBP 10:211cb54339a0 188 uint32_t free();
HBP 5:a0fe74dce80d 189
HBP 3:1cbc15648de1 190 /** Format new file system
HBP 3:1cbc15648de1 191 *
HBP 10:211cb54339a0 192 * \note Keep in mind that only first byte is checked for functionality and
HBP 10:211cb54339a0 193 * if it's broken the who file system is useless.
HBP 3:1cbc15648de1 194 *
HBP 3:1cbc15648de1 195 * @param createLabel Create volume label at the begining of the file system. (there is no specified use for volume labels atm).
HBP 10:211cb54339a0 196 * @returns Number of bad block headers.
HBP 3:1cbc15648de1 197 */
HBP 10:211cb54339a0 198 uint32_t mkfs(bool createLabel);
HBP 0:cbf45dde2b49 199 };
HBP 0:cbf45dde2b49 200
HBP 4:c77812997c9c 201 /** mFS File handle class
HBP 4:c77812997c9c 202 *
HBP 10:211cb54339a0 203 * This class provides a file handle and data manipulation methods to be
HBP 10:211cb54339a0 204 * used for files stored in mFS files system.
HBP 4:c77812997c9c 205 */
HBP 0:cbf45dde2b49 206 class file {
HBP 0:cbf45dde2b49 207 private:
HBP 0:cbf45dde2b49 208 mfs *fs; // Reference to the file system in use
HBP 0:cbf45dde2b49 209 char attr; // RW = 1; RO = 0
HBP 0:cbf45dde2b49 210 char buffer[BUF]; // Write buffer
HBP 9:52c01cb100ac 211 uint32_t bufPos; // "Cursor" position in buffer
HBP 0:cbf45dde2b49 212 char firstBlock; // First block of the file
HBP 0:cbf45dde2b49 213 char currBlock; // Current block in use
HBP 9:52c01cb100ac 214 uint32_t blockPos; // "head" position on the current block
HBP 0:cbf45dde2b49 215 public:
HBP 10:211cb54339a0 216 /** Create file handle
HBP 3:1cbc15648de1 217 *
HBP 10:211cb54339a0 218 * \warning File must be created before it can be opened!
HBP 10:211cb54339a0 219 * Opening non-existing file will trip the system to error();
HBP 10:211cb54339a0 220 * If read only file is opened in rw mode system will trip to error().
HBP 3:1cbc15648de1 221 *
HBP 3:1cbc15648de1 222 * @param filename[20] Filename input.
HBP 10:211cb54339a0 223 * @param operation 0 = read only, 1 = read and write.
HBP 3:1cbc15648de1 224 */
HBP 3:1cbc15648de1 225 file(mfs *fs_ref, char filename[20], char operation);
HBP 5:a0fe74dce80d 226
HBP 3:1cbc15648de1 227 /** Close file handle
HBP 3:1cbc15648de1 228 *
HBP 3:1cbc15648de1 229 * Flushes the file and closes the handle.
HBP 3:1cbc15648de1 230 */
HBP 0:cbf45dde2b49 231 ~file(); // Close file handle and flush
HBP 5:a0fe74dce80d 232
HBP 3:1cbc15648de1 233 /** Rewind to the start postion of the file
HBP 5:a0fe74dce80d 234 *
HBP 3:1cbc15648de1 235 */
HBP 3:1cbc15648de1 236 void rewind();
HBP 5:a0fe74dce80d 237
HBP 5:a0fe74dce80d 238 /** Rewind n bytes back
HBP 5:a0fe74dce80d 239 *
HBP 5:a0fe74dce80d 240 * @param n Number of blocks to be rewinded.
HBP 10:211cb54339a0 241 * @returns Error code: 0 = OK, 1 = First block of file.
HBP 5:a0fe74dce80d 242 */
HBP 9:52c01cb100ac 243 char rewind(uint32_t n);
HBP 5:a0fe74dce80d 244
HBP 5:a0fe74dce80d 245 /** Forward one byte
HBP 5:a0fe74dce80d 246 *
HBP 10:211cb54339a0 247 * @returns Error code: 0 = OK, 1 = End of file.
HBP 3:1cbc15648de1 248 */
HBP 3:1cbc15648de1 249 char forward();
HBP 5:a0fe74dce80d 250
HBP 5:a0fe74dce80d 251 /** Forward n bytes
HBP 5:a0fe74dce80d 252 *
HBP 5:a0fe74dce80d 253 * @param n Number of blocks.
HBP 10:211cb54339a0 254 * @returns Error code: 0 = OK, 1 = End of file.
HBP 5:a0fe74dce80d 255 */
HBP 9:52c01cb100ac 256 char forward(uint32_t n);
HBP 5:a0fe74dce80d 257
HBP 3:1cbc15648de1 258 /** Reads a string of bytes
HBP 3:1cbc15648de1 259 *
HBP 3:1cbc15648de1 260 * Always places '\0' at the end of string.
HBP 3:1cbc15648de1 261 *
HBP 3:1cbc15648de1 262 * @param data Output buffer.
HBP 3:1cbc15648de1 263 * @param n Number of bytes to be read.
HBP 5:a0fe74dce80d 264 * @returns Error code. 0 = OK, 1 = Last block of the file
HBP 3:1cbc15648de1 265 */
HBP 9:52c01cb100ac 266 void read(char *data, uint32_t n);
HBP 3:1cbc15648de1 267 /** Reads a binary array of bytes
HBP 3:1cbc15648de1 268 *
HBP 3:1cbc15648de1 269 * Doesn't add '\0' at the end of data array and doesn't respect mEOF byte.
HBP 3:1cbc15648de1 270 *
HBP 3:1cbc15648de1 271 * @param data Output buffer.
HBP 3:1cbc15648de1 272 * @param n Number of bytes to be read.
HBP 3:1cbc15648de1 273 */
HBP 5:a0fe74dce80d 274
HBP 9:52c01cb100ac 275 void readBin(char *data, uint32_t n);
HBP 3:1cbc15648de1 276 /** Write byte array to a file (buffer)
HBP 3:1cbc15648de1 277 *
HBP 3:1cbc15648de1 278 * @param data Input data.
HBP 3:1cbc15648de1 279 * @param n Number of bytes to be read.
HBP 10:211cb54339a0 280 * @returns Error code: 0 = OK, 1 = Flush failed.
HBP 3:1cbc15648de1 281 */
HBP 5:a0fe74dce80d 282
HBP 9:52c01cb100ac 283 char write(char *data, uint32_t n);
HBP 5:a0fe74dce80d 284
HBP 3:1cbc15648de1 285 /** Flush file buffer
HBP 3:1cbc15648de1 286 * Writes buffer to the EEPROM chip in use.
HBP 3:1cbc15648de1 287 */
HBP 0:cbf45dde2b49 288 char flush();
HBP 0:cbf45dde2b49 289 };
HBP 0:cbf45dde2b49 290
HBP 0:cbf45dde2b49 291 #endif