[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/imote2/tos/lib/FlashLogger BlockStorage.h, NONE, 1.1 FileMount.nc, NONE, 1.1 FileRead.nc, NONE, 1.1 FileStorage.nc, NONE, 1.1 FileWrite.nc, NONE, 1.1 FlashLoggerC.nc, NONE, 1.1 FlashLoggerM.nc, NONE, 1.1 Storage.h, NONE, 1.1

Lama Nachman lnachman at users.sourceforge.net
Tue Oct 10 17:11:11 PDT 2006


Update of /cvsroot/tinyos/tinyos-1.x/contrib/imote2/tos/lib/FlashLogger
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv29170

Added Files:
	BlockStorage.h FileMount.nc FileRead.nc FileStorage.nc 
	FileWrite.nc FlashLoggerC.nc FlashLoggerM.nc Storage.h 
Log Message:
Pushed out new release OCT 2006


--- NEW FILE: BlockStorage.h ---
// $Id: BlockStorage.h,v 1.1 2006/10/11 00:11:09 lnachman Exp $

/*									tab:4
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/*
 * @author: Jonathan Hui <jwhui at cs.berkeley.edu>
 */

#ifndef __BLOCK_STORAGE_H__
#define __BLOCK_STORAGE_H__

typedef uint32_t block_addr_t;
typedef uint8_t blockstorage_t;

#endif

--- NEW FILE: FileMount.nc ---
// $Id: FileMount.nc,v 1.1 2006/10/11 00:11:09 lnachman Exp $

/*									tab:2
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/*
 * @author: Jonathan Hui <jwhui at cs.berkeley.edu>
 *
 * Ported to imote2 by Junaith Ahemed
 */

includes Storage;

interface FileMount 
{
  /**
   * FileMount.fopen
   *
   * Open a file for read / write by passing file name as the parameter.
   * The function calls mount and commit for a particular file and returns
   * SUCCESS | FAIL. Inorder to access the file it is required to wire
   * the BlockRead and BlockWrite interface with the same blockId as the
   * mount interface.
   *
   * @param filename Name of the file to be opened.
   * @return SUCCESS | FAIL
   */
  command result_t fopen (const uint8_t* filename);

  /**
   * FileMount.fclose
   *
   * 
   */
  command result_t fclose (const uint8_t* filename);

  /**
   * Mount.mount
   *
   * Mount a file for reading / writing. The function takes volumeId of
   * a file as paramter which could be obtained through FormatStorage interface. 
   * It is required to  wire the BlockRead and BlockWrite interface with the 
   * same blockId as the mount interface.
   * 
   * @param id Volume id of a file.
   * @return SUCCESS | FAIL
   */  
  command result_t mount(volume_id_t id);

  /**
   * Mount.mountDone
   *
   * Event generated for a mount or an fopen call from the SectorStorage
   * module. The first parameter will be the success code and the second is
   * the volume id of the file.
   * 
   * @param result STORAGE_OK | STORAGE_FAIL
   */  
  event void mountDone(storage_result_t result, volume_id_t id);
}

--- NEW FILE: FileRead.nc ---
// $Id: FileRead.nc,v 1.1 2006/10/11 00:11:09 lnachman Exp $

/*									tab:2
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/*
 * @author: Jonathan Hui <jwhui at cs.berkeley.edu>
 * Ported to Imote2 by Junaith Ahemed
 */

includes BlockStorage;

interface FileRead 
{
  /**
   * BlockRead.read
   *
   * Read data from the mounted file and store it in buf. It is required
   * that the caller should allocate enough memory for <I>buf</I> to hold
   * data of size <I>len</I>.
   * The starting logical address has to be passed as the first parameter.
   *
   * @param addr Virtual address ranging from 0x0 to SIZE_OF_FILE
   * @param buf Buffer in which the file data will be stored.
   * @param len Number of bytes to be read from the file.
   * 
   * @return SUCCESS | FAIL
   */
  command result_t read(block_addr_t addr, void* buf, block_addr_t len);
  event void readDone(storage_result_t result, block_addr_t addr, 
                      void* buf, block_addr_t len);
  /**
   * BlockRead.fread
   *
   * Read data from the mounted file and store it in buf. It is required
   * that the caller should allocate enough memory for <I>buf</I> to hold
   * data of size <I>len</I>. The read starts from the current read pointer
   * location.
   *
   * @param buf Buffer in which the file data will be stored.
   * @param len Number of bytes to be read from the file.
   *
   * @return SUCCESS | FAIL
   */
  command result_t fread(void* buf, block_addr_t len);

  /**
   * BlockRead.verify
   *
   * NOTE IMPLEMENTED
   * FIXME Should be removed.
   */
  command result_t verify();
  event void verifyDone(storage_result_t result);

  /**
   * BlockRead.computeCrc
   * 
   * Compute CRC of a given section of the file or the whole file based on
   * <I>addr</I> and <I>len</I>.
   * 
   * @return SUCCESS | FAIL
   */
  command result_t computeCrc(block_addr_t addr, block_addr_t len);
  event void computeCrcDone(storage_result_t result, uint16_t crc, 
                            block_addr_t addr, block_addr_t len);

  /**
   * BlockRead.getSize
   * 
   * The function returns the size of the currently mounted file. The
   * function calls getVolumeSize of SectorStorage which calculate
   * the file size based on the number of blocks allocated to the file.
   *
   * @return size The size of the file.
   */  
  command block_addr_t getSize();

  /**
   * BlockRead.getReadPtr
   *
   * Function returns the current read pointer for the mounted
   * file. Note that the read pointer is a ram variable and will
   * be automatically reset when the system restarts.
   *
   * @return readPtr Logical address of the Read Pointer.
   */  
  command block_addr_t getReadPtr ();

  /**
   * BlockRead.resetReadPtr
   *
   * Reset the logical address of the read pointer to 0x0 for the
   * file mounted using blockId.
   *
   * @return SUCCESS | FAIL
   */  
  command result_t resetReadPtr ();

  /**
   * BlockRead.rseek
   *
   * Move the read pointer to a given location within the file. The
   * first parameter should range from 0x0 to Size_of_file.
   *
   * @param addr Virtual address ranging from 0x0 to SIZE_OF_FILE
   * 
   * @return SUCCESS | FAIL
   */  
  command result_t rseek (block_addr_t addr);
}

--- NEW FILE: FileStorage.nc ---
// $Id: FileStorage.nc,v 1.1 2006/10/11 00:11:09 lnachman Exp $

/*									tab:2
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/*
 * @author: Jonathan Hui <jwhui at cs.berkeley.edu>
 */

includes Storage;

interface FileStorage 
{
  command result_t fcreate(const uint8_t* filename,
                          storage_addr_t size);

  /**
   * FormatStorage.allocate
   *
   * This is external interface function exposed to the user for allocating 
   * space for a new file.
   * The funciton uses the allocate routine to check for space availability 
   * and makes sure that the file name is unique.
   * If the allocation is successful then the meta data for the newly created 
   * file is added to the uncommited list, waiting for the user to call commit.
   *
   * @param id VolumeId for the new file.
   * @parma size File size in multiples of block size.
   * @param name Name of the new file.
   *
   * @return SUCCESS | FAIL
   */
  command result_t allocate(volume_id_t id, 
                            storage_addr_t size,
                            const uint8_t* name);

  /**
   * FormatStorage.allocateFixed
   *
   * This is a legacy function, it will work but doesnt make
   * the job any easier for the user.
   * FIXME Remove this function if the app doesnt use it.
   *
   * @param id VolumeId for the new file.
   * @parma addr Starting physical address for the file.
   * @parma size File size in multiples of block size.
   *
   * @return SUCCESS | FAIL
   */
  command result_t allocateFixed(volume_id_t id, 
                                 storage_addr_t addr, 
                                 storage_addr_t size);
  /**
   * FormatStorage.commit
   *
   * The file created has to be commited using this function for
   * completing creation of the file.
   *
   * @return SUCCESS | FAIL
   */
  command result_t commit();

  event void commitDone(storage_result_t result);

  /**
   * FormatStorage.fdelete
   *
   * Delete a file by passing its file name as the first parameter. The
   * function clears the entries in the sector table, removes the
   * meta data from the linked list and invalidates the meta data entry
   * for the file. The file blocks are not erased because every create
   * will clean up the blocks during allocation.
   *
   * @param filename Name of the file to be deleted.
   * @return SUCCESS | FAIL
   */
  command result_t fdelete (const uint8_t* filename);

 /**
  * FormatStorage.init
  *
  * This function initalizes the logger file system, by intializing
  * the global variables, the metadata linked list, the sector
  * table block and meta data block.
  *
  * @return SUCCESS | FAIL
  */
  command result_t init();

  /**
   * FileStorage.getNextId
   *
   * Generate the Next Volume id for creating a new file. The current
   * method is to scan through a valid set of id's to identify an
   * unused volume id.
   *
   * @return Unique volume id for file creation.
   */
  command volume_id_t getNextId();

  /**
   * FileStorage.cleanAllFiles
   *
   * The function deletes all the files, cleans the sector table, erases
   * the meta data block and deletes the linked list of valid files.
   * The whole file system is completely reset.
   *
   * @return SUCCESS | FAIL
   */  
  command result_t cleanAllFiles ();

  /**
   * FileStorage.getFileCount
   *
   * Returns the number of valid files in the current context. The
   * function scans the list of valid files to get the count.
   *
   * @return Number of valid files.
   */  
  command volume_id_t getFileCount();

  /**
   * FileStorage.getFileName
   *
   * The function returns the file name of the requested index in
   * the list of valid files. The index must range from 0 - CurrNumFiles.
   * This function is usefull for listing the names of the currently
   * valid files.
   *
   * @param indx Index in the file list, range from 0 - NumFiles.
   *
   * @return SUCCESS | FAIL
   */  
  command char* getFileName(uint16_t indx);

  /**
   * FileStorage.isFileMounted
   *
   * Check if a file is open or closed using its file name.
   * The functions scans the list of valid files and returns
   * the mount status on a name match. If the file name
   * does not exist in the list then it returns TRUE to prevent
   * mounting of a wrong file.
   *
   * FIXME Needs a new error code for unknown file.
   *
   * @param filename Name of the file for which the mount status is required.
   *
   * @return status TRUE | FALSE
   */  
  command bool isFileMounted (const char* filename);

  /**
   * FormatStorage.getSectorTable
   *
   * Sector table is the back bone of the flash logger file system,
   * it keeps track of the correlation between the volumeId and
   * the block number.
   * The sector table is populated during the init process and is
   * used by StorageManger module for different file operations. This
   * function serves as an access routine to get access to the
   * sector table.
   *
   * @return sectorTable The current valid sector table after file system init.
   */
  command SectorTable* getSectorTable();

  /**
   * FormatStorage.getVolumeId
   *
   * The function is more for utility purposes at the higher level since
   * the VolumeId is hidden from the user level. The function returns
   * the volume id of a file given the file name.
   *
   * @param filename Name of the file.
   *
   * @return SUCCESS | FAIL
   */
  command volume_id_t getVolumeId (const uint8_t* filename);

  /**
   * FormatStorage.updateWritePtr
   *
   * This function will update the write pointer in the meta data
   * entry of a particular file. For every write operation the 
   * write pointer is added with the corresponding length and a
   * new entry is added to the meta data block after invalidating
   * the old one for the file. The write pointer is the virtual
   * address of the current write location in the file.
   *
   * @param id VolumeId for the new file.
   * @parma vaddr Current virtual address for writing.
   * @param vlen Length of data written in to the file.
   *
   * @return SUCCESS | FAIL
   */
  command result_t updateWritePtr (volume_id_t id,
                                   storage_addr_t vaddr,
                                   storage_addr_t vlen);

  /**
   * FileStorage.getWritePtr
   *
   * The function returns the current logical address of the write pointer
   * for a given file name. The logical address could range from 0 - Allocated 
   * Size of the file. The logical position also represents the number of
   * bytes written to the file. If the file name does not exist in the
   * valid file list then an INVALID_PTR will be returned.
   *
   * @param filename Name of the file for which the write pointer is requested.
   *
   * @return Current write pointer or INVALID_PTR
   */  
  command storage_addr_t getWritePtr (const uint8_t* filename);

  /**
   * FileStorage.getWritePtr1
   *
   * The function returns the current logical address of the write pointer
   * for a given volume id. The logical address could range from 0 - Allocated 
   * Size of the file. The logical position also represents the number of
   * bytes written to the file. If the requested volme id does not exist in the
   * valid file list then an INVALID_PTR will be returned.
   *
   * @param filename Name of the file for which the write pointer is requested.
   *
   * @return Current write pointer or INVALID_PTR
   */  
  command storage_addr_t getWritePtr1 (volume_id_t id);

  /**
   * FileStorage.getReadPtr
   *
   * The function returns the current read pointer for a give file. Read
   * pointer is a logical position in the file based on the previous read
   * calls and the length of data read. The value of read pointer represents
   * the number of bytes read from the file after the file was opened.
   * If the file name does not exist in the valid file list then an INVALID_PTR
   * will be returned.
   *
   * @parma filename Name of the file for which the read pointer is required.
   *
   * @return Current read pointer of the file or INVALID_PTR
   */
  command storage_addr_t getReadPtr (const uint8_t* filename);

  /**
   * FileStorage.getReadPtr1
   *
   * The function returns the current read pointer for a give file. Read
   * pointer is a logical position in the file based on the previous read
   * calls and the length of data read. The value of read pointer represents
   * the number of bytes read from the file after the file was opened.
   * If the volume id does not exist in the valid file list then an INVALID_PTR
   * will be returned.
   *
   * @parma id VolumeId of the file for which the read pointer is required.
   *
   * @return Current read pointer of the file or INVALID_PTR
   */  
  command storage_addr_t getReadPtr1 (volume_id_t id);

  /**
   * FormatStorage.updateReadPtr
   *
   * The function updates the read pointer of a file in its meta
   * data entry. The read pointer is maintained only in the linked
   * list entry and is not update to the meta data block, it will
   * be reset to 0x0 during reset. The function returns fail if
   * the volume id is invalid.
   *
   * @param id VolumeId for the file.
   * @parma vaddr Current virtual address for reading.
   * @param vlen Length of data read from the file.
   *
   * @return SUCCESS | FAIL
   */
  command result_t updateReadPtr (volume_id_t id,
                                   storage_addr_t vaddr,
                                   storage_addr_t vlen);
}

--- NEW FILE: FileWrite.nc ---
// $Id: FileWrite.nc,v 1.1 2006/10/11 00:11:09 lnachman Exp $

/*									tab:4
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/*
 * @author: Jonathan Hui <jwhui at cs.berkeley.edu>
 * 
 * Ported to Imote2 by Junaith Ahemed
 */

includes BlockStorage;

interface FileWrite 
{
  /**
   * BlockWrite.write
   * 
   * Data from <I>buf</I> is written to a mounted file for given length <I>len</I>.
   * The logical starting address should be passed as the first parameter to the
   * function. addr + len should be less than the size of the file or the funtion will
   * return an error.
   *
   * @param addr Virtual address ranging from 0x0 to SIZE_OF_FILE
   * @param buf Buffer contaning data to be written to the file.
   * @param len Number of bytes to be written to the file.
   *  
   * @return SUCCESS | FAIL
   */
  command result_t write(block_addr_t addr, void* buf, block_addr_t len);

  /**
   * SectorStorage.writeDone
   *
   * Event generated by StorageManager module to notify that the
   * write request is completed.
   *
   * @param result STORAGE_OK | STORAGE_FAIL
   */  
  event void writeDone(storage_result_t result, block_addr_t addr, 
                       void* buf, block_addr_t len);

  /**
   * BlockWrite.erase
   *
   * The blocks allocated for the file will be erased and the write pointer will
   * be set to 0x0. This is useful if a file has to be reused with new data because
   * the current implementation does not allow the manipulation of already used
   * space in a file.
   * 
   * @return SUCCESS | FAIL
   */
  command result_t erase();

  /**
   * SectorStorage.eraseDone
   *
   * Event generated by StorageManager module to notify that the
   * erase request is completed.
   *
   * @param result STORAGE_OK | STORAGE_FAIL
   */  
  event void eraseDone(storage_result_t result);

  /**
   * BlockWrite.commit
   * 
   * NOT IMPLEMENTED. DOES NOT HAVE ANY EFFECT.
   * FIXME Should be removed.
   */  
  command result_t commit();
  event void commitDone(storage_result_t result);

  /**
   * BlockWrite.append
   *
   * Data from <I>buf</I> is appended to a mounted file for given length <I>len</I>.
   * Current write pointer will be used as the logical starting address. len + 
   * currWritePtr should be less than the size of the file or the funtion will
   * return an error.
   * The function performs the book keeping locally and changes the state to
   * S_WRITE.
   *
   * @param buf Buffer contaning data to be written to the file.
   * @param len Number of bytes to be written to the file.
   *  
   * @return SUCCESS | FAIL
   */  
  command result_t append(void* buf, block_addr_t len);

  /**
   * BlockWrite.getWritePtr
   *
   * The function returns the current write pointer for the mounted
   * file.
   *
   * @return WritePtr Current logical address for writing or Write Pointer.
   *
   * @return SUCCESS | FAIL
   */  
  command block_addr_t getWritePtr ();

  /**
   * BlockWrite.resetWritePtr
   *
   * The function will reset the write pointer to 0x0. This is essentially
   * same as erasing the file as resetting the write pointer will allow
   * write operation starting from 0x0, which in turn means that the 
   * blocks has to be prepared for writing new data.
   *
   * NOTE - The content of the file will be lost.
   *
   * @return SUCCESS | FAIL
   */  
  command result_t resetWritePtr ();
}

--- NEW FILE: FlashLoggerC.nc ---
// $Id: FlashLoggerC.nc,v 1.1 2006/10/11 00:11:09 lnachman Exp $

/*									tab:4
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/*
 * @author: Jonathan Hui <jwhui at cs.berkeley.edu>
 * 
 * Ported to Imote2 by Junaith Ahemed Shahahbdeen. The file provides the
 * wiring for the flash logger implementation.
 */

includes BlockStorage;

#ifdef KAJIMA_APP
  includes TestFlashFS;
#endif

configuration FlashLoggerC 
{
  provides 
  {
    interface FileMount [blockstorage_t blockId];
    interface FileRead [blockstorage_t blockId];
    interface FileWrite [blockstorage_t blockId];
  }
}

implementation 
{
  components FlashLoggerM, Main, StorageManagerC, LedsC as Leds;

#ifdef KAJIMA_APP
  components TestFlashFSM, BluSHC, FormatStorageC;
#endif

  FileMount = FlashLoggerM.Mount;
  FileRead = FlashLoggerM.BlockRead;
  FileWrite = FlashLoggerM.BlockWrite;

  Main.StdControl -> StorageManagerC;

  FlashLoggerM.SectorStorage -> StorageManagerC.SectorStorage;
  FlashLoggerM.ActualMount -> StorageManagerC.Mount;
  FlashLoggerM.StorageManager -> StorageManagerC.StorageManager;
  FlashLoggerM.Leds -> Leds;

#ifdef KAJIMA_APP
  Main.StdControl -> TestFlashFSM;

  TestFlashFSM.FormatStorage -> FormatStorageC.FileStorage;
  TestFlashFSM.Leds -> Leds;

  TestFlashFSM.Mount -> FlashLoggerM.Mount[FLASH_INTERFACE];
  TestFlashFSM.BlockWrite -> FlashLoggerM.BlockWrite[FLASH_INTERFACE];
  TestFlashFSM.BlockRead -> FlashLoggerM.BlockRead[FLASH_INTERFACE];

  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FSCreate;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FSClean;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.NumFiles;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.MntFiles;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FSInit;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FWrite;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FErase;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FRead;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FRseek;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FDel;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FList;
  BluSHC.BluSH_AppI[unique("BluSH")] -> TestFlashFSM.FClose;
#endif
}

--- NEW FILE: FlashLoggerM.nc ---
// $Id: FlashLoggerM.nc,v 1.1 2006/10/11 00:11:09 lnachman Exp $

/*									tab:4
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/*
 * @author: Jonathan Hui <jwhui at cs.berkeley.edu>
 *
 * Ported to imote2 by Junaith Ahemed Shahabdeen. The file acts as a user
 * level abstraction for the real implementation of the interfaces.
 * All of the functionalities are actually implemented in SectorStorage
 * module and the FormatStorage module.
 *
 */

module FlashLoggerM 
{
  provides 
  {
    interface FileMount as Mount[blockstorage_t blockId];
    interface FileRead as BlockRead[blockstorage_t blockId];
    interface FileWrite as BlockWrite[blockstorage_t blockId];
  }
  uses 
  {
    interface SectorStorage[blockstorage_t blockId];
    interface FileMount as ActualMount[blockstorage_t blockId];
    interface StorageManager[blockstorage_t blockId];
    interface Leds;
  }
}

implementation 
{
  /*Enumerated values of the states of the flash logger.*/
  enum 
  {
    S_IDLE,
    S_WRITE,
    S_ERASE,
    S_COMMIT,
    S_READ,
    S_VERIFY,
    S_CRC,
  };

  uint8_t state;  /*Current logger state.*/
  uint8_t client; /*Refers to the interface Id*/

  block_addr_t rwAddr, rwLen;
  void* rwBuf;
  uint16_t crc;

  /**
   * Mount.fopen
   *
   * Open a file for read / write by passing file name as the parameter.
   * The function calls mount and commit for a particular file and returns
   * SUCCESS | FAIL. Inorder to access the file it is required to wire
   * the BlockRead and BlockWrite interface with the same blockId as the
   * mount interface.
   *
   * @param filename Name of the file to be opened.
   * @return SUCCESS | FAIL
   */
  command result_t Mount.fopen[blockstorage_t blockId](const uint8_t* filename)
  {
    return call ActualMount.fopen[blockId](filename);
  }

  /**
   * Mount.mount
   *
   * Mount a file for reading / writing. The function takes volumeId of
   * a file as paramter which could be obtained through FormatStorage interface. 
   * It is required to  wire the BlockRead and BlockWrite interface with the 
   * same blockId as the mount interface.
   * 
   * @param id Volume id of a file.
   * @return SUCCESS | FAIL
   */
  command result_t Mount.mount[blockstorage_t blockId](volume_id_t id) 
  {
    return call ActualMount.mount[blockId](id);
  }

  /**
   * Mount.fclose
   *
   */
  command result_t Mount.fclose[blockstorage_t blockId](const uint8_t* filename)
  {
    return call ActualMount.fclose[blockId](filename);
  }  

  /**
   * ActualMount.mountDone
   *
   * Event generated for a mount or an fopen call from the SectorStorage
   * module. The first parameter will be the success code and the second is
   * the volume id of the file.
   * 
   * @param result STORAGE_OK | STORAGE_FAIL
   */
  event void ActualMount.mountDone[blockstorage_t blockId](storage_result_t result, volume_id_t id) 
  {
    signal Mount.mountDone[blockId](result, id);
  }

  /**
   * signalDone
   *
   * The function generates events to notify the higher lever modules about the
   * completion of a command. The event generated is based on the current stage
   * or the last command invoked, the state variable acts as a semaphore to 
   * prevent over lapping command calls.
   * 
   * @param result STORAGE_OK | STORAGE_FAIL
   */
  void signalDone(storage_result_t result) 
  {
    uint8_t tmpState = state;
    state = S_IDLE;
    switch(tmpState) 
    {
      case S_WRITE: 
        signal BlockWrite.writeDone[client](result, rwAddr, rwBuf, rwLen); 
      break;
      case S_ERASE: 
        signal BlockWrite.eraseDone[client](result); 
        break;
      case S_COMMIT: 
        signal BlockWrite.commitDone[client](result); 
      break;
      case S_READ: 
        signal BlockRead.readDone[client](result, rwAddr, rwBuf, rwLen); 
      break;
      case S_VERIFY: 
        signal BlockRead.verifyDone[client](result); 
      break;
      case S_CRC: 
        signal BlockRead.computeCrcDone[client](result, crc, rwAddr, rwLen); 
      break;
    }
  }

  /**
   * signalDoneTask
   *
   * Delayed task to generate events.
   */
  task void signalDoneTask() 
  {
    signalDone(STORAGE_OK);
  }

  /**
   * newRequest
   *
   * The function is invoked by all of the commands because the book keeping
   * is a common piece. The function invokes the corresponding command
   * in the SectorStorage module based on the current state, where the real
   * work is done. On successful return from SectorStorage the current
   * state is changed from S_IDLE to prevent any new commands.
   *
   * @param newState The current state is the current command defined in the enm.
   * @param blockId  The interface Id.
   * @param addr     Logical address within the file.
   * @param buf      Required if it is a read or write command. Basically data buffer.
   * @param len      Length of the buffer.
   *
   * @return SUCCESS | FAIL
   */
  result_t newRequest(uint8_t newState, blockstorage_t blockId, 
                      block_addr_t addr, void* buf, block_addr_t len) 
  {
    result_t result = FAIL;

    if (state != S_IDLE)
      return FAIL;

    client = blockId;

    rwAddr = addr;
    rwBuf = buf;
    rwLen = len;

    switch(newState) 
    {
      case S_READ:
        result = call SectorStorage.read[blockId](rwAddr, rwBuf, rwLen);
      break;
      case S_CRC:
        result = call SectorStorage.computeCrc[blockId](&crc, 0, rwAddr, rwLen);
      break;
      case S_VERIFY:
      break;
      case S_WRITE:
        result = call SectorStorage.write[blockId](rwAddr, rwBuf, rwLen);
      break;
      case S_ERASE:
        result = call SectorStorage.erase[blockId](0, 
                            call StorageManager.getNumSectors[blockId]());
      break;
      case S_COMMIT:
        result = SUCCESS;
      break;
    }

    if (newState == S_READ || newState == S_CRC || 
        newState == S_COMMIT || newState == S_VERIFY) 
    {
      if (result == SUCCESS) 
        result = post signalDoneTask();
    }

    if (result == SUCCESS)
      state = newState;

    return result;
  }
  
  /**
   * BlockRead.getSize
   * 
   * The function returns the size of the currently mounted file. The
   * function calls getVolumeSize of SectorStorage which calculate
   * the file size based on the number of blocks allocated to the file.
   *
   * @return size The size of the file.
   */
  command uint32_t BlockRead.getSize[blockstorage_t blockId]() 
  {
    return call StorageManager.getVolumeSize[blockId]();
  }

  /**
   * BlockRead.getReadPtr
   *
   * Function returns the current read pointer for the mounted
   * file. Note that the read pointer is a ram variable and will
   * be automatically reset when the system restarts.
   *
   * @return readPtr Logical address of the Read Pointer.
   */
  command block_addr_t BlockRead.getReadPtr[blockstorage_t blockId] ()
  {
    return call SectorStorage.getReadPtr[blockId]();
  }

  /**
   * BlockRead.resetReadPtr
   *
   * Reset the logical address of the read pointer to 0x0 for the
   * file mounted using blockId.
   *
   * @return SUCCESS | FAIL
   */
  command result_t BlockRead.resetReadPtr[blockstorage_t blockId] ()
  {
    return call SectorStorage.resetReadPtr[blockId]();
  }

  /**
   * BlockRead.rseek
   *
   * Move the read pointer to a given location within the file. The
   * first parameter should range from 0x0 to Size_of_file.
   *
   * @param addr Virtual address ranging from 0x0 to SIZE_OF_FILE
   * 
   * @return SUCCESS | FAIL
   */
  command result_t BlockRead.rseek[blockstorage_t blockId] (block_addr_t addr)
  {
    return call SectorStorage.rseek[blockId](addr);
  }

  /**
   * BlockRead.fread
   *
   * Read data from the mounted file and store it in buf. It is required
   * that the caller should allocate enough memory for <I>buf</I> to hold
   * data of size <I>len</I>. The read starts from the current read pointer
   * location.
   *
   * @param buf Buffer in which the file data will be stored.
   * @param len Number of bytes to be read from the file.
   *
   * @return SUCCESS | FAIL
   */
  command result_t BlockRead.fread[blockstorage_t blockId](void* buf, 
                                                           block_addr_t len) 
  {
    result_t res = FAIL;

    client = blockId;
    rwAddr = 0x0;
    rwBuf = buf;
    rwLen = len;
    state = S_READ;
    res = call SectorStorage.fread[blockId](buf, len);
    if (res == SUCCESS)
      signalDone(STORAGE_OK);
    else
      signalDone(STORAGE_FAIL);
    return res;
  }
  
  /**
   * BlockRead.read
   *
   * Read data from the mounted file and store it in buf. It is required
   * that the caller should allocate enough memory for <I>buf</I> to hold
   * data of size <I>len</I>.
   * The starting logical address has to be passed as the first parameter.
   *
   * @param addr Virtual address ranging from 0x0 to SIZE_OF_FILE
   * @param buf Buffer in which the file data will be stored.
   * @param len Number of bytes to be read from the file.
   * 
   * @return SUCCESS | FAIL
   */
  command result_t BlockRead.read[blockstorage_t blockId](block_addr_t addr, 
                                                void* buf, block_addr_t len) 
  {
    return newRequest(S_READ, blockId, addr, buf, len);
  }

  /**
   * BlockRead.verify
   *
   * NOTE IMPLEMENTED
   * FIXME Should be removed.
   */
  command result_t BlockRead.verify[blockstorage_t blockId]() 
  {
    return newRequest(S_VERIFY, blockId, 0, NULL, 0);
  }

  /**
   * BlockRead.computeCrc
   * 
   * Compute CRC of a given section of the file or the whole file based on
   * <I>addr</I> and <I>len</I>.
   * 
   * @return SUCCESS | FAIL
   */
  command result_t BlockRead.computeCrc[blockstorage_t blockId](block_addr_t addr,
                                                                block_addr_t len) 
  {
    return newRequest(S_CRC, blockId, addr, NULL, len);
  }

  /**
   * BlockWrite.erase
   *
   * The blocks allocated for the file will be erased and the write pointer will
   * be set to 0x0. This is useful if a file has to be reused with new data because
   * the current implementation does not allow the manipulation of already used
   * space in a file.
   * 
   * @return SUCCESS | FAIL
   */
  command result_t BlockWrite.erase[blockstorage_t blockId]() 
  {
    return newRequest(S_ERASE, blockId, 0, NULL, 0);
  }

  /**
   * BlockWrite.append
   *
   * Data from <I>buf</I> is appended to a mounted file for given length <I>len</I>.
   * Current write pointer will be used as the logical starting address. len + 
   * currWritePtr should be less than the size of the file or the funtion will
   * return an error.
   * The function performs the book keeping locally and changes the state to
   * S_WRITE.
   *
   * @param buf Buffer contaning data to be written to the file.
   * @param len Number of bytes to be written to the file.
   *  
   * @return SUCCESS | FAIL
   */
  command result_t BlockWrite.append[blockstorage_t blockId] (void* buf, 
                                                              block_addr_t len)
  {
    result_t res = FAIL;
    client = blockId;
    rwAddr = 0x0;
    rwBuf = buf;
    rwLen = len;
    state = S_WRITE; 
    res = call SectorStorage.append[blockId](buf, len);
    if (res == FAIL)
      signalDone(STORAGE_FAIL);
    return res;
  }

  /**
   * BlockWrite.write
   * 
   * Data from <I>buf</I> is written to a mounted file for given length <I>len</I>.
   * The logical starting address should be passed as the first parameter to the
   * function. addr + len should be less than the size of the file or the funtion will
   * return an error.
   *
   * @param addr Virtual address ranging from 0x0 to SIZE_OF_FILE
   * @param buf Buffer contaning data to be written to the file.
   * @param len Number of bytes to be written to the file.
   *  
   * @return SUCCESS | FAIL
   */
  command result_t BlockWrite.write[blockstorage_t blockId](block_addr_t addr, 
                                                   void* buf, block_addr_t len) 
  {
    return newRequest(S_WRITE, blockId, addr, buf, len);
  }

  /**
   * BlockWrite.commit
   * 
   * NOT IMPLEMENTED. DOES NOT HAVE ANY EFFECT.
   * FIXME Should be removed.
   */
  command result_t BlockWrite.commit[blockstorage_t blockId]() 
  {
    return newRequest(S_COMMIT, blockId, 0, NULL, 0);
  }

  /**
   * BlockWrite.getWritePtr
   *
   * The function returns the current write pointer for the mounted
   * file.
   *
   * @return WritePtr Current logical address for writing or Write Pointer.
   *
   * @return SUCCESS | FAIL
   */
  command block_addr_t BlockWrite.getWritePtr[blockstorage_t blockId]()
  {
    return call SectorStorage.getWritePtr[blockId]();
  }

  /**
   * BlockWrite.resetWritePtr
   *
   * The function will reset the write pointer to 0x0. This is essentially
   * same as erasing the file as resetting the write pointer will allow
   * write operation starting from 0x0, which in turn means that the 
   * blocks has to be prepared for writing new data.
   *
   * NOTE - The content of the file will be lost.
   *
   * @return SUCCESS | FAIL
   */
  command result_t BlockWrite.resetWritePtr[blockstorage_t blockId]()
  {
    return call SectorStorage.resetWritePtr[blockId]();
  }
  
  /**
   * SectorStorage.writeDone
   *
   * Event generated by StorageManager module to notify that the
   * write request is completed.
   *
   * @param result STORAGE_OK | STORAGE_FAIL
   */
  event void SectorStorage.writeDone[blockstorage_t blockId]
                                                 (storage_result_t result) 
  {
    signalDone(result);
  }
  
  /**
   * SectorStorage.eraseDone
   *
   * Event generated by StorageManager module to notify that the
   * erase request is completed.
   *
   * @param result STORAGE_OK | STORAGE_FAIL
   */  
  event void SectorStorage.eraseDone[blockstorage_t blockId]
                                                 (storage_result_t result) 
  {
    signalDone(result);
  }

  default event void BlockWrite.writeDone[blockstorage_t blockId]
                                                 (storage_result_t result, 
                                                  block_addr_t addr, 
                                                  void* buf, 
                                                  block_addr_t len) 
  { ; }

  default event void BlockWrite.eraseDone[blockstorage_t blockId]
                                             (storage_result_t result) 
  { ; }

  default event void BlockWrite.commitDone[blockstorage_t blockId]
                                             (storage_result_t result) 
  { ; }

  default event void BlockRead.readDone[blockstorage_t blockId]
                                             (storage_result_t result, 
                                              block_addr_t addr, 
                                              void* buf, 
                                              block_addr_t len) 
  { ; }

  default event void BlockRead.verifyDone[blockstorage_t blockId]
                                                (storage_result_t result) 
  { ; }

  default event void BlockRead.computeCrcDone[blockstorage_t blockId]
                                                 (storage_result_t result, 
                                                  uint16_t crcResult, 
                                                  block_addr_t addr, 
                                                  block_addr_t len) 
  { ; }

  default event void Mount.mountDone[blockstorage_t blockId](storage_result_t result, volume_id_t id) { ; }

}

--- NEW FILE: Storage.h ---
// $Id: Storage.h,v 1.1 2006/10/11 00:11:09 lnachman Exp $

/*									tab:2
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 */

/*
 * @author: Jonathan Hui <jwhui at cs.berkeley.edu>
 */

#ifndef __STORAGE_H__
#define __STORAGE_H__
#include <PXAFlash.h>
enum {
  STORAGE_OK,
  STORAGE_FAIL,
  STORAGE_INVALID_SIGNATURE,
  STORAGE_INVALID_CRC
};

enum {
  STORAGE_INVALID_ADDR = (uint32_t)0xffff,
};

typedef uint16_t volume_t;
typedef uint16_t volume_id_t;
typedef uint8_t storage_result_t;

#include "Storage_chip.h"

#endif



More information about the Tinyos-contrib-commits mailing list