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