Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
EEPROM.cxx
Go to the documentation of this file.
1
34#include "EEPROM.hxx"
35
36#include <fcntl.h>
37
45off_t EEPROM::lseek(File* file, off_t offset, int whence)
46{
47 lock_.lock();
48 off_t result = FileIO::lseek(file, offset, whence);
49 if (result > (off_t)fileSize)
50 {
51 file->offset = fileSize;
52 result = fileSize;
53 }
54 lock_.unlock();
55
56 return result;
57}
58
66int EEPROM::open(File* file, const char *path, int flags, int mode)
67{
68 if (flags & O_APPEND || flags & O_TRUNC)
69 {
70 return -EINVAL;
71 }
72
73 if ((flags & O_CREAT) && (flags & O_EXCL))
74 {
75 return -EEXIST;
76 }
77
78 file->offset = 0;
79
80 return Node::open(file, path, flags, mode);
81}
82
89ssize_t EEPROM::read(File *file, void *buf, size_t count)
90{
91 if ((file->flags & O_ACCMODE) == O_WRONLY)
92 {
93 return -EBADF;
94 }
95
96 lock_.lock();
97 if ((file->offset + count) > fileSize)
98 {
99 /* adjust to read just to the end of the file */
100 count = fileSize - file->offset;
101 }
102 if (count > 0)
103 {
104 /* if there is anything left to read */
105 read(file->offset, buf, count);
106 file->offset += count;
107 }
108 lock_.unlock();
109
110 return count;
111}
112
119ssize_t EEPROM::write(File *file, const void *buf, size_t count)
120{
121 if ((file->flags & O_ACCMODE) == O_RDONLY)
122 {
123 return -EBADF;
124 }
125
126 lock_.lock();
127 if ((file->offset + count) > fileSize)
128 {
129 /* adjust to write just to the end of the file */
130 count = fileSize - file->offset;
131 }
132 if (count > 0)
133 {
134 /* if there is anything left to write */
135 write(file->offset, buf, count);
136 file->offset += count;
137 }
138 lock_.unlock();
139
140 return count;
141}
142
143
off_t lseek(File *file, off_t offset, int whence) OVERRIDE
Seek method.
Definition EEPROM.cxx:45
size_t fileSize
Maximum file size we can grow to.
Definition EEPROM.hxx:87
virtual void read(unsigned int index, void *buf, size_t len)=0
Read from the EEPROM.
virtual void write(unsigned int index, const void *buf, size_t len)=0
Write to the EEPROM.
int open(File *file, const char *path, int flags, int mode) OVERRIDE
Open a device.
Definition EEPROM.cxx:66
static _off_t lseek(struct _reent *reent, int fd, _off_t offset, int whence)
Change the offset index of a file or device.
Definition Fileio.cxx:163
int open(File *, const char *, int, int) OVERRIDE
Open method.
OSMutex lock_
protects internal structures.
Definition Devtab.hxx:588
void lock()
Lock a mutex.
Definition OS.hxx:446
void unlock()
Unlock a mutex.
Definition OS.hxx:453
File information.
Definition Devtab.hxx:52
off_t offset
current offset within file
Definition Devtab.hxx:62
int flags
open flags
Definition Devtab.hxx:63