Support for Windows PuTTY connecting straight to a local serial port
[u/mdw/putty] / windows / winser.c
1 /*
2 * Serial back end (Windows-specific).
3 */
4
5 /*
6 * TODO:
7 *
8 * - sending breaks?
9 * + looks as if you do this by calling SetCommBreak(handle),
10 * then waiting a bit, then doing ClearCommBreak(handle). A
11 * small job for timing.c, methinks.
12 *
13 * - why are we dropping data when talking to judicator?
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <limits.h>
19
20 #include "putty.h"
21
22 #define SERIAL_MAX_BACKLOG 4096
23
24 typedef struct serial_backend_data {
25 HANDLE port;
26 struct handle *out, *in;
27 void *frontend;
28 int bufsize;
29 } *Serial;
30
31 static void serial_terminate(Serial serial)
32 {
33 if (serial->out) {
34 handle_free(serial->out);
35 serial->out = NULL;
36 }
37 if (serial->in) {
38 handle_free(serial->in);
39 serial->in = NULL;
40 }
41 if (serial->port) {
42 CloseHandle(serial->port);
43 serial->port = NULL;
44 }
45 }
46
47 static int serial_gotdata(struct handle *h, void *data, int len)
48 {
49 Serial serial = (Serial)handle_get_privdata(h);
50 if (len <= 0) {
51 const char *error_msg;
52
53 /*
54 * Currently, len==0 should never happen because we're
55 * ignoring EOFs. However, it seems not totally impossible
56 * that this same back end might be usable to talk to named
57 * pipes or some other non-serial device, in which case EOF
58 * may become meaningful here.
59 */
60 if (len == 0)
61 error_msg = "End of file reading from serial device";
62 else
63 error_msg = "Error reading from serial device";
64
65 serial_terminate(serial);
66
67 notify_remote_exit(serial->frontend);
68
69 logevent(serial->frontend, error_msg);
70
71 connection_fatal(serial->frontend, "%s", error_msg);
72
73 return 0; /* placate optimiser */
74 } else {
75 return from_backend(serial->frontend, 0, data, len);
76 }
77 }
78
79 static void serial_sentdata(struct handle *h, int new_backlog)
80 {
81 Serial serial = (Serial)handle_get_privdata(h);
82 if (new_backlog < 0) {
83 const char *error_msg = "Error writing to serial device";
84
85 serial_terminate(serial);
86
87 notify_remote_exit(serial->frontend);
88
89 logevent(serial->frontend, error_msg);
90
91 connection_fatal(serial->frontend, "%s", error_msg);
92 } else {
93 serial->bufsize = new_backlog;
94 }
95 }
96
97 static const char *serial_configure(Serial serial, HANDLE serport, Config *cfg)
98 {
99 DCB dcb;
100 COMMTIMEOUTS timeouts;
101
102 /*
103 * Set up the serial port parameters. If we can't even
104 * GetCommState, we ignore the problem on the grounds that the
105 * user might have pointed us at some other type of two-way
106 * device instead of a serial port.
107 */
108 if (GetCommState(serport, &dcb)) {
109 char *msg;
110 const char *str;
111
112 /*
113 * Boilerplate.
114 */
115 dcb.fBinary = TRUE;
116 dcb.fDtrControl = DTR_CONTROL_ENABLE;
117 dcb.fDsrSensitivity = FALSE;
118 dcb.fTXContinueOnXoff = FALSE;
119 dcb.fOutX = FALSE;
120 dcb.fInX = FALSE;
121 dcb.fErrorChar = FALSE;
122 dcb.fNull = FALSE;
123 dcb.fRtsControl = RTS_CONTROL_ENABLE;
124 dcb.fAbortOnError = FALSE;
125 dcb.fOutxCtsFlow = FALSE;
126 dcb.fOutxDsrFlow = FALSE;
127
128 /*
129 * Configurable parameters.
130 */
131 dcb.BaudRate = cfg->serspeed;
132 msg = dupprintf("Configuring baud rate %d", cfg->serspeed);
133 logevent(serial->frontend, msg);
134 sfree(msg);
135
136 dcb.ByteSize = cfg->serdatabits;
137 msg = dupprintf("Configuring %d data bits", cfg->serdatabits);
138 logevent(serial->frontend, msg);
139 sfree(msg);
140
141 switch (cfg->serstopbits) {
142 case 2: dcb.StopBits = ONESTOPBIT; str = "1"; break;
143 case 3: dcb.StopBits = ONE5STOPBITS; str = "1.5"; break;
144 case 4: dcb.StopBits = TWOSTOPBITS; str = "2"; break;
145 default: return "Invalid number of stop bits (need 1, 1.5 or 2)";
146 }
147 msg = dupprintf("Configuring %s data bits", str);
148 logevent(serial->frontend, msg);
149 sfree(msg);
150
151 switch (cfg->serparity) {
152 case SER_PAR_NONE: dcb.Parity = NOPARITY; str = "no"; break;
153 case SER_PAR_ODD: dcb.Parity = ODDPARITY; str = "odd"; break;
154 case SER_PAR_EVEN: dcb.Parity = EVENPARITY; str = "even"; break;
155 case SER_PAR_MARK: dcb.Parity = MARKPARITY; str = "mark"; break;
156 case SER_PAR_SPACE: dcb.Parity = SPACEPARITY; str = "space"; break;
157 }
158 msg = dupprintf("Configuring %s parity", str);
159 logevent(serial->frontend, msg);
160 sfree(msg);
161
162 switch (cfg->serflow) {
163 case SER_FLOW_NONE:
164 str = "no";
165 break;
166 case SER_FLOW_XONXOFF:
167 dcb.fOutX = dcb.fInX = TRUE;
168 str = "XON/XOFF";
169 break;
170 case SER_FLOW_RTSCTS:
171 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
172 dcb.fOutxCtsFlow = TRUE;
173 str = "RTS/CTS";
174 break;
175 case SER_FLOW_DSRDTR:
176 dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
177 dcb.fOutxDsrFlow = TRUE;
178 str = "DSR/DTR";
179 break;
180 }
181 msg = dupprintf("Configuring %s flow control", str);
182 logevent(serial->frontend, msg);
183 sfree(msg);
184
185 if (!SetCommState(serport, &dcb))
186 return "Unable to configure serial port";
187
188 timeouts.ReadIntervalTimeout = 1;
189 timeouts.ReadTotalTimeoutMultiplier = 0;
190 timeouts.ReadTotalTimeoutConstant = 0;
191 timeouts.WriteTotalTimeoutMultiplier = 0;
192 timeouts.WriteTotalTimeoutConstant = 0;
193 if (!SetCommTimeouts(serport, &timeouts))
194 return "Unable to configure serial timeouts";
195 }
196
197 return NULL;
198 }
199
200 /*
201 * Called to set up the serial connection.
202 *
203 * Returns an error message, or NULL on success.
204 *
205 * Also places the canonical host name into `realhost'. It must be
206 * freed by the caller.
207 */
208 static const char *serial_init(void *frontend_handle, void **backend_handle,
209 Config *cfg,
210 char *host, int port, char **realhost, int nodelay,
211 int keepalive)
212 {
213 Serial serial;
214 HANDLE serport;
215 const char *err;
216
217 serial = snew(struct serial_backend_data);
218 serial->port = NULL;
219 serial->out = serial->in = NULL;
220 *backend_handle = serial;
221
222 serial->frontend = frontend_handle;
223
224 {
225 char *msg = dupprintf("Opening serial device %s", host);
226 logevent(serial->frontend, msg);
227 }
228
229 serport = CreateFile(cfg->serline, GENERIC_READ | GENERIC_WRITE, 0, NULL,
230 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
231 if (serport == INVALID_HANDLE_VALUE)
232 return "Unable to open serial port";
233
234 err = serial_configure(serial, serport, cfg);
235 if (err)
236 return err;
237
238 serial->port = serport;
239 serial->out = handle_output_new(serport, serial_sentdata, serial,
240 HANDLE_FLAG_OVERLAPPED);
241 serial->in = handle_input_new(serport, serial_gotdata, serial,
242 HANDLE_FLAG_OVERLAPPED |
243 HANDLE_FLAG_IGNOREEOF);
244
245 *realhost = dupstr(cfg->serline);
246
247 return NULL;
248 }
249
250 static void serial_free(void *handle)
251 {
252 Serial serial = (Serial) handle;
253
254 serial_terminate(serial);
255 sfree(serial);
256 }
257
258 static void serial_reconfig(void *handle, Config *cfg)
259 {
260 Serial serial = (Serial) handle;
261 const char *err;
262
263 err = serial_configure(serial, serial->port, cfg);
264
265 /*
266 * FIXME: what should we do if err returns something?
267 */
268 }
269
270 /*
271 * Called to send data down the serial connection.
272 */
273 static int serial_send(void *handle, char *buf, int len)
274 {
275 Serial serial = (Serial) handle;
276
277 if (serial->out == NULL)
278 return 0;
279
280 serial->bufsize = handle_write(serial->out, buf, len);
281 return serial->bufsize;
282 }
283
284 /*
285 * Called to query the current sendability status.
286 */
287 static int serial_sendbuffer(void *handle)
288 {
289 Serial serial = (Serial) handle;
290 return serial->bufsize;
291 }
292
293 /*
294 * Called to set the size of the window
295 */
296 static void serial_size(void *handle, int width, int height)
297 {
298 /* Do nothing! */
299 return;
300 }
301
302 /*
303 * Send serial special codes.
304 */
305 static void serial_special(void *handle, Telnet_Special code)
306 {
307 /*
308 * FIXME: serial break? XON? XOFF?
309 */
310 return;
311 }
312
313 /*
314 * Return a list of the special codes that make sense in this
315 * protocol.
316 */
317 static const struct telnet_special *serial_get_specials(void *handle)
318 {
319 /*
320 * FIXME: serial break? XON? XOFF?
321 */
322 return NULL;
323 }
324
325 static int serial_connected(void *handle)
326 {
327 return 1; /* always connected */
328 }
329
330 static int serial_sendok(void *handle)
331 {
332 return 1;
333 }
334
335 static void serial_unthrottle(void *handle, int backlog)
336 {
337 Serial serial = (Serial) handle;
338 if (serial->in)
339 handle_unthrottle(serial->in, backlog);
340 }
341
342 static int serial_ldisc(void *handle, int option)
343 {
344 /*
345 * Local editing and local echo are off by default.
346 */
347 return 0;
348 }
349
350 static void serial_provide_ldisc(void *handle, void *ldisc)
351 {
352 /* This is a stub. */
353 }
354
355 static void serial_provide_logctx(void *handle, void *logctx)
356 {
357 /* This is a stub. */
358 }
359
360 static int serial_exitcode(void *handle)
361 {
362 Serial serial = (Serial) handle;
363 if (serial->port != NULL)
364 return -1; /* still connected */
365 else
366 /* Exit codes are a meaningless concept with serial ports */
367 return INT_MAX;
368 }
369
370 /*
371 * cfg_info for Serial does nothing at all.
372 */
373 static int serial_cfg_info(void *handle)
374 {
375 return 0;
376 }
377
378 Backend serial_backend = {
379 serial_init,
380 serial_free,
381 serial_reconfig,
382 serial_send,
383 serial_sendbuffer,
384 serial_size,
385 serial_special,
386 serial_get_specials,
387 serial_connected,
388 serial_exitcode,
389 serial_sendok,
390 serial_ldisc,
391 serial_provide_ldisc,
392 serial_provide_logctx,
393 serial_unthrottle,
394 serial_cfg_info,
395 1
396 };