forked from embedded2013/freertos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfio.c
329 lines (277 loc) · 7.98 KB
/
fio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <string.h>
#include <FreeRTOS.h>
#include <semphr.h>
#include <unistd.h>
#include <stdarg.h> /* For variable list */
#include "fio.h"
#include "filesystem.h"
#include "osdebug.h"
#include "hash-djb2.h"
#include "util.h"
/* send_byte() is in main.c
* TODO: Separate USART function out of main.c */
extern void send_byte(char ch);
extern char receive_byte();
static struct fddef_t fio_fds[MAX_FDS];
static ssize_t stdin_read(void * opaque, void * buf, size_t count) {
int i;
char * data = buf;
for (i = 0; i < count; i++)
data[i] = receive_byte();
return count;
}
static ssize_t stdout_write(void * opaque, const void * buf, size_t count) {
int i;
const char * data = (const char *) buf;
for (i = 0; i < count; i++)
send_byte(data[i]);
return count;
}
static xSemaphoreHandle fio_sem = NULL;
__attribute__((constructor)) void fio_init() {
memset(fio_fds, 0, sizeof(fio_fds));
fio_fds[0].fdread = stdin_read;
fio_fds[1].fdwrite = stdout_write;
fio_fds[2].fdwrite = stdout_write;
fio_sem = xSemaphoreCreateMutex();
}
struct fddef_t * fio_getfd(int fd) {
if ((fd < 0) || (fd >= MAX_FDS))
return NULL;
return fio_fds + fd;
}
static int fio_is_open_int(int fd) {
if ((fd < 0) || (fd >= MAX_FDS))
return 0;
int r = !((fio_fds[fd].fdread == NULL) &&
(fio_fds[fd].fdwrite == NULL) &&
(fio_fds[fd].fdseek == NULL) &&
(fio_fds[fd].fdclose == NULL) &&
(fio_fds[fd].opaque == NULL));
return r;
}
static int fio_findfd() {
int i;
for (i = 0; i < MAX_FDS; i++) {
if (!fio_is_open_int(i))
return i;
}
return -1;
}
int fio_is_open(int fd) {
int r = 0;
xSemaphoreTake(fio_sem, portMAX_DELAY);
r = fio_is_open_int(fd);
xSemaphoreGive(fio_sem);
return r;
}
int fio_open(fdread_t fdread, fdwrite_t fdwrite, fdseek_t fdseek, fdclose_t fdclose, void * opaque) {
int fd;
// DBGOUT("fio_open(%p, %p, %p, %p, %p)\r\n", fdread, fdwrite, fdseek, fdclose, opaque);
xSemaphoreTake(fio_sem, portMAX_DELAY);
fd = fio_findfd();
if (fd >= 0) {
fio_fds[fd].fdread = fdread;
fio_fds[fd].fdwrite = fdwrite;
fio_fds[fd].fdseek = fdseek;
fio_fds[fd].fdclose = fdclose;
fio_fds[fd].opaque = opaque;
}
xSemaphoreGive(fio_sem);
return fd;
}
ssize_t fio_read(int fd, void * buf, size_t count) {
ssize_t r = 0;
// DBGOUT("fio_read(%i, %p, %i)\r\n", fd, buf, count);
if (fio_is_open_int(fd)) {
if (fio_fds[fd].fdread) {
r = fio_fds[fd].fdread(fio_fds[fd].opaque, buf, count);
} else {
r = -3;
}
} else {
r = -2;
}
return r;
}
ssize_t fio_write(int fd, const void * buf, size_t count) {
ssize_t r = 0;
// DBGOUT("fio_write(%i, %p, %i)\r\n", fd, buf, count);
if (fio_is_open_int(fd)) {
if (fio_fds[fd].fdwrite) {
r = fio_fds[fd].fdwrite(fio_fds[fd].opaque, buf, count);
} else {
r = -3;
}
} else {
r = -2;
}
return r;
}
off_t fio_seek(int fd, off_t offset, int whence) {
off_t r = 0;
// DBGOUT("fio_seek(%i, %i, %i)\r\n", fd, offset, whence);
if (fio_is_open_int(fd)) {
if (fio_fds[fd].fdseek) {
r = fio_fds[fd].fdseek(fio_fds[fd].opaque, offset, whence);
} else {
r = -3;
}
} else {
r = -2;
}
return r;
}
int fio_close(int fd) {
int r = 0;
// DBGOUT("fio_close(%i)\r\n", fd);
if (fio_is_open_int(fd)) {
if (fio_fds[fd].fdclose)
r = fio_fds[fd].fdclose(fio_fds[fd].opaque);
xSemaphoreTake(fio_sem, portMAX_DELAY);
memset(fio_fds + fd, 0, sizeof(struct fddef_t));
xSemaphoreGive(fio_sem);
} else {
r = -2;
}
return r;
}
void fio_set_opaque(int fd, void * opaque) {
if (fio_is_open_int(fd))
fio_fds[fd].opaque = opaque;
}
#define stdin_hash 0x0BA00421
#define stdout_hash 0x7FA08308
#define stderr_hash 0x7FA058A3
static int devfs_open(void * opaque, const char * path, int flags, int mode) {
uint32_t h = hash_djb2((const uint8_t *) path, -1);
// DBGOUT("devfs_open(%p, \"%s\", %i, %i)\r\n", opaque, path, flags, mode);
switch (h) {
case stdin_hash:
if (flags & (O_WRONLY | O_RDWR))
return -1;
return fio_open(stdin_read, NULL, NULL, NULL, NULL);
break;
case stdout_hash:
if (flags & O_RDONLY)
return -1;
return fio_open(NULL, stdout_write, NULL, NULL, NULL);
break;
case stderr_hash:
if (flags & O_RDONLY)
return -1;
return fio_open(NULL, stdout_write, NULL, NULL, NULL);
break;
}
return -1;
}
void register_devfs() {
DBGOUT("Registering devfs.\r\n");
register_fs("dev", devfs_open, NULL);
}
int puts(const char *msg)
{
if (!msg) {
return -1;
}
return (int)fio_write(1, msg, strlen(msg));
}
static int printf_cb(char *dest, const char *src)
{
return puts(src);
}
static int sprintf_cb(char *dest, const char *src)
{
return (int)strcat(dest, src);
}
typedef int (*proc_str_func_t)(char *, const char *);
/* Common body for sprintf and printf */
static int base_printf(proc_str_func_t proc_str, \
char *dest, const char *fmt_str, va_list param)
{
char param_chr[] = {0, 0};
int param_int = 0;
long int param_lint = 0;
char *str_to_output = 0;
char itoa_buf[MAX_ITOA_CHARS] = {0};
int curr_char = 0;
/* Make sure strlen(dest) is 0
* for first strcat */
if (dest) {
dest[0] = 0;
}
/* Let's parse */
while (fmt_str[curr_char]) {
/* Deal with normal string
* increase index by 1 here */
if (fmt_str[curr_char++] != '%') {
param_chr[0] = fmt_str[curr_char - 1];
str_to_output = param_chr;
}
/* % case-> retrive latter params */
else {
switch (fmt_str[curr_char]) {
case 'S':
case 's':
{
str_to_output = va_arg(param, char *);
}
break;
case 'd':
case 'D':
{
param_int = va_arg(param, int);
str_to_output = itoa(param_int, itoa_buf);
}
break;
case 'X':
case 'x':
{
param_int = va_arg(param, int);
str_to_output = htoa(param_int, itoa_buf);
}
break;
case 'P':
case 'p':
{
param_lint = va_arg(param, long int);
str_to_output = addrtoa(param_lint, itoa_buf);
}
break;
case 'c':
case 'C':
{
param_chr[0] = (char) va_arg(param, int);
str_to_output = param_chr;
break;
}
default:
{
param_chr[0] = fmt_str[curr_char];
str_to_output = param_chr;
}
} /* switch (fmt_str[curr_char]) */
curr_char++;
} /* if (fmt_str[curr_char++] == '%') */
proc_str(dest, str_to_output);
} /* while (fmt_str[curr_char]) */
return curr_char;
}
int sprintf(char *str, const char *format, ...)
{
int rval = 0;
va_list param = {0};
va_start(param, format);
rval = base_printf(sprintf_cb, (char *)str, format, param);
va_end(param);
return rval;
}
int printf(const char *format, ...)
{
int rval = 0;
va_list param = {0};
va_start(param, format);
rval = base_printf(printf_cb, (char *)0, format, param);
va_end(param);
return rval;
}