-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathfs_device.c
More file actions
224 lines (175 loc) · 5.2 KB
/
fs_device.c
File metadata and controls
224 lines (175 loc) · 5.2 KB
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
/*
fs_device.c - redirector for file i/o to stream (device)
Part of grblHAL
Copyright (c) 2025 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "vfs.h"
#include "stream.h"
#include "nuts_bolts.h"
struct fs_device {
io_stream_t io_stream;
stream_claim_ptr open;
char name[10];
bool write;
vfs_file_t file;
struct fs_device *next;
};
typedef struct fs_device fs_device_t;
static fs_device_t *root = NULL;
bool device_fs_add_stream (io_stream_properties_t *stream)
{
if(!stream->flags.claimable || !stream->flags.modbus_ready)
return false;
fs_device_t *add = calloc(1, sizeof(fs_device_t));
if(add) {
switch(stream->type) {
case StreamType_Serial:
strcpy(add->name, /*stream->flags.is_usb*/ 0 ? "usb" : "uart");
break;
case StreamType_Bluetooth:
strcpy(add->name, "bt");
break;
default: break;
}
add->open = stream->claim;
strcat(add->name, uitoa(stream->instance));
if(root == NULL)
root = add;
else {
fs_device_t *fss = root;
while(fss->next)
fss = fss->next;
fss->next = add;
}
}
return add != NULL;
}
static fs_device_t *find (const char *filename)
{
fs_device_t *device = root;
if(device) do {
if(!strcmp(device->name, filename + 1))
break;
} while((device = device->next));
return device;
}
static inline io_stream_t *iostream (io_stream_t **io_stream)
{
return (io_stream_t *)*io_stream;
}
static inline io_stream_t *iostream_ptr (io_stream_t *io_stream)
{
return io_stream;
}
static vfs_file_t *fs_open (const char *filename, const char *mode)
{
vfs_file_t *file = NULL;
fs_device_t *device = find(filename);
if(device && (device->write = strchr(mode, 'w') != NULL)) {
const io_stream_t *io_stream;
if(device->open) {
if((io_stream = device->open(115200))) {
memcpy(&device->io_stream, io_stream, sizeof(io_stream_t));
device->open = NULL;
} else
return NULL;
}
if(device->io_stream.state.passthru)
return NULL;
device->io_stream.state.passthru = On; // flag open
if((file = calloc(1, sizeof(vfs_file_t) + sizeof(io_stream_t *)))) {
io_stream = &device->io_stream;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess"
#endif
memcpy(iostream_ptr((io_stream_t *)&file->handle), &io_stream, sizeof(io_stream_t *));
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}
}
return file;
}
static void fs_close (vfs_file_t *file)
{
iostream((io_stream_t **)&file->handle)->state.passthru = Off;
free(file);
}
static size_t fs_read (void *buffer, size_t size, size_t count, vfs_file_t *file)
{
uint8_t *pos = (uint8_t *)buffer;
size_t rcount = iostream((io_stream_t **)&file->handle)->get_rx_buffer_count();
if((rcount = min(rcount, size * count))) {
while(rcount--)
*pos++ = (uint8_t)iostream((io_stream_t **)&file->handle)->read();
}
return rcount;
}
static size_t fs_write (const void *buffer, size_t size, size_t count, vfs_file_t *file)
{
iostream((io_stream_t **)&file->handle)->write_n((uint8_t *)buffer, size * count);
return size * count;
}
static size_t fs_tell (vfs_file_t *file)
{
return 0;
}
static bool fs_eof (vfs_file_t *file)
{
return iostream((io_stream_t **)&file->handle)->get_rx_buffer_count() == 0;
}
static int fs_unlink (const char *filename)
{
return -1;
}
static int fs_dirop (const char *path)
{
return -1;
}
static vfs_dir_t *fs_opendir (const char *path)
{
return NULL;
}
static void fs_closedir (vfs_dir_t *dir)
{
}
static int fs_stat (const char *filename, vfs_stat_t *st)
{
fs_device_t *device = find(filename);
st->st_size = device ? device->io_stream.get_rx_buffer_count() : 0;
return device ? 0 : -1;
}
void fs_device_mount (void)
{
PROGMEM static const vfs_t fs = {
.fopen = fs_open,
.fclose = fs_close,
.fread = fs_read,
.fwrite = fs_write,
.ftell = fs_tell,
.feof = fs_eof,
.funlink = fs_unlink,
.fmkdir = fs_dirop,
.fchdir = fs_dirop,
.frmdir = fs_dirop,
.fopendir = fs_opendir,
.fclosedir = fs_closedir,
.fstat = fs_stat
};
static bool ok = false;
if(!ok)
ok = vfs_mount("/dev", &fs, (vfs_st_mode_t){ .directory = true, .hidden = true });
}