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