Annotation of src/usr.bin/aucat/sock.c, Revision 1.55
1.55 ! ratchov 1: /* $OpenBSD: sock.c,v 1.54 2011/04/08 13:00:57 ratchov Exp $ */
1.1 ratchov 2: /*
3: * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17:
1.44 ratchov 18: #include <sys/types.h>
19: #include <sys/socket.h>
20: #include <unistd.h>
1.1 ratchov 21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
1.20 ratchov 24:
25: #include "abuf.h"
1.1 ratchov 26: #include "aproc.h"
1.20 ratchov 27: #include "conf.h"
1.1 ratchov 28: #include "dev.h"
1.19 ratchov 29: #include "midi.h"
30: #include "opt.h"
1.20 ratchov 31: #include "sock.h"
1.37 ratchov 32: #ifdef DEBUG
33: #include "dbg.h"
34: #endif
1.1 ratchov 35:
1.42 ratchov 36: void sock_attach(struct sock *, int);
1.1 ratchov 37: int sock_read(struct sock *);
38: int sock_write(struct sock *);
39: int sock_execmsg(struct sock *);
40: void sock_reset(struct sock *);
1.44 ratchov 41: void sock_close(struct file *);
1.1 ratchov 42:
43: struct fileops sock_ops = {
44: "sock",
45: sizeof(struct sock),
1.44 ratchov 46: sock_close,
1.1 ratchov 47: pipe_read,
48: pipe_write,
49: NULL, /* start */
50: NULL, /* stop */
51: pipe_nfds,
52: pipe_pollfd,
53: pipe_revents
54: };
1.30 ratchov 55:
1.37 ratchov 56: #ifdef DEBUG
57: void
58: sock_dbg(struct sock *f)
59: {
1.51 ratchov 60: static char *pstates[] = {
61: "hel", "ini", "sta", "rdy", "run", "stp", "mid"
62: };
1.37 ratchov 63: static char *rstates[] = { "rdat", "rmsg", "rret" };
64: static char *wstates[] = { "widl", "wmsg", "wdat" };
1.48 ratchov 65: struct aproc *midi;
1.37 ratchov 66:
1.48 ratchov 67: midi = f->dev ? f->dev->midi : NULL;
68: if (f->slot >= 0 && APROC_OK(midi)) {
69: dbg_puts(midi->u.ctl.slot[f->slot].name);
70: dbg_putu(midi->u.ctl.slot[f->slot].unit);
1.37 ratchov 71: } else
72: dbg_puts(f->pipe.file.name);
73: dbg_puts("/");
74: dbg_puts(pstates[f->pstate]);
75: dbg_puts("|");
76: dbg_puts(rstates[f->rstate]);
77: dbg_puts("|");
78: dbg_puts(wstates[f->wstate]);
79: }
80: #endif
1.1 ratchov 81:
1.34 ratchov 82: void sock_setvol(void *, unsigned);
1.35 ratchov 83: void sock_startreq(void *);
1.42 ratchov 84: void sock_stopreq(void *);
1.48 ratchov 85: void sock_quitreq(void *);
1.42 ratchov 86: void sock_locreq(void *, unsigned);
1.34 ratchov 87:
88: struct ctl_ops ctl_sockops = {
89: sock_setvol,
1.42 ratchov 90: sock_startreq,
91: sock_stopreq,
1.48 ratchov 92: sock_locreq,
93: sock_quitreq
1.34 ratchov 94: };
95:
1.44 ratchov 96: unsigned sock_sesrefs = 0; /* connections to the session */
97: uid_t sock_sesuid; /* owner of the session */
98:
99: void
1.48 ratchov 100: sock_close(struct file *arg)
1.44 ratchov 101: {
1.48 ratchov 102: struct sock *f = (struct sock *)arg;
103:
1.44 ratchov 104: sock_sesrefs--;
1.48 ratchov 105: pipe_close(&f->pipe.file);
106: if (f->dev) {
107: dev_unref(f->dev);
108: f->dev = NULL;
109: }
1.44 ratchov 110: }
111:
1.1 ratchov 112: void
113: rsock_done(struct aproc *p)
114: {
115: struct sock *f = (struct sock *)p->u.io.file;
116:
1.11 ratchov 117: if (f == NULL)
118: return;
1.1 ratchov 119: sock_reset(f);
120: f->pipe.file.rproc = NULL;
121: if (f->pipe.file.wproc) {
1.34 ratchov 122: if (f->slot >= 0)
1.48 ratchov 123: ctl_slotdel(f->dev->midi, f->slot);
1.1 ratchov 124: aproc_del(f->pipe.file.wproc);
125: file_del(&f->pipe.file);
126: }
1.11 ratchov 127: p->u.io.file = NULL;
1.1 ratchov 128: }
129:
130: int
131: rsock_in(struct aproc *p, struct abuf *ibuf_dummy)
132: {
133: struct sock *f = (struct sock *)p->u.io.file;
134: struct abuf *obuf;
135:
136: if (!sock_read(f))
137: return 0;
1.45 ratchov 138: obuf = LIST_FIRST(&p->outs);
1.34 ratchov 139: if (obuf && f->pstate >= SOCK_RUN) {
1.1 ratchov 140: if (!abuf_flush(obuf))
141: return 0;
142: }
143: return 1;
144: }
145:
146: int
147: rsock_out(struct aproc *p, struct abuf *obuf)
148: {
149: struct sock *f = (struct sock *)p->u.io.file;
150:
1.19 ratchov 151: if (f->pipe.file.state & FILE_RINUSE)
1.1 ratchov 152: return 0;
153:
1.12 ratchov 154: /*
1.20 ratchov 155: * When calling sock_read(), we may receive a ``STOP'' command,
1.1 ratchov 156: * and detach ``obuf''. In this case, there's no more caller and
1.20 ratchov 157: * we'll stop processing further messages, resulting in a deadlock.
1.1 ratchov 158: * The solution is to iterate over sock_read() in order to
159: * consume all messages().
160: */
161: for (;;) {
162: if (!sock_read(f))
1.12 ratchov 163: return 0;
1.1 ratchov 164: }
165: return 1;
166: }
167:
168: void
169: rsock_eof(struct aproc *p, struct abuf *ibuf_dummy)
170: {
171: aproc_del(p);
172: }
173:
174: void
175: rsock_hup(struct aproc *p, struct abuf *ibuf)
176: {
177: aproc_del(p);
178: }
179:
180: void
181: rsock_opos(struct aproc *p, struct abuf *obuf, int delta)
182: {
183: struct sock *f = (struct sock *)p->u.io.file;
184:
1.51 ratchov 185: if (f->mode & MODE_RECMASK)
1.14 ratchov 186: return;
187:
188: f->delta += delta;
1.37 ratchov 189: #ifdef DEBUG
190: if (debug_level >= 4) {
191: aproc_dbg(p);
192: dbg_puts(": moved to delta = ");
193: dbg_puti(f->delta);
194: dbg_puts("\n");
195: }
196: #endif
1.14 ratchov 197: f->tickpending++;
1.1 ratchov 198: for (;;) {
199: if (!sock_write(f))
200: break;
201: }
202: }
203:
204: struct aproc_ops rsock_ops = {
205: "rsock",
206: rsock_in,
207: rsock_out,
208: rsock_eof,
209: rsock_hup,
210: NULL, /* newin */
211: NULL, /* newout */
212: NULL, /* ipos */
213: rsock_opos,
214: rsock_done
215: };
216:
217: void
218: wsock_done(struct aproc *p)
219: {
220: struct sock *f = (struct sock *)p->u.io.file;
221:
1.11 ratchov 222: if (f == NULL)
223: return;
1.1 ratchov 224: sock_reset(f);
225: f->pipe.file.wproc = NULL;
226: if (f->pipe.file.rproc) {
1.34 ratchov 227: if (f->slot >= 0)
1.48 ratchov 228: ctl_slotdel(f->dev->midi, f->slot);
1.1 ratchov 229: aproc_del(f->pipe.file.rproc);
230: file_del(&f->pipe.file);
231: }
1.11 ratchov 232: p->u.io.file = NULL;
1.1 ratchov 233: }
234:
235: int
236: wsock_in(struct aproc *p, struct abuf *ibuf)
237: {
238: struct sock *f = (struct sock *)p->u.io.file;
239:
1.19 ratchov 240: if (f->pipe.file.state & FILE_WINUSE)
1.1 ratchov 241: return 0;
242: /*
1.20 ratchov 243: * See remark in rsock_out().
1.1 ratchov 244: */
245: for (;;) {
246: if (!sock_write(f))
247: return 0;
248: }
249: return 1;
250: }
251:
252: int
253: wsock_out(struct aproc *p, struct abuf *obuf_dummy)
254: {
1.45 ratchov 255: struct abuf *ibuf = LIST_FIRST(&p->ins);
1.1 ratchov 256: struct sock *f = (struct sock *)p->u.io.file;
257:
258: if (ibuf) {
259: if (!abuf_fill(ibuf))
260: return 0;
261: }
262: if (!sock_write(f))
263: return 0;
264: return 1;
265: }
266:
267: void
268: wsock_eof(struct aproc *p, struct abuf *obuf)
269: {
270: aproc_del(p);
271: }
272:
273: void
274: wsock_hup(struct aproc *p, struct abuf *obuf_dummy)
275: {
276: aproc_del(p);
277: }
278:
279: void
280: wsock_ipos(struct aproc *p, struct abuf *obuf, int delta)
281: {
282: struct sock *f = (struct sock *)p->u.io.file;
283:
1.51 ratchov 284: if (!(f->mode & MODE_RECMASK))
1.14 ratchov 285: return;
1.1 ratchov 286:
1.14 ratchov 287: f->delta += delta;
1.37 ratchov 288: #ifdef DEBUG
289: if (debug_level >= 4) {
290: aproc_dbg(p);
291: dbg_puts(": moved to delta = ");
292: dbg_puti(f->delta);
293: dbg_puts("\n");
294: }
295: #endif
1.33 ratchov 296: f->tickpending++;
1.1 ratchov 297: for (;;) {
298: if (!sock_write(f))
299: break;
300: }
301: }
302:
303: struct aproc_ops wsock_ops = {
304: "wsock",
305: wsock_in,
306: wsock_out,
307: wsock_eof,
308: wsock_hup,
309: NULL, /* newin */
310: NULL, /* newout */
311: wsock_ipos,
312: NULL, /* opos */
313: wsock_done
314: };
315:
316: /*
1.20 ratchov 317: * Initialise socket in the SOCK_HELLO state with default
318: * parameters.
1.1 ratchov 319: */
320: struct sock *
1.19 ratchov 321: sock_new(struct fileops *ops, int fd)
1.1 ratchov 322: {
323: struct aproc *rproc, *wproc;
324: struct sock *f;
1.44 ratchov 325: uid_t uid, gid;
326:
327: /*
328: * ensure that all connections belong to the same user,
329: * for privacy reasons.
330: *
331: * XXX: is there a portable way of doing this ?
332: */
333: if (getpeereid(fd, &uid, &gid) < 0) {
334: close(fd);
335: return NULL;
336: }
337: if (sock_sesrefs == 0) {
338: /* start a new session */
339: sock_sesuid = uid;
340: } else if (uid != sock_sesuid) {
341: /* session owned by another user, drop connection */
342: close(fd);
343: return NULL;
344: }
345: sock_sesrefs++;
1.1 ratchov 346:
1.19 ratchov 347: f = (struct sock *)pipe_new(ops, fd, "sock");
1.46 ratchov 348: if (f == NULL) {
349: close(fd);
350: return NULL;
351: }
1.18 ratchov 352: f->pstate = SOCK_HELLO;
1.1 ratchov 353: f->mode = 0;
1.48 ratchov 354: f->opt = NULL;
355: f->dev = NULL;
1.51 ratchov 356: f->xrun = XRUN_IGNORE;
1.14 ratchov 357: f->delta = 0;
358: f->tickpending = 0;
1.49 ratchov 359: f->startpos = 0;
1.42 ratchov 360: f->startpending = 0;
1.25 ratchov 361: f->vol = f->lastvol = MIDI_MAXCTL;
1.24 ratchov 362: f->slot = -1;
1.1 ratchov 363:
1.19 ratchov 364: wproc = aproc_new(&wsock_ops, f->pipe.file.name);
1.1 ratchov 365: wproc->u.io.file = &f->pipe.file;
1.42 ratchov 366: wproc->u.io.partial = 0;
1.1 ratchov 367: f->pipe.file.wproc = wproc;
368: f->wstate = SOCK_WIDLE;
369: f->wtodo = 0xdeadbeef;
370:
1.19 ratchov 371: rproc = aproc_new(&rsock_ops, f->pipe.file.name);
1.1 ratchov 372: rproc->u.io.file = &f->pipe.file;
1.42 ratchov 373: rproc->u.io.partial = 0;
1.1 ratchov 374: f->pipe.file.rproc = rproc;
375: f->rstate = SOCK_RMSG;
376: f->rtodo = sizeof(struct amsg);
377: return f;
378: }
379:
380: /*
1.20 ratchov 381: * Free buffers.
1.1 ratchov 382: */
383: void
384: sock_freebuf(struct sock *f)
385: {
386: struct abuf *rbuf, *wbuf;
1.12 ratchov 387:
1.1 ratchov 388: f->pstate = SOCK_INIT;
1.37 ratchov 389: #ifdef DEBUG
390: if (debug_level >= 3) {
391: sock_dbg(f);
392: dbg_puts(": freeing buffers\n");
393: }
394: #endif
1.45 ratchov 395: wbuf = LIST_FIRST(&f->pipe.file.wproc->ins);
396: rbuf = LIST_FIRST(&f->pipe.file.rproc->outs);
1.35 ratchov 397: if (rbuf || wbuf)
1.48 ratchov 398: ctl_slotstop(f->dev->midi, f->slot);
1.1 ratchov 399: if (rbuf)
400: abuf_eof(rbuf);
401: if (wbuf)
402: abuf_hup(wbuf);
1.32 ratchov 403: f->tickpending = 0;
1.42 ratchov 404: f->startpending = 0;
1.1 ratchov 405: }
406:
407: /*
1.20 ratchov 408: * Allocate buffers, so client can start filling write-end.
1.1 ratchov 409: */
410: void
411: sock_allocbuf(struct sock *f)
412: {
413: struct abuf *rbuf = NULL, *wbuf = NULL;
1.50 ratchov 414: unsigned bufsz;
1.1 ratchov 415:
1.50 ratchov 416: bufsz = f->bufsz + f->dev->bufsz / f->dev->round * f->round;
1.42 ratchov 417: f->pstate = SOCK_START;
1.51 ratchov 418: if (f->mode & MODE_PLAY) {
1.50 ratchov 419: rbuf = abuf_new(bufsz, &f->rpar);
1.1 ratchov 420: aproc_setout(f->pipe.file.rproc, rbuf);
1.42 ratchov 421: if (!ABUF_WOK(rbuf) || (f->pipe.file.state & FILE_EOF))
422: f->pstate = SOCK_READY;
1.50 ratchov 423: f->rmax = bufsz * aparams_bpf(&f->rpar);
1.1 ratchov 424: }
1.51 ratchov 425: if (f->mode & MODE_RECMASK) {
1.50 ratchov 426: wbuf = abuf_new(bufsz, &f->wpar);
1.1 ratchov 427: aproc_setin(f->pipe.file.wproc, wbuf);
1.42 ratchov 428: f->walign = f->round;
1.50 ratchov 429: f->wmax = 0;
1.1 ratchov 430: }
1.14 ratchov 431: f->delta = 0;
1.49 ratchov 432: f->startpos = 0;
1.14 ratchov 433: f->tickpending = 0;
1.42 ratchov 434: f->startpending = 0;
1.37 ratchov 435: #ifdef DEBUG
436: if (debug_level >= 3) {
437: sock_dbg(f);
438: dbg_puts(": allocating ");
439: dbg_putu(f->bufsz);
1.50 ratchov 440: dbg_puts("/");
441: dbg_putu(bufsz);
1.42 ratchov 442: dbg_puts(" fr buffers, rmax = ");
443: dbg_putu(f->rmax);
444: dbg_puts("\n");
1.37 ratchov 445: }
446: #endif
1.51 ratchov 447: if (f->mode & MODE_PLAY) {
1.42 ratchov 448: f->pstate = SOCK_START;
449: } else {
450: f->pstate = SOCK_READY;
1.48 ratchov 451: if (ctl_slotstart(f->dev->midi, f->slot))
1.42 ratchov 452: (void)sock_attach(f, 0);
453: }
1.1 ratchov 454: }
455:
456: /*
1.25 ratchov 457: * Set volume. Callback invoked when volume is modified externally
1.3 ratchov 458: */
459: void
1.25 ratchov 460: sock_setvol(void *arg, unsigned vol)
1.3 ratchov 461: {
1.25 ratchov 462: struct sock *f = (struct sock *)arg;
1.3 ratchov 463: struct abuf *rbuf;
1.12 ratchov 464:
1.3 ratchov 465: f->vol = vol;
1.45 ratchov 466: rbuf = LIST_FIRST(&f->pipe.file.rproc->outs);
1.3 ratchov 467: if (!rbuf) {
1.37 ratchov 468: #ifdef DEBUG
469: if (debug_level >= 3) {
470: sock_dbg(f);
471: dbg_puts(": no read buffer to set volume yet\n");
472: }
473: #endif
1.3 ratchov 474: return;
475: }
1.48 ratchov 476: dev_setvol(f->dev, rbuf, MIDI_TO_ADATA(vol));
1.3 ratchov 477: }
478:
479: /*
1.35 ratchov 480: * Attach the stream. Callback invoked when MMC start
481: */
482: void
483: sock_startreq(void *arg)
484: {
485: struct sock *f = (struct sock *)arg;
486:
1.37 ratchov 487: #ifdef DEBUG
1.42 ratchov 488: if (f->pstate != SOCK_READY) {
1.37 ratchov 489: sock_dbg(f);
1.42 ratchov 490: dbg_puts(": not in READY state\n");
1.37 ratchov 491: dbg_panic();
492: }
493: #endif
1.35 ratchov 494: (void)sock_attach(f, 0);
1.52 ratchov 495: }
496:
497: /*
1.42 ratchov 498: * Callback invoked by MMC stop
499: */
500: void
501: sock_stopreq(void *arg)
502: {
503: #ifdef DEBUG
504: struct sock *f = (struct sock *)arg;
505:
506: if (debug_level >= 3) {
507: sock_dbg(f);
508: dbg_puts(": ignored STOP signal\n");
509: }
510: #endif
511: }
512:
513: /*
514: * Callback invoked by MMC relocate, ignored
515: */
516: void
517: sock_locreq(void *arg, unsigned mmcpos)
518: {
519: #ifdef DEBUG
520: struct sock *f = (struct sock *)arg;
521:
522: if (debug_level >= 3) {
523: sock_dbg(f);
524: dbg_puts(": ignored RELOCATE signal\n");
525: }
526: #endif
527: }
528:
529: /*
1.48 ratchov 530: * Callback invoked when slot is gone
531: */
532: void
533: sock_quitreq(void *arg)
534: {
535: struct sock *f = (struct sock *)arg;
536:
537: #ifdef DEBUG
538: if (debug_level >= 3) {
539: sock_dbg(f);
540: dbg_puts(": slot gone\n");
541: }
542: #endif
543: file_close(&f->pipe.file);
544: }
545:
546: /*
1.51 ratchov 547: * Attach play and/or record buffers to the device
1.1 ratchov 548: */
1.42 ratchov 549: void
1.1 ratchov 550: sock_attach(struct sock *f, int force)
551: {
552: struct abuf *rbuf, *wbuf;
553:
1.45 ratchov 554: rbuf = LIST_FIRST(&f->pipe.file.rproc->outs);
555: wbuf = LIST_FIRST(&f->pipe.file.wproc->ins);
1.1 ratchov 556:
557: /*
1.20 ratchov 558: * If in SOCK_START state, dont attach until
559: * the buffer isn't completely filled.
1.1 ratchov 560: */
561: if (!force && rbuf && ABUF_WOK(rbuf))
1.42 ratchov 562: return;
1.12 ratchov 563:
1.42 ratchov 564: /*
1.46 ratchov 565: * start the device (dev_getpos() and dev_attach() must
566: * be called on a started device
567: */
1.48 ratchov 568: dev_wakeup(f->dev);
1.46 ratchov 569:
570: /*
1.42 ratchov 571: * get the current position, the origin is when
572: * the first sample is played/recorded
573: */
1.49 ratchov 574: f->startpos = dev_getpos(f->dev) * (int)f->round / (int)f->dev->round;
1.42 ratchov 575: f->startpending = 1;
576: f->pstate = SOCK_RUN;
1.37 ratchov 577: #ifdef DEBUG
578: if (debug_level >= 3) {
579: sock_dbg(f);
1.42 ratchov 580: dbg_puts(": attaching at ");
1.49 ratchov 581: dbg_puti(f->startpos);
1.42 ratchov 582: dbg_puts("\n");
1.37 ratchov 583: }
584: #endif
1.1 ratchov 585: /*
1.42 ratchov 586: * We dont check whether the device is dying,
587: * because dev_xxx() functions are supposed to
588: * work (i.e., not to crash)
1.1 ratchov 589: */
1.48 ratchov 590: dev_attach(f->dev, f->pipe.file.name, f->mode,
1.43 ratchov 591: rbuf, &f->rpar,
592: f->opt->join ? f->opt->rpar.cmax - f->opt->rpar.cmin + 1 : 0,
593: wbuf, &f->wpar,
594: f->opt->join ? f->opt->wpar.cmax - f->opt->wpar.cmin + 1 : 0,
595: f->xrun, f->opt->maxweight);
1.51 ratchov 596: if (f->mode & MODE_PLAY)
1.48 ratchov 597: dev_setvol(f->dev, rbuf, MIDI_TO_ADATA(f->vol));
1.1 ratchov 598: }
599:
600: void
601: sock_reset(struct sock *f)
602: {
603: switch (f->pstate) {
604: case SOCK_START:
1.42 ratchov 605: case SOCK_READY:
1.48 ratchov 606: if (ctl_slotstart(f->dev->midi, f->slot)) {
1.35 ratchov 607: (void)sock_attach(f, 1);
608: f->pstate = SOCK_RUN;
609: }
1.1 ratchov 610: /* PASSTHROUGH */
611: case SOCK_RUN:
612: sock_freebuf(f);
613: f->pstate = SOCK_INIT;
614: /* PASSTHROUGH */
615: case SOCK_INIT:
616: /* nothing yet */
617: break;
618: }
619: }
620:
621: /*
1.20 ratchov 622: * Read a message from the file descriptor, return 1 if done, 0
623: * otherwise. The message is stored in f->rmsg.
1.1 ratchov 624: */
625: int
626: sock_rmsg(struct sock *f)
627: {
628: unsigned count;
629: unsigned char *data;
630:
631: while (f->rtodo > 0) {
632: if (!(f->pipe.file.state & FILE_ROK)) {
1.37 ratchov 633: #ifdef DEBUG
634: if (debug_level >= 4) {
635: sock_dbg(f);
636: dbg_puts(": reading message blocked, ");
637: dbg_putu(f->rtodo);
638: dbg_puts(" bytes remaining\n");
639: }
640: #endif
1.1 ratchov 641: return 0;
642: }
643: data = (unsigned char *)&f->rmsg;
644: data += sizeof(struct amsg) - f->rtodo;
645: count = file_read(&f->pipe.file, data, f->rtodo);
646: if (count == 0)
647: return 0;
648: f->rtodo -= count;
649: }
1.37 ratchov 650: #ifdef DEBUG
651: if (debug_level >= 4) {
652: sock_dbg(f);
653: dbg_puts(": read full message\n");
654: }
655: #endif
1.1 ratchov 656: return 1;
657: }
658:
659: /*
1.20 ratchov 660: * Write a message to the file descriptor, return 1 if done, 0
1.1 ratchov 661: * otherwise. The "m" argument is f->rmsg or f->wmsg, and the "ptodo"
662: * points to the f->rtodo or f->wtodo respectively.
663: */
664: int
665: sock_wmsg(struct sock *f, struct amsg *m, unsigned *ptodo)
666: {
667: unsigned count;
668: unsigned char *data;
669:
670: while (*ptodo > 0) {
671: if (!(f->pipe.file.state & FILE_WOK)) {
1.37 ratchov 672: #ifdef DEBUG
673: if (debug_level >= 4) {
674: sock_dbg(f);
675: dbg_puts(": writing message blocked, ");
676: dbg_putu(*ptodo);
677: dbg_puts(" bytes remaining\n");
678: }
679: #endif
1.1 ratchov 680: return 0;
681: }
682: data = (unsigned char *)m;
683: data += sizeof(struct amsg) - *ptodo;
684: count = file_write(&f->pipe.file, data, *ptodo);
685: if (count == 0)
686: return 0;
687: *ptodo -= count;
688: }
1.37 ratchov 689: #ifdef DEBUG
690: if (debug_level >= 4) {
691: sock_dbg(f);
692: dbg_puts(": wrote full message\n");
693: }
694: #endif
1.1 ratchov 695: return 1;
696: }
697:
698: /*
1.20 ratchov 699: * Read data chunk from the file descriptor, return 1 if at least one
1.1 ratchov 700: * byte was read, 0 if the file blocked.
701: */
702: int
703: sock_rdata(struct sock *f)
704: {
705: struct aproc *p;
706: struct abuf *obuf;
1.42 ratchov 707: unsigned n;
1.1 ratchov 708:
1.37 ratchov 709: #ifdef DEBUG
1.55 ! ratchov 710: if (f->rtodo == 0) {
1.37 ratchov 711: sock_dbg(f);
712: dbg_puts(": data block already read\n");
713: dbg_panic();
714: }
715: #endif
1.1 ratchov 716: p = f->pipe.file.rproc;
1.45 ratchov 717: obuf = LIST_FIRST(&p->outs);
1.19 ratchov 718: if (obuf == NULL)
719: return 0;
1.42 ratchov 720: if (!ABUF_WOK(obuf) || !(f->pipe.file.state & FILE_ROK))
1.41 ratchov 721: return 0;
1.55 ! ratchov 722: if (!rfile_do(p, f->rtodo, &n))
! 723: return 0;
! 724: f->rtodo -= n;
! 725: if (f->pstate == SOCK_START) {
! 726: if (!ABUF_WOK(obuf) || (f->pipe.file.state & FILE_EOF))
! 727: f->pstate = SOCK_READY;
1.42 ratchov 728: }
1.1 ratchov 729: return 1;
730: }
731:
732: /*
1.20 ratchov 733: * Write data chunk to the file descriptor, return 1 if at least one
1.1 ratchov 734: * byte was written, 0 if the file blocked.
735: */
736: int
737: sock_wdata(struct sock *f)
738: {
739: struct aproc *p;
740: struct abuf *ibuf;
1.42 ratchov 741: unsigned n;
1.1 ratchov 742:
1.37 ratchov 743: #ifdef DEBUG
1.55 ! ratchov 744: if (f->wtodo == 0) {
1.37 ratchov 745: sock_dbg(f);
746: dbg_puts(": attempted to write zero-sized data block\n");
747: dbg_panic();
748: }
749: #endif
1.1 ratchov 750: if (!(f->pipe.file.state & FILE_WOK))
751: return 0;
752: p = f->pipe.file.wproc;
1.45 ratchov 753: ibuf = LIST_FIRST(&p->ins);
1.42 ratchov 754: #ifdef DEBUG
1.55 ! ratchov 755: if (ibuf == NULL) {
1.42 ratchov 756: sock_dbg(f);
757: dbg_puts(": attempted to write on detached buffer\n");
758: dbg_panic();
759: }
760: #endif
761: if (ibuf == NULL)
762: return 0;
763: if (!ABUF_ROK(ibuf))
764: return 0;
1.55 ! ratchov 765: if (!wfile_do(p, f->wtodo, &n))
! 766: return 0;
! 767: f->wtodo -= n;
1.1 ratchov 768: return 1;
769: }
770:
771: int
772: sock_setpar(struct sock *f)
773: {
774: struct amsg_par *p = &f->rmsg.u.par;
1.9 ratchov 775: unsigned min, max, rate;
1.12 ratchov 776:
1.1 ratchov 777: if (AMSG_ISSET(p->bits)) {
778: if (p->bits < BITS_MIN || p->bits > BITS_MAX) {
1.37 ratchov 779: #ifdef DEBUG
780: if (debug_level >= 1) {
781: sock_dbg(f);
782: dbg_puts(": ");
783: dbg_putu(p->bits);
784: dbg_puts(": bits out of bounds\n");
785: }
786: #endif
1.1 ratchov 787: return 0;
788: }
789: if (AMSG_ISSET(p->bps)) {
790: if (p->bps < ((p->bits + 7) / 8) || p->bps > 4) {
1.37 ratchov 791: #ifdef DEBUG
792: if (debug_level >= 1) {
793: sock_dbg(f);
794: dbg_puts(": ");
795: dbg_putu(p->bps);
796: dbg_puts(": wrong bytes per sample\n");
797: }
798: #endif
1.1 ratchov 799: return 0;
800: }
801: } else
802: p->bps = APARAMS_BPS(p->bits);
803: f->rpar.bits = f->wpar.bits = p->bits;
804: f->rpar.bps = f->wpar.bps = p->bps;
1.37 ratchov 805: #ifdef DEBUG
806: if (debug_level >= 3) {
807: sock_dbg(f);
808: dbg_puts(": using ");
809: dbg_putu(p->bits);
810: dbg_puts("bits, ");
811: dbg_putu(p->bps);
812: dbg_puts(" bytes per sample\n");
813: }
814: #endif
1.1 ratchov 815: }
816: if (AMSG_ISSET(p->sig))
817: f->rpar.sig = f->wpar.sig = p->sig ? 1 : 0;
818: if (AMSG_ISSET(p->le))
819: f->rpar.le = f->wpar.le = p->le ? 1 : 0;
820: if (AMSG_ISSET(p->msb))
821: f->rpar.msb = f->wpar.msb = p->msb ? 1 : 0;
1.51 ratchov 822: if (AMSG_ISSET(p->rchan) && (f->mode & MODE_RECMASK)) {
1.1 ratchov 823: if (p->rchan < 1)
824: p->rchan = 1;
1.6 ratchov 825: if (p->rchan > NCHAN_MAX)
826: p->rchan = NCHAN_MAX;
1.19 ratchov 827: f->wpar.cmin = f->opt->wpar.cmin;
828: f->wpar.cmax = f->opt->wpar.cmin + p->rchan - 1;
829: if (f->wpar.cmax > f->opt->wpar.cmax)
830: f->wpar.cmax = f->opt->wpar.cmax;
1.37 ratchov 831: #ifdef DEBUG
832: if (debug_level >= 3) {
833: sock_dbg(f);
834: dbg_puts(": using recording channels ");
835: dbg_putu(f->wpar.cmin);
836: dbg_puts("..");
837: dbg_putu(f->wpar.cmax);
838: dbg_puts("\n");
839: }
840: #endif
1.1 ratchov 841: }
1.51 ratchov 842: if (AMSG_ISSET(p->pchan) && (f->mode & MODE_PLAY)) {
1.1 ratchov 843: if (p->pchan < 1)
844: p->pchan = 1;
1.6 ratchov 845: if (p->pchan > NCHAN_MAX)
846: p->pchan = NCHAN_MAX;
1.19 ratchov 847: f->rpar.cmin = f->opt->rpar.cmin;
848: f->rpar.cmax = f->opt->rpar.cmin + p->pchan - 1;
849: if (f->rpar.cmax > f->opt->rpar.cmax)
850: f->rpar.cmax = f->opt->rpar.cmax;
1.37 ratchov 851: #ifdef DEBUG
852: if (debug_level >= 3) {
853: sock_dbg(f);
854: dbg_puts(": using playback channels ");
855: dbg_putu(f->rpar.cmin);
856: dbg_puts("..");
857: dbg_putu(f->rpar.cmax);
858: dbg_puts("\n");
859: }
860: #endif
1.1 ratchov 861: }
862: if (AMSG_ISSET(p->rate)) {
863: if (p->rate < RATE_MIN)
864: p->rate = RATE_MIN;
865: if (p->rate > RATE_MAX)
866: p->rate = RATE_MAX;
1.48 ratchov 867: f->round = dev_roundof(f->dev, p->rate);
1.1 ratchov 868: f->rpar.rate = f->wpar.rate = p->rate;
1.10 ratchov 869: if (!AMSG_ISSET(p->appbufsz)) {
1.48 ratchov 870: p->appbufsz = f->dev->bufsz / f->dev->round * f->round;
1.37 ratchov 871: #ifdef DEBUG
872: if (debug_level >= 3) {
873: sock_dbg(f);
874: dbg_puts(": using ");
875: dbg_putu(p->appbufsz);
876: dbg_puts(" fr app buffer size\n");
877: }
878: #endif
879: }
880: #ifdef DEBUG
881: if (debug_level >= 3) {
882: sock_dbg(f);
883: dbg_puts(": using ");
884: dbg_putu(p->rate);
885: dbg_puts("Hz sample rate, ");
886: dbg_putu(f->round);
887: dbg_puts(" fr block size\n");
1.10 ratchov 888: }
1.37 ratchov 889: #endif
1.1 ratchov 890: }
891: if (AMSG_ISSET(p->xrun)) {
1.51 ratchov 892: if (p->xrun != XRUN_IGNORE &&
893: p->xrun != XRUN_SYNC &&
894: p->xrun != XRUN_ERROR) {
1.37 ratchov 895: #ifdef DEBUG
896: if (debug_level >= 1) {
897: sock_dbg(f);
898: dbg_puts(": ");
899: dbg_putx(p->xrun);
900: dbg_puts(": bad xrun policy\n");
901: }
902: #endif
1.1 ratchov 903: return 0;
904: }
905: f->xrun = p->xrun;
1.51 ratchov 906: if (f->opt->mmc && f->xrun == XRUN_IGNORE)
907: f->xrun = XRUN_SYNC;
1.37 ratchov 908: #ifdef DEBUG
909: if (debug_level >= 3) {
910: sock_dbg(f);
911: dbg_puts(": using 0x");
912: dbg_putx(f->xrun);
913: dbg_puts(" xrun policy\n");
914: }
915: #endif
1.1 ratchov 916: }
1.10 ratchov 917: if (AMSG_ISSET(p->appbufsz)) {
1.51 ratchov 918: rate = (f->mode & MODE_PLAY) ? f->rpar.rate : f->wpar.rate;
1.42 ratchov 919: min = 1;
1.48 ratchov 920: max = 1 + rate / f->dev->round;
1.9 ratchov 921: min *= f->round;
922: max *= f->round;
1.10 ratchov 923: p->appbufsz += f->round - 1;
924: p->appbufsz -= p->appbufsz % f->round;
925: if (p->appbufsz < min)
926: p->appbufsz = min;
927: if (p->appbufsz > max)
928: p->appbufsz = max;
929: f->bufsz = p->appbufsz;
1.37 ratchov 930: #ifdef DEBUG
931: if (debug_level >= 3) {
932: sock_dbg(f);
933: dbg_puts(": using ");
934: dbg_putu(f->bufsz);
935: dbg_puts(" buffer size\n");
936: }
937: #endif
938: }
939: #ifdef DEBUG
940: if (debug_level >= 2) {
1.48 ratchov 941: if (APROC_OK(f->dev->midi)) {
942: dbg_puts(f->dev->midi->u.ctl.slot[f->slot].name);
943: dbg_putu(f->dev->midi->u.ctl.slot[f->slot].unit);
1.37 ratchov 944: } else
945: dbg_puts(f->pipe.file.name);
946: dbg_puts(": buffer size = ");
947: dbg_putu(f->bufsz);
1.51 ratchov 948: if (f->mode & MODE_PLAY) {
1.37 ratchov 949: dbg_puts(", play = ");
950: aparams_dbg(&f->rpar);
951: }
1.51 ratchov 952: if (f->mode & MODE_RECMASK) {
1.37 ratchov 953: dbg_puts(", rec:");
954: aparams_dbg(&f->wpar);
955: }
956: dbg_puts("\n");
1.1 ratchov 957: }
1.37 ratchov 958: #endif
1.1 ratchov 959: return 1;
960: }
961:
1.19 ratchov 962: /*
963: * allocate buffers, so client can start filling write-end.
964: */
965: void
1.51 ratchov 966: sock_midiattach(struct sock *f)
1.19 ratchov 967: {
968: struct abuf *rbuf = NULL, *wbuf = NULL;
969:
1.51 ratchov 970: if (f->mode & MODE_MIDIOUT) {
1.22 ratchov 971: rbuf = abuf_new(MIDI_BUFSZ, &aparams_none);
1.19 ratchov 972: aproc_setout(f->pipe.file.rproc, rbuf);
973: }
1.51 ratchov 974: if (f->mode & MODE_MIDIIN) {
1.22 ratchov 975: wbuf = abuf_new(MIDI_BUFSZ, &aparams_none);
1.19 ratchov 976: aproc_setin(f->pipe.file.wproc, wbuf);
977: }
1.51 ratchov 978: f->pstate = SOCK_MIDI;
1.48 ratchov 979: dev_midiattach(f->dev, rbuf, wbuf);
1.19 ratchov 980: }
981:
1.17 ratchov 982: int
983: sock_hello(struct sock *f)
984: {
985: struct amsg_hello *p = &f->rmsg.u.hello;
986:
1.37 ratchov 987: #ifdef DEBUG
988: if (debug_level >= 3) {
989: sock_dbg(f);
990: dbg_puts(": hello from <");
991: dbg_puts(p->who);
1.51 ratchov 992: dbg_puts(">, mode = ");
993: dbg_putx(p->mode);
1.37 ratchov 994: dbg_puts(", ver ");
995: dbg_putu(p->version);
996: dbg_puts("\n");
997: }
998: #endif
1.33 ratchov 999: if (p->version != AMSG_VERSION) {
1.37 ratchov 1000: #ifdef DEBUG
1001: if (debug_level >= 1) {
1002: sock_dbg(f);
1003: dbg_puts(": ");
1004: dbg_putu(p->version);
1.51 ratchov 1005: dbg_puts(": unsupported protocol version\n");
1006: }
1007: #endif
1008: return 0;
1009: }
1010: switch (p->mode) {
1011: case MODE_MIDIIN:
1012: case MODE_MIDIOUT:
1013: case MODE_MIDIOUT | MODE_MIDIIN:
1014: case MODE_REC:
1015: case MODE_PLAY:
1016: case MODE_PLAY | MODE_REC:
1017: break;
1018: default:
1019: #ifdef DEBUG
1020: if (debug_level >= 1) {
1021: sock_dbg(f);
1022: dbg_puts(": ");
1023: dbg_putx(p->mode);
1024: dbg_puts(": unsupported mode\n");
1.37 ratchov 1025: }
1026: #endif
1.33 ratchov 1027: return 0;
1028: }
1.48 ratchov 1029: f->opt = opt_byname(p->opt);
1030: if (f->opt == NULL)
1031: return 0;
1032: if (!dev_ref(f->opt->dev))
1033: return 0;
1.51 ratchov 1034: if ((p->mode & MODE_REC) && (f->opt->mode & MODE_MON)) {
1035: p->mode &= ~MODE_REC;
1036: p->mode |= MODE_MON;
1037: }
1.48 ratchov 1038: f->dev = f->opt->dev;
1.51 ratchov 1039: f->mode = (p->mode & f->opt->mode) & f->dev->mode;
1.37 ratchov 1040: #ifdef DEBUG
1.51 ratchov 1041: if (debug_level >= 3) {
1042: sock_dbg(f);
1043: dbg_puts(": using mode = ");
1044: dbg_putx(f->mode);
1045: dbg_puts("\n");
1046: }
1.37 ratchov 1047: #endif
1.51 ratchov 1048: if (f->mode != p->mode) {
1.37 ratchov 1049: #ifdef DEBUG
1050: if (debug_level >= 1) {
1051: sock_dbg(f);
1.51 ratchov 1052: dbg_puts(": requested mode not available\n");
1.37 ratchov 1053: }
1054: #endif
1.17 ratchov 1055: return 0;
1056: }
1.51 ratchov 1057: if (f->mode & (MODE_MIDIOUT | MODE_MIDIIN)) {
1058: sock_midiattach(f);
1059: return 1;
1.24 ratchov 1060: }
1.51 ratchov 1061: if (f->mode & MODE_PLAY)
1062: f->rpar = f->opt->rpar;
1063: if (f->mode & MODE_RECMASK)
1064: f->wpar = f->opt->wpar;
1065: f->xrun = (f->opt->mmc) ? XRUN_SYNC : XRUN_IGNORE;
1066: f->bufsz = f->dev->bufsz;
1067: f->round = f->dev->round;
1068: f->slot = ctl_slotnew(f->dev->midi, p->who,
1069: &ctl_sockops, f,
1070: f->opt->mmc);
1071: if (f->slot < 0)
1072: return 0;
1.18 ratchov 1073: f->pstate = SOCK_INIT;
1.17 ratchov 1074: return 1;
1075: }
1076:
1.1 ratchov 1077: /*
1.20 ratchov 1078: * Execute message in f->rmsg and change the state accordingly; return 1
1.1 ratchov 1079: * on success, and 0 on failure, in which case the socket is destroyed.
1080: */
1081: int
1082: sock_execmsg(struct sock *f)
1083: {
1084: struct amsg *m = &f->rmsg;
1.42 ratchov 1085: struct abuf *obuf;
1.1 ratchov 1086:
1087: switch (m->cmd) {
1088: case AMSG_DATA:
1.37 ratchov 1089: #ifdef DEBUG
1090: if (debug_level >= 4) {
1091: sock_dbg(f);
1092: dbg_puts(": DATA message\n");
1093: }
1094: #endif
1.55 ! ratchov 1095: if (f->pstate != SOCK_MIDI && f->pstate != SOCK_RUN &&
! 1096: f->pstate != SOCK_START && f->pstate != SOCK_READY) {
1.37 ratchov 1097: #ifdef DEBUG
1098: if (debug_level >= 1) {
1099: sock_dbg(f);
1100: dbg_puts(": DATA, bad state\n");
1101: }
1102: #endif
1.8 ratchov 1103: aproc_del(f->pipe.file.rproc);
1104: return 0;
1105: }
1.55 ! ratchov 1106: if (!(f->mode & (MODE_PLAY | MODE_MIDIOUT))) {
1.37 ratchov 1107: #ifdef DEBUG
1108: if (debug_level >= 1) {
1109: sock_dbg(f);
1110: dbg_puts(": DATA not allowed in record-only mode\n");
1111: }
1112: #endif
1.1 ratchov 1113: aproc_del(f->pipe.file.rproc);
1114: return 0;
1115: }
1.45 ratchov 1116: obuf = LIST_FIRST(&f->pipe.file.rproc->outs);
1.42 ratchov 1117: if (f->pstate == SOCK_START && !ABUF_WOK(obuf)) {
1.37 ratchov 1118: #ifdef DEBUG
1119: if (debug_level >= 1) {
1120: sock_dbg(f);
1121: dbg_puts(": DATA client violates flow control\n");
1122: }
1123: #endif
1.34 ratchov 1124: aproc_del(f->pipe.file.rproc);
1125: return 0;
1126: }
1.42 ratchov 1127: if (m->u.data.size % obuf->bpf != 0) {
1128: #ifdef DEBUG
1129: if (debug_level >= 1) {
1130: sock_dbg(f);
1131: dbg_puts(": unaligned data chunk\n");
1132: }
1133: #endif
1134: aproc_del(f->pipe.file.rproc);
1135: return 0;
1136: }
1.1 ratchov 1137: f->rstate = SOCK_RDATA;
1.42 ratchov 1138: f->rtodo = m->u.data.size / obuf->bpf;
1139: #ifdef DEBUG
1.55 ! ratchov 1140: if (debug_level >= 2 &&
! 1141: f->pstate != SOCK_MIDI && f->rtodo > f->rmax) {
1.42 ratchov 1142: sock_dbg(f);
1143: dbg_puts(": received past current position, rtodo = ");
1144: dbg_putu(f->rtodo);
1145: dbg_puts(", rmax = ");
1146: dbg_putu(f->rmax);
1147: dbg_puts("\n");
1148: aproc_del(f->pipe.file.rproc);
1149: return 0;
1150: }
1151: #endif
1.55 ! ratchov 1152: if (f->pstate != SOCK_MIDI)
! 1153: f->rmax -= f->rtodo;
1.1 ratchov 1154: if (f->rtodo == 0) {
1.37 ratchov 1155: #ifdef DEBUG
1156: if (debug_level >= 1) {
1157: sock_dbg(f);
1158: dbg_puts(": zero-length data chunk\n");
1159: }
1160: #endif
1.1 ratchov 1161: aproc_del(f->pipe.file.rproc);
1162: return 0;
1163: }
1164: break;
1165: case AMSG_START:
1.37 ratchov 1166: #ifdef DEBUG
1167: if (debug_level >= 3) {
1168: sock_dbg(f);
1169: dbg_puts(": START message\n");
1170: }
1171: #endif
1.1 ratchov 1172: if (f->pstate != SOCK_INIT) {
1.37 ratchov 1173: #ifdef DEBUG
1174: if (debug_level >= 1) {
1175: sock_dbg(f);
1176: dbg_puts(": START, bad state\n");
1177: }
1178: #endif
1.1 ratchov 1179: aproc_del(f->pipe.file.rproc);
1180: return 0;
1181: }
1182: sock_allocbuf(f);
1183: f->rstate = SOCK_RMSG;
1184: f->rtodo = sizeof(struct amsg);
1185: break;
1186: case AMSG_STOP:
1.37 ratchov 1187: #ifdef DEBUG
1188: if (debug_level >= 3) {
1189: sock_dbg(f);
1190: dbg_puts(": STOP message\n");
1191: }
1192: #endif
1.42 ratchov 1193: if (f->pstate != SOCK_RUN &&
1194: f->pstate != SOCK_START && f->pstate != SOCK_READY) {
1.37 ratchov 1195: #ifdef DEBUG
1196: if (debug_level >= 1) {
1197: sock_dbg(f);
1198: dbg_puts(": STOP, bad state\n");
1199: }
1200: #endif
1.1 ratchov 1201: aproc_del(f->pipe.file.rproc);
1202: return 0;
1.52 ratchov 1203: }
1.42 ratchov 1204: /*
1205: * XXX: device could have desappeared at this point,
1206: * see how this is fixed in wav.c
1207: */
1208: if ((f->pstate == SOCK_START || f->pstate == SOCK_READY) &&
1.48 ratchov 1209: ctl_slotstart(f->dev->midi, f->slot))
1.1 ratchov 1210: (void)sock_attach(f, 1);
1.42 ratchov 1211: if (f->wstate != SOCK_WDATA || f->wtodo == 0)
1212: sock_freebuf(f);
1213: else
1214: f->pstate = SOCK_STOP;
1.1 ratchov 1215: AMSG_INIT(m);
1.55 ! ratchov 1216: m->cmd = AMSG_STOP;
1.1 ratchov 1217: f->rstate = SOCK_RRET;
1218: f->rtodo = sizeof(struct amsg);
1219: break;
1220: case AMSG_SETPAR:
1.37 ratchov 1221: #ifdef DEBUG
1222: if (debug_level >= 3) {
1223: sock_dbg(f);
1224: dbg_puts(": SETPAR message\n");
1225: }
1226: #endif
1.1 ratchov 1227: if (f->pstate != SOCK_INIT) {
1.37 ratchov 1228: #ifdef DEBUG
1229: if (debug_level >= 1) {
1230: sock_dbg(f);
1231: dbg_puts(": SETPAR, bad state\n");
1232: }
1233: #endif
1.1 ratchov 1234: aproc_del(f->pipe.file.rproc);
1235: return 0;
1236: }
1237: if (!sock_setpar(f)) {
1238: aproc_del(f->pipe.file.rproc);
1239: return 0;
1240: }
1241: f->rtodo = sizeof(struct amsg);
1242: f->rstate = SOCK_RMSG;
1243: break;
1244: case AMSG_GETPAR:
1.37 ratchov 1245: #ifdef DEBUG
1246: if (debug_level >= 3) {
1247: sock_dbg(f);
1248: dbg_puts(": GETPAR message\n");
1249: }
1250: #endif
1.1 ratchov 1251: if (f->pstate != SOCK_INIT) {
1.37 ratchov 1252: #ifdef DEBUG
1253: if (debug_level >= 1) {
1254: sock_dbg(f);
1255: dbg_puts(": GETPAR, bad state\n");
1256: }
1257: #endif
1.1 ratchov 1258: aproc_del(f->pipe.file.rproc);
1259: return 0;
1260: }
1261: AMSG_INIT(m);
1262: m->cmd = AMSG_GETPAR;
1.18 ratchov 1263: m->u.par.legacy_mode = f->mode;
1.51 ratchov 1264: if (f->mode & MODE_PLAY) {
1.42 ratchov 1265: m->u.par.bits = f->rpar.bits;
1266: m->u.par.bps = f->rpar.bps;
1267: m->u.par.sig = f->rpar.sig;
1268: m->u.par.le = f->rpar.le;
1269: m->u.par.msb = f->rpar.msb;
1270: m->u.par.rate = f->rpar.rate;
1271: m->u.par.pchan = f->rpar.cmax - f->rpar.cmin + 1;
1272: }
1.51 ratchov 1273: if (f->mode & MODE_RECMASK) {
1.42 ratchov 1274: m->u.par.bits = f->wpar.bits;
1275: m->u.par.bps = f->wpar.bps;
1276: m->u.par.sig = f->wpar.sig;
1277: m->u.par.le = f->wpar.le;
1278: m->u.par.msb = f->wpar.msb;
1279: m->u.par.rate = f->wpar.rate;
1280: m->u.par.rchan = f->wpar.cmax - f->wpar.cmin + 1;
1281: }
1.10 ratchov 1282: m->u.par.appbufsz = f->bufsz;
1.12 ratchov 1283: m->u.par.bufsz =
1.48 ratchov 1284: f->bufsz + (f->dev->bufsz / f->dev->round) * f->round;
1.1 ratchov 1285: m->u.par.round = f->round;
1286: f->rstate = SOCK_RRET;
1287: f->rtodo = sizeof(struct amsg);
1288: break;
1289: case AMSG_GETCAP:
1.37 ratchov 1290: #ifdef DEBUG
1291: if (debug_level >= 3) {
1292: sock_dbg(f);
1293: dbg_puts(": GETCAP message\n");
1294: }
1295: #endif
1.1 ratchov 1296: if (f->pstate != SOCK_INIT) {
1.37 ratchov 1297: #ifdef DEBUG
1298: if (debug_level >= 1) {
1299: sock_dbg(f);
1300: dbg_puts(": GETCAP, bad state\n");
1301: }
1302: #endif
1.1 ratchov 1303: aproc_del(f->pipe.file.rproc);
1304: return 0;
1305: }
1306: AMSG_INIT(m);
1307: m->cmd = AMSG_GETCAP;
1.48 ratchov 1308: m->u.cap.rate = f->dev->rate;
1.42 ratchov 1309: m->u.cap.pchan = (f->opt->mode & MODE_PLAY) ?
1.19 ratchov 1310: (f->opt->rpar.cmax - f->opt->rpar.cmin + 1) : 0;
1.42 ratchov 1311: m->u.cap.rchan = (f->opt->mode & (MODE_PLAY | MODE_REC)) ?
1.19 ratchov 1312: (f->opt->wpar.cmax - f->opt->wpar.cmin + 1) : 0;
1.53 ratchov 1313: m->u.cap.bits = ADATA_BITS;
1314: m->u.cap.bps = sizeof(adata_t);
1.1 ratchov 1315: f->rstate = SOCK_RRET;
1316: f->rtodo = sizeof(struct amsg);
1.3 ratchov 1317: break;
1318: case AMSG_SETVOL:
1.37 ratchov 1319: #ifdef DEBUG
1320: if (debug_level >= 3) {
1321: sock_dbg(f);
1322: dbg_puts(": SETVOL message\n");
1323: }
1324: #endif
1.42 ratchov 1325: if (f->pstate != SOCK_RUN && f->pstate != SOCK_START &&
1326: f->pstate != SOCK_INIT && f->pstate != SOCK_READY) {
1.37 ratchov 1327: #ifdef DEBUG
1328: if (debug_level >= 1) {
1329: sock_dbg(f);
1330: dbg_puts(": SETVOL, bad state\n");
1331: }
1332: #endif
1.3 ratchov 1333: aproc_del(f->pipe.file.rproc);
1334: return 0;
1335: }
1336: if (m->u.vol.ctl > MIDI_MAXCTL) {
1.37 ratchov 1337: #ifdef DEBUG
1338: if (debug_level >= 1) {
1339: sock_dbg(f);
1340: dbg_puts(": SETVOL, volume out of range\n");
1341: }
1342: #endif
1.3 ratchov 1343: aproc_del(f->pipe.file.rproc);
1344: return 0;
1345: }
1.25 ratchov 1346: sock_setvol(f, m->u.vol.ctl);
1.34 ratchov 1347: if (f->slot >= 0)
1.48 ratchov 1348: ctl_slotvol(f->dev->midi, f->slot, m->u.vol.ctl);
1.3 ratchov 1349: f->rtodo = sizeof(struct amsg);
1350: f->rstate = SOCK_RMSG;
1.17 ratchov 1351: break;
1352: case AMSG_HELLO:
1.37 ratchov 1353: #ifdef DEBUG
1354: if (debug_level >= 3) {
1355: sock_dbg(f);
1356: dbg_puts(": HELLO message\n");
1357: }
1358: #endif
1.18 ratchov 1359: if (f->pstate != SOCK_HELLO) {
1.37 ratchov 1360: #ifdef DEBUG
1361: if (debug_level >= 1) {
1362: sock_dbg(f);
1363: dbg_puts(": HELLO, bad state\n");
1364: }
1365: #endif
1.17 ratchov 1366: aproc_del(f->pipe.file.rproc);
1367: return 0;
1368: }
1369: if (!sock_hello(f)) {
1370: aproc_del(f->pipe.file.rproc);
1371: return 0;
1372: }
1373: AMSG_INIT(m);
1374: m->cmd = AMSG_ACK;
1375: f->rstate = SOCK_RRET;
1376: f->rtodo = sizeof(struct amsg);
1.1 ratchov 1377: break;
1.28 ratchov 1378: case AMSG_BYE:
1.37 ratchov 1379: #ifdef DEBUG
1380: if (debug_level >= 3) {
1381: sock_dbg(f);
1382: dbg_puts(": BYE message\n");
1383: }
1384: #endif
1.55 ! ratchov 1385: if (f->pstate != SOCK_INIT && f->pstate != SOCK_MIDI) {
1.37 ratchov 1386: #ifdef DEBUG
1387: if (debug_level >= 1) {
1388: sock_dbg(f);
1389: dbg_puts(": BYE, bad state\n");
1390: }
1391: #endif
1.29 ratchov 1392: }
1.28 ratchov 1393: aproc_del(f->pipe.file.rproc);
1394: return 0;
1.1 ratchov 1395: default:
1.37 ratchov 1396: #ifdef DEBUG
1397: if (debug_level >= 1) {
1398: sock_dbg(f);
1399: dbg_puts(": unknown command in message\n");
1400: }
1401: #endif
1.1 ratchov 1402: aproc_del(f->pipe.file.rproc);
1403: return 0;
1404: }
1405: return 1;
1406: }
1407:
1408: /*
1.20 ratchov 1409: * Create a new data/pos message.
1.1 ratchov 1410: */
1411: int
1412: sock_buildmsg(struct sock *f)
1413: {
1414: struct aproc *p;
1415: struct abuf *ibuf;
1.42 ratchov 1416: unsigned size, max;
1.12 ratchov 1417:
1.1 ratchov 1418: /*
1.49 ratchov 1419: * Send initial position
1420: */
1421: if (f->startpending) {
1422: #ifdef DEBUG
1423: if (debug_level >= 4) {
1424: sock_dbg(f);
1425: dbg_puts(": building POS message, pos = ");
1426: dbg_puti(f->startpos);
1427: dbg_puts("\n");
1428: }
1429: #endif
1430: AMSG_INIT(&f->wmsg);
1431: f->wmsg.cmd = AMSG_POS;
1432: f->wmsg.u.ts.delta = f->startpos;
1.50 ratchov 1433: f->rmax += f->startpos;
1.49 ratchov 1434: f->wtodo = sizeof(struct amsg);
1435: f->wstate = SOCK_WMSG;
1436: f->startpending = 0;
1437: return 1;
1438: }
1439:
1440: /*
1.20 ratchov 1441: * If pos changed, build a MOVE message.
1.1 ratchov 1442: */
1.49 ratchov 1443: if (f->tickpending) {
1.37 ratchov 1444: #ifdef DEBUG
1445: if (debug_level >= 4) {
1446: sock_dbg(f);
1.49 ratchov 1447: dbg_puts(": building MOVE message, delta = ");
1.37 ratchov 1448: dbg_puti(f->delta);
1449: dbg_puts("\n");
1450: }
1451: #endif
1.42 ratchov 1452: f->wmax += f->delta;
1.49 ratchov 1453: f->rmax += f->delta;
1.1 ratchov 1454: AMSG_INIT(&f->wmsg);
1455: f->wmsg.cmd = AMSG_MOVE;
1.14 ratchov 1456: f->wmsg.u.ts.delta = f->delta;
1.1 ratchov 1457: f->wtodo = sizeof(struct amsg);
1458: f->wstate = SOCK_WMSG;
1.14 ratchov 1459: f->delta = 0;
1460: f->tickpending = 0;
1.25 ratchov 1461: return 1;
1462: }
1463:
1464: /*
1465: * if volume changed build a SETVOL message
1466: */
1.27 ratchov 1467: if (f->pstate >= SOCK_START && f->vol != f->lastvol) {
1.37 ratchov 1468: #ifdef DEBUG
1469: if (debug_level >= 4) {
1470: sock_dbg(f);
1471: dbg_puts(": building SETVOL message, vol = ");
1472: dbg_puti(f->vol);
1473: dbg_puts("\n");
1474: }
1475: #endif
1.25 ratchov 1476: AMSG_INIT(&f->wmsg);
1477: f->wmsg.cmd = AMSG_SETVOL;
1478: f->wmsg.u.vol.ctl = f->vol;
1479: f->wtodo = sizeof(struct amsg);
1480: f->wstate = SOCK_WMSG;
1481: f->lastvol = f->vol;
1.1 ratchov 1482: return 1;
1483: }
1484:
1485: /*
1.20 ratchov 1486: * If data available, build a DATA message.
1.1 ratchov 1487: */
1488: p = f->pipe.file.wproc;
1.45 ratchov 1489: ibuf = LIST_FIRST(&p->ins);
1.1 ratchov 1490: if (ibuf && ABUF_ROK(ibuf)) {
1.42 ratchov 1491: #ifdef DEBUG
1.55 ! ratchov 1492: if (debug_level >= 3 &&
! 1493: f->pstate != SOCK_MIDI && ibuf->used > f->wmax) {
1.42 ratchov 1494: sock_dbg(f);
1.46 ratchov 1495: dbg_puts(": attempt to send past current position: used = ");
1496: dbg_putu(ibuf->used);
1497: dbg_puts(" wmax = ");
1498: dbg_putu(f->wmax);
1499: dbg_puts("\n");
1.42 ratchov 1500: }
1501: #endif
1502: size = ibuf->used;
1.55 ! ratchov 1503: if (f->pstate == SOCK_MIDI) {
! 1504: if (size > AMSG_DATAMAX)
! 1505: size = AMSG_DATAMAX;
! 1506: if (size == 0)
! 1507: return 0;
! 1508: } else {
! 1509: max = AMSG_DATAMAX / ibuf->bpf;
! 1510: if (size > max)
! 1511: size = max;
! 1512: if (size > f->walign)
! 1513: size = f->walign;
! 1514: if (size > f->wmax)
! 1515: size = f->wmax;
! 1516: if (size == 0)
! 1517: return 0;
! 1518: f->walign -= size;
! 1519: f->wmax -= size;
! 1520: if (f->walign == 0)
! 1521: f->walign = f->round;
! 1522: size *= ibuf->bpf;
! 1523: }
1.1 ratchov 1524: AMSG_INIT(&f->wmsg);
1525: f->wmsg.cmd = AMSG_DATA;
1.55 ! ratchov 1526: f->wmsg.u.data.size = size;
1.1 ratchov 1527: f->wtodo = sizeof(struct amsg);
1528: f->wstate = SOCK_WMSG;
1529: return 1;
1530: }
1.37 ratchov 1531: #ifdef DEBUG
1532: if (debug_level >= 4) {
1533: sock_dbg(f);
1534: dbg_puts(": no messages to build anymore, idling...\n");
1535: }
1536: #endif
1.1 ratchov 1537: f->wstate = SOCK_WIDLE;
1538: return 0;
1539: }
1540:
1541: /*
1.20 ratchov 1542: * Read from the socket file descriptor, fill input buffer and update
1.1 ratchov 1543: * the state. Return 1 if at least one message or 1 data byte was
1544: * processed, 0 if something blocked.
1545: */
1546: int
1547: sock_read(struct sock *f)
1548: {
1.37 ratchov 1549: #ifdef DEBUG
1550: if (debug_level >= 4) {
1551: sock_dbg(f);
1.39 ratchov 1552: dbg_puts(": reading ");
1.37 ratchov 1553: dbg_putu(f->rtodo);
1.39 ratchov 1554: dbg_puts(" todo\n");
1.37 ratchov 1555: }
1556: #endif
1.1 ratchov 1557: switch (f->rstate) {
1558: case SOCK_RMSG:
1559: if (!sock_rmsg(f))
1560: return 0;
1561: if (!sock_execmsg(f))
1562: return 0;
1563: break;
1564: case SOCK_RDATA:
1565: if (!sock_rdata(f))
1566: return 0;
1.55 ! ratchov 1567: if (f->rtodo == 0) {
1.1 ratchov 1568: f->rstate = SOCK_RMSG;
1569: f->rtodo = sizeof(struct amsg);
1570: }
1.35 ratchov 1571: /*
1.42 ratchov 1572: * XXX: sock_attach() may not start if there's not enough
1.52 ratchov 1573: * samples queued, if so ctl_slotstart() will trigger
1.42 ratchov 1574: * other streams, but this one won't start.
1.35 ratchov 1575: */
1.48 ratchov 1576: if (f->pstate == SOCK_READY && ctl_slotstart(f->dev->midi, f->slot))
1.1 ratchov 1577: (void)sock_attach(f, 0);
1578: break;
1579: case SOCK_RRET:
1.37 ratchov 1580: #ifdef DEBUG
1581: if (debug_level >= 4) {
1582: sock_dbg(f);
1583: dbg_puts(": blocked by pending RRET message\n");
1584: }
1585: #endif
1.1 ratchov 1586: return 0;
1587: }
1.54 ratchov 1588: for (;;) {
1589: /*
1590: * send pending ACKs, initial positions, initial volumes
1591: */
1592: if (!sock_write(f))
1593: break;
1594: }
1.1 ratchov 1595: return 1;
1596: }
1597:
1598: /*
1.20 ratchov 1599: * Process messages to return.
1.1 ratchov 1600: */
1601: int
1602: sock_return(struct sock *f)
1603: {
1604: struct aproc *rp;
1605:
1606: while (f->rstate == SOCK_RRET) {
1607: if (!sock_wmsg(f, &f->rmsg, &f->rtodo))
1608: return 0;
1.37 ratchov 1609: #ifdef DEBUG
1610: if (debug_level >= 4) {
1611: sock_dbg(f);
1612: dbg_puts(": sent RRET message\n");
1613: }
1614: #endif
1.55 ! ratchov 1615: f->rstate = SOCK_RMSG;
! 1616: f->rtodo = sizeof(struct amsg);
1.19 ratchov 1617: if (f->pipe.file.state & FILE_RINUSE)
1618: break;
1619: f->pipe.file.state |= FILE_RINUSE;
1.1 ratchov 1620: for (;;) {
1621: /*
1622: * in() may trigger rsock_done and destroy the
1.20 ratchov 1623: * wsock.
1.1 ratchov 1624: */
1625: rp = f->pipe.file.rproc;
1626: if (!rp || !rp->ops->in(rp, NULL))
1627: break;
1628: }
1.19 ratchov 1629: f->pipe.file.state &= ~FILE_RINUSE;
1.1 ratchov 1630: if (f->pipe.file.wproc == NULL)
1631: return 0;
1632: }
1633: return 1;
1634: }
1635:
1636: /*
1.20 ratchov 1637: * Write messages and data on the socket file descriptor. Return 1 if
1.1 ratchov 1638: * at least one message or one data byte was processed, 0 if something
1639: * blocked.
1640: */
1641: int
1642: sock_write(struct sock *f)
1643: {
1.37 ratchov 1644: #ifdef DEBUG
1645: if (debug_level >= 4) {
1646: sock_dbg(f);
1.54 ratchov 1647: dbg_puts(": writing");
1648: if (f->wstate != SOCK_WIDLE) {
1649: dbg_puts(" todo = ");
1650: dbg_putu(f->wtodo);
1651: }
1652: dbg_puts("\n");
1.37 ratchov 1653: }
1654: #endif
1.1 ratchov 1655: switch (f->wstate) {
1656: case SOCK_WMSG:
1657: if (!sock_wmsg(f, &f->wmsg, &f->wtodo))
1658: return 0;
1659: if (f->wmsg.cmd != AMSG_DATA) {
1660: f->wstate = SOCK_WIDLE;
1661: f->wtodo = 0xdeadbeef;
1662: break;
1663: }
1.42 ratchov 1664: /*
1665: * XXX: why not set f->wtodo in sock_wmsg() ?
1666: */
1.1 ratchov 1667: f->wstate = SOCK_WDATA;
1.42 ratchov 1668: f->wtodo = f->wmsg.u.data.size /
1.45 ratchov 1669: LIST_FIRST(&f->pipe.file.wproc->ins)->bpf;
1.1 ratchov 1670: /* PASSTHROUGH */
1671: case SOCK_WDATA:
1672: if (!sock_wdata(f))
1673: return 0;
1.55 ! ratchov 1674: if (f->wtodo > 0)
1.1 ratchov 1675: break;
1676: f->wstate = SOCK_WIDLE;
1677: f->wtodo = 0xdeadbeef;
1.42 ratchov 1678: if (f->pstate == SOCK_STOP)
1679: sock_freebuf(f);
1.1 ratchov 1680: /* PASSTHROUGH */
1681: case SOCK_WIDLE:
1682: if (!sock_return(f))
1683: return 0;
1684: if (!sock_buildmsg(f))
1685: return 0;
1686: break;
1.37 ratchov 1687: #ifdef DEBUG
1688: default:
1689: sock_dbg(f);
1690: dbg_puts(": bad writing end state\n");
1691: dbg_panic();
1692: #endif
1.1 ratchov 1693: }
1694: return 1;
1695: }