~foura/midifs

ref: d084b72774385e5930b74cdfabca164768a39877 midifs/main.c -rw-r--r-- 3.4 KiB
d084b727james palmer implement reads of the ctl file 3 months ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <fcall.h>
#include <9p.h>

#include "midi.h"

enum {
	Qroot,
		Qctl,
		Qmidi,
		Qraw,
};

typedef struct Dirtab Dirtab;
struct Dirtab {
	char *name;
	Qid qid;
	ulong mode;
};

Dirtab root = { "/", {Qroot, 0, QTDIR}, 0555|DMDIR };
Dirtab roottab[] = {
	"ctl",  {Qctl,  0, QTFILE}, 0444,
	"midi", {Qmidi, 0, QTFILE}, 0444,
	"raw",  {Qraw,  0, QTFILE}, 0444,
};

Dirtab*
filebypath(uvlong path)
{
	int i;

	if(path == Qroot)
		return &root;
	for(i = 0; i < nelem(roottab); i++)
		if(path == roottab[i].qid.path)
			return &roottab[i];

	return nil;
}

void
fillstat(Dir *d, uvlong path, char *user)
{
	Dirtab *f;

	f = filebypath(path);

	d->name = estrdup9p(f->name);
	d->qid = f->qid;
	d->mode = f->mode;
	d->uid = estrdup9p(user);
	d->gid = estrdup9p(user);
	d->muid = estrdup9p(user);
	d->length = 0;
	d->atime = time(0);
	d->mtime = time(0);
}

int
rootgen(int n, Dir *d, void *aux)
{
	Req *r = aux;
	
	if(n >= nelem(roottab))
		return -1;
	fillstat(d, roottab[n].qid.path, r->fid->uid);
	return 0;
}

void
fsattach(Req *r)
{
	r->fid->qid = filebypath(Qroot)->qid;
	r->ofcall.qid = r->fid->qid;
	respond(r, nil);
}

void
fsread(Req *r)
{
	Midi *dev = r->srv->aux;
	uvlong path = r->fid->qid.path;
	long count;
	char buf[512];
	
	switch(path) {
	case Qroot:
		dirread9p(r, rootgen, r);
		respond(r, nil);
		break;
	case Qctl:
		snprint(buf, sizeof buf, "channel %d\ncable %d\n", dev->chan, dev->cable);
		readstr(r, buf);
		respond(r, nil);
		break;
	case Qraw:
		/* FIXME: this may block the srv loop */
		count = read(dev->fd, r->ofcall.data, r->ifcall.count);
		if(count < 0) {
			respond(r, "couldn't read from device");
			break;
		}

		r->ofcall.count = count;
		respond(r, nil);
		break;
	default:
		respond(r, "no.");
	}
}

void
fswrite(Req *r)
{
	Midi *dev = r->srv->aux;
	uvlong path = r->fid->qid.path;
	long count;

	switch(path) {
	case Qraw:
		/* FIXME: this may block the srv loop */
		count = write(dev->fd, r->ifcall.data, r->ifcall.count);
		if(count < 0) {
			respond(r, "couldn't write to device");
			break;
		}

		r->ofcall.count = count;
		respond(r, nil);
		break;
	default:
		respond(r, "no.");
	}
}

void
fsstat(Req *r)
{
	fillstat(&r->d, r->fid->qid.path, r->fid->uid);
	respond(r, nil);
	return;
}

char *
fswalk1(Fid *fid, char *name, Qid *qid)
{
	int i;

	switch(fid->qid.path) {
	case Qroot:
		for(i = 0; i <= nelem(roottab); i++) {
			if(strcmp(roottab[i].name, name) == 0) {
				*qid = roottab[i].qid;
				fid->qid = *qid;
				return nil;
			}
		}

		if(strcmp("..", name) == 0) {
			*qid = root.qid;
			fid->qid = *qid;
			return nil;
		}

		break;
	}

	return "not found";
}

void
fsend(Srv *s)
{
	freedev(s->aux);
}

Srv fs = {
	.attach = fsattach,
	.read = fsread,
	.write = fswrite,
	.stat = fsstat,
	.walk1 = fswalk1,

	.end = fsend,
};

void
usage(void)
{
	fprint(2, "usage: %s [-Dp] [-m mtpt] [-s srvn] device\n", argv0);	
	exits("usage");
}


void
main(int argc, char *argv[])
{
	Midi *dev;
	int pipe = 0;
	char *mtpt = "/mnt/midi";
	char *srvn = nil;

	ARGBEGIN {
	case 'p':
		pipe = 1;
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 's':
		srvn = EARGF(usage());
		break;
	case 'D':
		chatty9p++;
		break;
	default:
		usage();
	} ARGEND

	if(argc < 1)
		usage();

	dev = pipe
		? mkpipedev(argv[0])
		: mkusbdev(argv[0]);
	if(!dev) sysfatal("couldn't create device: %r");
	fs.aux = dev;

	postmountsrv(&fs, srvn, mtpt, MREPL);
	exits(nil);
}