Contiki 2.6

cfs-xmem.c

00001 /*
00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: cfs-xmem.c,v 1.11 2009/02/27 14:25:38 nvt-se Exp $
00034  */
00035 
00036 #include "cfs/cfs.h"
00037 #include "dev/xmem.h"
00038 
00039 struct filestate {
00040   int flag;
00041 #define FLAG_FILE_CLOSED 0
00042 #define FLAG_FILE_OPEN   1
00043   unsigned int fileptr;
00044   unsigned int filesize;
00045 };
00046 
00047 #ifdef CFS_XMEM_CONF_OFFSET
00048 #define CFS_XMEM_OFFSET CFS_XMEM_CONF_OFFSET
00049 #else
00050 #define CFS_XMEM_OFFSET 0
00051 #endif
00052 
00053 /* Note the CFS_XMEM_CONF_SIZE must be a tuple of XMEM_ERASE_UNIT_SIZE */
00054 #ifdef CFS_XMEM_CONF_SIZE
00055 #define CFS_XMEM_SIZE CFS_XMEM_CONF_SIZE
00056 #else
00057 #define CFS_XMEM_SIZE XMEM_ERASE_UNIT_SIZE
00058 #endif
00059 
00060 static struct filestate file;
00061 
00062 /*---------------------------------------------------------------------------*/
00063 int
00064 cfs_open(const char *n, int f)
00065 {
00066   if(file.flag == FLAG_FILE_CLOSED) {
00067     file.flag = FLAG_FILE_OPEN;
00068     if(f & CFS_READ) {
00069       file.fileptr = 0;
00070     }
00071     if(f & CFS_WRITE){
00072       if(f & CFS_APPEND) {
00073         file.fileptr = file.filesize;
00074       } else {
00075         file.fileptr = 0;
00076         file.filesize = 0;
00077         xmem_erase(CFS_XMEM_SIZE, CFS_XMEM_OFFSET);
00078       }
00079     }
00080     return 1;
00081   } else {
00082     return -1;
00083   }
00084 }
00085 /*---------------------------------------------------------------------------*/
00086 void
00087 cfs_close(int f)
00088 {
00089   file.flag = FLAG_FILE_CLOSED;
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 int
00093 cfs_read(int f, void *buf, unsigned int len)
00094 {
00095   if(file.fileptr + len > CFS_XMEM_SIZE) {
00096     len = CFS_XMEM_SIZE - file.fileptr;
00097   }
00098 
00099   if(file.fileptr + len > file.filesize) {
00100     len = file.filesize - file.fileptr;
00101   }
00102 
00103   if(f == 1) {
00104     xmem_pread(buf, len, CFS_XMEM_OFFSET + file.fileptr);
00105     file.fileptr += len;
00106     return len;
00107   } else {
00108     return -1;
00109   }
00110 }
00111 /*---------------------------------------------------------------------------*/
00112 int
00113 cfs_write(int f, const void *buf, unsigned int len)
00114 {
00115   if(file.fileptr >= CFS_XMEM_SIZE) {
00116     return 0;
00117   }
00118   if(file.fileptr + len > CFS_XMEM_SIZE) {
00119     len = CFS_XMEM_SIZE - file.fileptr;
00120   }
00121 
00122   if(file.fileptr + len > file.filesize) {
00123     /* Extend the size of the file. */
00124     file.filesize = file.fileptr + len;
00125   }
00126 
00127   if(f == 1) {
00128     xmem_pwrite(buf, len, CFS_XMEM_OFFSET + file.fileptr);
00129     file.fileptr += len;
00130     return len;
00131   } else {
00132     return -1;
00133   }
00134 }
00135 /*---------------------------------------------------------------------------*/
00136 cfs_offset_t
00137 cfs_seek(int f, cfs_offset_t o, int w)
00138 {
00139   if(w == CFS_SEEK_SET && f == 1) {
00140     if(o > file.filesize) {
00141       o = file.filesize;
00142     }
00143     file.fileptr = o;
00144     return o;
00145   }
00146   return -1;
00147 }
00148 /*---------------------------------------------------------------------------*/
00149 int
00150 cfs_remove(const char *name)
00151 {
00152   file.flag = FLAG_FILE_CLOSED;
00153   file.fileptr = 0;
00154   file.filesize = 0;
00155   xmem_erase(CFS_XMEM_SIZE, CFS_XMEM_OFFSET);
00156   return 0;
00157 }
00158 /*---------------------------------------------------------------------------*/
00159 int
00160 cfs_opendir(struct cfs_dir *p, const char *n)
00161 {
00162   return -1;
00163 }
00164 /*---------------------------------------------------------------------------*/
00165 int
00166 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
00167 {
00168   return -1;
00169 }
00170 /*---------------------------------------------------------------------------*/
00171 void
00172 cfs_closedir(struct cfs_dir *p)
00173 {
00174 }
00175 /*---------------------------------------------------------------------------*/