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 err
= serial_configure(serial
, cfg
);
360 * FIXME: what should we do if err returns something?
364 static int serial_select_result(int fd
, int event
)
369 int finished
= FALSE
;
371 serial
= find234(serial_by_fd
, &fd
, serial_find_by_fd
);
374 return 1; /* spurious event; keep going */
377 ret
= read(serial
->fd
, buf
, sizeof(buf
));
381 * Shouldn't happen on a real serial port, but I'm open
382 * to the idea that there might be two-way devices we
383 * can treat _like_ serial ports which can return EOF.
386 } else if (ret
< 0) {
389 return 1; /* spurious */
392 if (errno
== EWOULDBLOCK
)
393 return 1; /* spurious */
395 perror("read serial port");
397 } else if (ret
> 0) {
398 serial
->inbufsize
= from_backend(serial
->frontend
, 0, buf
, ret
);
399 serial_uxsel_setup(serial
); /* might acquire backlog and freeze */
401 } else if (event
== 2) {
403 * Attempt to send data down the pty.
405 serial_try_write(serial
);
409 serial_close(serial
);
411 serial
->finished
= TRUE
;
413 notify_remote_exit(serial
->frontend
);
419 static void serial_uxsel_setup(Serial serial
)
423 if (serial
->inbufsize
<= SERIAL_MAX_BACKLOG
)
425 if (bufchain_size(&serial
->output_data
))
426 rwx
|= 2; /* might also want to write to it */
427 uxsel_set(serial
->fd
, rwx
, serial_select_result
);
430 static void serial_try_write(Serial serial
)
435 assert(serial
->fd
>= 0);
437 while (bufchain_size(&serial
->output_data
) > 0) {
438 bufchain_prefix(&serial
->output_data
, &data
, &len
);
439 ret
= write(serial
->fd
, data
, len
);
441 if (ret
< 0 && (errno
== EWOULDBLOCK
)) {
443 * We've sent all we can for the moment.
448 perror("write serial port");
451 bufchain_consume(&serial
->output_data
, ret
);
454 serial_uxsel_setup(serial
);
458 * Called to send data down the serial connection.
460 static int serial_send(void *handle
, char *buf
, int len
)
462 Serial serial
= (Serial
) handle
;
467 bufchain_add(&serial
->output_data
, buf
, len
);
468 serial_try_write(serial
);
470 return bufchain_size(&serial
->output_data
);
474 * Called to query the current sendability status.
476 static int serial_sendbuffer(void *handle
)
478 Serial serial
= (Serial
) handle
;
479 return bufchain_size(&serial
->output_data
);
483 * Called to set the size of the window
485 static void serial_size(void *handle
, int width
, int height
)
492 * Send serial special codes.
494 static void serial_special(void *handle
, Telnet_Special code
)
496 Serial serial
= (Serial
) handle
;
498 if (serial
->fd
>= 0 && code
== TS_BRK
) {
499 tcsendbreak(serial
->fd
, 0);
500 logevent(serial
->frontend
, "Sending serial break at user request");
507 * Return a list of the special codes that make sense in this
510 static const struct telnet_special
*serial_get_specials(void *handle
)
512 static const struct telnet_special specials
[] = {
519 static int serial_connected(void *handle
)
521 return 1; /* always connected */
524 static int serial_sendok(void *handle
)
529 static void serial_unthrottle(void *handle
, int backlog
)
531 Serial serial
= (Serial
) handle
;
532 serial
->inbufsize
= backlog
;
533 serial_uxsel_setup(serial
);
536 static int serial_ldisc(void *handle
, int option
)
539 * Local editing and local echo are off by default.
544 static void serial_provide_ldisc(void *handle
, void *ldisc
)
546 /* This is a stub. */
549 static void serial_provide_logctx(void *handle
, void *logctx
)
551 /* This is a stub. */
554 static int serial_exitcode(void *handle
)
556 Serial serial
= (Serial
) handle
;
558 return -1; /* still connected */
560 /* Exit codes are a meaningless concept with serial ports */
565 * cfg_info for Serial does nothing at all.
567 static int serial_cfg_info(void *handle
)
572 Backend serial_backend
= {
585 serial_provide_ldisc
,
586 serial_provide_logctx
,