Olli Vanhoja / mFS
Committer:
HBP
Date:
Mon Feb 21 22:37:00 2011 +0000
Revision:
7:5ac5121bb4e0
Parent:
6:dd3346914d42
Child:
8:e67733ad4427
File rename function

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