Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
CC32xxDeviceFile.cxx
Go to the documentation of this file.
1
34#define SUPPORT_SL_R1_API
35
36#include "CC32xxDeviceFile.hxx"
37#include "CC32xxHelper.hxx"
38
39#include <fcntl.h>
40
41#include <ti/drivers/net/wifi/simplelink.h>
42
43/*
44 * CC32xxDeviceFile::open()
45 */
46int CC32xxDeviceFile::open(File* file, const char *path, int flags, int mode)
47{
48 if ((flags & O_ACCMODE) == O_RDWR)
49 {
50 return -EINVAL;
51 }
52
53 if (flags & O_APPEND)
54 {
55 /* we don't support append mode */
56 return -EINVAL;
57 }
58
59 if ((flags & O_ACCMODE) != O_RDONLY && !(flags & O_TRUNC))
60 {
61 /* we only support truncating the file on create and/or write mode */
62 return -EINVAL;
63 }
64
65 if ((flags & O_CREAT) && (flags & O_EXCL))
66 {
67 /* we don't support this flag combo, just too hard */
68 return -EINVAL;
69 }
70
71 if ((flags & O_CREAT) && (flags & O_ACCMODE) == O_RDONLY)
72 {
73 /* O_CREATE cannot be used along with O_RDONLY */
74 return -EINVAL;
75 }
76
77 file->offset = 0;
78
79 lock_.lock();
80 if (references_ > 0)
81 {
82 /* File system does not allow simultaneous access for both read and
83 * write. Check if file is already open with the wrong mode.
84 */
85 if ((((flags & O_ACCMODE) == O_WRONLY) && !writeEnable) ||
86 (((flags & O_ACCMODE) == O_RDONLY) && writeEnable))
87 {
88 lock_.unlock();
89 return -EACCES;
90 }
91 }
92 else
93 {
94 /* file not open yet, open and intialize metadata */
95 int32_t result = 0;
96 if (flags & O_CREAT)
97 {
98 result = sl_FsOpen((const unsigned char *)path,
99 SL_FS_CREATE |
100 SL_FS_CREATE_MAX_SIZE(maxSizeOnCreate),
101 nullptr);
102 }
103 if (result > 0)
104 {
105 writeEnable = true;
106 }
107 else if (flags & O_WRONLY)
108 {
109 result = sl_FsOpen((const unsigned char *)path, SL_FS_WRITE,
110 nullptr);
111 writeEnable = true;
112 }
113 else
114 {
115 result = sl_FsOpen((const unsigned char *)path, SL_FS_READ,
116 nullptr);
117 writeEnable = false;
118 }
119 if (result < 0)
120 {
121 /* error occured in opening file */
122 handle = -1;
123 lock_.unlock();
124 switch (result)
125 {
126 case SL_ERROR_FS_INVALID_MAGIC_NUM:
127 sl_FsDel((const unsigned char *)path, 0);
128 handle = sl_FsOpen((const unsigned char *)path,
129 SL_FS_CREATE |
130 SL_FS_CREATE_MAX_SIZE(maxSizeOnCreate),
131 nullptr);
132 sl_FsClose(handle, nullptr, nullptr, 0);
133 // This error happens when the file was created but not
134 // closed and thus the FAT entry is not correctly
135 // written. The workaround is to re-create the file, so
136 // we'll return as if it was nonexistant.
137 case SL_ERROR_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY:
138 // we fake a not existent error here. When the file gets
139 // opened for write, that will work.
140
141 return -ENOENT;
142 case SL_ERROR_FS_FILE_NOT_EXISTS:
143 return -ENOENT;
144 default:
145 SlCheckError(result);
146 return -ENOENT;
147 }
148 }
149 handle = result;
150 SlFsFileInfo_t info;
151 sl_FsGetInfo((const unsigned char *)path, 0, &info);
152 size = info.Len;
153 maxSize = info.MaxSize;
154 }
155 lock_.unlock();
156
157 return Node::open(file, path, flags, mode);
158}
159
160/*
161 * CC32xxDeviceFile::disable()
162 */
164{
165 if (handle > 0 && references_ <= 0)
166 {
167 sl_FsClose(handle, nullptr, nullptr, 0);
168 handle = -1;
169 writeEnable = false;
170 }
171}
172
173/*
174 * CC32xxDeviceFile::write()
175 */
176ssize_t CC32xxDeviceFile::write(unsigned int index, const void *buf, size_t len)
177{
178 if ((index + len) > maxSize)
179 {
180 return -EFBIG;
181 }
182
183 int32_t result = sl_FsWrite(handle, index, (unsigned char *)buf, len);
184
185 SlCheckError(result);
186
187 if ((index + result) > size)
188 {
189 size = index + result;
190 }
191
192 return result;
193}
194
195/*
196 * CC32xxDeviceFile::read()
197 */
198ssize_t CC32xxDeviceFile::read(unsigned int index, void *buf, size_t len)
199{
200 if (index >= size)
201 {
202 /* read starting position beyond end of file */
203 return 0;
204 }
205
206 if ((index + len) > size)
207 {
208 len = size - index;
209 }
210
211 int32_t result = sl_FsRead(handle, index, (unsigned char *)buf, len);
212
213 SlCheckError(result);
214
215 return result;
216}
217
218int CC32xxDeviceFile::fstat(File* file, struct stat *stat) {
219 Node::fstat(file, stat);
220 SlFsFileInfo_t fs_file_info;
221 int ret = sl_FsGetInfo((const uint8_t*)name, 0, &fs_file_info);
222 SlCheckResult(ret);
223 stat->st_size = fs_file_info.Len;
224 stat->st_blocks = fs_file_info.MaxSize / 512;
225 return 0;
226}
void SlCheckResult(int result, int expected)
Tests that a SimpleLink request has succeeded.
void SlCheckError(int result)
Tests that a SimpleLink request has succeeded (return >= 0).
int32_t handle
file handle
uint32_t size
size of file in bytes
int fstat(File *file, struct stat *stat) override
Get the status information of a file or device.
uint32_t maxSize
max size of file in bytes
int32_t maxSizeOnCreate
max size of file upon creation
ssize_t read(unsigned int index, void *buf, size_t len) override
Read from the CC32xxDeviceFile.
bool writeEnable
is the file open for write
ssize_t write(unsigned int index, const void *buf, size_t len) override
Write to the CC32xxDeviceFile.
int open(File *file, const char *path, int flags, int mode) override
Open a device.
void disable() OVERRIDE
function to disable device
const char * name
device name
Definition Devtab.hxx:266
virtual int fstat(File *file, struct stat *stat) override
Get the status information of a file or device.
unsigned int references_
number of open references
Definition Devtab.hxx:592
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
static int stat(struct _reent *reent, const char *path, struct stat *stat)
Get the status information of a file or device.
Definition Device.cxx:165
File information.
Definition Devtab.hxx:52
off_t offset
current offset within file
Definition Devtab.hxx:62