Contiki-NG
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  */
32 #include <stdio.h>
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 int simCFSSize = 0;
55 char simCFSChanged = 0;
56 int simCFSRead = 0;
57 int simCFSWritten = 0;
58 
59 /*---------------------------------------------------------------------------*/
60 int
61 cfs_open(const char *n, int f)
62 {
63  if(file.flag == FLAG_FILE_CLOSED) {
64  file.flag = FLAG_FILE_OPEN;
65  file.access = f;
66  file.fileptr = 0;
67  file.endptr = simCFSSize;
68  if(f & CFS_WRITE) {
69  if(f & CFS_APPEND) {
70  file.fileptr = file.endptr;
71  } else {
72  file.endptr = 0;
73  }
74  }
75  return 0;
76  } else {
77  return -1;
78  }
79 }
80 /*---------------------------------------------------------------------------*/
81 void
82 cfs_close(int f)
83 {
84  file.flag = FLAG_FILE_CLOSED;
85 }
86 /*---------------------------------------------------------------------------*/
87 int
88 cfs_read(int f, void *buf, unsigned int len)
89 {
90  if(file.flag == FLAG_FILE_OPEN && file.access & CFS_READ) {
91  if(file.fileptr + len >= file.endptr) {
92  len = file.endptr - file.fileptr;
93  }
94  memcpy(buf, &simCFSData[file.fileptr], len);
95  file.fileptr += len;
96  simCFSChanged = 1;
97  simCFSRead += len;
98  return len;
99  } else {
100  return -1;
101  }
102 }
103 /*---------------------------------------------------------------------------*/
104 int
105 cfs_write(int f, const void *buf, unsigned int len)
106 {
107  if(file.flag == FLAG_FILE_OPEN && file.access & CFS_WRITE) {
108  if(file.fileptr + len > CFS_BUF_SIZE) {
109  len = CFS_BUF_SIZE - file.fileptr;
110  printf("cfs-cooja.c: warning: write truncated\n");
111  }
112  memcpy(&simCFSData[file.fileptr], buf, len);
113  file.fileptr += len;
114  simCFSChanged = 1;
115  simCFSWritten += len;
116  if(file.fileptr > file.endptr) {
117  file.endptr = file.fileptr;
118  }
119  if(file.fileptr > simCFSSize) {
120  simCFSSize = file.fileptr;
121  }
122  return len;
123  } else {
124  return -1;
125  }
126 }
127 /*---------------------------------------------------------------------------*/
128 cfs_offset_t
129 cfs_seek(int f, cfs_offset_t o, int w)
130 {
131  if(file.flag == FLAG_FILE_OPEN) {
132  if(w == CFS_SEEK_SET) {
133  file.fileptr = o;
134  } else if(w == CFS_SEEK_CUR) {
135  file.fileptr += o;
136  } else if(w == CFS_SEEK_END) {
137  file.fileptr = file.endptr + o;
138  }
139  if(file.fileptr >= 0 && file.fileptr <= CFS_BUF_SIZE) {
140  if(file.fileptr > file.endptr) {
141  file.endptr = file.fileptr;
142  }
143  return file.fileptr;
144  }
145  }
146  return -1;
147 }
148 /*---------------------------------------------------------------------------*/
149 int
150 cfs_remove(const char *name)
151 {
152  memset(simCFSData, 0, sizeof(simCFSData));
153  return 0;
154 }
155 /*---------------------------------------------------------------------------*/
156 int
157 cfs_opendir(struct cfs_dir *p, const char *n)
158 {
159  return 0;
160 }
161 /*---------------------------------------------------------------------------*/
162 int
163 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
164 {
165  return 0;
166 }
167 /*---------------------------------------------------------------------------*/
168 void
169 cfs_closedir(struct cfs_dir *p)
170 {
171 }
172 /*---------------------------------------------------------------------------*/
173 static void
174 doInterfaceActionsBeforeTick(void)
175 {
176 }
177 /*---------------------------------------------------------------------------*/
178 static void
179 doInterfaceActionsAfterTick(void)
180 {
181 }
182 /*---------------------------------------------------------------------------*/
183 SIM_INTERFACE(cfs_interface,
184  doInterfaceActionsBeforeTick,
185  doInterfaceActionsAfterTick);
186 /*---------------------------------------------------------------------------*/
int cfs_open(const char *n, int f)
Open a file.
Definition: cfs-cooja.c:61
int cfs_remove(const char *name)
Remove a file.
Definition: cfs-cooja.c:150
cfs_offset_t cfs_seek(int f, cfs_offset_t o, int w)
Seek to a specified position in an open file.
Definition: cfs-cooja.c:129
#define CFS_APPEND
Specify that cfs_open() should append written data to the file rather than overwriting it...
Definition: cfs.h:120
int cfs_write(int f, const void *buf, unsigned int len)
Write data to an open file.
Definition: cfs-cooja.c:105
CFS header file.
int cfs_opendir(struct cfs_dir *p, const char *n)
Open a directory for reading directory entries.
Definition: cfs-cooja.c:157
void cfs_close(int f)
Close an open file.
Definition: cfs-cooja.c:82
int cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
Read a directory entry.
Definition: cfs-cooja.c:163
#define CFS_SEEK_END
Specify that cfs_seek() should compute the offset from the end of the file.
Definition: cfs.h:147
int cfs_read(int f, void *buf, unsigned int len)
Read data from an open file.
Definition: cfs-cooja.c:88
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:92
#define CFS_SEEK_CUR
Specify that cfs_seek() should compute the offset from the current position of the file pointer...
Definition: cfs.h:138
#define CFS_SEEK_SET
Specify that cfs_seek() should compute the offset from the beginning of the file. ...
Definition: cfs.h:129
void cfs_closedir(struct cfs_dir *p)
Close a directory opened with cfs_opendir().
Definition: cfs-cooja.c:169
#define CFS_WRITE
Specify that cfs_open() should open a file for writing.
Definition: cfs.h:106