// process output queue int process_output(uart_ctx_t* ctx, dthread_t* self) { dmessage_t* mp; int r = 0; while ((mp = ctx->oq.mesg) != NULL) { int n = mp->used - ctx->oq.offs; if ((r = write(ctx->fd, mp->buffer+ctx->oq.offs, n)) < 0) { if ((r < 0) && (errno == EAGAIN)) { DEBUGF("uart_unix: process_output: EAGAIN"); return 0; } return -1; } else if (r < n) { ctx->oq.offs += r; return 0; } if (mp->from) dthread_port_send_ok(mp->source, self, mp->from, mp->ref); dmessage_free(mp); ctx->oq.offs = 0; if ((mp = ctx->oq.front) == NULL) ctx->oq.mesg = NULL; else { if (!(ctx->oq.front = mp->next)) ctx->oq.rear = NULL; ctx->oq.mesg = mp; } } return r; }
// NOTE: when SMP is enabled the messages go straight to the caller // This code is here to allow non SMP emulator with the same code base. static void uart_drv_ready_input(ErlDrvData d, ErlDrvEvent e) { drv_ctx_t* ctx = (drv_ctx_t*) d; DEBUGF("uart_drv: ready_input called"); if (ctx->self.iq_signal[0] == e) { // got input ! dmessage_t* mp; DEBUGF("uart_drv: ready_input handle=%d", DTHREAD_EVENT(ctx->self.iq_signal[0])); if ((mp = dthread_recv(&ctx->self, NULL)) == NULL) { DEBUGF("uart_drv: ready_input signaled with no event! handle=%d", DTHREAD_EVENT(ctx->self.iq_signal[0])); return; } switch(mp->cmd) { case DTHREAD_OUTPUT_TERM: DEBUGF("uart_drv: ready_input (OUTPUT_TERM)"); driver_output_term(ctx->self.port, (ErlDrvTermData*) mp->buffer, mp->used / sizeof(ErlDrvTermData)); break; case DTHREAD_SEND_TERM: DEBUGF("uart_drv: ready_input (SEND_TERM)"); // dterm_dump(stderr, (ErlDrvTermData*) mp->buffer, // mp->used / sizeof(ErlDrvTermData)); driver_send_term(ctx->self.port, mp->to, /* orignal from ! */ (ErlDrvTermData*) mp->buffer, mp->used / sizeof(ErlDrvTermData)); break; case DTHREAD_OUTPUT: DEBUGF("uart_drv: ready_input (OUTPUT)"); driver_output(ctx->self.port, mp->buffer, mp->used); break; default: DEBUGF("uart_drv: read_input cmd=%d not matched", mp->cmd); break; } dmessage_free(mp); } else { DEBUGF("uart_drv: ready_input (NO MATCH)"); } }
// process output queue int process_output(uart_ctx_t* ctx, dthread_t* self) { dmessage_t* mp; while ((mp = ctx->oq.mesg) != NULL) { DWORD nbytes = mp->used - ctx->oq.offs; DWORD nwritten; if (!ctx->writing) { if (!WriteFile(ctx->fh, mp->buffer+ctx->oq.offs, nbytes, &nwritten, &ctx->out)) { if (GetLastError() != ERROR_IO_PENDING) // return uart_send_error(ctx, uart_errno(ctx)); return -1; } ctx->writing = 1; return 0; } else { if (!GetOverlappedResult(ctx->fh, &ctx->out, &nwritten, FALSE)) { if (GetLastError() == ERROR_IO_INCOMPLETE) // still waiting return 0; // return uart_send_error(ctx, uart_errno(ctx)); ctx->writing = 0; return -1; } ctx->writing = 0; } if (nwritten < nbytes) ctx->oq.offs += nwritten; else { if (mp->from) dthread_port_send_ok(mp->source, self, mp->from, mp->ref); dmessage_free(mp); ctx->oq.offs = 0; if ((mp = ctx->oq.front) == NULL) ctx->oq.mesg = NULL; else { if (!(ctx->oq.front = mp->next)) ctx->oq.rear = NULL; ctx->oq.mesg = mp; } } } return 0; }
// Cleanup the mess void dthread_finish(dthread_t* thr) { dmessage_t* mp; if (thr->iq_mtx) { erl_drv_mutex_destroy(thr->iq_mtx); thr->iq_mtx = NULL; } mp = thr->iq_front; while(mp) { dmessage_t* tmp = mp->next; dmessage_free(mp); mp = tmp; } thr->iq_front = thr->iq_rear = NULL; dthread_signal_finish(thr, 0); }
// thread main! int uart_unix_main(void* arg) { dthread_t* self = (dthread_t*) arg; dthread_t* other = (dthread_t*) self->arg; dmessage_t* mp = NULL; dthread_poll_event_t ev, *evp; size_t nev; dterm_t term; uart_ctx_t ctx; ErlDrvTermData mp_from; ErlDrvTermData mp_ref; dthread_t* mp_source; int tmo; int r; DEBUGF("uart_unix: thread started"); uart_init(&ctx, self, other); dterm_init(&term); again_tmo: tmo = next_timeout(&ctx); again: nev = 0; evp = NULL; if (ctx.fd >= 0) { ev.event = (ErlDrvEvent) ((long)ctx.fd); ev.events = 0; if ((ctx.option.active != UART_PASSIVE) || ctx.recv) { ev.events |= ERL_DRV_READ; if (ctx.option.ptypkt && (ctx.fd != ctx.tty_fd)) ev.events |= ERL_DRV_EXCEP; } if (ctx.oq.mesg) ev.events |= ERL_DRV_WRITE; if (ev.events) { evp = &ev; nev = 1; } DEBUGF("ctx.fd=%d, ev.events=%d", ctx.fd, ev.events); } DEBUGF("uart_unix_main: nev=%d, events=%x, timeout = %d", nev, ev.events, tmo); r = dthread_poll(self, evp, &nev, tmo); if (r < 0) { DEBUGF("uart_unix_main: dthread_poll failed=%d", r); goto again_tmo; } else { DEBUGF("uart_unix_main: nev=%d, r=%d", nev, r); if (evp && (nev == 1)) { if (evp->revents & ERL_DRV_WRITE) process_output(&ctx, self); if (evp->revents & (ERL_DRV_READ|ERL_DRV_EXCEP)) { while((process_input(&ctx, self, 0) == 1) && (ctx.option.active != UART_PASSIVE)) ; } } tmo = next_timeout(&ctx); DEBUGF("uart_unix_main: timeout = %d", tmo); if (ctx.recv) { if (tmo == 0) { uart_async_error_am(&ctx, ctx.dport, ctx.caller, am_timeout); clear_timeout(&ctx); ctx.remain = 0; } } if (r == 0) goto again; // r>0 (number of messages) DEBUGF("about to receive message r=%d", r); if ((mp = dthread_recv(self, NULL)) == NULL) { DEBUGF("uart_unix_main: message was NULL"); goto again; } mp_from = mp->from; mp_ref = mp->ref; mp_source = mp->source; switch (mp->cmd) { case DTHREAD_STOP: DEBUGF("uart_unix_main: STOP"); close_device(&ctx); uart_final(&ctx); dmessage_free(mp); DEBUGF("uart_unix_main: EXIT"); dthread_exit(0); break; case DTHREAD_OUTPUT: // async send! DEBUGF("uart_unix_main: OUTPUT"); if (ctx.fd < 0) { dmessage_free(mp); goto again; } if (enq_output(&ctx, self, mp, 0) < 0) { mp = NULL; goto error; } goto again; case UART_CMD_CONNECT: { ErlDrvTermData owner; if (mp->used != 0) goto badarg; owner = driver_connected(self->port); self->owner = owner; other->owner = owner; goto ok; } case UART_CMD_CLOSE: DEBUGF("uart_unix_main: CLOSE"); close_device(&ctx); goto ok; case UART_CMD_SEND: // sync send DEBUGF("uart_unix_main: SEND"); if (ctx.fd < 0) goto ebadf; if (enq_output(&ctx, self, mp, mp_from) < 0) { mp = NULL; goto error; } goto again; case UART_CMD_SENDCHAR: // sync send DEBUGF("uart_unix_main: SENDCHAR"); if (ctx.fd < 0) goto ebadf; if (enq_output(&ctx, self, mp, mp_from) < 0) { mp = NULL; goto error; } goto again; case UART_CMD_RECV: { // <<Time:32, Length:32>> Time=0xffffffff=inf uint32_t tm; int len; DEBUGF("uart_unix_main: RECV"); if (ctx.fd < 0) goto ebadf; if (ctx.recv) goto ealready; if (mp->used != 8) goto badarg; if (ctx.option.active != UART_PASSIVE) goto badarg; tm = get_uint32((uint8_t*) mp->buffer); len = (int) get_uint32((uint8_t*) (mp->buffer+4)); if ((len < 0) || (len > UART_MAX_PACKET_SIZE)) goto badarg; ctx.ref = mp_ref; ctx.caller = mp_from; set_timeout(&ctx, tm); ctx.recv = 1; DEBUGF("recv timeout %lu", tm); process_input(&ctx, self, len); dmessage_free(mp); goto again_tmo; } case UART_CMD_UNRECV: { // argument is data to push back uart_buf_push(&ctx.ib, mp->buffer, mp->used); DEBUGF("unrecived %d bytes", ctx.ib.ptr - ctx.ib.ptr_start); if (ctx.option.active != UART_PASSIVE) { while((process_input(&ctx, self, 0) == 1) && (ctx.option.active != UART_PASSIVE)) ; } goto ok; } case UART_CMD_SETOPTS: { uart_com_state_t state = ctx.state; uart_opt_t option = ctx.option; uint32_t sflags = ctx.sflags; // parse & update options in state,option and sflag if (uart_parse_opts(mp->buffer, mp->used, &state, &option, &sflags) < 0) goto badarg; // apply the changed values if ((r=apply_opts(&ctx, &state, &option, sflags)) < 0) goto error; if (r == 1) { while((process_input(&ctx, self, 0) == 1) && (ctx.option.active != UART_PASSIVE)) ; } goto ok; } case UART_CMD_GETOPTS: { dterm_mark_t m1; dterm_mark_t m2; // {Ref, {ok,List}} || {Ref, {error,Reason}} dterm_tuple_begin(&term, &m1); { dterm_uint(&term, mp_ref); dterm_tuple_begin(&term, &m2); { dterm_atom(&term, am_ok); if (uart_get_opts(&term, &ctx,(uint8_t*)mp->buffer,mp->used) < 0) { dterm_reset(&term); goto badarg; } } dterm_tuple_end(&term, &m2); } dterm_tuple_end(&term, &m1); dthread_port_send_dterm(mp_source, self, mp_from, &term); dterm_reset(&term); dmessage_free(mp); goto again; } case UART_CMD_GET_MODEM: { dterm_mark_t m1; dterm_mark_t m2; uart_modem_state_t mstate; if (ctx.tty_fd < 0) goto ebadf; if (get_modem_state(ctx.tty_fd, &mstate) < 0) goto error; dterm_tuple_begin(&term, &m1); { dterm_uint(&term, mp_ref); dterm_tuple_begin(&term, &m2); { dterm_atom(&term, am_ok); modem_state_dterm(&term, mstate); } dterm_tuple_end(&term, &m2); } dterm_tuple_end(&term, &m1); dthread_port_send_dterm(mp_source, self, mp_from, &term); dterm_reset(&term); dmessage_free(mp); goto again; } case UART_CMD_SET_MODEM: { uart_modem_state_t mstate; if (ctx.tty_fd < 0) goto ebadf; if (mp->used != 4) goto badarg; mstate = (uart_modem_state_t) get_uint32((uint8_t*) mp->buffer); if (set_modem_state(ctx.tty_fd, mstate, 1) < 0) goto error; goto ok; } case UART_CMD_CLR_MODEM: { uart_modem_state_t mstate; if (ctx.tty_fd < 0) goto ebadf; if (mp->used != 4) goto badarg; mstate = (uart_modem_state_t) get_uint32((uint8_t*) mp->buffer); if (set_modem_state(ctx.tty_fd, mstate, 0) < 0) goto error; goto ok; } case UART_CMD_HANGUP: { struct termios tio; int r; if (ctx.tty_fd < 0) goto ebadf; if (mp->used != 0) goto badarg; if ((r = tcgetattr(ctx.tty_fd, &tio)) < 0) { INFOF("tcgetattr: error=%s\n", strerror(errno)); goto badarg; } cfsetispeed(&tio, B0); cfsetospeed(&tio, B0); if ((r = tcsetattr(ctx.tty_fd, TCSANOW, &tio)) < 0) { INFOF("tcsetattr: error=%s\n", strerror(errno)); goto badarg; } goto ok; } case UART_CMD_BREAK: { int duration; if (ctx.tty_fd < 0) goto ebadf; if (mp->used != 4) goto badarg; duration = (int) get_uint32((uint8_t*) mp->buffer); if (tcsendbreak(ctx.tty_fd, duration) < 0) goto error; goto ok; } case UART_CMD_FLOW: if (ctx.tty_fd < 0) goto ebadf; if (mp->used != 1) goto badarg; switch(mp->buffer[0]) { case 0: r = tcflow(ctx.tty_fd, TCIOFF); break; case 1: r = tcflow(ctx.tty_fd, TCION); break; case 2: r = tcflow(ctx.tty_fd, TCOOFF); break; case 3: r = tcflow(ctx.tty_fd, TCOON); break; default: goto badarg; break; } if (r < 0) goto error; goto ok; default: goto badarg; } } ok: dthread_port_send_ok(mp_source, self, mp_from, mp_ref); if (mp) dmessage_free(mp); goto again; ebadf: errno = EBADF; goto error; badarg: errno = EINVAL; goto error; ealready: errno = EALREADY; goto error; error: dthread_port_send_error(mp_source, self, mp_from, mp_ref, uart_errno(&ctx)); if (mp) dmessage_free(mp); goto again; }
// thread main! int uart_win32_main(void* arg) { dthread_t* self = (dthread_t*) arg; dthread_t* other = (dthread_t*) self->arg; dmessage_t* mp = NULL; dthread_poll_event_t ev[3]; dthread_poll_event_t* evp; size_t nev; dterm_t term; uart_ctx_t ctx; ErlDrvTermData mp_from; ErlDrvTermData mp_ref; dthread_t* mp_source; int tmo; int r; DEBUGF("uart_win32: thread started"); uart_init(&ctx, self, other); dterm_init(&term); again_tmo: tmo = next_timeout(&ctx); again: nev = 0; if (ctx.writing) { ev[nev].event = (ErlDrvEvent) ctx.out.hEvent; ev[nev].events = ERL_DRV_READ; // yepp, even for write nev++; } while(!ctx.reading && (ctx.recv || (ctx.option.active != UART_PASSIVE))) process_input(&ctx, self, 0); if (ctx.reading) { ev[nev].event = (ErlDrvEvent) ctx.in.hEvent; ev[nev].events = ERL_DRV_READ; nev++; } evp = nev ? &ev[0] : NULL; DEBUGF("uart_win32_main: ctx.fh=%d, nev=%u, timeout = %d", ctx.fh, nev, tmo); r = dthread_poll(self, evp, &nev, tmo); if (r < 0) { DEBUGF("uart_win32_main: dthread_poll failed=%d", r); goto again_tmo; } else { DWORD i; DEBUGF("uart_win32_main: nev=%u, r=%d", nev, r); for (i = 0; i < nev; i++) { if (ev[i].revents & ERL_DRV_READ) { if (ev[i].event == (ErlDrvEvent) ctx.in.hEvent) { while((process_input(&ctx, self, 0) == 1) && (ctx.option.active != UART_PASSIVE)) ; } else if (ev[i].event == (ErlDrvEvent) ctx.out.hEvent) { process_output(&ctx, self); } } } tmo = next_timeout(&ctx); DEBUGF("uart_win32_main: timeout = %d", tmo); if (ctx.recv) { if (tmo == 0) { uart_async_error_am(&ctx, ctx.dport, ctx.caller, am_timeout); clear_timeout(&ctx); ctx.remain = 0; } } if (r == 0) goto again; // r>0 (number of messages) DEBUGF("about to receive message r=%d", r); if ((mp = dthread_recv(self, NULL)) == NULL) { DEBUGF("uart_win32_main: message was NULL"); goto again; } mp_from = mp->from; mp_ref = mp->ref; mp_source = mp->source; switch (mp->cmd) { case DTHREAD_STOP: DEBUGF("uart_win32_main: STOP"); close_device(&ctx); uart_final(&ctx); dmessage_free(mp); DEBUGF("uart_win32_main: EXIT"); dthread_exit(0); break; case DTHREAD_OUTPUT: // async send! DEBUGF("uart_win32_main: OUTPUT"); if (ctx.fh == INVALID_HANDLE_VALUE) { dmessage_free(mp); goto again; } if (enq_output(&ctx, self, mp, 0) < 0) { mp = NULL; goto error; } goto again; case UART_CMD_CONNECT: { ErlDrvTermData owner; if (mp->used != 0) goto badarg; owner = driver_connected(self->port); self->owner = owner; other->owner = owner; goto ok; } case UART_CMD_CLOSE: DEBUGF("uart_win32_main: CLOSE"); close_device(&ctx); goto ok; case UART_CMD_SEND: // sync send DEBUGF("uart_win32_main: SEND"); if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (enq_output(&ctx, self, mp, mp_from) < 0) { mp = NULL; goto error; } goto again; case UART_CMD_SENDCHAR: // sync send DEBUGF("uart_win32_main: SENDCHAR"); if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (enq_output(&ctx, self, mp, mp_from) < 0) { mp = NULL; goto error; } goto again; case UART_CMD_RECV: { // <<Time:32, Length:32>> Time=0xffffffff=inf uint32_t tm; int len; DEBUGF("uart_win32_main: RECV"); if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (ctx.recv) goto ealready; if (mp->used != 8) goto badarg; if (ctx.option.active != UART_PASSIVE) goto badarg; tm = get_uint32((uint8_t*) mp->buffer); len = (int) get_uint32((uint8_t*) (mp->buffer+4)); if ((len < 0) || (len > UART_MAX_PACKET_SIZE)) goto badarg; ctx.ref = mp_ref; ctx.caller = mp_from; set_timeout(&ctx, tm); ctx.recv = 1; DEBUGF("recv timeout %lu", tm); process_input(&ctx, self, len); dmessage_free(mp); goto again_tmo; } case UART_CMD_UNRECV: { // argument is data to push back uart_buf_push(&ctx.ib, mp->buffer, mp->used); DEBUGF("unrecived %d bytes", ctx.ib.ptr - ctx.ib.ptr_start); if (ctx.option.active != UART_PASSIVE) { while((process_input(&ctx, self, 0) == 1) && (ctx.option.active != UART_PASSIVE)) ; } goto ok; } case UART_CMD_SETOPTS: { uart_com_state_t state = ctx.state; uart_opt_t option = ctx.option; uint32_t sflags = ctx.sflags; // parse & update options in state,option and sflag if (uart_parse_opts(mp->buffer, mp->used, &state, &option, &sflags) < 0) goto badarg; // apply the changed values if ((r=apply_opts(&ctx, &state, &option, sflags)) < 0) { goto error; } goto ok; } case UART_CMD_GETOPTS: { dterm_mark_t m1; dterm_mark_t m2; // {Ref, {ok,List}} || {Ref, {error,Reason}} dterm_tuple_begin(&term, &m1); { dterm_uint(&term, mp_ref); dterm_tuple_begin(&term, &m2); { dterm_atom(&term, am_ok); if (uart_get_opts(&term, &ctx,(uint8_t*)mp->buffer,mp->used) < 0) { dterm_reset(&term); goto badarg; } } dterm_tuple_end(&term, &m2); } dterm_tuple_end(&term, &m1); dthread_port_send_dterm(mp_source, self, mp_from, &term); dterm_reset(&term); dmessage_free(mp); goto again; } case UART_CMD_GET_MODEM: { dterm_mark_t m1; dterm_mark_t m2; uart_modem_state_t mstate; if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (get_modem_state(ctx.fh, &mstate) < 0) goto error; dterm_tuple_begin(&term, &m1); { dterm_uint(&term, mp_ref); dterm_tuple_begin(&term, &m2); { dterm_atom(&term, am_ok); modem_state_dterm(&term, mstate); } dterm_tuple_end(&term, &m2); } dterm_tuple_end(&term, &m1); dthread_port_send_dterm(mp_source, self, mp_from, &term); dterm_reset(&term); dmessage_free(mp); goto again; } case UART_CMD_SET_MODEM: { uart_modem_state_t mstate; if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (mp->used != 4) goto badarg; mstate = (uart_modem_state_t) get_uint32((uint8_t*) mp->buffer); if (set_modem_state(ctx.fh, mstate, 1) < 0) goto error; goto ok; } case UART_CMD_CLR_MODEM: { uart_modem_state_t mstate; if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (mp->used != 4) goto badarg; mstate = (uart_modem_state_t) get_uint32((uint8_t*) mp->buffer); if (set_modem_state(ctx.fh, mstate, 0) < 0) goto error; goto ok; } case UART_CMD_HANGUP: { if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (mp->used != 0) goto badarg; // FIXME? goto ok; } case UART_CMD_BREAK: { int duration; if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (mp->used != 4) goto badarg; duration = (int) get_uint32((uint8_t*) mp->buffer); if (!EscapeCommFunction(ctx.fh, SETBREAK)) { DEBUG_ERROR("EscapeCommFunction: error %d", GetLastError()); goto error; } Sleep(duration); if (!EscapeCommFunction(ctx.fh, CLRBREAK)) { DEBUG_ERROR("EscapeCommFunction: error %d", GetLastError()); goto error; } goto ok; } case UART_CMD_FLOW: if (ctx.fh == INVALID_HANDLE_VALUE) goto ebadf; if (mp->used != 1) goto badarg; switch(mp->buffer[0]) { case 0: if (!EscapeCommFunction(ctx.fh, SETXOFF)) { DEBUG_ERROR("EscapeCommFunction: error %d", GetLastError()); goto error; } break; case 1: if (!EscapeCommFunction(ctx.fh, SETXON)) { DEBUG_ERROR("EscapeCommFunction: error %d", GetLastError()); goto error; } break; case 2: // TransmitCommChar(ctx.fh, XOFF); break; case 3: // TransmitCommChar(ctx.fh, XON); break; default: goto badarg; } goto ok; default: goto badarg; } } ok: dthread_port_send_ok(mp_source, self, mp_from, mp_ref); if (mp) dmessage_free(mp); goto again; ebadf: errno = EBADF; goto error; badarg: errno = EINVAL; goto error; ealready: errno = EBUSY; goto error; error: dthread_port_send_error(mp_source, self, mp_from, mp_ref, uart_errno(&ctx)); if (mp) dmessage_free(mp); goto again; }