d3b85d02 |
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; |
dcf640d6 |
220 | serial->bufsize = 0; |
d3b85d02 |
221 | *backend_handle = serial; |
222 | |
223 | serial->frontend = frontend_handle; |
224 | |
225 | { |
e5a76e12 |
226 | char *msg = dupprintf("Opening serial device %s", cfg->serline); |
d3b85d02 |
227 | logevent(serial->frontend, msg); |
228 | } |
229 | |
230 | serport = CreateFile(cfg->serline, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
231 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); |
232 | if (serport == INVALID_HANDLE_VALUE) |
233 | return "Unable to open serial port"; |
234 | |
235 | err = serial_configure(serial, serport, cfg); |
236 | if (err) |
237 | return err; |
238 | |
239 | serial->port = serport; |
240 | serial->out = handle_output_new(serport, serial_sentdata, serial, |
241 | HANDLE_FLAG_OVERLAPPED); |
242 | serial->in = handle_input_new(serport, serial_gotdata, serial, |
243 | HANDLE_FLAG_OVERLAPPED | |
c606abbc |
244 | HANDLE_FLAG_IGNOREEOF | |
245 | HANDLE_FLAG_UNITBUFFER); |
d3b85d02 |
246 | |
247 | *realhost = dupstr(cfg->serline); |
248 | |
249 | return NULL; |
250 | } |
251 | |
252 | static void serial_free(void *handle) |
253 | { |
254 | Serial serial = (Serial) handle; |
255 | |
256 | serial_terminate(serial); |
257 | sfree(serial); |
258 | } |
259 | |
260 | static void serial_reconfig(void *handle, Config *cfg) |
261 | { |
262 | Serial serial = (Serial) handle; |
263 | const char *err; |
264 | |
265 | err = serial_configure(serial, serial->port, cfg); |
266 | |
267 | /* |
268 | * FIXME: what should we do if err returns something? |
269 | */ |
270 | } |
271 | |
272 | /* |
273 | * Called to send data down the serial connection. |
274 | */ |
275 | static int serial_send(void *handle, char *buf, int len) |
276 | { |
277 | Serial serial = (Serial) handle; |
278 | |
279 | if (serial->out == NULL) |
280 | return 0; |
281 | |
282 | serial->bufsize = handle_write(serial->out, buf, len); |
283 | return serial->bufsize; |
284 | } |
285 | |
286 | /* |
287 | * Called to query the current sendability status. |
288 | */ |
289 | static int serial_sendbuffer(void *handle) |
290 | { |
291 | Serial serial = (Serial) handle; |
292 | return serial->bufsize; |
293 | } |
294 | |
295 | /* |
296 | * Called to set the size of the window |
297 | */ |
298 | static void serial_size(void *handle, int width, int height) |
299 | { |
300 | /* Do nothing! */ |
301 | return; |
302 | } |
303 | |
304 | /* |
305 | * Send serial special codes. |
306 | */ |
307 | static void serial_special(void *handle, Telnet_Special code) |
308 | { |
309 | /* |
310 | * FIXME: serial break? XON? XOFF? |
311 | */ |
312 | return; |
313 | } |
314 | |
315 | /* |
316 | * Return a list of the special codes that make sense in this |
317 | * protocol. |
318 | */ |
319 | static const struct telnet_special *serial_get_specials(void *handle) |
320 | { |
321 | /* |
322 | * FIXME: serial break? XON? XOFF? |
323 | */ |
324 | return NULL; |
325 | } |
326 | |
327 | static int serial_connected(void *handle) |
328 | { |
329 | return 1; /* always connected */ |
330 | } |
331 | |
332 | static int serial_sendok(void *handle) |
333 | { |
334 | return 1; |
335 | } |
336 | |
337 | static void serial_unthrottle(void *handle, int backlog) |
338 | { |
339 | Serial serial = (Serial) handle; |
340 | if (serial->in) |
341 | handle_unthrottle(serial->in, backlog); |
342 | } |
343 | |
344 | static int serial_ldisc(void *handle, int option) |
345 | { |
346 | /* |
347 | * Local editing and local echo are off by default. |
348 | */ |
349 | return 0; |
350 | } |
351 | |
352 | static void serial_provide_ldisc(void *handle, void *ldisc) |
353 | { |
354 | /* This is a stub. */ |
355 | } |
356 | |
357 | static void serial_provide_logctx(void *handle, void *logctx) |
358 | { |
359 | /* This is a stub. */ |
360 | } |
361 | |
362 | static int serial_exitcode(void *handle) |
363 | { |
364 | Serial serial = (Serial) handle; |
365 | if (serial->port != NULL) |
366 | return -1; /* still connected */ |
367 | else |
368 | /* Exit codes are a meaningless concept with serial ports */ |
369 | return INT_MAX; |
370 | } |
371 | |
372 | /* |
373 | * cfg_info for Serial does nothing at all. |
374 | */ |
375 | static int serial_cfg_info(void *handle) |
376 | { |
377 | return 0; |
378 | } |
379 | |
380 | Backend serial_backend = { |
381 | serial_init, |
382 | serial_free, |
383 | serial_reconfig, |
384 | serial_send, |
385 | serial_sendbuffer, |
386 | serial_size, |
387 | serial_special, |
388 | serial_get_specials, |
389 | serial_connected, |
390 | serial_exitcode, |
391 | serial_sendok, |
392 | serial_ldisc, |
393 | serial_provide_ldisc, |
394 | serial_provide_logctx, |
395 | serial_unthrottle, |
396 | serial_cfg_info, |
397 | 1 |
398 | }; |