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
|
#include "kernel.h"
#include "fd.h"
#include "fifo.h"
#include "kmalloc.h"
#include "ext2.h"
#include "log.h"
#include "lib/string/string.h"
#include "lib/printf/printf.h"
#include "ringbuffer.h"
bool fd_write(fd* f,uint8_t c)
{
return f->write(f->data,c);
}
uint8_t fd_read(fd* f)
{
return f->read(f->data);
}
bool fd_has(fd* f)
{
return f->has(f->data);
}
bool fd_eof(fd* f)
{
return f->eof(f->data);
}
bool fd_close(fd* f)
{
return f->close(f->data);
}
//----
void* ext2_init(char *path)
{
uint32_t *data=kballoc(1);
uint32_t inode= ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,path);
data[0]=inode;// inode
data[1]=0; //pos
return data;
}
void ext2_write(void* x,uint8_t b)
{
kpanic("not impl");
}
uint8_t ext2_read(uint32_t* data)
{
char c;
ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1);
return c;
}
bool ext2_has(uint32_t* data)
{
return 1;
}
bool ext2_eof(uint32_t* data)
{
uint32_t save=data[1];
uint32_t c=ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1);
data[1]=save;
return (c==0);
}
void ext2_close(void* x)
{
kbfree(x);
}
uint8_t sysfs_get(uint32_t *data)
{
return ringbuffer_get(data);
}
void sysfs_write(void* x,uint8_t b)
{
uint32_t **pp=x+sizeof(ringbuffer);
void (*p)(uint32_t v)=*pp;
uint32_t *val=x+sizeof(ringbuffer)+4;
val[val[0]]=b;
if(val[0]==4) // 4bytes / 32bit
{
val[0]=1;
p(val[1]+256*val[2]+256*256*val[3]+256*256*256*val[4]);
}
val[0]+=1;
}
bool sysfs_has(uint32_t *data)
{
return true;
}
bool sysfs_eof(uint32_t *data)
{
return !ringbuffer_has(data);
}
void sysfs_close(uint32_t *data)
{
ringbuffer_free(data); // free ringbuffer buffer
kbfree(data); // free memory holding ringbuffer information
}
void pipe_close(uint32_t *data)
{
ringbuffer *r=data;
ringbuffer_free(data); // free ringbuffer buffer
kbfree(data); // free memory holding ringbuffer information
}
void get_sysfs_data(ringbuffer *r,char *format_string, ...)
{
char buf[256];
va_list va;
va_start(va,format_string);
tfp_vsprintf(buf,format_string,va);
va_end(va);
for(int i=0;i<strlen(buf);i++)
ringbuffer_put(r,buf[i]);
ringbuffer_put(r,'\n');
}
bool fifo_eof(uint32_t* data)
{
return false;
}
// TODO remove and use fd_from_ringbuffer?
fd fd_from_fifo(fifo* fif)
{
fd f;
f.type=FD_TYPE_FIFO_BUFFERED;
f.data=fif;
f.read=fifo_get;
f.write=fifo_put;
f.eof=fifo_eof;
// f.close=fd_close; //TODO.. etc also otehr and cleanups
f.has=fifo_has;
return f;
}
fd fd_from_pipe()
{
fd f;
f.type=FD_TYPE_PIPE;
f.data=kballoc(1);
ringbuffer r=ringbuffer_init(1);
memcpy(f.data,&r,sizeof(ringbuffer));
/*
f.read=sysfs_get;
f.write=0;
f.has=sysfs_has;
f.eof=sysfs_eof;
*/
f.close=pipe_close;
return f;
}
fd fd_from_path(char* path)
{
fd f;
f.type=FD_TYPE_EXT2_FILE;
f.data=ext2_init(path);
f.read=ext2_read;
f.write=ext2_write;
f.close=ext2_close;
f.eof=ext2_eof;
f.has=ext2_has;
return f;
}
fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...)),void (*h)(uint32_t in))
{
fd f;
f.type=FD_TYPE_SYSFS;
f.data=kballoc(1);
uint32_t **pp=f.data+sizeof(ringbuffer);
*pp=h;
uint32_t *val=f.data+sizeof(ringbuffer)+4;
val[0]=1; // waiting for first byte of the 32bit value
ringbuffer r=ringbuffer_init(1);
g(&r,get_sysfs_data);
memcpy(f.data,&r,sizeof(ringbuffer));
f.read=sysfs_get;
f.write=sysfs_write;
f.close=sysfs_close;
f.has=sysfs_has;
f.eof=sysfs_eof;
return f;
}
|