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_cflag &= ~(IXON|IXOFF);
166 if (cfg->serflow == SER_FLOW_XONXOFF) {
167 options.c_cflag |= IXON | IXOFF;
169 } else if (cfg->serflow == SER_FLOW_RTSCTS) {
171 options.c_cflag |= CRTSCTS;
174 options.c_cflag |= CNEW_RTSCTS;
179 msg = dupprintf("Configuring %s flow control", str);
180 logevent(serial->frontend, msg);
184 if (cfg->serparity == SER_PAR_ODD) {
185 options.c_cflag |= PARENB;
186 options.c_cflag |= PARODD;
188 } else if (cfg->serparity == SER_PAR_EVEN) {
189 options.c_cflag |= PARENB;
190 options.c_cflag &= ~PARODD;
193 options.c_cflag &= ~PARENB;
196 msg = dupprintf("Configuring %s parity", str);
197 logevent(serial->frontend, msg);
200 options.c_cflag |= CLOCAL | CREAD;
201 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
202 options.c_oflag &= ~OPOST;
203 options.c_cc[VMIN] = 1;
204 options.c_cc[VTIME] = 0;
206 if (tcsetattr(serial->fd, TCSANOW, &options) < 0)
207 return "Unable to configure serial port";
213 * Called to set up the serial connection.
215 * Returns an error message, or NULL on success.
217 * Also places the canonical host name into `realhost'. It must be
218 * freed by the caller.
220 static const char *serial_init(void *frontend_handle, void **backend_handle,
222 char *host, int port, char **realhost, int nodelay,
228 serial = snew(struct serial_backend_data);
229 *backend_handle = serial;
231 serial->frontend = frontend_handle;
232 serial->finished = FALSE;
233 serial->inbufsize = 0;
234 bufchain_init(&serial->output_data);
237 char *msg = dupprintf("Opening serial device %s", cfg->serline);
238 logevent(serial->frontend, msg);
241 serial->fd = open(cfg->serline, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
243 return "Unable to open serial port";
245 err = serial_configure(serial, cfg);
249 *realhost = dupstr(cfg->serline);
252 serial_by_fd = newtree234(serial_compare_by_fd);
253 add234(serial_by_fd, serial);
255 serial_uxsel_setup(serial);
258 * Specials are always available.
260 update_specials_menu(serial->frontend);
265 static void serial_close(Serial serial)
267 if (serial->fd >= 0) {
273 static void serial_free(void *handle)
275 Serial serial = (Serial) handle;
277 serial_close(serial);
279 bufchain_clear(&serial->output_data);
284 static void serial_reconfig(void *handle, Config *cfg)
286 Serial serial = (Serial) handle;
289 err = serial_configure(serial, cfg);
292 * FIXME: what should we do if err returns something?
296 static int serial_select_result(int fd, int event)
301 int finished = FALSE;
303 serial = find234(serial_by_fd, &fd, serial_find_by_fd);
306 return 1; /* spurious event; keep going */
309 ret = read(serial->fd, buf, sizeof(buf));
313 * Shouldn't happen on a real serial port, but I'm open
314 * to the idea that there might be two-way devices we
315 * can treat _like_ serial ports which can return EOF.
318 } else if (ret < 0) {
319 perror("read serial port");
321 } else if (ret > 0) {
322 serial->inbufsize = from_backend(serial->frontend, 0, buf, ret);
323 serial_uxsel_setup(serial); /* might acquire backlog and freeze */
325 } else if (event == 2) {
327 * Attempt to send data down the pty.
329 serial_try_write(serial);
333 serial_close(serial);
335 serial->finished = TRUE;
337 notify_remote_exit(serial->frontend);
343 static void serial_uxsel_setup(Serial serial)
347 if (serial->inbufsize <= SERIAL_MAX_BACKLOG)
349 if (bufchain_size(&serial->output_data))
350 rwx |= 2; /* might also want to write to it */
351 uxsel_set(serial->fd, rwx, serial_select_result);
354 static void serial_try_write(Serial serial)
359 assert(serial->fd >= 0);
361 while (bufchain_size(&serial->output_data) > 0) {
362 bufchain_prefix(&serial->output_data, &data, &len);
363 ret = write(serial->fd, data, len);
365 if (ret < 0 && (errno == EWOULDBLOCK)) {
367 * We've sent all we can for the moment.
372 perror("write serial port");
375 bufchain_consume(&serial->output_data, ret);
378 serial_uxsel_setup(serial);
382 * Called to send data down the serial connection.
384 static int serial_send(void *handle, char *buf, int len)
386 Serial serial = (Serial) handle;
391 bufchain_add(&serial->output_data, buf, len);
392 serial_try_write(serial);
394 return bufchain_size(&serial->output_data);
398 * Called to query the current sendability status.
400 static int serial_sendbuffer(void *handle)
402 Serial serial = (Serial) handle;
403 return bufchain_size(&serial->output_data);
407 * Called to set the size of the window
409 static void serial_size(void *handle, int width, int height)
416 * Send serial special codes.
418 static void serial_special(void *handle, Telnet_Special code)
420 Serial serial = (Serial) handle;
422 if (serial->fd >= 0 && code == TS_BRK) {
423 tcsendbreak(serial->fd, 0);
424 logevent(serial->frontend, "Sending serial break at user request");
431 * Return a list of the special codes that make sense in this
434 static const struct telnet_special *serial_get_specials(void *handle)
436 static const struct telnet_special specials[] = {
443 static int serial_connected(void *handle)
445 return 1; /* always connected */
448 static int serial_sendok(void *handle)
453 static void serial_unthrottle(void *handle, int backlog)
455 Serial serial = (Serial) handle;
456 serial->inbufsize = backlog;
457 serial_uxsel_setup(serial);
460 static int serial_ldisc(void *handle, int option)
463 * Local editing and local echo are off by default.
468 static void serial_provide_ldisc(void *handle, void *ldisc)
470 /* This is a stub. */
473 static void serial_provide_logctx(void *handle, void *logctx)
475 /* This is a stub. */
478 static int serial_exitcode(void *handle)
480 Serial serial = (Serial) handle;
482 return -1; /* still connected */
484 /* Exit codes are a meaningless concept with serial ports */
489 * cfg_info for Serial does nothing at all.
491 static int serial_cfg_info(void *handle)
496 Backend serial_backend = {
509 serial_provide_ldisc,
510 serial_provide_logctx,