Contiki 3.x
cfs-ram.c
1 /*
2  * Copyright (c) 2004, 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  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 #include <string.h>
36 
37 #include "cfs/cfs.h"
38 
39 struct filestate {
40  int flag;
41 #define FLAG_FILE_CLOSED 0
42 #define FLAG_FILE_OPEN 1
43  int fileptr;
44  int filesize;
45 };
46 
47 #ifdef CFS_RAM_CONF_SIZE
48 #define CFS_RAM_SIZE CFS_RAM_CONF_SIZE
49 #else
50 #define CFS_RAM_SIZE 4096
51 #endif
52 
53 static struct filestate file;
54 static char filemem[CFS_RAM_SIZE];
55 
56 /*---------------------------------------------------------------------------*/
57 int
58 cfs_open(const char *n, int f)
59 {
60  if(file.flag == FLAG_FILE_CLOSED) {
61  file.flag = FLAG_FILE_OPEN;
62  if(f & CFS_READ) {
63  file.fileptr = 0;
64  }
65  if(f & CFS_WRITE){
66  if(f & CFS_APPEND) {
67  file.fileptr = file.filesize;
68  } else {
69  file.fileptr = 0;
70  file.filesize = 0;
71  }
72  }
73  return 1;
74  } else {
75  return -1;
76  }
77 }
78 /*---------------------------------------------------------------------------*/
79 void
80 cfs_close(int f)
81 {
82  file.flag = FLAG_FILE_CLOSED;
83 }
84 /*---------------------------------------------------------------------------*/
85 int
86 cfs_read(int f, void *buf, unsigned int len)
87 {
88  if(file.fileptr + len > sizeof(filemem)) {
89  len = sizeof(filemem) - file.fileptr;
90  }
91 
92  if(file.fileptr + len > file.filesize) {
93  len = file.filesize - file.fileptr;
94  }
95 
96  if(f == 1) {
97  memcpy(buf, &filemem[file.fileptr], len);
98  file.fileptr += len;
99  return len;
100  } else {
101  return -1;
102  }
103 }
104 /*---------------------------------------------------------------------------*/
105 int
106 cfs_write(int f, const void *buf, unsigned int len)
107 {
108  if(file.fileptr >= sizeof(filemem)) {
109  return 0;
110  }
111  if(file.fileptr + len > sizeof(filemem)) {
112  len = sizeof(filemem) - file.fileptr;
113  }
114 
115  if(file.fileptr + len > file.filesize) {
116  /* Extend the size of the file. */
117  file.filesize = file.fileptr + len;
118  }
119 
120  if(f == 1) {
121  memcpy(&filemem[file.fileptr], buf, len);
122  file.fileptr += len;
123  return len;
124  } else {
125  return -1;
126  }
127 }
128 /*---------------------------------------------------------------------------*/
129 cfs_offset_t
130 cfs_seek(int f, cfs_offset_t o, int w)
131 {
132  if(w == CFS_SEEK_SET && f == 1) {
133  if(o > file.filesize) {
134  o = file.filesize;
135  }
136  file.fileptr = o;
137  return o;
138  }
139  return (cfs_offset_t)-1;
140 }
141 /*---------------------------------------------------------------------------*/
142 int
143 cfs_remove(const char *name)
144 {
145  return -1;
146 }
147 /*---------------------------------------------------------------------------*/
148 int
149 cfs_opendir(struct cfs_dir *p, const char *n)
150 {
151  return -1;
152 }
153 /*---------------------------------------------------------------------------*/
154 int
155 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
156 {
157  return -1;
158 }
159 /*---------------------------------------------------------------------------*/
160 void
161 cfs_closedir(struct cfs_dir *p)
162 {
163 }
164 /*---------------------------------------------------------------------------*/
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_WRITE
Specify that cfs_open() should open a file for writing.
Definition: cfs.h:104
#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.