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