mFS file system library for EEPROM memory chips.

mfs.h

Committer:
HBP
Date:
2011-02-21
Revision:
0:cbf45dde2b49
Child:
3:1cbc15648de1

File content as of revision 0:cbf45dde2b49:

/*H****************************************************************************
* FILENAME :        mfs.h                                                     *
*                                                                             *
* DESCRIPTION :                                                               *
*       mFS file system implementation for mBED with external I2C EEEPROM.    *
*                                                                             *
* Block Flags:                                                                *
* 7:FBOF      Begining of file                                                *
* 6:LBOF      Last block of file                                              *
* 5:RO        Read only file (Used only with FBOF)                            *
* 4:HIDDEN    Hidden file (Used only with FBOF)                               *
* 3:INUSE     Block in use                                                    *
* 2:NBAD      Bad block (INV)                                                 *
* 1:VOL       Volume label (Used only with FBOF)                              *
* 0:LOCK      Locked file (Used only with FBOF)                               *
*                                                                             *
* AUTHOR :    Olli Vanhoja        START DATE :    2011-02-18                  *
*******************************************************************************
*
* CHANGES :
*
* VERSION DATE       WHO             DETAIL
* 0.1     2011-02-21 Olli Vanhoja    Initial release version
*
*H*/

#ifndef MFS_H
#define MFS_H

#include "i2c_eeprom.h"

const unsigned int VOL_SIZE=40960; // 40 kB EEPROM chip
const unsigned int BS=4096; // 4096 bytes per block
const unsigned int BC=VOL_SIZE / BS; // 128 blocks
const char mEOF='\x01'; // End Of File/Section marked
const unsigned int BUF=400; // file buffer length

typedef unsigned short int uint16;

class mfs {
private:
    i2c_eeprom *mem; // Only 512 kB I2C EEPROM is supported ATM
public:
    mfs(int i2c_address);
    char read(char *data, char block, unsigned int byteOffset, unsigned int n); // Read bytes from block
    char write(char *data, char block, unsigned int byteOffset, unsigned int n); // write bytes to block
    char getNextFreeBlock(char *blockOut); // Reserve next free block from begining
    unsigned int findNextFile(unsigned int block, char *filenameOut); // Returns block number
    uint16 getFirstBlockOfFile(char filename[20]);
    char createFile(char filename[20]);
    char removeFile(char filename[20]);
    char setFileFlags(char *flags, char filename[20]);
    char getFileFlags(char *flags, char filename[20]);
    uint16 free();
    char mkfs(char);
};

class file {
private:
    mfs *fs;   // Reference to the file system in use
    char attr; // RW = 1; RO = 0
    char buffer[BUF]; // Write buffer
    unsigned int bufPos; // "Cursor" position in buffer
    char firstBlock; // First block of the file
    char currBlock; // Current block in use
    unsigned int blockPos; // "head" position on the current block
    void needsFlush(); // check if flush is needed before read operation
public:
    file(mfs *fs_ref, char filename[20], char operation); // Open file handle
    ~file(); // Close file handle and flush
    void rewind(); // Rewind to start postion of the file
    char forward(); // Forward one block
    //char seek(unsigned int byte); // seek to the byte
    void read(char *data, unsigned int n);
    void readBin(char *data, unsigned int n);
    char write(char *data, unsigned int n); // Write byte array
    char flush();
};

#endif