2 * Serial back end (Unix-specific).
18 #define SERIAL_MAX_BACKLOG 4096
20 typedef struct serial_backend_data
{
29 * We store our serial backends in a tree sorted by fd, so that
30 * when we get an uxsel notification we know which backend instance
31 * is the owner of the serial port that caused it.
33 static int serial_compare_by_fd(void *av
, void *bv
)
35 Serial a
= (Serial
)av
;
36 Serial b
= (Serial
)bv
;
40 else if (a
->fd
> b
->fd
)
45 static int serial_find_by_fd(void *av
, void *bv
)
48 Serial b
= (Serial
)bv
;
57 static tree234
*serial_by_fd
= NULL
;
59 static int serial_select_result(int fd
, int event
);
60 static void serial_uxsel_setup(Serial serial
);
61 static void serial_try_write(Serial serial
);
63 static const char *serial_configure(Serial serial
, Config
*cfg
)
65 struct termios options
;
71 return "Unable to reconfigure already-closed serial connection";
73 tcgetattr(serial
->fd
, &options
);
76 * Find the appropriate baud rate flag.
78 #define SETBAUD(x) (bflag = B ## x, bval = x)
79 #define CHECKBAUD(x) do { if (cfg->serspeed >= x) SETBAUD(x); } while (0)
179 cfsetispeed(&options
, bflag
);
180 cfsetospeed(&options
, bflag
);
181 msg
= dupprintf("Configuring baud rate %d", bval
);
182 logevent(serial
->frontend
, msg
);
185 options
.c_cflag
&= ~CSIZE
;
186 switch (cfg
->serdatabits
) {
187 case 5: options
.c_cflag
|= CS5
; break;
188 case 6: options
.c_cflag
|= CS6
; break;
189 case 7: options
.c_cflag
|= CS7
; break;
190 case 8: options
.c_cflag
|= CS8
; break;
191 default: return "Invalid number of data bits (need 5, 6, 7 or 8)";
193 msg
= dupprintf("Configuring %d data bits", cfg
->serdatabits
);
194 logevent(serial
->frontend
, msg
);
197 if (cfg
->serstopbits
>= 4) {
198 options
.c_cflag
|= CSTOPB
;
200 options
.c_cflag
&= ~CSTOPB
;
202 msg
= dupprintf("Configuring %d stop bits",
203 (options
.c_cflag
& CSTOPB ?
2 : 1));
204 logevent(serial
->frontend
, msg
);
207 options
.c_iflag
&= ~(IXON
|IXOFF
);
209 options
.c_cflag
&= ~CRTSCTS
;
212 options
.c_cflag
&= ~CNEW_RTSCTS
;
214 if (cfg
->serflow
== SER_FLOW_XONXOFF
) {
215 options
.c_iflag
|= IXON
| IXOFF
;
217 } else if (cfg
->serflow
== SER_FLOW_RTSCTS
) {
219 options
.c_cflag
|= CRTSCTS
;
222 options
.c_cflag
|= CNEW_RTSCTS
;
227 msg
= dupprintf("Configuring %s flow control", str
);
228 logevent(serial
->frontend
, msg
);
232 if (cfg
->serparity
== SER_PAR_ODD
) {
233 options
.c_cflag
|= PARENB
;
234 options
.c_cflag
|= PARODD
;
236 } else if (cfg
->serparity
== SER_PAR_EVEN
) {
237 options
.c_cflag
|= PARENB
;
238 options
.c_cflag
&= ~PARODD
;
241 options
.c_cflag
&= ~PARENB
;
244 msg
= dupprintf("Configuring %s parity", str
);
245 logevent(serial
->frontend
, msg
);
248 options
.c_cflag
|= CLOCAL
| CREAD
;
249 options
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ISIG
);
250 options
.c_iflag
&= ~(ISTRIP
| IGNCR
| INLCR
| ICRNL
255 options
.c_oflag
&= ~(OPOST
269 options
.c_cc
[VMIN
] = 1;
270 options
.c_cc
[VTIME
] = 0;
272 if (tcsetattr(serial
->fd
, TCSANOW
, &options
) < 0)
273 return "Unable to configure serial port";
279 * Called to set up the serial connection.
281 * Returns an error message, or NULL on success.
283 * Also places the canonical host name into `realhost'. It must be
284 * freed by the caller.
286 static const char *serial_init(void *frontend_handle
, void **backend_handle
,
288 char *host
, int port
, char **realhost
, int nodelay
,
294 serial
= snew(struct serial_backend_data
);
295 *backend_handle
= serial
;
297 serial
->frontend
= frontend_handle
;
298 serial
->finished
= FALSE
;
299 serial
->inbufsize
= 0;
300 bufchain_init(&serial
->output_data
);
303 char *msg
= dupprintf("Opening serial device %s", cfg
->serline
);
304 logevent(serial
->frontend
, msg
);
307 serial
->fd
= open(cfg
->serline
, O_RDWR
| O_NOCTTY
| O_NDELAY
| O_NONBLOCK
);
309 return "Unable to open serial port";
313 err
= serial_configure(serial
, cfg
);
317 *realhost
= dupstr(cfg
->serline
);
320 serial_by_fd
= newtree234(serial_compare_by_fd
);
321 add234(serial_by_fd
, serial
);
323 serial_uxsel_setup(serial
);
326 * Specials are always available.
328 update_specials_menu(serial
->frontend
);
333 static void serial_close(Serial serial
)
335 if (serial
->fd
>= 0) {
341 static void serial_free(void *handle
)
343 Serial serial
= (Serial
) handle
;
345 serial_close(serial
);
347 bufchain_clear(&serial
->output_data
);
352 static void serial_reconfig(void *handle
, Config
*cfg
)
354 Serial serial
= (Serial
) handle
;
357 * FIXME: what should we do if this returns an error?
359 serial_configure(serial
, cfg
);
362 static int serial_select_result(int fd
, int event
)
367 int finished
= FALSE
;
369 serial
= find234(serial_by_fd
, &fd
, serial_find_by_fd
);
372 return 1; /* spurious event; keep going */
375 ret
= read(serial
->fd
, buf
, sizeof(buf
));
379 * Shouldn't happen on a real serial port, but I'm open
380 * to the idea that there might be two-way devices we
381 * can treat _like_ serial ports which can return EOF.
384 } else if (ret
< 0) {
387 return 1; /* spurious */
390 if (errno
== EWOULDBLOCK
)
391 return 1; /* spurious */
393 perror("read serial port");
395 } else if (ret
> 0) {
396 serial
->inbufsize
= from_backend(serial
->frontend
, 0, buf
, ret
);
397 serial_uxsel_setup(serial
); /* might acquire backlog and freeze */
399 } else if (event
== 2) {
401 * Attempt to send data down the pty.
403 serial_try_write(serial
);
407 serial_close(serial
);
409 serial
->finished
= TRUE
;
411 notify_remote_exit(serial
->frontend
);
417 static void serial_uxsel_setup(Serial serial
)
421 if (serial
->inbufsize
<= SERIAL_MAX_BACKLOG
)
423 if (bufchain_size(&serial
->output_data
))
424 rwx
|= 2; /* might also want to write to it */
425 uxsel_set(serial
->fd
, rwx
, serial_select_result
);
428 static void serial_try_write(Serial serial
)
433 assert(serial
->fd
>= 0);
435 while (bufchain_size(&serial
->output_data
) > 0) {
436 bufchain_prefix(&serial
->output_data
, &data
, &len
);
437 ret
= write(serial
->fd
, data
, len
);
439 if (ret
< 0 && (errno
== EWOULDBLOCK
)) {
441 * We've sent all we can for the moment.
446 perror("write serial port");
449 bufchain_consume(&serial
->output_data
, ret
);
452 serial_uxsel_setup(serial
);
456 * Called to send data down the serial connection.
458 static int serial_send(void *handle
, char *buf
, int len
)
460 Serial serial
= (Serial
) handle
;
465 bufchain_add(&serial
->output_data
, buf
, len
);
466 serial_try_write(serial
);
468 return bufchain_size(&serial
->output_data
);
472 * Called to query the current sendability status.
474 static int serial_sendbuffer(void *handle
)
476 Serial serial
= (Serial
) handle
;
477 return bufchain_size(&serial
->output_data
);
481 * Called to set the size of the window
483 static void serial_size(void *handle
, int width
, int height
)
490 * Send serial special codes.
492 static void serial_special(void *handle
, Telnet_Special code
)
494 Serial serial
= (Serial
) handle
;
496 if (serial
->fd
>= 0 && code
== TS_BRK
) {
497 tcsendbreak(serial
->fd
, 0);
498 logevent(serial
->frontend
, "Sending serial break at user request");
505 * Return a list of the special codes that make sense in this
508 static const struct telnet_special
*serial_get_specials(void *handle
)
510 static const struct telnet_special specials
[] = {
517 static int serial_connected(void *handle
)
519 return 1; /* always connected */
522 static int serial_sendok(void *handle
)
527 static void serial_unthrottle(void *handle
, int backlog
)
529 Serial serial
= (Serial
) handle
;
530 serial
->inbufsize
= backlog
;
531 serial_uxsel_setup(serial
);
534 static int serial_ldisc(void *handle
, int option
)
537 * Local editing and local echo are off by default.
542 static void serial_provide_ldisc(void *handle
, void *ldisc
)
544 /* This is a stub. */
547 static void serial_provide_logctx(void *handle
, void *logctx
)
549 /* This is a stub. */
552 static int serial_exitcode(void *handle
)
554 Serial serial
= (Serial
) handle
;
556 return -1; /* still connected */
558 /* Exit codes are a meaningless concept with serial ports */
563 * cfg_info for Serial does nothing at all.
565 static int serial_cfg_info(void *handle
)
570 Backend serial_backend
= {
583 serial_provide_ldisc
,
584 serial_provide_logctx
,