Contiki 2.5
cfs-cooja.c
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * $Id: cfs-cooja.c,v 1.12 2010/11/15 21:44:37 adamdunkels Exp $
32  */
33 #include <string.h>
34 #include "lib/simEnvChange.h"
35 #include "cfs/cfs.h"
36 
37 #define FLAG_FILE_CLOSED 0
38 #define FLAG_FILE_OPEN 1
39 
40 struct filestate {
41  char flag;
42  int access;
43  int fileptr;
44  int endptr;
45 };
46 
47 static struct filestate file;
48 
49 const struct simInterface cfs_interface;
50 
51 // COOJA variables
52 #define CFS_BUF_SIZE 4000 /* Configure CFS size here and in ContikiCFS.java */
53 char simCFSData[CFS_BUF_SIZE] = { 0 };
54 char simCFSChanged = 0;
55 int simCFSRead = 0;
56 int simCFSWritten = 0;
57 
58 /*---------------------------------------------------------------------------*/
59 int
60 cfs_open(const char *n, int f)
61 {
62  if(file.flag == FLAG_FILE_CLOSED) {
63  file.flag = FLAG_FILE_OPEN;
64  file.access = f;
65  if(f & CFS_APPEND) {
66  file.fileptr = file.endptr;
67  } else {
68  file.fileptr = 0;
69  }
70  return 0;
71  } else {
72  return -1;
73  }
74 }
75 /*---------------------------------------------------------------------------*/
76 void
77 cfs_close(int f)
78 {
79  file.flag = FLAG_FILE_CLOSED;
80 }
81 /*---------------------------------------------------------------------------*/
82 int
83 cfs_read(int f, void *buf, unsigned int len)
84 {
85  if(file.flag == FLAG_FILE_OPEN && file.access & CFS_READ) {
86  if(file.fileptr + len >= file.endptr) {
87  len = file.endptr - file.fileptr;
88  }
89  memcpy(buf, &simCFSData[file.fileptr], len);
90  file.fileptr += len;
91  simCFSChanged = 1;
92  simCFSRead += len;
93  return len;
94  } else {
95  return -1;
96  }
97 }
98 /*---------------------------------------------------------------------------*/
99 int
100 cfs_write(int f, const void *buf, unsigned int len)
101 {
102  if(file.flag == FLAG_FILE_OPEN && file.access & CFS_WRITE) {
103  if(file.fileptr + len > CFS_BUF_SIZE) {
104  len = CFS_BUF_SIZE - file.fileptr;
105  printf("cfs-cooja.c: warning: write truncated\n");
106  }
107  memcpy(&simCFSData[file.fileptr], buf, len);
108  file.fileptr += len;
109  simCFSChanged = 1;
110  simCFSWritten += len;
111  if(file.fileptr > file.endptr) {
112  file.endptr = file.fileptr;
113  }
114  return len;
115  } else {
116  return -1;
117  }
118 }
119 /*---------------------------------------------------------------------------*/
120 cfs_offset_t
121 cfs_seek(int f, cfs_offset_t o, int w)
122 {
123  if(file.flag == FLAG_FILE_OPEN) {
124  if(w == CFS_SEEK_SET) {
125  file.fileptr = o;
126  } else if(w == CFS_SEEK_CUR) {
127  file.fileptr += o;
128  } else if(w == CFS_SEEK_END) {
129  file.fileptr = file.endptr + o;
130  }
131  if(file.fileptr >= 0 && file.fileptr <= CFS_BUF_SIZE) {
132  if(file.fileptr > file.endptr) {
133  file.endptr = file.fileptr;
134  }
135  return file.fileptr;
136  }
137  }
138  return -1;
139 }
140 /*---------------------------------------------------------------------------*/
141 int
142 cfs_remove(const char *name)
143 {
144  memset(simCFSData, 0, sizeof(simCFSData));
145  return 0;
146 }
147 /*---------------------------------------------------------------------------*/
148 int
149 cfs_opendir(struct cfs_dir *p, const char *n)
150 {
151  return 0;
152 }
153 /*---------------------------------------------------------------------------*/
154 int
155 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
156 {
157  return 0;
158 }
159 /*---------------------------------------------------------------------------*/
160 void
161 cfs_closedir(struct cfs_dir *p)
162 {
163 }
164 /*---------------------------------------------------------------------------*/
165 static void
166 doInterfaceActionsBeforeTick(void)
167 {
168 }
169 /*---------------------------------------------------------------------------*/
170 static void
171 doInterfaceActionsAfterTick(void)
172 {
173 }
174 /*---------------------------------------------------------------------------*/
175 SIM_INTERFACE(cfs_interface,
176  doInterfaceActionsBeforeTick,
177  doInterfaceActionsAfterTick);
178 /*---------------------------------------------------------------------------*/