2 * Serial back end (Windows-specific).
\r
9 * + looks as if you do this by calling SetCommBreak(handle),
\r
10 * then waiting a bit, then doing ClearCommBreak(handle). A
\r
11 * small job for timing.c, methinks.
\r
13 * - why are we dropping data when talking to judicator?
\r
22 #define SERIAL_MAX_BACKLOG 4096
\r
24 typedef struct serial_backend_data {
\r
26 struct handle *out, *in;
\r
31 static void serial_terminate(Serial serial)
\r
34 handle_free(serial->out);
\r
38 handle_free(serial->in);
\r
42 CloseHandle(serial->port);
\r
43 serial->port = NULL;
\r
47 static int serial_gotdata(struct handle *h, void *data, int len)
\r
49 Serial serial = (Serial)handle_get_privdata(h);
\r
51 const char *error_msg;
\r
54 * Currently, len==0 should never happen because we're
\r
55 * ignoring EOFs. However, it seems not totally impossible
\r
56 * that this same back end might be usable to talk to named
\r
57 * pipes or some other non-serial device, in which case EOF
\r
58 * may become meaningful here.
\r
61 error_msg = "End of file reading from serial device";
\r
63 error_msg = "Error reading from serial device";
\r
65 serial_terminate(serial);
\r
67 notify_remote_exit(serial->frontend);
\r
69 logevent(serial->frontend, error_msg);
\r
71 connection_fatal(serial->frontend, "%s", error_msg);
\r
73 return 0; /* placate optimiser */
\r
75 return from_backend(serial->frontend, 0, data, len);
\r
79 static void serial_sentdata(struct handle *h, int new_backlog)
\r
81 Serial serial = (Serial)handle_get_privdata(h);
\r
82 if (new_backlog < 0) {
\r
83 const char *error_msg = "Error writing to serial device";
\r
85 serial_terminate(serial);
\r
87 notify_remote_exit(serial->frontend);
\r
89 logevent(serial->frontend, error_msg);
\r
91 connection_fatal(serial->frontend, "%s", error_msg);
\r
93 serial->bufsize = new_backlog;
\r
97 static const char *serial_configure(Serial serial, HANDLE serport, Config *cfg)
\r
100 COMMTIMEOUTS timeouts;
\r
103 * Set up the serial port parameters. If we can't even
\r
104 * GetCommState, we ignore the problem on the grounds that the
\r
105 * user might have pointed us at some other type of two-way
\r
106 * device instead of a serial port.
\r
108 if (GetCommState(serport, &dcb)) {
\r
115 dcb.fBinary = TRUE;
\r
116 dcb.fDtrControl = DTR_CONTROL_ENABLE;
\r
117 dcb.fDsrSensitivity = FALSE;
\r
118 dcb.fTXContinueOnXoff = FALSE;
\r
121 dcb.fErrorChar = FALSE;
\r
123 dcb.fRtsControl = RTS_CONTROL_ENABLE;
\r
124 dcb.fAbortOnError = FALSE;
\r
125 dcb.fOutxCtsFlow = FALSE;
\r
126 dcb.fOutxDsrFlow = FALSE;
\r
129 * Configurable parameters.
\r
131 dcb.BaudRate = cfg->serspeed;
\r
132 msg = dupprintf("Configuring baud rate %d", cfg->serspeed);
\r
133 logevent(serial->frontend, msg);
\r
136 dcb.ByteSize = cfg->serdatabits;
\r
137 msg = dupprintf("Configuring %d data bits", cfg->serdatabits);
\r
138 logevent(serial->frontend, msg);
\r
141 switch (cfg->serstopbits) {
\r
142 case 2: dcb.StopBits = ONESTOPBIT; str = "1"; break;
\r
143 case 3: dcb.StopBits = ONE5STOPBITS; str = "1.5"; break;
\r
144 case 4: dcb.StopBits = TWOSTOPBITS; str = "2"; break;
\r
145 default: return "Invalid number of stop bits (need 1, 1.5 or 2)";
\r
147 msg = dupprintf("Configuring %s data bits", str);
\r
148 logevent(serial->frontend, msg);
\r
151 switch (cfg->serparity) {
\r
152 case SER_PAR_NONE: dcb.Parity = NOPARITY; str = "no"; break;
\r
153 case SER_PAR_ODD: dcb.Parity = ODDPARITY; str = "odd"; break;
\r
154 case SER_PAR_EVEN: dcb.Parity = EVENPARITY; str = "even"; break;
\r
155 case SER_PAR_MARK: dcb.Parity = MARKPARITY; str = "mark"; break;
\r
156 case SER_PAR_SPACE: dcb.Parity = SPACEPARITY; str = "space"; break;
\r
158 msg = dupprintf("Configuring %s parity", str);
\r
159 logevent(serial->frontend, msg);
\r
162 switch (cfg->serflow) {
\r
163 case SER_FLOW_NONE:
\r
166 case SER_FLOW_XONXOFF:
\r
167 dcb.fOutX = dcb.fInX = TRUE;
\r
170 case SER_FLOW_RTSCTS:
\r
171 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
\r
172 dcb.fOutxCtsFlow = TRUE;
\r
175 case SER_FLOW_DSRDTR:
\r
176 dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
\r
177 dcb.fOutxDsrFlow = TRUE;
\r
181 msg = dupprintf("Configuring %s flow control", str);
\r
182 logevent(serial->frontend, msg);
\r
185 if (!SetCommState(serport, &dcb))
\r
186 return "Unable to configure serial port";
\r
188 timeouts.ReadIntervalTimeout = 1;
\r
189 timeouts.ReadTotalTimeoutMultiplier = 0;
\r
190 timeouts.ReadTotalTimeoutConstant = 0;
\r
191 timeouts.WriteTotalTimeoutMultiplier = 0;
\r
192 timeouts.WriteTotalTimeoutConstant = 0;
\r
193 if (!SetCommTimeouts(serport, &timeouts))
\r
194 return "Unable to configure serial timeouts";
\r
201 * Called to set up the serial connection.
\r
203 * Returns an error message, or NULL on success.
\r
205 * Also places the canonical host name into `realhost'. It must be
\r
206 * freed by the caller.
\r
208 static const char *serial_init(void *frontend_handle, void **backend_handle,
\r
210 char *host, int port, char **realhost, int nodelay,
\r
217 serial = snew(struct serial_backend_data);
\r
218 serial->port = NULL;
\r
219 serial->out = serial->in = NULL;
\r
220 *backend_handle = serial;
\r
222 serial->frontend = frontend_handle;
\r
225 char *msg = dupprintf("Opening serial device %s", host);
\r
226 logevent(serial->frontend, msg);
\r
229 serport = CreateFile(cfg->serline, GENERIC_READ | GENERIC_WRITE, 0, NULL,
\r
230 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
\r
231 if (serport == INVALID_HANDLE_VALUE)
\r
232 return "Unable to open serial port";
\r
234 err = serial_configure(serial, serport, cfg);
\r
238 serial->port = serport;
\r
239 serial->out = handle_output_new(serport, serial_sentdata, serial,
\r
240 HANDLE_FLAG_OVERLAPPED);
\r
241 serial->in = handle_input_new(serport, serial_gotdata, serial,
\r
242 HANDLE_FLAG_OVERLAPPED |
\r
243 HANDLE_FLAG_IGNOREEOF);
\r
245 *realhost = dupstr(cfg->serline);
\r
250 static void serial_free(void *handle)
\r
252 Serial serial = (Serial) handle;
\r
254 serial_terminate(serial);
\r
258 static void serial_reconfig(void *handle, Config *cfg)
\r
260 Serial serial = (Serial) handle;
\r
263 err = serial_configure(serial, serial->port, cfg);
\r
266 * FIXME: what should we do if err returns something?
\r
271 * Called to send data down the serial connection.
\r
273 static int serial_send(void *handle, char *buf, int len)
\r
275 Serial serial = (Serial) handle;
\r
277 if (serial->out == NULL)
\r
280 serial->bufsize = handle_write(serial->out, buf, len);
\r
281 return serial->bufsize;
\r
285 * Called to query the current sendability status.
\r
287 static int serial_sendbuffer(void *handle)
\r
289 Serial serial = (Serial) handle;
\r
290 return serial->bufsize;
\r
294 * Called to set the size of the window
\r
296 static void serial_size(void *handle, int width, int height)
\r
303 * Send serial special codes.
\r
305 static void serial_special(void *handle, Telnet_Special code)
\r
308 * FIXME: serial break? XON? XOFF?
\r
314 * Return a list of the special codes that make sense in this
\r
317 static const struct telnet_special *serial_get_specials(void *handle)
\r
320 * FIXME: serial break? XON? XOFF?
\r
325 static int serial_connected(void *handle)
\r
327 return 1; /* always connected */
\r
330 static int serial_sendok(void *handle)
\r
335 static void serial_unthrottle(void *handle, int backlog)
\r
337 Serial serial = (Serial) handle;
\r
339 handle_unthrottle(serial->in, backlog);
\r
342 static int serial_ldisc(void *handle, int option)
\r
345 * Local editing and local echo are off by default.
\r
350 static void serial_provide_ldisc(void *handle, void *ldisc)
\r
352 /* This is a stub. */
\r
355 static void serial_provide_logctx(void *handle, void *logctx)
\r
357 /* This is a stub. */
\r
360 static int serial_exitcode(void *handle)
\r
362 Serial serial = (Serial) handle;
\r
363 if (serial->port != NULL)
\r
364 return -1; /* still connected */
\r
366 /* Exit codes are a meaningless concept with serial ports */
\r
371 * cfg_info for Serial does nothing at all.
\r
373 static int serial_cfg_info(void *handle)
\r
378 Backend serial_backend = {
\r
386 serial_get_specials,
\r
391 serial_provide_ldisc,
\r
392 serial_provide_logctx,
\r