/[rdesktop]/sourceforge.net/trunk/rdesktop/serial.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sourceforge.net/trunk/rdesktop/serial.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 801 - (hide annotations)
Tue Nov 23 13:29:12 2004 UTC (19 years, 6 months ago) by astrand
File MIME type: text/plain
File size: 26087 byte(s)
Indent fixes

1 matthewc 432 #include <unistd.h>
2     #include <fcntl.h>
3     #include <termios.h>
4 stargo 570 #include <strings.h>
5 stargo 757 #include <sys/ioctl.h>
6 stargo 798
7     #ifdef HAVE_SYS_MODEM_H
8     #include <sys/modem.h>
9     #endif
10     #ifdef HAVE_SYS_FILIO_H
11     #include <sys/filio.h>
12     #endif
13     #ifdef HAVE_SYS_STRTIO_H
14     #include <sys/strtio.h>
15     #endif
16    
17 matthewc 432 #include "rdesktop.h"
18    
19 stargo 795 #ifdef WITH_DEBUG_SERIAL
20     #define DEBUG_SERIAL(args) printf args;
21     #else
22     #define DEBUG_SERIAL(args)
23     #endif
24    
25 matthewc 432 #define FILE_DEVICE_SERIAL_PORT 0x1b
26    
27     #define SERIAL_SET_BAUD_RATE 1
28     #define SERIAL_SET_QUEUE_SIZE 2
29     #define SERIAL_SET_LINE_CONTROL 3
30     #define SERIAL_SET_BREAK_ON 4
31     #define SERIAL_SET_BREAK_OFF 5
32     #define SERIAL_IMMEDIATE_CHAR 6
33     #define SERIAL_SET_TIMEOUTS 7
34     #define SERIAL_GET_TIMEOUTS 8
35     #define SERIAL_SET_DTR 9
36     #define SERIAL_CLR_DTR 10
37     #define SERIAL_RESET_DEVICE 11
38     #define SERIAL_SET_RTS 12
39     #define SERIAL_CLR_RTS 13
40     #define SERIAL_SET_XOFF 14
41     #define SERIAL_SET_XON 15
42     #define SERIAL_GET_WAIT_MASK 16
43     #define SERIAL_SET_WAIT_MASK 17
44     #define SERIAL_WAIT_ON_MASK 18
45     #define SERIAL_PURGE 19
46     #define SERIAL_GET_BAUD_RATE 20
47     #define SERIAL_GET_LINE_CONTROL 21
48     #define SERIAL_GET_CHARS 22
49     #define SERIAL_SET_CHARS 23
50     #define SERIAL_GET_HANDFLOW 24
51     #define SERIAL_SET_HANDFLOW 25
52     #define SERIAL_GET_MODEMSTATUS 26
53     #define SERIAL_GET_COMMSTATUS 27
54     #define SERIAL_XOFF_COUNTER 28
55     #define SERIAL_GET_PROPERTIES 29
56     #define SERIAL_GET_DTRRTS 30
57     #define SERIAL_LSRMST_INSERT 31
58     #define SERIAL_CONFIG_SIZE 32
59     #define SERIAL_GET_COMMCONFIG 33
60     #define SERIAL_SET_COMMCONFIG 34
61     #define SERIAL_GET_STATS 35
62     #define SERIAL_CLEAR_STATS 36
63     #define SERIAL_GET_MODEM_CONTROL 37
64     #define SERIAL_SET_MODEM_CONTROL 38
65     #define SERIAL_SET_FIFO_CONTROL 39
66    
67     #define STOP_BITS_1 0
68     #define STOP_BITS_2 2
69    
70     #define NO_PARITY 0
71     #define ODD_PARITY 1
72     #define EVEN_PARITY 2
73    
74 n-ki 622 #define SERIAL_PURGE_TXABORT 0x00000001
75     #define SERIAL_PURGE_RXABORT 0x00000002
76     #define SERIAL_PURGE_TXCLEAR 0x00000004
77     #define SERIAL_PURGE_RXCLEAR 0x00000008
78    
79     /* SERIAL_WAIT_ON_MASK */
80     #define SERIAL_EV_RXCHAR 0x0001 // Any Character received
81     #define SERIAL_EV_RXFLAG 0x0002 // Received certain character
82     #define SERIAL_EV_TXEMPTY 0x0004 // Transmitt Queue Empty
83     #define SERIAL_EV_CTS 0x0008 // CTS changed state
84     #define SERIAL_EV_DSR 0x0010 // DSR changed state
85     #define SERIAL_EV_RLSD 0x0020 // RLSD changed state
86     #define SERIAL_EV_BREAK 0x0040 // BREAK received
87     #define SERIAL_EV_ERR 0x0080 // Line status error occurred
88     #define SERIAL_EV_RING 0x0100 // Ring signal detected
89     #define SERIAL_EV_PERR 0x0200 // Printer error occured
90     #define SERIAL_EV_RX80FULL 0x0400 // Receive buffer is 80 percent full
91     #define SERIAL_EV_EVENT1 0x0800 // Provider specific event 1
92     #define SERIAL_EV_EVENT2 0x1000 // Provider specific event 2
93    
94 stargo 757 /* Modem Status */
95 stargo 795 #define SERIAL_MS_DTR 0x01
96     #define SERIAL_MS_RTS 0x02
97 stargo 757 #define SERIAL_MS_CTS 0x10
98     #define SERIAL_MS_DSR 0x20
99     #define SERIAL_MS_RNG 0x40
100     #define SERIAL_MS_CAR 0x80
101    
102 stargo 795 /* Handflow */
103     #define SERIAL_DTR_CONTROL 0x01
104     #define SERIAL_CTS_HANDSHAKE 0x08
105     #define SERIAL_ERROR_ABORT 0x80000000
106    
107     #define SERIAL_XON_HANDSHAKE 0x01
108     #define SERIAL_XOFF_HANDSHAKE 0x02
109     #define SERIAL_DSR_SENSITIVITY 0x40
110    
111     #define SERIAL_CHAR_EOF 0
112     #define SERIAL_CHAR_ERROR 1
113     #define SERIAL_CHAR_BREAK 2
114     #define SERIAL_CHAR_EVENT 3
115     #define SERIAL_CHAR_XON 4
116     #define SERIAL_CHAR_XOFF 5
117    
118 stargo 686 #ifndef CRTSCTS
119     #define CRTSCTS 0
120     #endif
121 n-ki 622
122 stargo 795 /* FIONREAD should really do the same thing as TIOCINQ, where it is
123     * not available */
124     #ifndef TIOCINQ
125     #define TIOCINQ FIONREAD
126     #endif
127 stargo 686
128 astrand 580 extern RDPDR_DEVICE g_rdpdr_device[];
129 n-ki 569
130 astrand 665 static SERIAL_DEVICE *
131 jsorg71 776 get_serial_info(NTHANDLE handle)
132 matthewc 432 {
133 astrand 580 int index;
134 matthewc 432
135 astrand 580 for (index = 0; index < RDPDR_MAX_DEVICES; index++)
136     {
137     if (handle == g_rdpdr_device[index].handle)
138     return (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
139     }
140     return NULL;
141 n-ki 569 }
142 matthewc 432
143 astrand 665 static BOOL
144 jsorg71 776 get_termios(SERIAL_DEVICE * pser_inf, NTHANDLE serial_fd)
145 n-ki 569 {
146 astrand 580 speed_t speed;
147     struct termios *ptermios;
148 n-ki 569
149 astrand 580 ptermios = pser_inf->ptermios;
150 n-ki 569
151 astrand 580 if (tcgetattr(serial_fd, ptermios) == -1)
152     return False;
153 n-ki 569
154 astrand 580 speed = cfgetispeed(ptermios);
155     switch (speed)
156     {
157 stargo 545 #ifdef B75
158 astrand 580 case B75:
159     pser_inf->baud_rate = 75;
160     break;
161 stargo 545 #endif
162     #ifdef B110
163 astrand 580 case B110:
164     pser_inf->baud_rate = 110;
165     break;
166 stargo 545 #endif
167     #ifdef B134
168 astrand 580 case B134:
169     pser_inf->baud_rate = 134;
170     break;
171 stargo 545 #endif
172     #ifdef B150
173 astrand 580 case B150:
174     pser_inf->baud_rate = 150;
175     break;
176 stargo 545 #endif
177     #ifdef B300
178 astrand 580 case B300:
179     pser_inf->baud_rate = 300;
180     break;
181 stargo 545 #endif
182     #ifdef B600
183 astrand 580 case B600:
184     pser_inf->baud_rate = 600;
185     break;
186 stargo 545 #endif
187     #ifdef B1200
188 astrand 580 case B1200:
189     pser_inf->baud_rate = 1200;
190     break;
191 stargo 545 #endif
192     #ifdef B1800
193 astrand 580 case B1800:
194     pser_inf->baud_rate = 1800;
195     break;
196 stargo 545 #endif
197     #ifdef B2400
198 astrand 580 case B2400:
199     pser_inf->baud_rate = 2400;
200     break;
201 stargo 545 #endif
202     #ifdef B4800
203 astrand 580 case B4800:
204     pser_inf->baud_rate = 4800;
205     break;
206 stargo 545 #endif
207     #ifdef B9600
208 astrand 580 case B9600:
209     pser_inf->baud_rate = 9600;
210     break;
211 stargo 545 #endif
212     #ifdef B19200
213 astrand 580 case B19200:
214     pser_inf->baud_rate = 19200;
215     break;
216 stargo 545 #endif
217     #ifdef B38400
218 astrand 580 case B38400:
219     pser_inf->baud_rate = 38400;
220     break;
221 stargo 545 #endif
222     #ifdef B57600
223 astrand 580 case B57600:
224     pser_inf->baud_rate = 57600;
225     break;
226 stargo 545 #endif
227     #ifdef B115200
228 astrand 580 case B115200:
229     pser_inf->baud_rate = 115200;
230     break;
231 stargo 545 #endif
232 stargo 795 #ifdef B230400
233     case B230400:
234     pser_inf->baud_rate = 230400;
235     break;
236     #endif
237     #ifdef B460800
238     case B460800:
239     pser_inf->baud_rate = 460800;
240     break;
241     #endif
242 astrand 580 default:
243 stargo 795 pser_inf->baud_rate = 9600;
244 astrand 580 break;
245     }
246 matthewc 432
247 astrand 580 speed = cfgetospeed(ptermios);
248     pser_inf->dtr = (speed == B0) ? 0 : 1;
249 matthewc 432
250 astrand 580 pser_inf->stop_bits = (ptermios->c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BITS_1;
251     pser_inf->parity =
252     (ptermios->
253     c_cflag & PARENB) ? ((ptermios->
254     c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY;
255     switch (ptermios->c_cflag & CSIZE)
256     {
257     case CS5:
258     pser_inf->word_length = 5;
259     break;
260     case CS6:
261     pser_inf->word_length = 6;
262     break;
263     case CS7:
264     pser_inf->word_length = 7;
265     break;
266     default:
267     pser_inf->word_length = 8;
268     break;
269     }
270 matthewc 432
271 stargo 795 if (ptermios->c_cflag & CRTSCTS)
272     {
273     pser_inf->control = SERIAL_DTR_CONTROL | SERIAL_CTS_HANDSHAKE | SERIAL_ERROR_ABORT;
274     }
275     else
276     {
277     pser_inf->control = SERIAL_DTR_CONTROL | SERIAL_ERROR_ABORT;
278     }
279 n-ki 622
280 stargo 795 pser_inf->xonoff = SERIAL_DSR_SENSITIVITY;
281     if (ptermios->c_iflag & IXON)
282     pser_inf->xonoff |= SERIAL_XON_HANDSHAKE;
283    
284     if (ptermios->c_iflag & IXOFF)
285     pser_inf->xonoff |= SERIAL_XOFF_HANDSHAKE;
286    
287     pser_inf->chars[SERIAL_CHAR_XON] = ptermios->c_cc[VSTART];
288     pser_inf->chars[SERIAL_CHAR_XOFF] = ptermios->c_cc[VSTOP];
289     pser_inf->chars[SERIAL_CHAR_EOF] = ptermios->c_cc[VEOF];
290     pser_inf->chars[SERIAL_CHAR_BREAK] = ptermios->c_cc[VINTR];
291     pser_inf->chars[SERIAL_CHAR_ERROR] = ptermios->c_cc[VKILL];
292    
293 astrand 580 return True;
294 matthewc 432 }
295    
296     static void
297 jsorg71 776 set_termios(SERIAL_DEVICE * pser_inf, NTHANDLE serial_fd)
298 matthewc 432 {
299     speed_t speed;
300    
301 n-ki 588 struct termios *ptermios;
302    
303     ptermios = pser_inf->ptermios;
304    
305    
306     switch (pser_inf->baud_rate)
307 matthewc 432 {
308 stargo 545 #ifdef B75
309 astrand 580 case 75:
310     speed = B75;
311     break;
312 stargo 545 #endif
313     #ifdef B110
314 astrand 580 case 110:
315     speed = B110;
316     break;
317 stargo 545 #endif
318     #ifdef B134
319 astrand 580 case 134:
320     speed = B134;
321     break;
322 stargo 545 #endif
323     #ifdef B150
324 astrand 580 case 150:
325     speed = B150;
326     break;
327 stargo 545 #endif
328     #ifdef B300
329 astrand 580 case 300:
330     speed = B300;
331     break;
332 stargo 545 #endif
333     #ifdef B600
334 astrand 580 case 600:
335     speed = B600;
336     break;
337 stargo 545 #endif
338     #ifdef B1200
339 astrand 580 case 1200:
340     speed = B1200;
341     break;
342 stargo 545 #endif
343     #ifdef B1800
344 astrand 580 case 1800:
345     speed = B1800;
346     break;
347 stargo 545 #endif
348     #ifdef B2400
349 astrand 580 case 2400:
350     speed = B2400;
351     break;
352 stargo 545 #endif
353     #ifdef B4800
354 astrand 580 case 4800:
355     speed = B4800;
356     break;
357 stargo 545 #endif
358     #ifdef B9600
359 astrand 580 case 9600:
360     speed = B9600;
361     break;
362 stargo 545 #endif
363     #ifdef B19200
364 astrand 580 case 19200:
365     speed = B19200;
366     break;
367 stargo 545 #endif
368     #ifdef B38400
369 astrand 580 case 38400:
370     speed = B38400;
371     break;
372 stargo 545 #endif
373     #ifdef B57600
374 astrand 580 case 57600:
375     speed = B57600;
376     break;
377 stargo 545 #endif
378     #ifdef B115200
379 astrand 580 case 115200:
380     speed = B115200;
381     break;
382 stargo 545 #endif
383 stargo 795 #ifdef B230400
384     case 230400:
385     speed = B115200;
386     break;
387     #endif
388     #ifdef B460800
389     case 460800:
390     speed = B115200;
391     break;
392     #endif
393 astrand 580 default:
394 stargo 795 speed = B9600;
395 astrand 580 break;
396 matthewc 432 }
397    
398 stargo 799 #ifdef CBAUD
399     ptermios->c_cflag &= ~CBAUD;
400     ptermios->c_cflag |= speed;
401     #else
402 matthewc 432 /* on systems with separate ispeed and ospeed, we can remember the speed
403     in ispeed while changing DTR with ospeed */
404 n-ki 588 cfsetispeed(pser_inf->ptermios, speed);
405     cfsetospeed(pser_inf->ptermios, pser_inf->dtr ? speed : 0);
406 stargo 795 #endif
407 matthewc 432
408 n-ki 622 ptermios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE | CRTSCTS);
409 n-ki 588 switch (pser_inf->stop_bits)
410 matthewc 432 {
411     case STOP_BITS_2:
412 n-ki 588 ptermios->c_cflag |= CSTOPB;
413 matthewc 432 break;
414 stargo 795 default:
415     ptermios->c_cflag &= ~CSTOPB;
416     break;
417 matthewc 432 }
418 n-ki 622
419 n-ki 588 switch (pser_inf->parity)
420 matthewc 432 {
421     case EVEN_PARITY:
422 n-ki 588 ptermios->c_cflag |= PARENB;
423 matthewc 432 break;
424     case ODD_PARITY:
425 n-ki 588 ptermios->c_cflag |= PARENB | PARODD;
426 matthewc 432 break;
427 stargo 795 case NO_PARITY:
428     ptermios->c_cflag &= ~(PARENB | PARODD);
429     break;
430 matthewc 432 }
431 n-ki 622
432 n-ki 588 switch (pser_inf->word_length)
433 matthewc 432 {
434 astrand 435 case 5:
435 n-ki 588 ptermios->c_cflag |= CS5;
436 astrand 435 break;
437     case 6:
438 n-ki 588 ptermios->c_cflag |= CS6;
439 astrand 435 break;
440     case 7:
441 n-ki 588 ptermios->c_cflag |= CS7;
442 astrand 435 break;
443     default:
444 n-ki 588 ptermios->c_cflag |= CS8;
445 astrand 435 break;
446 matthewc 432 }
447    
448 stargo 795 #if 0
449 n-ki 622 if (pser_inf->rts)
450     ptermios->c_cflag |= CRTSCTS;
451 stargo 795 else
452     ptermios->c_cflag &= ~CRTSCTS;
453     #endif
454 n-ki 622
455 stargo 795 if (pser_inf->control & SERIAL_CTS_HANDSHAKE)
456     {
457     ptermios->c_cflag |= CRTSCTS;
458     }
459     else
460     {
461     ptermios->c_cflag &= ~CRTSCTS;
462     }
463    
464    
465     if (pser_inf->xonoff & SERIAL_XON_HANDSHAKE)
466     {
467     ptermios->c_iflag |= IXON | IMAXBEL;
468     }
469     if (pser_inf->xonoff & SERIAL_XOFF_HANDSHAKE)
470     {
471     ptermios->c_iflag |= IXOFF | IMAXBEL;
472     }
473    
474     if ((pser_inf->xonoff & (SERIAL_XOFF_HANDSHAKE | SERIAL_XON_HANDSHAKE)) == 0)
475     {
476     ptermios->c_iflag &= ~IXON;
477     ptermios->c_iflag &= ~IXOFF;
478     }
479    
480     ptermios->c_cc[VSTART] = pser_inf->chars[SERIAL_CHAR_XON];
481     ptermios->c_cc[VSTOP] = pser_inf->chars[SERIAL_CHAR_XOFF];
482     ptermios->c_cc[VEOF] = pser_inf->chars[SERIAL_CHAR_EOF];
483     ptermios->c_cc[VINTR] = pser_inf->chars[SERIAL_CHAR_BREAK];
484     ptermios->c_cc[VKILL] = pser_inf->chars[SERIAL_CHAR_ERROR];
485    
486 n-ki 588 tcsetattr(serial_fd, TCSANOW, ptermios);
487 matthewc 432 }
488    
489 n-ki 569 /* Enumeration of devices from rdesktop.c */
490     /* returns numer of units found and initialized. */
491     /* optarg looks like ':com1=/dev/ttyS0' */
492     /* when it arrives to this function. */
493 n-ki 578 /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */
494 n-ki 569 int
495 astrand 608 serial_enum_devices(uint32 * id, char *optarg)
496 matthewc 432 {
497 astrand 580 SERIAL_DEVICE *pser_inf;
498 matthewc 432
499 n-ki 578 char *pos = optarg;
500     char *pos2;
501     int count = 0;
502 matthewc 432
503 n-ki 578 // skip the first colon
504     optarg++;
505     while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
506     {
507 n-ki 569 // Init data structures for device
508     pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE));
509     pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios));
510 stargo 755 memset(pser_inf->ptermios, 0, sizeof(struct termios));
511 n-ki 569 pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios));
512 stargo 755 memset(pser_inf->pold_termios, 0, sizeof(struct termios));
513 n-ki 569
514 n-ki 578 pos2 = next_arg(optarg, '=');
515     strcpy(g_rdpdr_device[*id].name, optarg);
516 n-ki 569
517 n-ki 578 toupper_str(g_rdpdr_device[*id].name);
518 n-ki 569
519 n-ki 578 g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);
520     strcpy(g_rdpdr_device[*id].local_path, pos2);
521 astrand 580 printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name,
522     g_rdpdr_device[*id].local_path);
523 n-ki 569 // set device type
524     g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL;
525     g_rdpdr_device[*id].pdevice_data = (void *) pser_inf;
526 n-ki 578 count++;
527 n-ki 569 (*id)++;
528    
529 n-ki 578 optarg = pos;
530 n-ki 569 }
531 n-ki 578 return count;
532 matthewc 432 }
533    
534 astrand 665 static NTSTATUS
535 astrand 580 serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition,
536 jsorg71 776 uint32 flags_and_attributes, char *filename, NTHANDLE * handle)
537 n-ki 569 {
538 jsorg71 776 NTHANDLE serial_fd;
539 astrand 580 SERIAL_DEVICE *pser_inf;
540     struct termios *ptermios;
541 n-ki 569
542 astrand 580 pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;
543     ptermios = pser_inf->ptermios;
544 stargo 755 serial_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR | O_NOCTTY | O_NONBLOCK);
545 n-ki 569
546 astrand 580 if (serial_fd == -1)
547 n-ki 588 {
548     perror("open");
549 astrand 580 return STATUS_ACCESS_DENIED;
550 n-ki 588 }
551 n-ki 569
552 astrand 580 if (!get_termios(pser_inf, serial_fd))
553 n-ki 622 {
554     printf("INFO: SERIAL %s access denied\n", g_rdpdr_device[device_id].name);
555     fflush(stdout);
556 astrand 580 return STATUS_ACCESS_DENIED;
557 n-ki 622 }
558 n-ki 569
559 n-ki 578 // Store handle for later use
560 astrand 580 g_rdpdr_device[device_id].handle = serial_fd;
561 n-ki 569
562 n-ki 578 /* some sane information */
563 stargo 795 DEBUG_SERIAL(("INFO: SERIAL %s to %s\nINFO: speed %u baud, stop bits %u, parity %u, word length %u bits, dtr %u, rts %u\n", g_rdpdr_device[device_id].name, g_rdpdr_device[device_id].local_path, pser_inf->baud_rate, pser_inf->stop_bits, pser_inf->parity, pser_inf->word_length, pser_inf->dtr, pser_inf->rts));
564 n-ki 622
565 astrand 782 pser_inf->ptermios->c_iflag &=
566     ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
567 stargo 759 pser_inf->ptermios->c_oflag &= ~OPOST;
568 stargo 795 pser_inf->ptermios->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN | XCASE);
569 astrand 782 pser_inf->ptermios->c_cflag &= ~(CSIZE | PARENB);
570 stargo 759 pser_inf->ptermios->c_cflag |= CS8;
571 stargo 795
572 stargo 759 tcsetattr(serial_fd, TCSANOW, pser_inf->ptermios);
573 n-ki 592
574 stargo 795 pser_inf->event_txempty = 0;
575     pser_inf->event_cts = 0;
576     pser_inf->event_dsr = 0;
577     pser_inf->event_rlsd = 0;
578     pser_inf->event_pending = 0;
579    
580 astrand 580 *handle = serial_fd;
581 n-ki 592
582     /* all read and writes should be non blocking */
583     if (fcntl(*handle, F_SETFL, O_NONBLOCK) == -1)
584     perror("fcntl");
585    
586 stargo 795 pser_inf->read_total_timeout_constant = 5;
587    
588 astrand 580 return STATUS_SUCCESS;
589 n-ki 569 }
590    
591 matthewc 432 static NTSTATUS
592 jsorg71 776 serial_close(NTHANDLE handle)
593 matthewc 432 {
594 n-ki 622 int i = get_device_index(handle);
595     if (i >= 0)
596     g_rdpdr_device[i].handle = 0;
597 stargo 795
598     rdpdr_abort_io(handle, 0, STATUS_TIMEOUT);
599 n-ki 588 close(handle);
600 matthewc 432 return STATUS_SUCCESS;
601     }
602    
603 astrand 665 static NTSTATUS
604 jsorg71 776 serial_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
605 matthewc 432 {
606 astrand 580 long timeout;
607     SERIAL_DEVICE *pser_inf;
608     struct termios *ptermios;
609 stargo 795 #ifdef WITH_DEBUG_SERIAL
610     int bytes_inqueue;
611     #endif
612 n-ki 569
613 stargo 795
614 n-ki 592 timeout = 90;
615 astrand 580 pser_inf = get_serial_info(handle);
616     ptermios = pser_inf->ptermios;
617 n-ki 569
618 astrand 580 // Set timeouts kind of like the windows serial timeout parameters. Multiply timeout
619     // with requested read size
620     if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant)
621     {
622     timeout =
623     (pser_inf->read_total_timeout_multiplier * length +
624     pser_inf->read_total_timeout_constant + 99) / 100;
625     }
626     else if (pser_inf->read_interval_timeout)
627     {
628     timeout = (pser_inf->read_interval_timeout * length + 99) / 100;
629     }
630 n-ki 569
631 astrand 580 // If a timeout is set, do a blocking read, which times out after some time.
632     // It will make rdesktop less responsive, but it will improve serial performance, by not
633     // reading one character at a time.
634     if (timeout == 0)
635     {
636     ptermios->c_cc[VTIME] = 0;
637     ptermios->c_cc[VMIN] = 0;
638     }
639     else
640     {
641     ptermios->c_cc[VTIME] = timeout;
642     ptermios->c_cc[VMIN] = 1;
643     }
644     tcsetattr(handle, TCSANOW, ptermios);
645 n-ki 592
646 stargo 795 #ifdef WITH_DEBUG_SERIAL
647     ioctl(handle, TIOCINQ, &bytes_inqueue);
648     DEBUG_SERIAL(("serial_read inqueue: %d expected %d\n", bytes_inqueue, length));
649     #endif
650 n-ki 592
651 astrand 580 *result = read(handle, data, length);
652 n-ki 592
653 stargo 795 #ifdef WITH_DEBUG_SERIAL
654     DEBUG_SERIAL(("serial_read Bytes %d\n", *result));
655     if (*result > 0)
656     hexdump(data, *result);
657     #endif
658 n-ki 622
659 astrand 580 return STATUS_SUCCESS;
660 matthewc 432 }
661    
662 astrand 665 static NTSTATUS
663 jsorg71 776 serial_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
664 matthewc 432 {
665 stargo 795 SERIAL_DEVICE *pser_inf;
666    
667     pser_inf = get_serial_info(handle);
668    
669 astrand 580 *result = write(handle, data, length);
670 stargo 795
671     if (*result > 0)
672     pser_inf->event_txempty = *result;
673    
674     DEBUG_SERIAL(("serial_write length %d, offset %d result %d\n", length, offset, *result));
675    
676 astrand 580 return STATUS_SUCCESS;
677 matthewc 432 }
678    
679     static NTSTATUS
680 jsorg71 776 serial_device_control(NTHANDLE handle, uint32 request, STREAM in, STREAM out)
681 matthewc 432 {
682 n-ki 622 int flush_mask, purge_mask;
683 stargo 757 uint32 result, modemstate;
684 matthewc 432 uint8 immediate;
685 n-ki 588 SERIAL_DEVICE *pser_inf;
686     struct termios *ptermios;
687 matthewc 432
688     if ((request >> 16) != FILE_DEVICE_SERIAL_PORT)
689     return STATUS_INVALID_PARAMETER;
690    
691 n-ki 588 pser_inf = get_serial_info(handle);
692     ptermios = pser_inf->ptermios;
693    
694 matthewc 432 /* extract operation */
695     request >>= 2;
696     request &= 0xfff;
697    
698     switch (request)
699     {
700     case SERIAL_SET_BAUD_RATE:
701 n-ki 588 in_uint32_le(in, pser_inf->baud_rate);
702     set_termios(pser_inf, handle);
703 astrand 801 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BAUD_RATE %d\n",
704     pser_inf->baud_rate));
705 matthewc 432 break;
706     case SERIAL_GET_BAUD_RATE:
707 n-ki 588 out_uint32_le(out, pser_inf->baud_rate);
708 astrand 801 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_BAUD_RATE %d\n",
709     pser_inf->baud_rate));
710 matthewc 432 break;
711     case SERIAL_SET_QUEUE_SIZE:
712 n-ki 588 in_uint32_le(in, pser_inf->queue_in_size);
713     in_uint32_le(in, pser_inf->queue_out_size);
714 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_QUEUE_SIZE in %d out %d\n",
715 astrand 801 pser_inf->queue_in_size, pser_inf->queue_out_size));
716 matthewc 432 break;
717     case SERIAL_SET_LINE_CONTROL:
718 n-ki 588 in_uint8(in, pser_inf->stop_bits);
719     in_uint8(in, pser_inf->parity);
720     in_uint8(in, pser_inf->word_length);
721     set_termios(pser_inf, handle);
722 astrand 801 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_LINE_CONTROL stop %d parity %d word %d\n", pser_inf->stop_bits, pser_inf->parity, pser_inf->word_length));
723 matthewc 432 break;
724     case SERIAL_GET_LINE_CONTROL:
725 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_LINE_CONTROL\n"));
726 n-ki 588 out_uint8(out, pser_inf->stop_bits);
727     out_uint8(out, pser_inf->parity);
728     out_uint8(out, pser_inf->word_length);
729 matthewc 432 break;
730     case SERIAL_IMMEDIATE_CHAR:
731 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_IMMEDIATE_CHAR\n"));
732 matthewc 432 in_uint8(in, immediate);
733 n-ki 569 serial_write(handle, &immediate, 1, 0, &result);
734 matthewc 432 break;
735     case SERIAL_CONFIG_SIZE:
736 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CONFIG_SIZE\n"));
737 matthewc 432 out_uint32_le(out, 0);
738     break;
739     case SERIAL_GET_CHARS:
740 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_CHARS\n"));
741     out_uint8a(out, pser_inf->chars, 6);
742 matthewc 432 break;
743     case SERIAL_SET_CHARS:
744 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_CHARS\n"));
745     in_uint8a(in, pser_inf->chars, 6);
746     #ifdef WITH_DEBUG_SERIAL
747     hexdump(pser_inf->chars, 6);
748     #endif
749     set_termios(pser_inf, handle);
750 matthewc 432 break;
751     case SERIAL_GET_HANDFLOW:
752 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_HANDFLOW\n"));
753     get_termios(pser_inf, handle);
754     out_uint32_le(out, pser_inf->control);
755     out_uint32_le(out, pser_inf->xonoff); /* Xon/Xoff */
756     out_uint32_le(out, pser_inf->onlimit);
757     out_uint32_le(out, pser_inf->offlimit);
758 matthewc 432 break;
759     case SERIAL_SET_HANDFLOW:
760 stargo 795 in_uint32_le(in, pser_inf->control);
761     in_uint32_le(in, pser_inf->xonoff);
762     in_uint32_le(in, pser_inf->onlimit);
763     in_uint32_le(in, pser_inf->offlimit);
764     DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_HANDFLOW %x %x %x %x\n",
765 astrand 801 pser_inf->control, pser_inf->xonoff, pser_inf->onlimit,
766     pser_inf->onlimit));
767 stargo 795 set_termios(pser_inf, handle);
768 matthewc 432 break;
769     case SERIAL_SET_TIMEOUTS:
770 stargo 795 in_uint32(in, pser_inf->read_interval_timeout);
771     in_uint32(in, pser_inf->read_total_timeout_multiplier);
772     in_uint32(in, pser_inf->read_total_timeout_constant);
773     in_uint32(in, pser_inf->write_total_timeout_multiplier);
774     in_uint32(in, pser_inf->write_total_timeout_constant);
775     DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_TIMEOUTS read timeout %d %d %d\n",
776 astrand 801 pser_inf->read_interval_timeout,
777     pser_inf->read_total_timeout_multiplier,
778     pser_inf->read_total_timeout_constant));
779 matthewc 432 break;
780     case SERIAL_GET_TIMEOUTS:
781 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_TIMEOUTS read timeout %d %d %d\n",
782 astrand 801 pser_inf->read_interval_timeout,
783     pser_inf->read_total_timeout_multiplier,
784     pser_inf->read_total_timeout_constant));
785 stargo 795
786     out_uint32(out, pser_inf->read_interval_timeout);
787     out_uint32(out, pser_inf->read_total_timeout_multiplier);
788     out_uint32(out, pser_inf->read_total_timeout_constant);
789     out_uint32(out, pser_inf->write_total_timeout_multiplier);
790     out_uint32(out, pser_inf->write_total_timeout_constant);
791 matthewc 432 break;
792     case SERIAL_GET_WAIT_MASK:
793 astrand 801 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_WAIT_MASK %X\n",
794     pser_inf->wait_mask); out_uint32(out, pser_inf->wait_mask));
795 matthewc 432 break;
796     case SERIAL_SET_WAIT_MASK:
797 n-ki 588 in_uint32(in, pser_inf->wait_mask);
798 astrand 801 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_WAIT_MASK %X\n",
799     pser_inf->wait_mask));
800 matthewc 432 break;
801     case SERIAL_SET_DTR:
802 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_DTR\n"));
803     ioctl(handle, TIOCMGET, &result);
804     result |= TIOCM_DTR;
805     ioctl(handle, TIOCMSET, &result);
806 n-ki 588 pser_inf->dtr = 1;
807 matthewc 432 break;
808     case SERIAL_CLR_DTR:
809 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CLR_DTR\n"));
810     ioctl(handle, TIOCMGET, &result);
811     result &= ~TIOCM_DTR;
812     ioctl(handle, TIOCMSET, &result);
813 n-ki 588 pser_inf->dtr = 0;
814 matthewc 432 break;
815 n-ki 622 case SERIAL_SET_RTS:
816 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_RTS\n"));
817     ioctl(handle, TIOCMGET, &result);
818     result |= TIOCM_RTS;
819     ioctl(handle, TIOCMSET, &result);
820 n-ki 622 pser_inf->rts = 1;
821 matthewc 432 break;
822 n-ki 622 case SERIAL_CLR_RTS:
823 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_CLR_RTS\n"));
824     ioctl(handle, TIOCMGET, &result);
825     result &= ~TIOCM_RTS;
826     ioctl(handle, TIOCMSET, &result);
827 n-ki 622 pser_inf->rts = 0;
828 matthewc 432 break;
829 n-ki 622 case SERIAL_GET_MODEMSTATUS:
830 stargo 757 modemstate = 0;
831     #ifdef TIOCMGET
832     ioctl(handle, TIOCMGET, &result);
833     if (result & TIOCM_CTS)
834     modemstate |= SERIAL_MS_CTS;
835     if (result & TIOCM_DSR)
836     modemstate |= SERIAL_MS_DSR;
837     if (result & TIOCM_RNG)
838     modemstate |= SERIAL_MS_RNG;
839     if (result & TIOCM_CAR)
840     modemstate |= SERIAL_MS_CAR;
841 stargo 795 if (result & TIOCM_DTR)
842     modemstate |= SERIAL_MS_DTR;
843     if (result & TIOCM_RTS)
844     modemstate |= SERIAL_MS_RTS;
845 stargo 757 #endif
846 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_MODEMSTATUS %X\n", modemstate));
847 stargo 757 out_uint32_le(out, modemstate);
848 n-ki 622 break;
849     case SERIAL_GET_COMMSTATUS:
850     out_uint32_le(out, 0); /* Errors */
851     out_uint32_le(out, 0); /* Hold reasons */
852 stargo 795
853     result = 0;
854     ioctl(handle, TIOCINQ, &result);
855     out_uint32_le(out, result); /* Amount in in queue */
856     if (result)
857 astrand 801 DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_COMMSTATUS in queue %d\n",
858     result));
859 stargo 795
860     result = 0;
861     ioctl(handle, TIOCOUTQ, &result);
862     out_uint32_le(out, result); /* Amount in out queue */
863     if (result)
864     DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_COMMSTATUS out queue %d\n", result));
865    
866 n-ki 622 out_uint8(out, 0); /* EofReceived */
867     out_uint8(out, 0); /* WaitForImmediate */
868     break;
869 matthewc 432 case SERIAL_PURGE:
870 astrand 580 in_uint32(in, purge_mask);
871 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_PURGE purge_mask %X\n", purge_mask));
872     flush_mask = 0;
873     if (purge_mask & SERIAL_PURGE_TXCLEAR)
874 astrand 580 flush_mask |= TCOFLUSH;
875 stargo 795 if (purge_mask & SERIAL_PURGE_RXCLEAR)
876 astrand 580 flush_mask |= TCIFLUSH;
877     if (flush_mask != 0)
878     tcflush(handle, flush_mask);
879 stargo 795 if (purge_mask & SERIAL_PURGE_TXABORT)
880 astrand 580 rdpdr_abort_io(handle, 4, STATUS_CANCELLED);
881 stargo 795 if (purge_mask & SERIAL_PURGE_RXABORT)
882 astrand 580 rdpdr_abort_io(handle, 3, STATUS_CANCELLED);
883     break;
884 n-ki 622 case SERIAL_WAIT_ON_MASK:
885 astrand 801 DEBUG_SERIAL(("serial_ioctl -> SERIAL_WAIT_ON_MASK %X\n",
886     pser_inf->wait_mask));
887 stargo 795 pser_inf->event_pending = 1;
888     if (serial_get_event(handle, &result))
889     {
890     DEBUG_SERIAL(("WAIT end event = %x\n", result));
891     out_uint32_le(out, result);
892     break;
893     }
894     return STATUS_PENDING;
895 n-ki 622 break;
896     case SERIAL_SET_BREAK_ON:
897 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BREAK_ON\n"));
898     tcsendbreak(handle, 0);
899 n-ki 622 break;
900 matthewc 432 case SERIAL_RESET_DEVICE:
901 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_RESET_DEVICE\n"));
902     break;
903 matthewc 432 case SERIAL_SET_BREAK_OFF:
904 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BREAK_OFF\n"));
905     break;
906 matthewc 432 case SERIAL_SET_XOFF:
907 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_XOFF\n"));
908     break;
909 matthewc 432 case SERIAL_SET_XON:
910 stargo 795 DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_XON\n"));
911     tcflow(handle, TCION);
912 matthewc 432 break;
913     default:
914     unimpl("SERIAL IOCTL %d\n", request);
915     return STATUS_INVALID_PARAMETER;
916     }
917    
918     return STATUS_SUCCESS;
919     }
920    
921 stargo 795 BOOL
922     serial_get_event(NTHANDLE handle, uint32 * result)
923     {
924     int index;
925     SERIAL_DEVICE *pser_inf;
926     int bytes;
927     BOOL ret = False;
928    
929     *result = 0;
930     index = get_device_index(handle);
931     if (index < 0)
932     return False;
933    
934     pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
935    
936    
937     ioctl(handle, TIOCINQ, &bytes);
938    
939     if (bytes > 0)
940     {
941     DEBUG_SERIAL(("serial_get_event Bytes %d\n", bytes));
942     if (bytes > pser_inf->event_rlsd)
943     {
944     pser_inf->event_rlsd = bytes;
945     if (pser_inf->wait_mask & SERIAL_EV_RLSD)
946     {
947     DEBUG_SERIAL(("Event -> SERIAL_EV_RLSD \n"));
948     *result |= SERIAL_EV_RLSD;
949     ret = True;
950     }
951    
952     }
953    
954     if ((bytes > 1) && (pser_inf->wait_mask & SERIAL_EV_RXFLAG))
955     {
956     DEBUG_SERIAL(("Event -> SERIAL_EV_RXFLAG Bytes %d\n", bytes));
957     *result |= SERIAL_EV_RXFLAG;
958     ret = True;
959     }
960     if ((pser_inf->wait_mask & SERIAL_EV_RXCHAR))
961     {
962     DEBUG_SERIAL(("Event -> SERIAL_EV_RXCHAR Bytes %d\n", bytes));
963     *result |= SERIAL_EV_RXCHAR;
964     ret = True;
965     }
966    
967     }
968     else
969     {
970     pser_inf->event_rlsd = 0;
971     }
972    
973    
974     ioctl(handle, TIOCOUTQ, &bytes);
975     if ((bytes == 0)
976     && (pser_inf->event_txempty > 0) && (pser_inf->wait_mask & SERIAL_EV_TXEMPTY))
977     {
978    
979     DEBUG_SERIAL(("Event -> SERIAL_EV_TXEMPTY\n"));
980     *result |= SERIAL_EV_TXEMPTY;
981     ret = True;
982     }
983     pser_inf->event_txempty = bytes;
984    
985    
986     ioctl(handle, TIOCMGET, &bytes);
987     if ((bytes & TIOCM_DSR) != pser_inf->event_dsr)
988     {
989     pser_inf->event_dsr = bytes & TIOCM_DSR;
990     if (pser_inf->wait_mask & SERIAL_EV_DSR)
991     {
992 astrand 801 DEBUG_SERIAL(("event -> SERIAL_EV_DSR %s\n",
993     (bytes & TIOCM_DSR) ? "ON" : "OFF"));
994 stargo 795 *result |= SERIAL_EV_DSR;
995     ret = True;
996     }
997     }
998    
999     if ((bytes & TIOCM_CTS) != pser_inf->event_cts)
1000     {
1001     pser_inf->event_cts = bytes & TIOCM_CTS;
1002     if (pser_inf->wait_mask & SERIAL_EV_CTS)
1003     {
1004 astrand 801 DEBUG_SERIAL((" EVENT-> SERIAL_EV_CTS %s\n",
1005     (bytes & TIOCM_CTS) ? "ON" : "OFF"));
1006 stargo 795 *result |= SERIAL_EV_CTS;
1007     ret = True;
1008     }
1009     }
1010    
1011     if (ret)
1012     pser_inf->event_pending = 0;
1013    
1014     return ret;
1015     }
1016    
1017 n-ki 569 /* Read timeout for a given file descripter (device) when adding fd's to select() */
1018     BOOL
1019 jsorg71 776 serial_get_timeout(NTHANDLE handle, uint32 length, uint32 * timeout, uint32 * itv_timeout)
1020 n-ki 569 {
1021 astrand 580 int index;
1022     SERIAL_DEVICE *pser_inf;
1023 n-ki 569
1024 astrand 580 index = get_device_index(handle);
1025 n-ki 622 if (index < 0)
1026     return True;
1027 n-ki 569
1028 astrand 580 if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)
1029     {
1030     return False;
1031     }
1032 n-ki 569
1033 astrand 580 pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
1034 n-ki 569
1035 astrand 580 *timeout =
1036     pser_inf->read_total_timeout_multiplier * length +
1037     pser_inf->read_total_timeout_constant;
1038     *itv_timeout = pser_inf->read_interval_timeout;
1039     return True;
1040 n-ki 569 }
1041    
1042 astrand 435 DEVICE_FNS serial_fns = {
1043 matthewc 432 serial_create,
1044     serial_close,
1045     serial_read,
1046     serial_write,
1047     serial_device_control
1048     };

  ViewVC Help
Powered by ViewVC 1.1.26