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