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