cfs-cooja.c

00001 /*
00002  * Copyright (c) 2006, 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  * $Id: cfs-cooja.c,v 1.12 2010/11/15 21:44:37 adamdunkels Exp $
00032  */
00033 #include <string.h>
00034 #include "lib/simEnvChange.h"
00035 #include "cfs/cfs.h"
00036 
00037 #define FLAG_FILE_CLOSED 0
00038 #define FLAG_FILE_OPEN   1
00039 
00040 struct filestate {
00041   char flag;
00042   int access;
00043   int fileptr;
00044   int endptr;
00045 };
00046 
00047 static struct filestate file;
00048 
00049 const struct simInterface cfs_interface;
00050 
00051 // COOJA variables
00052 #define CFS_BUF_SIZE 4000 /* Configure CFS size here and in ContikiCFS.java */
00053 char simCFSData[CFS_BUF_SIZE] = { 0 };
00054 char simCFSChanged = 0;
00055 int simCFSRead = 0;
00056 int simCFSWritten = 0;
00057 
00058 /*---------------------------------------------------------------------------*/
00059 int
00060 cfs_open(const char *n, int f)
00061 {
00062   if(file.flag == FLAG_FILE_CLOSED) {
00063     file.flag = FLAG_FILE_OPEN;
00064     file.access = f;
00065                 if(f & CFS_APPEND) {
00066                         file.fileptr = file.endptr;
00067                 } else {
00068             file.fileptr = 0;
00069                 }
00070     return 0;
00071   } else {
00072     return -1;
00073   }
00074 }
00075 /*---------------------------------------------------------------------------*/
00076 void
00077 cfs_close(int f)
00078 {
00079   file.flag = FLAG_FILE_CLOSED;
00080 }
00081 /*---------------------------------------------------------------------------*/
00082 int
00083 cfs_read(int f, void *buf, unsigned int len)
00084 {
00085         if(file.flag == FLAG_FILE_OPEN && file.access & CFS_READ) {
00086                 if(file.fileptr + len >= file.endptr) {
00087                         len = file.endptr - file.fileptr;
00088                 }
00089                 memcpy(buf, &simCFSData[file.fileptr], len);
00090     file.fileptr += len;
00091                 simCFSChanged = 1;
00092                 simCFSRead += len;
00093     return len;
00094   } else {
00095     return -1;
00096   }
00097 }
00098 /*---------------------------------------------------------------------------*/
00099 int
00100 cfs_write(int f, const void *buf, unsigned int len)
00101 {
00102         if(file.flag == FLAG_FILE_OPEN && file.access & CFS_WRITE) {
00103                 if(file.fileptr + len > CFS_BUF_SIZE) {
00104                         len = CFS_BUF_SIZE - file.fileptr;
00105                         printf("cfs-cooja.c: warning: write truncated\n");
00106                 }
00107                 memcpy(&simCFSData[file.fileptr], buf, len);
00108         file.fileptr += len;
00109                 simCFSChanged = 1;
00110                 simCFSWritten += len;
00111                 if(file.fileptr > file.endptr) {
00112                         file.endptr = file.fileptr;
00113                 }
00114     return len;
00115   } else {
00116     return -1;
00117   }
00118 }
00119 /*---------------------------------------------------------------------------*/
00120 cfs_offset_t
00121 cfs_seek(int f, cfs_offset_t o, int w)
00122 {
00123   if(file.flag == FLAG_FILE_OPEN) {
00124         if(w == CFS_SEEK_SET) {
00125         file.fileptr = o;
00126         } else if(w == CFS_SEEK_CUR) {
00127                         file.fileptr += o;
00128         } else if(w == CFS_SEEK_END) {
00129                 file.fileptr = file.endptr + o;
00130         }
00131         if(file.fileptr >= 0 && file.fileptr <= CFS_BUF_SIZE) {
00132                 if(file.fileptr > file.endptr) {
00133                         file.endptr = file.fileptr;
00134                 }
00135                   return file.fileptr;
00136         }
00137   }
00138   return -1;
00139 }
00140 /*---------------------------------------------------------------------------*/
00141 int
00142 cfs_remove(const char *name)
00143 {
00144         memset(simCFSData, 0, sizeof(simCFSData));
00145   return 0;
00146 }
00147 /*---------------------------------------------------------------------------*/
00148 int
00149 cfs_opendir(struct cfs_dir *p, const char *n)
00150 {
00151   return 0;
00152 }
00153 /*---------------------------------------------------------------------------*/
00154 int
00155 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
00156 {
00157   return 0;
00158 }
00159 /*---------------------------------------------------------------------------*/
00160 void
00161 cfs_closedir(struct cfs_dir *p)
00162 {
00163 }
00164 /*---------------------------------------------------------------------------*/
00165 static void
00166 doInterfaceActionsBeforeTick(void)
00167 {
00168 }
00169 /*---------------------------------------------------------------------------*/
00170 static void
00171 doInterfaceActionsAfterTick(void)
00172 {
00173 }
00174 /*---------------------------------------------------------------------------*/
00175 SIM_INTERFACE(cfs_interface,
00176               doInterfaceActionsBeforeTick,
00177               doInterfaceActionsAfterTick);
00178 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:43 2011 for Contiki 2.5 by  doxygen 1.6.1