Lines Matching defs:f

134 ctlgroup(struct sock *f, struct ctl *c)
136 if (f->ctlslot->opt == NULL)
138 if (strcmp(c->group, f->ctlslot->opt->name) == 0)
140 if (strcmp(c->group, f->ctlslot->opt->dev->name) == 0)
146 sock_close(struct sock *f)
152 for (pf = &sock_list; *pf != f; pf = &(*pf)->next) {
160 *pf = f->next;
163 logx(3, "sock %d: closing", f->fd);
165 if (f->pstate > SOCK_AUTH)
166 sock_sesrefs -= f->sesrefs;
167 if (f->slot) {
168 slot_del(f->slot);
169 f->slot = NULL;
171 if (f->midi) {
172 tags = midi_tags(f->midi);
177 midi_del(f->midi);
178 f->midi = NULL;
180 if (f->port) {
181 port_unref(f->port);
182 f->port = NULL;
184 if (f->ctlslot) {
185 ctlslot_del(f->ctlslot);
186 f->ctlslot = NULL;
187 xfree(f->ctldesc);
189 file_del(f->file);
190 close(f->fd);
192 xfree(f);
198 struct sock *f = arg;
199 struct slot *s = f->slot;
201 f->fillpending += s->round;
204 s->name, s->unit, f->rmax, f->fillpending);
211 struct sock *f = arg;
212 struct slot *s = f->slot;
214 f->wmax += s->round * s->sub.bpf;
216 logx(4, "%s%u: flush, wmax -> %d", s->name, s->unit, f->wmax);
223 struct sock *f = arg;
225 struct slot *s = f->slot;
229 f->stoppending = 1;
235 struct sock *f = (struct sock *)arg;
236 struct slot *s = f->slot;
243 f->tickpending++;
249 struct sock *f = (struct sock *)arg;
250 struct slot *s = f->slot;
262 struct sock *f = arg;
264 midi_send(f->midi, msg, size);
270 struct sock *f = arg;
272 midi_out(f->midi, msg, size);
278 struct sock *f = arg;
280 f->fillpending += count;
286 struct sock *f = arg;
288 if (f->ctlops & SOCK_CTLDESC)
289 f->ctlsyncpending = 1;
295 struct sock *f;
297 f = xmalloc(sizeof(struct sock));
298 f->pstate = SOCK_AUTH;
299 f->slot = NULL;
300 f->port = NULL;
301 f->midi = NULL;
302 f->ctlslot = NULL;
303 f->tickpending = 0;
304 f->fillpending = 0;
305 f->stoppending = 0;
306 f->wstate = SOCK_WIDLE;
307 f->wtodo = 0xdeadbeef;
308 f->rstate = SOCK_RMSG;
309 f->rtodo = sizeof(struct amsg);
310 f->wmax = f->rmax = 0;
311 f->lastvol = -1;
312 f->ctlops = 0;
313 f->ctlsyncpending = 0;
314 f->file = file_new(&sock_fileops, f, "sock", 1);
315 f->fd = fd;
316 if (f->file == NULL) {
317 xfree(f);
320 f->next = sock_list;
321 sock_list = f;
322 return f;
328 struct sock *f = (struct sock *)arg;
331 logx(3, "sock %d: exit", f->fd);
333 sock_close(f);
340 sock_fdwrite(struct sock *f, void *data, int count)
344 n = write(f->fd, data, count);
353 logx(1, "sock %d: write failed, errno = %d", f->fd, errno);
354 sock_close(f);
357 logx(4, "sock %d: write blocked", f->fd);
363 sock_close(f);
373 sock_fdread(struct sock *f, void *data, int count)
377 n = read(f->fd, data, count);
386 logx(1, "sock %d: read failed, errno = %d", f->fd, errno);
387 sock_close(f);
390 logx(4, "sock %d: read blocked", f->fd);
396 sock_close(f);
403 * read the next message into f->rmsg, return 1 on success
406 sock_rmsg(struct sock *f)
412 if (f->rtodo == 0) {
413 logx(0, "%s: sock %d: nothing to read", __func__, f->fd);
417 data = (char *)&f->rmsg + sizeof(struct amsg) - f->rtodo;
418 n = sock_fdread(f, data, f->rtodo);
421 if (n < f->rtodo) {
422 f->rtodo -= n;
425 f->rtodo = 0;
427 logx(4, "sock %d: read full message", f->fd);
433 * write the message in f->rmsg, return 1 on success
436 sock_wmsg(struct sock *f)
442 if (f->wtodo == 0) {
443 logx(0, "%s: sock %d: already written", __func__, f->fd);
447 data = (char *)&f->wmsg + sizeof(struct amsg) - f->wtodo;
448 n = sock_fdwrite(f, data, f->wtodo);
451 if (n < f->wtodo) {
452 f->wtodo -= n;
455 f->wtodo = 0;
457 logx(4, "sock %d: wrote full message", f->fd);
466 sock_rdata(struct sock *f)
473 if (f->rtodo == 0) {
474 logx(0, "%s: sock %d: data block already read", __func__, f->fd);
478 while (f->rtodo > 0) {
479 if (f->slot)
480 data = abuf_wgetblk(&f->slot->mix.buf, &count);
485 if (count > f->rtodo)
486 count = f->rtodo;
487 n = sock_fdread(f, data, count);
490 f->rtodo -= n;
491 if (f->slot)
492 abuf_wcommit(&f->slot->mix.buf, n);
494 midi_in(f->midi, midibuf, n);
497 logx(4, "sock %d: read complete block", f->fd);
499 if (f->slot)
500 slot_write(f->slot);
508 sock_wdata(struct sock *f)
515 if (f->wtodo == 0) {
516 logx(0, "%s: sock %d: zero-sized data block", __func__, f->fd);
520 if (f->pstate == SOCK_STOP) {
521 while (f->wtodo > 0) {
522 n = sock_fdwrite(f, dummy, f->wtodo);
525 f->wtodo -= n;
528 logx(4, "sock %d: zero-filled remaining block", f->fd);
532 while (f->wtodo > 0) {
534 * f->slot and f->midi are set by sock_hello(), so
537 if (f->slot)
538 data = abuf_rgetblk(&f->slot->sub.buf, &count);
539 else if (f->midi)
540 data = abuf_rgetblk(&f->midi->obuf, &count);
542 data = f->ctldesc + (f->wsize - f->wtodo);
543 count = f->wtodo;
545 if (count > f->wtodo)
546 count = f->wtodo;
547 n = sock_fdwrite(f, data, count);
550 f->wtodo -= n;
551 if (f->slot)
552 abuf_rdiscard(&f->slot->sub.buf, n);
553 else if (f->midi)
554 abuf_rdiscard(&f->midi->obuf, n);
556 if (f->slot)
557 slot_read(f->slot);
558 if (f->midi)
559 midi_fill(f->midi);
561 logx(4, "sock %d: wrote complete block", f->fd);
567 sock_setpar(struct sock *f)
569 struct slot *s = f->slot;
571 struct amsg_par *p = &f->rmsg.u.par;
584 logx(1, "sock %d: %d: bits out of bounds", f->fd, p->bits);
592 f->fd, p->bps);
636 logx(1, "sock %d: %u: bad xrun policy", f->fd, p->xrun);
662 sock_auth(struct sock *f)
664 struct amsg_auth *p = &f->rmsg.u.auth;
671 if (getpeereid(f->fd, &euid, &egid) == 0 && euid == 0) {
672 f->pstate = SOCK_HELLO;
673 f->sesrefs = 0;
680 f->sesrefs = 1;
685 sock_sesrefs += f->sesrefs;
686 f->pstate = SOCK_HELLO;
691 sock_hello(struct sock *f)
693 struct amsg_hello *p = &f->rmsg.u.hello;
703 f->fd, p->who, mode, p->version);
706 logx(1, "sock %d: %u: unsupported version", f->fd, p->version);
722 logx(1, "sock %d: %u: unsupported mode", f->fd, mode);
726 f->pstate = SOCK_INIT;
727 f->port = NULL;
729 f->slot = NULL;
730 f->midi = midi_new(&sock_midiops, f, mode);
731 if (f->midi == NULL)
740 midi_tag(f->midi, opt->num);
747 midi_tag(f->midi, opt->num);
749 midi_tag(f->midi, p->devnum);
754 f->port = c;
755 midi_link(f->midi, c->midi);
770 f->ctlslot = ctlslot_new(opt, &sock_ctlops, f);
771 if (f->ctlslot == NULL) {
772 logx(2, "sock %d: couldn't get ctlslot", f->fd);
775 f->ctldesc = xmalloc(SOCK_CTLDESC_SIZE);
776 f->ctlops = 0;
777 f->ctlsyncpending = 0;
784 f->slot = slot_new(opt, id, p->who, &sock_slotops, f, mode);
785 if (f->slot == NULL)
787 f->midi = NULL;
792 * execute the message in f->rmsg, return 1 on success
795 sock_execmsg(struct sock *f)
798 struct slot *s = f->slot;
799 struct amsg *m = &f->rmsg;
809 logx(4, "sock %d: DATA message", f->fd);
811 if (s != NULL && f->pstate != SOCK_START) {
813 logx(1, "sock %d: DATA, wrong state", f->fd);
815 sock_close(f);
818 if ((f->slot && !(f->slot->mode & MODE_PLAY)) ||
819 (f->midi && !(f->midi->mode & MODE_MIDIOUT))) {
821 logx(1, "sock %d: DATA, input-only mode", f->fd);
823 sock_close(f);
829 logx(1, "sock %d: zero size payload", f->fd);
831 sock_close(f);
836 logx(1, "sock %d: not aligned to frame", f->fd);
838 sock_close(f);
841 if (s != NULL && size > f->ralign) {
844 "not aligned to block", f->fd, size, f->ralign);
846 sock_close(f);
849 f->rstate = SOCK_RDATA;
850 f->rsize = f->rtodo = size;
852 f->ralign -= size;
853 if (f->ralign == 0)
854 f->ralign = s->round * s->mix.bpf;
856 if (f->rtodo > f->rmax) {
859 f->fd, size, f->rmax);
861 sock_close(f);
864 f->rmax -= f->rtodo;
865 if (f->rtodo == 0) {
867 logx(1, "sock %d: zero-length data chunk", f->fd);
869 sock_close(f);
875 logx(3, "sock %d: START message", f->fd);
877 if (f->pstate != SOCK_INIT || s == NULL) {
879 logx(1, "sock %d: START, wrong state", f->fd);
881 sock_close(f);
884 f->tickpending = 0;
885 f->stoppending = 0;
888 f->fillpending = s->appbufsz;
889 f->ralign = s->round * s->mix.bpf;
890 f->rmax = 0;
893 f->walign = s->round * s->sub.bpf;
894 f->wmax = 0;
896 f->pstate = SOCK_START;
897 f->rstate = SOCK_RMSG;
898 f->rtodo = sizeof(struct amsg);
902 logx(3, "sock %d: STOP message", f->fd);
904 if (f->pstate != SOCK_START) {
906 logx(1, "sock %d: STOP, wrong state", f->fd);
908 sock_close(f);
911 f->rmax = 0;
913 f->stoppending = 1;
914 f->pstate = SOCK_STOP;
915 f->rstate = SOCK_RMSG;
916 f->rtodo = sizeof(struct amsg);
918 if (f->ralign < s->round * s->mix.bpf) {
921 if (size < f->ralign) {
924 f->fd, size, f->ralign);
929 enc_sil_do(&conv, data, f->ralign / s->mix.bpf);
930 abuf_wcommit(&s->mix.buf, f->ralign);
931 f->ralign = s->round * s->mix.bpf;
938 logx(3, "sock %d: SETPAR message", f->fd);
940 if (f->pstate != SOCK_INIT || s == NULL) {
942 logx(1, "sock %d: SETPAR, wrong state", f->fd);
944 sock_close(f);
947 if (!sock_setpar(f)) {
948 sock_close(f);
951 f->rtodo = sizeof(struct amsg);
952 f->rstate = SOCK_RMSG;
956 logx(3, "sock %d: GETPAR message", f->fd);
958 if (f->pstate != SOCK_INIT || s == NULL) {
960 logx(1, "sock %d: GETPAR, wrong state", f->fd);
962 sock_close(f);
982 f->rstate = SOCK_RRET;
983 f->rtodo = sizeof(struct amsg);
987 logx(3, "sock %d: SETVOL message", f->fd);
989 if (f->pstate < SOCK_INIT || s == NULL) {
991 logx(1, "sock %d: SETVOL, wrong state", f->fd);
993 sock_close(f);
999 logx(1, "sock %d: SETVOL, volume out of range", f->fd);
1001 sock_close(f);
1004 f->rtodo = sizeof(struct amsg);
1005 f->rstate = SOCK_RMSG;
1006 f->lastvol = ctl; /* dont trigger feedback message */
1015 f->fd, m->u.ctlsub.desc, m->u.ctlsub.val);
1017 if (f->pstate != SOCK_INIT || f->ctlslot == NULL) {
1019 logx(1, "sock %d: CTLSUB, wrong state", f->fd);
1021 sock_close(f);
1025 if (!(f->ctlops & SOCK_CTLDESC)) {
1026 ctl = f->ctlslot->self;
1029 if (ctlslot_visible(f->ctlslot, c))
1033 f->ctlops |= SOCK_CTLDESC;
1034 f->ctlsyncpending = 1;
1035 f->ctl_desc_size = (cmd == AMSG_CTLSUB) ?
1040 f->ctlops &= ~SOCK_CTLDESC;
1042 f->ctlops |= SOCK_CTLVAL;
1044 f->ctlops &= ~SOCK_CTLVAL;
1045 f->rstate = SOCK_RMSG;
1046 f->rtodo = sizeof(struct amsg);
1050 logx(3, "sock %d: CTLSET message", f->fd);
1052 if (f->pstate < SOCK_INIT || f->ctlslot == NULL) {
1054 logx(1, "sock %d: CTLSET, wrong state", f->fd);
1056 sock_close(f);
1060 c = ctlslot_lookup(f->ctlslot, ntohs(m->u.ctlset.addr));
1063 logx(1, "sock %d: CTLSET, wrong addr", f->fd);
1065 sock_close(f);
1070 logx(1, "sock %d: CTLSET, bad value", f->fd);
1072 sock_close(f);
1075 f->rtodo = sizeof(struct amsg);
1076 f->rstate = SOCK_RMSG;
1080 logx(3, "sock %d: AUTH message", f->fd);
1082 if (f->pstate != SOCK_AUTH) {
1084 logx(1, "sock %d: AUTH, wrong state", f->fd);
1086 sock_close(f);
1089 if (!sock_auth(f)) {
1090 sock_close(f);
1093 f->rstate = SOCK_RMSG;
1094 f->rtodo = sizeof(struct amsg);
1098 logx(3, "sock %d: HELLO message", f->fd);
1100 if (f->pstate != SOCK_HELLO) {
1102 logx(1, "sock %d: HELLO, wrong state", f->fd);
1104 sock_close(f);
1107 if (!sock_hello(f)) {
1108 sock_close(f);
1113 f->rstate = SOCK_RRET;
1114 f->rtodo = sizeof(struct amsg);
1118 logx(3, "sock %d: BYE message", f->fd);
1120 if (s != NULL && f->pstate != SOCK_INIT) {
1122 logx(1, "sock %d: BYE, wrong state", f->fd);
1125 sock_close(f);
1129 logx(1, "sock %d: unknown command in message", f->fd);
1131 sock_close(f);
1138 * build a message in f->wmsg, return 1 on success and 0 if
1139 * there's nothing to do. Assume f->wstate is SOCK_WIDLE
1142 sock_buildmsg(struct sock *f)
1151 if (f->tickpending) {
1153 logx(4, "sock %d: building MOVE message, delta = %d", f->fd, f->slot->delta);
1155 AMSG_INIT(&f->wmsg);
1156 f->wmsg.cmd = htonl(AMSG_MOVE);
1157 f->wmsg.u.ts.delta = htonl(f->slot->delta);
1158 f->wtodo = sizeof(struct amsg);
1159 f->wstate = SOCK_WMSG;
1160 f->tickpending = 0;
1165 f->slot->delta = 0;
1169 if (f->fillpending > 0) {
1170 AMSG_INIT(&f->wmsg);
1171 f->wmsg.cmd = htonl(AMSG_FLOWCTL);
1172 f->wmsg.u.ts.delta = htonl(f->fillpending);
1173 size = f->fillpending;
1174 if (f->slot)
1175 size *= f->slot->mix.bpf;
1176 f->rmax += size;
1179 "count = %d, rmax -> %d", f->fd, f->fillpending, f->rmax);
1181 f->wtodo = sizeof(struct amsg);
1182 f->wstate = SOCK_WMSG;
1183 f->fillpending = 0;
1190 if (f->pstate >= SOCK_START && f->slot->vol != f->lastvol) {
1192 logx(3, "sock %d: building SETVOL message, vol = %d", f->fd,
1193 f->slot->vol);
1195 AMSG_INIT(&f->wmsg);
1196 f->wmsg.cmd = htonl(AMSG_SETVOL);
1197 f->wmsg.u.vol.ctl = htonl(f->slot->vol);
1198 f->wtodo = sizeof(struct amsg);
1199 f->wstate = SOCK_WMSG;
1200 f->lastvol = f->slot->vol;
1204 if (f->midi != NULL && f->midi->obuf.used > 0) {
1205 size = f->midi->obuf.used;
1208 AMSG_INIT(&f->wmsg);
1209 f->wmsg.cmd = htonl(AMSG_DATA);
1210 f->wmsg.u.data.size = htonl(size);
1211 f->wtodo = sizeof(struct amsg);
1212 f->wstate = SOCK_WMSG;
1219 if (f->slot != NULL && f->wmax > 0 && f->slot->sub.buf.used > 0) {
1220 size = f->slot->sub.buf.used;
1223 if (size > f->walign)
1224 size = f->walign;
1225 if (size > f->wmax)
1226 size = f->wmax;
1227 size -= size % f->slot->sub.bpf;
1230 logx(0, "sock %d: sock_buildmsg size == 0", f->fd);
1234 f->walign -= size;
1235 f->wmax -= size;
1236 if (f->walign == 0)
1237 f->walign = f->slot->round * f->slot->sub.bpf;
1239 logx(4, "sock %d: building audio DATA message, size = %d", f->fd, size);
1241 AMSG_INIT(&f->wmsg);
1242 f->wmsg.cmd = htonl(AMSG_DATA);
1243 f->wmsg.u.data.size = htonl(size);
1244 f->wtodo = sizeof(struct amsg);
1245 f->wstate = SOCK_WMSG;
1249 if (f->stoppending) {
1251 logx(3, "sock %d: building STOP message", f->fd);
1253 f->stoppending = 0;
1254 f->pstate = SOCK_INIT;
1255 AMSG_INIT(&f->wmsg);
1256 f->wmsg.cmd = htonl(AMSG_STOP);
1257 f->wtodo = sizeof(struct amsg);
1258 f->wstate = SOCK_WMSG;
1268 if (f->ctlslot && (f->ctlops & SOCK_CTLDESC)) {
1269 mask = f->ctlslot->self;
1278 if (size + f->ctl_desc_size > SOCK_CTLDESC_SIZE)
1280 desc = (struct amsg_ctl_desc *)(f->ctldesc + size);
1283 type = ctlslot_visible(f->ctlslot, c) ?
1285 strlcpy(desc->group, ctlgroup(f, c), AMSG_CTL_NAMEMAX);
1299 if (f->ctl_desc_size >= offsetof(struct amsg_ctl_desc,
1304 size += f->ctl_desc_size;
1319 AMSG_INIT(&f->wmsg);
1320 f->wmsg.cmd = htonl(AMSG_DATA);
1321 f->wmsg.u.data.size = htonl(size);
1322 f->wtodo = sizeof(struct amsg);
1323 f->wstate = SOCK_WMSG;
1325 logx(3, "sock %d: building control DATA message", f->fd);
1330 if (f->ctlslot && (f->ctlops & SOCK_CTLVAL)) {
1331 mask = f->ctlslot->self;
1333 if (!ctlslot_visible(f->ctlslot, c))
1338 AMSG_INIT(&f->wmsg);
1339 f->wmsg.cmd = htonl(AMSG_CTLSET);
1340 f->wmsg.u.ctlset.addr = htons(c->addr);
1341 f->wmsg.u.ctlset.val = htons(c->curval);
1342 f->wtodo = sizeof(struct amsg);
1343 f->wstate = SOCK_WMSG;
1345 logx(3, "sock %d: building CTLSET message", f->fd);
1350 if (f->ctlslot && f->ctlsyncpending) {
1351 f->ctlsyncpending = 0;
1352 f->wmsg.cmd = htonl(AMSG_CTLSYNC);
1353 f->wtodo = sizeof(struct amsg);
1354 f->wstate = SOCK_WMSG;
1356 logx(3, "sock %d: building CTLSYNC message", f->fd);
1361 logx(4, "sock %d: no messages to build anymore, idling...", f->fd);
1363 f->wstate = SOCK_WIDLE;
1371 sock_read(struct sock *f)
1374 logx(4, "sock %d: reading %u todo", f->fd, f->rtodo);
1376 switch (f->rstate) {
1380 if (!sock_rmsg(f))
1382 if (!sock_execmsg(f))
1386 if (!sock_rdata(f))
1388 f->rstate = SOCK_RMSG;
1389 f->rtodo = sizeof(struct amsg);
1392 if (f->wstate != SOCK_WIDLE) {
1394 logx(4, "sock %d: can't reply, write-end blocked", f->fd);
1398 f->wmsg = f->rmsg;
1399 f->wstate = SOCK_WMSG;
1400 f->wtodo = sizeof(struct amsg);
1401 f->rstate = SOCK_RMSG;
1402 f->rtodo = sizeof(struct amsg);
1404 logx(4, "sock %d: copied RRET message", f->fd);
1414 sock_write(struct sock *f)
1417 logx(4, "sock %d: writing", f->fd);
1419 switch (f->wstate) {
1421 if (!sock_wmsg(f))
1424 * f->wmsg is either build by sock_buildmsg() or
1425 * copied from f->rmsg (in the SOCK_RRET state), so
1428 if (ntohl(f->wmsg.cmd) != AMSG_DATA) {
1429 f->wstate = SOCK_WIDLE;
1430 f->wtodo = 0xdeadbeef;
1433 f->wstate = SOCK_WDATA;
1434 f->wsize = f->wtodo = ntohl(f->wmsg.u.data.size);
1437 if (!sock_wdata(f))
1439 if (f->wtodo > 0)
1441 f->wstate = SOCK_WIDLE;
1442 f->wtodo = 0xdeadbeef;
1443 if (f->pstate == SOCK_STOP) {
1444 f->pstate = SOCK_INIT;
1445 f->wmax = 0;
1447 logx(4, "sock %d: drained, moved to INIT state", f->fd);
1452 if (f->rstate == SOCK_RRET) {
1453 f->wmsg = f->rmsg;
1454 f->wstate = SOCK_WMSG;
1455 f->wtodo = sizeof(struct amsg);
1456 f->rstate = SOCK_RMSG;
1457 f->rtodo = sizeof(struct amsg);
1459 logx(4, "sock %d: copied RRET message", f->fd);
1462 if (!sock_buildmsg(f))
1468 logx(0, "sock %d: bad writing end state", f->fd);
1478 struct sock *f = arg;
1489 if (f->wstate == SOCK_WIDLE && f->rstate != SOCK_RRET)
1490 sock_buildmsg(f);
1492 if (f->rstate == SOCK_RMSG ||
1493 f->rstate == SOCK_RDATA)
1495 if (f->rstate == SOCK_RRET ||
1496 f->wstate == SOCK_WMSG ||
1497 f->wstate == SOCK_WDATA)
1499 pfd->fd = f->fd;
1513 struct sock *f = arg;
1515 while (sock_read(f))
1522 struct sock *f = arg;
1524 while (sock_write(f))
1531 struct sock *f = arg;
1533 sock_close(f);