static void mux_chr_be_event(Chardev *chr, int event) { MuxChardev *d = MUX_CHARDEV(chr); if (d->focus != -1) { mux_chr_send_event(d, d->focus, event); } }
static bool qemu_chr_is_busy(Chardev *s) { if (CHARDEV_IS_MUX(s)) { MuxChardev *d = MUX_CHARDEV(s); return d->mux_cnt >= 0; } else { return s->be != NULL; } }
static GSource *mux_chr_add_watch(Chardev *s, GIOCondition cond) { MuxChardev *d = MUX_CHARDEV(s); Chardev *chr = qemu_chr_fe_get_driver(&d->chr); ChardevClass *cc = CHARDEV_GET_CLASS(chr); if (!cc->chr_add_watch) { return NULL; } return cc->chr_add_watch(chr, cond); }
static void mux_chr_accept_input(Chardev *chr) { MuxChardev *d = MUX_CHARDEV(chr); int m = d->focus; CharBackend *be = d->backends[m]; while (be && d->prod[m] != d->cons[m] && be->chr_can_read && be->chr_can_read(be->opaque)) { be->chr_read(be->opaque, &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); } }
void mux_chr_set_handlers(Chardev *chr, GMainContext *context) { MuxChardev *d = MUX_CHARDEV(chr); /* Fix up the real driver with mux routines */ qemu_chr_fe_set_handlers(&d->chr, mux_chr_can_read, mux_chr_read, mux_chr_event, NULL, chr, context, true); }
static void char_mux_finalize(Object *obj) { MuxChardev *d = MUX_CHARDEV(obj); int i; for (i = 0; i < d->mux_cnt; i++) { CharBackend *be = d->backends[i]; if (be) { be->chr = NULL; } } qemu_chr_fe_deinit(&d->chr, false); }
void mux_set_focus(Chardev *chr, int focus) { MuxChardev *d = MUX_CHARDEV(chr); assert(focus >= 0); assert(focus < d->mux_cnt); if (d->focus != -1) { mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); } d->focus = focus; mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); }
void mux_chr_send_all_event(Chardev *chr, int event) { MuxChardev *d = MUX_CHARDEV(chr); int i; if (!machine_init_done) { return; } /* Send the event to all registered listeners */ for (i = 0; i < d->mux_cnt; i++) { mux_chr_send_event(d, i, event); } }
static int mux_chr_can_read(void *opaque) { MuxChardev *d = MUX_CHARDEV(opaque); int m = d->focus; CharBackend *be = d->backends[m]; if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) { return 1; } if (be && be->chr_can_read) { return be->chr_can_read(be->opaque); } return 0; }
/* Called with chr_write_lock held. */ static int mux_chr_write(Chardev *chr, const uint8_t *buf, int len) { MuxChardev *d = MUX_CHARDEV(chr); int ret; if (!d->timestamps) { ret = qemu_chr_fe_write(&d->chr, buf, len); } else { int i; ret = 0; for (i = 0; i < len; i++) { if (d->linestart) { char buf1[64]; int64_t ti; int secs; ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); if (d->timestamps_start == -1) { d->timestamps_start = ti; } ti -= d->timestamps_start; secs = ti / 1000; snprintf(buf1, sizeof(buf1), "[%02d:%02d:%02d.%03d] ", secs / 3600, (secs / 60) % 60, secs % 60, (int)(ti % 1000)); /* XXX this blocks entire thread. Rewrite to use * qemu_chr_fe_write and background I/O callbacks */ qemu_chr_fe_write_all(&d->chr, (uint8_t *)buf1, strlen(buf1)); d->linestart = 0; } ret += qemu_chr_fe_write(&d->chr, buf + i, 1); if (buf[i] == '\n') { d->linestart = 1; } } } return ret; }
static void mux_chr_read(void *opaque, const uint8_t *buf, int size) { Chardev *chr = CHARDEV(opaque); MuxChardev *d = MUX_CHARDEV(opaque); int m = d->focus; CharBackend *be = d->backends[m]; int i; mux_chr_accept_input(opaque); for (i = 0; i < size; i++) if (mux_proc_byte(chr, d, buf[i])) { if (d->prod[m] == d->cons[m] && be && be->chr_can_read && be->chr_can_read(be->opaque)) { be->chr_read(be->opaque, &buf[i], 1); } else { d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; } } }
static void qemu_chr_open_mux(Chardev *chr, ChardevBackend *backend, bool *be_opened, Error **errp) { ChardevMux *mux = backend->u.mux.data; Chardev *drv; MuxChardev *d = MUX_CHARDEV(chr); drv = qemu_chr_find(mux->chardev); if (drv == NULL) { error_setg(errp, "mux: base chardev %s not found", mux->chardev); return; } d->focus = -1; /* only default to opened state if we've realized the initial * set of muxes */ *be_opened = machine_init_done; qemu_chr_fe_init(&d->chr, drv, errp); }