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)
137 cfsetispeed(&options
, bflag
);
138 cfsetospeed(&options
, bflag
);
139 msg
= dupprintf("Configuring baud rate %d", bval
);
140 logevent(serial
->frontend
, msg
);
143 options
.c_cflag
&= ~CSIZE
;
144 switch (cfg
->serdatabits
) {
145 case 5: options
.c_cflag
|= CS5
; break;
146 case 6: options
.c_cflag
|= CS6
; break;
147 case 7: options
.c_cflag
|= CS7
; break;
148 case 8: options
.c_cflag
|= CS8
; break;
149 default: return "Invalid number of data bits (need 5, 6, 7 or 8)";
151 msg
= dupprintf("Configuring %d data bits", cfg
->serdatabits
);
152 logevent(serial
->frontend
, msg
);
155 if (cfg
->serstopbits
>= 4) {
156 options
.c_cflag
|= CSTOPB
;
158 options
.c_cflag
&= ~CSTOPB
;
160 msg
= dupprintf("Configuring %d stop bits",
161 (options
.c_cflag
& CSTOPB ?
2 : 1));
162 logevent(serial
->frontend
, msg
);
165 options
.c_iflag
&= ~(IXON
|IXOFF
);
167 options
.c_cflag
&= ~CRTSCTS
;
170 options
.c_cflag
&= ~CNEW_RTSCTS
;
172 if (cfg
->serflow
== SER_FLOW_XONXOFF
) {
173 options
.c_iflag
|= IXON
| IXOFF
;
175 } else if (cfg
->serflow
== SER_FLOW_RTSCTS
) {
177 options
.c_cflag
|= CRTSCTS
;
180 options
.c_cflag
|= CNEW_RTSCTS
;
185 msg
= dupprintf("Configuring %s flow control", str
);
186 logevent(serial
->frontend
, msg
);
190 if (cfg
->serparity
== SER_PAR_ODD
) {
191 options
.c_cflag
|= PARENB
;
192 options
.c_cflag
|= PARODD
;
194 } else if (cfg
->serparity
== SER_PAR_EVEN
) {
195 options
.c_cflag
|= PARENB
;
196 options
.c_cflag
&= ~PARODD
;
199 options
.c_cflag
&= ~PARENB
;
202 msg
= dupprintf("Configuring %s parity", str
);
203 logevent(serial
->frontend
, msg
);
206 options
.c_cflag
|= CLOCAL
| CREAD
;
207 options
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ISIG
);
208 options
.c_iflag
&= ~(ISTRIP
| IGNCR
| INLCR
| ICRNL
213 options
.c_oflag
&= ~(OPOST
217 | OCRNL
| ONOCR
| ONLRET
);
218 options
.c_cc
[VMIN
] = 1;
219 options
.c_cc
[VTIME
] = 0;
221 if (tcsetattr(serial
->fd
, TCSANOW
, &options
) < 0)
222 return "Unable to configure serial port";
228 * Called to set up the serial connection.
230 * Returns an error message, or NULL on success.
232 * Also places the canonical host name into `realhost'. It must be
233 * freed by the caller.
235 static const char *serial_init(void *frontend_handle
, void **backend_handle
,
237 char *host
, int port
, char **realhost
, int nodelay
,
243 serial
= snew(struct serial_backend_data
);
244 *backend_handle
= serial
;
246 serial
->frontend
= frontend_handle
;
247 serial
->finished
= FALSE
;
248 serial
->inbufsize
= 0;
249 bufchain_init(&serial
->output_data
);
252 char *msg
= dupprintf("Opening serial device %s", cfg
->serline
);
253 logevent(serial
->frontend
, msg
);
256 serial
->fd
= open(cfg
->serline
, O_RDWR
| O_NOCTTY
| O_NDELAY
| O_NONBLOCK
);
258 return "Unable to open serial port";
262 err
= serial_configure(serial
, cfg
);
266 *realhost
= dupstr(cfg
->serline
);
269 serial_by_fd
= newtree234(serial_compare_by_fd
);
270 add234(serial_by_fd
, serial
);
272 serial_uxsel_setup(serial
);
275 * Specials are always available.
277 update_specials_menu(serial
->frontend
);
282 static void serial_close(Serial serial
)
284 if (serial
->fd
>= 0) {
290 static void serial_free(void *handle
)
292 Serial serial
= (Serial
) handle
;
294 serial_close(serial
);
296 bufchain_clear(&serial
->output_data
);
301 static void serial_reconfig(void *handle
, Config
*cfg
)
303 Serial serial
= (Serial
) handle
;
306 err
= serial_configure(serial
, cfg
);
309 * FIXME: what should we do if err returns something?
313 static int serial_select_result(int fd
, int event
)
318 int finished
= FALSE
;
320 serial
= find234(serial_by_fd
, &fd
, serial_find_by_fd
);
323 return 1; /* spurious event; keep going */
326 ret
= read(serial
->fd
, buf
, sizeof(buf
));
330 * Shouldn't happen on a real serial port, but I'm open
331 * to the idea that there might be two-way devices we
332 * can treat _like_ serial ports which can return EOF.
335 } else if (ret
< 0) {
336 perror("read serial port");
338 } else if (ret
> 0) {
339 serial
->inbufsize
= from_backend(serial
->frontend
, 0, buf
, ret
);
340 serial_uxsel_setup(serial
); /* might acquire backlog and freeze */
342 } else if (event
== 2) {
344 * Attempt to send data down the pty.
346 serial_try_write(serial
);
350 serial_close(serial
);
352 serial
->finished
= TRUE
;
354 notify_remote_exit(serial
->frontend
);
360 static void serial_uxsel_setup(Serial serial
)
364 if (serial
->inbufsize
<= SERIAL_MAX_BACKLOG
)
366 if (bufchain_size(&serial
->output_data
))
367 rwx
|= 2; /* might also want to write to it */
368 uxsel_set(serial
->fd
, rwx
, serial_select_result
);
371 static void serial_try_write(Serial serial
)
376 assert(serial
->fd
>= 0);
378 while (bufchain_size(&serial
->output_data
) > 0) {
379 bufchain_prefix(&serial
->output_data
, &data
, &len
);
380 ret
= write(serial
->fd
, data
, len
);
382 if (ret
< 0 && (errno
== EWOULDBLOCK
)) {
384 * We've sent all we can for the moment.
389 perror("write serial port");
392 bufchain_consume(&serial
->output_data
, ret
);
395 serial_uxsel_setup(serial
);
399 * Called to send data down the serial connection.
401 static int serial_send(void *handle
, char *buf
, int len
)
403 Serial serial
= (Serial
) handle
;
408 bufchain_add(&serial
->output_data
, buf
, len
);
409 serial_try_write(serial
);
411 return bufchain_size(&serial
->output_data
);
415 * Called to query the current sendability status.
417 static int serial_sendbuffer(void *handle
)
419 Serial serial
= (Serial
) handle
;
420 return bufchain_size(&serial
->output_data
);
424 * Called to set the size of the window
426 static void serial_size(void *handle
, int width
, int height
)
433 * Send serial special codes.
435 static void serial_special(void *handle
, Telnet_Special code
)
437 Serial serial
= (Serial
) handle
;
439 if (serial
->fd
>= 0 && code
== TS_BRK
) {
440 tcsendbreak(serial
->fd
, 0);
441 logevent(serial
->frontend
, "Sending serial break at user request");
448 * Return a list of the special codes that make sense in this
451 static const struct telnet_special
*serial_get_specials(void *handle
)
453 static const struct telnet_special specials
[] = {
460 static int serial_connected(void *handle
)
462 return 1; /* always connected */
465 static int serial_sendok(void *handle
)
470 static void serial_unthrottle(void *handle
, int backlog
)
472 Serial serial
= (Serial
) handle
;
473 serial
->inbufsize
= backlog
;
474 serial_uxsel_setup(serial
);
477 static int serial_ldisc(void *handle
, int option
)
480 * Local editing and local echo are off by default.
485 static void serial_provide_ldisc(void *handle
, void *ldisc
)
487 /* This is a stub. */
490 static void serial_provide_logctx(void *handle
, void *logctx
)
492 /* This is a stub. */
495 static int serial_exitcode(void *handle
)
497 Serial serial
= (Serial
) handle
;
499 return -1; /* still connected */
501 /* Exit codes are a meaningless concept with serial ports */
506 * cfg_info for Serial does nothing at all.
508 static int serial_cfg_info(void *handle
)
513 Backend serial_backend
= {
526 serial_provide_ldisc
,
527 serial_provide_logctx
,