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-eeprom.c,v 1.11 2009/07/23 16:13:48 dak664 Exp $ 00034 */ 00035 00036 #include "cfs/cfs.h" 00037 #include "dev/eeprom.h" 00038 00039 struct filestate { 00040 int flag; 00041 #define FLAG_FILE_CLOSED 0 00042 #define FLAG_FILE_OPEN 1 00043 eeprom_addr_t fileptr; 00044 eeprom_addr_t filesize; 00045 }; 00046 00047 static struct filestate file; 00048 00049 #ifdef CFS_EEPROM_CONF_OFFSET 00050 #define CFS_EEPROM_OFFSET CFS_EEPROM_CONF_OFFSET 00051 #else 00052 #define CFS_EEPROM_OFFSET 0 00053 #endif 00054 00055 /*---------------------------------------------------------------------------*/ 00056 int 00057 cfs_open(const char *n, int f) 00058 { 00059 if(file.flag == FLAG_FILE_CLOSED) { 00060 file.flag = FLAG_FILE_OPEN; 00061 if(f & CFS_READ) { 00062 file.fileptr = 0; 00063 } 00064 if(f & CFS_WRITE){ 00065 if(f & CFS_APPEND) { 00066 file.fileptr = file.filesize; 00067 } else { 00068 file.fileptr = 0; 00069 file.filesize = 0; 00070 } 00071 } 00072 return 1; 00073 } else { 00074 return -1; 00075 } 00076 } 00077 /*---------------------------------------------------------------------------*/ 00078 void 00079 cfs_close(int f) 00080 { 00081 file.flag = FLAG_FILE_CLOSED; 00082 } 00083 /*---------------------------------------------------------------------------*/ 00084 int 00085 cfs_read(int f, void *buf, unsigned int len) 00086 { 00087 if(f == 1) { 00088 eeprom_read(CFS_EEPROM_OFFSET + file.fileptr, buf, len); 00089 file.fileptr += len; 00090 return len; 00091 } else { 00092 return -1; 00093 } 00094 } 00095 /*---------------------------------------------------------------------------*/ 00096 int 00097 cfs_write(int f, const void *buf, unsigned int len) 00098 { 00099 if(f == 1) { 00100 eeprom_write(CFS_EEPROM_OFFSET + file.fileptr, (unsigned char *)buf, len); 00101 file.fileptr += len; 00102 return len; 00103 } else { 00104 return -1; 00105 } 00106 } 00107 /*---------------------------------------------------------------------------*/ 00108 cfs_offset_t 00109 cfs_seek(int f, cfs_offset_t o, int w) 00110 { 00111 if(w == CFS_SEEK_SET && f == 1) { 00112 file.fileptr = o; 00113 return o; 00114 } else { 00115 return -1; 00116 } 00117 } 00118 /*---------------------------------------------------------------------------*/ 00119 int 00120 cfs_remove(const char *name) 00121 { 00122 return -1; 00123 } 00124 /*---------------------------------------------------------------------------*/ 00125 int 00126 cfs_opendir(struct cfs_dir *p, const char *n) 00127 { 00128 return -1; 00129 } 00130 /*---------------------------------------------------------------------------*/ 00131 int 00132 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e) 00133 { 00134 return -1; 00135 } 00136 /*---------------------------------------------------------------------------*/ 00137 void 00138 cfs_closedir(struct cfs_dir *p) 00139 { 00140 } 00141 /*---------------------------------------------------------------------------*/