Contiki 2.6

cfs-ram.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-ram.c,v 1.10 2009/02/27 14:25:38 nvt-se Exp $
00034  */
00035 
00036 #include <string.h>
00037 
00038 #include "cfs/cfs.h"
00039 
00040 struct filestate {
00041   int flag;
00042 #define FLAG_FILE_CLOSED 0
00043 #define FLAG_FILE_OPEN   1
00044   int fileptr;
00045   int filesize;
00046 };
00047 
00048 #ifdef CFS_RAM_CONF_SIZE
00049 #define CFS_RAM_SIZE CFS_RAM_CONF_SIZE
00050 #else
00051 #define CFS_RAM_SIZE 4096
00052 #endif
00053 
00054 static struct filestate file;
00055 static char filemem[CFS_RAM_SIZE];
00056 
00057 /*---------------------------------------------------------------------------*/
00058 int
00059 cfs_open(const char *n, int f)
00060 {
00061   if(file.flag == FLAG_FILE_CLOSED) {
00062     file.flag = FLAG_FILE_OPEN;
00063     if(f & CFS_READ) {
00064       file.fileptr = 0;
00065     }
00066     if(f & CFS_WRITE){
00067       if(f & CFS_APPEND) {
00068         file.fileptr = file.filesize;
00069       } else {
00070         file.fileptr = 0;
00071         file.filesize = 0;
00072       }
00073     }
00074     return 1;
00075   } else {
00076     return -1;
00077   }
00078 }
00079 /*---------------------------------------------------------------------------*/
00080 void
00081 cfs_close(int f)
00082 {
00083   file.flag = FLAG_FILE_CLOSED;
00084 }
00085 /*---------------------------------------------------------------------------*/
00086 int
00087 cfs_read(int f, void *buf, unsigned int len)
00088 {
00089   if(file.fileptr + len > sizeof(filemem)) {
00090     len = sizeof(filemem) - file.fileptr;
00091   }
00092   
00093   if(file.fileptr + len > file.filesize) {
00094     len = file.filesize - file.fileptr;
00095   }
00096 
00097   if(f == 1) {
00098     memcpy(buf, &filemem[file.fileptr], len);
00099     file.fileptr += len;
00100     return len;
00101   } else {
00102     return -1;
00103   }
00104 }
00105 /*---------------------------------------------------------------------------*/
00106 int
00107 cfs_write(int f, const void *buf, unsigned int len)
00108 {
00109   if(file.fileptr >= sizeof(filemem)) {
00110     return 0;
00111   }
00112   if(file.fileptr + len > sizeof(filemem)) {
00113     len = sizeof(filemem) - file.fileptr;
00114   }
00115 
00116   if(file.fileptr + len > file.filesize) {
00117     /* Extend the size of the file. */
00118     file.filesize = file.fileptr + len;
00119   }
00120   
00121   if(f == 1) {
00122     memcpy(&filemem[file.fileptr], buf, len);
00123     file.fileptr += len;
00124     return len;
00125   } else {
00126     return -1;
00127   }
00128 }
00129 /*---------------------------------------------------------------------------*/
00130 cfs_offset_t
00131 cfs_seek(int f, cfs_offset_t o, int w)
00132 {
00133   if(w == CFS_SEEK_SET && f == 1) {
00134     if(o > file.filesize) {
00135       o = file.filesize;
00136     }
00137     file.fileptr = o;
00138     return o;
00139   }
00140   return (cfs_offset_t)-1;
00141 }
00142 /*---------------------------------------------------------------------------*/
00143 int
00144 cfs_remove(const char *name)
00145 {
00146   return -1;
00147 }
00148 /*---------------------------------------------------------------------------*/
00149 int
00150 cfs_opendir(struct cfs_dir *p, const char *n)
00151 {
00152   return -1;
00153 }
00154 /*---------------------------------------------------------------------------*/
00155 int
00156 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
00157 {
00158   return -1;
00159 }
00160 /*---------------------------------------------------------------------------*/
00161 void
00162 cfs_closedir(struct cfs_dir *p)
00163 {
00164 }
00165 /*---------------------------------------------------------------------------*/