Contiki 3.x
cfs-eeprom.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/eeprom.h"
37 
38 struct filestate {
39  int flag;
40 #define FLAG_FILE_CLOSED 0
41 #define FLAG_FILE_OPEN 1
42  eeprom_addr_t fileptr;
43  eeprom_addr_t filesize;
44 };
45 
46 static struct filestate file;
47 
48 #ifdef CFS_EEPROM_CONF_OFFSET
49 #define CFS_EEPROM_OFFSET CFS_EEPROM_CONF_OFFSET
50 #else
51 #define CFS_EEPROM_OFFSET 0
52 #endif
53 
54 /*---------------------------------------------------------------------------*/
55 int
56 cfs_open(const char *n, int f)
57 {
58  if(file.flag == FLAG_FILE_CLOSED) {
59  file.flag = FLAG_FILE_OPEN;
60  if(f & CFS_READ) {
61  file.fileptr = 0;
62  }
63  if(f & CFS_WRITE){
64  if(f & CFS_APPEND) {
65  file.fileptr = file.filesize;
66  } else {
67  file.fileptr = 0;
68  file.filesize = 0;
69  }
70  }
71  return 1;
72  } else {
73  return -1;
74  }
75 }
76 /*---------------------------------------------------------------------------*/
77 void
78 cfs_close(int f)
79 {
80  file.flag = FLAG_FILE_CLOSED;
81 }
82 /*---------------------------------------------------------------------------*/
83 int
84 cfs_read(int f, void *buf, unsigned int len)
85 {
86  if(f == 1) {
87  eeprom_read(CFS_EEPROM_OFFSET + file.fileptr, buf, len);
88  file.fileptr += len;
89  return len;
90  } else {
91  return -1;
92  }
93 }
94 /*---------------------------------------------------------------------------*/
95 int
96 cfs_write(int f, const void *buf, unsigned int len)
97 {
98  if(f == 1) {
99  eeprom_write(CFS_EEPROM_OFFSET + file.fileptr, (unsigned char *)buf, len);
100  file.fileptr += len;
101  return len;
102  } else {
103  return -1;
104  }
105 }
106 /*---------------------------------------------------------------------------*/
107 cfs_offset_t
108 cfs_seek(int f, cfs_offset_t o, int w)
109 {
110  if(w == CFS_SEEK_SET && f == 1) {
111  file.fileptr = o;
112  return o;
113  } else {
114  return -1;
115  }
116 }
117 /*---------------------------------------------------------------------------*/
118 int
119 cfs_remove(const char *name)
120 {
121  return -1;
122 }
123 /*---------------------------------------------------------------------------*/
124 int
125 cfs_opendir(struct cfs_dir *p, const char *n)
126 {
127  return -1;
128 }
129 /*---------------------------------------------------------------------------*/
130 int
131 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
132 {
133  return -1;
134 }
135 /*---------------------------------------------------------------------------*/
136 void
137 cfs_closedir(struct cfs_dir *p)
138 {
139 }
140 /*---------------------------------------------------------------------------*/
void eeprom_read(eeprom_addr_t addr, unsigned char *buf, int size)
Read data from the EEPROM.
Definition: eeprom.c:49
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
void eeprom_write(eeprom_addr_t addr, unsigned char *buf, int size)
Write a buffer into EEPROM.
Definition: eeprom.c:42
#define CFS_APPEND
Specify that cfs_open() should append written data to the file rather than overwriting it...
Definition: cfs.h:118
EEPROM functions.
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.