VirtualBox

source: vbox/trunk/src/VBox/RDP/client/serial.c@ 37202

最後變更 在這個檔案從37202是 33656,由 vboxsync 提交於 14 年 前

*: rebrand Sun (L)GPL disclaimers

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette