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