1 |
|
/* -*- c-basic-offset: 8 -*- |
2 |
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
4 |
|
Copyright (C) Matthew Chapman 1999-2005 |
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 |
#include <unistd.h> |
#include <unistd.h> |
21 |
#include <fcntl.h> |
#include <fcntl.h> |
22 |
#include <termios.h> |
#include <termios.h> |
96 |
#define SERIAL_PURGE_RXCLEAR 0x00000008 |
#define SERIAL_PURGE_RXCLEAR 0x00000008 |
97 |
|
|
98 |
/* SERIAL_WAIT_ON_MASK */ |
/* SERIAL_WAIT_ON_MASK */ |
99 |
#define SERIAL_EV_RXCHAR 0x0001 // Any Character received |
#define SERIAL_EV_RXCHAR 0x0001 /* Any Character received */ |
100 |
#define SERIAL_EV_RXFLAG 0x0002 // Received certain character |
#define SERIAL_EV_RXFLAG 0x0002 /* Received certain character */ |
101 |
#define SERIAL_EV_TXEMPTY 0x0004 // Transmitt Queue Empty |
#define SERIAL_EV_TXEMPTY 0x0004 /* Transmitt Queue Empty */ |
102 |
#define SERIAL_EV_CTS 0x0008 // CTS changed state |
#define SERIAL_EV_CTS 0x0008 /* CTS changed state */ |
103 |
#define SERIAL_EV_DSR 0x0010 // DSR changed state |
#define SERIAL_EV_DSR 0x0010 /* DSR changed state */ |
104 |
#define SERIAL_EV_RLSD 0x0020 // RLSD changed state |
#define SERIAL_EV_RLSD 0x0020 /* RLSD changed state */ |
105 |
#define SERIAL_EV_BREAK 0x0040 // BREAK received |
#define SERIAL_EV_BREAK 0x0040 /* BREAK received */ |
106 |
#define SERIAL_EV_ERR 0x0080 // Line status error occurred |
#define SERIAL_EV_ERR 0x0080 /* Line status error occurred */ |
107 |
#define SERIAL_EV_RING 0x0100 // Ring signal detected |
#define SERIAL_EV_RING 0x0100 /* Ring signal detected */ |
108 |
#define SERIAL_EV_PERR 0x0200 // Printer error occured |
#define SERIAL_EV_PERR 0x0200 /* Printer error occured */ |
109 |
#define SERIAL_EV_RX80FULL 0x0400 // Receive buffer is 80 percent full |
#define SERIAL_EV_RX80FULL 0x0400 /* Receive buffer is 80 percent full */ |
110 |
#define SERIAL_EV_EVENT1 0x0800 // Provider specific event 1 |
#define SERIAL_EV_EVENT1 0x0800 /* Provider specific event 1 */ |
111 |
#define SERIAL_EV_EVENT2 0x1000 // Provider specific event 2 |
#define SERIAL_EV_EVENT2 0x1000 /* Provider specific event 2 */ |
112 |
|
|
113 |
/* Modem Status */ |
/* Modem Status */ |
114 |
#define SERIAL_MS_DTR 0x01 |
#define SERIAL_MS_DTR 0x01 |
140 |
|
|
141 |
/* FIONREAD should really do the same thing as TIOCINQ, where it is |
/* FIONREAD should really do the same thing as TIOCINQ, where it is |
142 |
* not available */ |
* not available */ |
143 |
#ifndef TIOCINQ |
#if !defined(TIOCINQ) && defined(FIONREAD) |
144 |
#define TIOCINQ FIONREAD |
#define TIOCINQ FIONREAD |
145 |
#endif |
#endif |
146 |
|
#if !defined(TIOCOUTQ) && defined(FIONWRITE) |
147 |
|
#define TIOCOUTQ FIONWRITE |
148 |
|
#endif |
149 |
|
|
150 |
extern RDPDR_DEVICE g_rdpdr_device[]; |
extern RDPDR_DEVICE g_rdpdr_device[]; |
151 |
|
|
522 |
char *pos2; |
char *pos2; |
523 |
int count = 0; |
int count = 0; |
524 |
|
|
525 |
// skip the first colon |
/* skip the first colon */ |
526 |
optarg++; |
optarg++; |
527 |
while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) |
while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) |
528 |
{ |
{ |
529 |
// Init data structures for device |
/* Init data structures for device */ |
530 |
pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE)); |
pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE)); |
531 |
pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios)); |
pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios)); |
532 |
memset(pser_inf->ptermios, 0, sizeof(struct termios)); |
memset(pser_inf->ptermios, 0, sizeof(struct termios)); |
542 |
strcpy(g_rdpdr_device[*id].local_path, pos2); |
strcpy(g_rdpdr_device[*id].local_path, pos2); |
543 |
printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name, |
printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name, |
544 |
g_rdpdr_device[*id].local_path); |
g_rdpdr_device[*id].local_path); |
545 |
// set device type |
/* set device type */ |
546 |
g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL; |
g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL; |
547 |
g_rdpdr_device[*id].pdevice_data = (void *) pser_inf; |
g_rdpdr_device[*id].pdevice_data = (void *) pser_inf; |
548 |
count++; |
count++; |
578 |
return STATUS_ACCESS_DENIED; |
return STATUS_ACCESS_DENIED; |
579 |
} |
} |
580 |
|
|
581 |
// Store handle for later use |
/* Store handle for later use */ |
582 |
g_rdpdr_device[device_id].handle = serial_fd; |
g_rdpdr_device[device_id].handle = serial_fd; |
583 |
|
|
584 |
/* some sane information */ |
/* some sane information */ |
587 |
pser_inf->ptermios->c_iflag &= |
pser_inf->ptermios->c_iflag &= |
588 |
~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); |
~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); |
589 |
pser_inf->ptermios->c_oflag &= ~OPOST; |
pser_inf->ptermios->c_oflag &= ~OPOST; |
590 |
pser_inf->ptermios->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN | XCASE); |
pser_inf->ptermios->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); |
591 |
pser_inf->ptermios->c_cflag &= ~(CSIZE | PARENB); |
pser_inf->ptermios->c_cflag &= ~(CSIZE | PARENB); |
592 |
pser_inf->ptermios->c_cflag |= CS8; |
pser_inf->ptermios->c_cflag |= CS8; |
593 |
|
|
637 |
pser_inf = get_serial_info(handle); |
pser_inf = get_serial_info(handle); |
638 |
ptermios = pser_inf->ptermios; |
ptermios = pser_inf->ptermios; |
639 |
|
|
640 |
// Set timeouts kind of like the windows serial timeout parameters. Multiply timeout |
/* Set timeouts kind of like the windows serial timeout parameters. Multiply timeout |
641 |
// with requested read size |
with requested read size */ |
642 |
if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant) |
if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant) |
643 |
{ |
{ |
644 |
timeout = |
timeout = |
650 |
timeout = (pser_inf->read_interval_timeout * length + 99) / 100; |
timeout = (pser_inf->read_interval_timeout * length + 99) / 100; |
651 |
} |
} |
652 |
|
|
653 |
// If a timeout is set, do a blocking read, which times out after some time. |
/* If a timeout is set, do a blocking read, which times out after some time. |
654 |
// It will make rdesktop less responsive, but it will improve serial performance, by not |
It will make rdesktop less responsive, but it will improve serial performance, by not |
655 |
// reading one character at a time. |
reading one character at a time. */ |
656 |
if (timeout == 0) |
if (timeout == 0) |
657 |
{ |
{ |
658 |
ptermios->c_cc[VTIME] = 0; |
ptermios->c_cc[VTIME] = 0; |
665 |
} |
} |
666 |
tcsetattr(handle, TCSANOW, ptermios); |
tcsetattr(handle, TCSANOW, ptermios); |
667 |
|
|
668 |
#ifdef WITH_DEBUG_SERIAL |
#if defined(WITH_DEBUG_SERIAL) && defined(TIOCINQ) |
669 |
ioctl(handle, TIOCINQ, &bytes_inqueue); |
ioctl(handle, TIOCINQ, &bytes_inqueue); |
670 |
DEBUG_SERIAL(("serial_read inqueue: %d expected %d\n", bytes_inqueue, length)); |
DEBUG_SERIAL(("serial_read inqueue: %d expected %d\n", bytes_inqueue, length)); |
671 |
#endif |
#endif |
722 |
case SERIAL_SET_BAUD_RATE: |
case SERIAL_SET_BAUD_RATE: |
723 |
in_uint32_le(in, pser_inf->baud_rate); |
in_uint32_le(in, pser_inf->baud_rate); |
724 |
set_termios(pser_inf, handle); |
set_termios(pser_inf, handle); |
725 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BAUD_RATE %d\n", pser_inf->baud_rate)); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_BAUD_RATE %d\n", |
726 |
|
pser_inf->baud_rate)); |
727 |
break; |
break; |
728 |
case SERIAL_GET_BAUD_RATE: |
case SERIAL_GET_BAUD_RATE: |
729 |
out_uint32_le(out, pser_inf->baud_rate); |
out_uint32_le(out, pser_inf->baud_rate); |
730 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_BAUD_RATE %d\n", pser_inf->baud_rate)); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_BAUD_RATE %d\n", |
731 |
|
pser_inf->baud_rate)); |
732 |
break; |
break; |
733 |
case SERIAL_SET_QUEUE_SIZE: |
case SERIAL_SET_QUEUE_SIZE: |
734 |
in_uint32_le(in, pser_inf->queue_in_size); |
in_uint32_le(in, pser_inf->queue_in_size); |
735 |
in_uint32_le(in, pser_inf->queue_out_size); |
in_uint32_le(in, pser_inf->queue_out_size); |
736 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_QUEUE_SIZE in %d out %d\n", |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_QUEUE_SIZE in %d out %d\n", |
737 |
pser_inf->queue_in_size, pser_inf->queue_out_size)); |
pser_inf->queue_in_size, pser_inf->queue_out_size)); |
738 |
break; |
break; |
739 |
case SERIAL_SET_LINE_CONTROL: |
case SERIAL_SET_LINE_CONTROL: |
740 |
in_uint8(in, pser_inf->stop_bits); |
in_uint8(in, pser_inf->stop_bits); |
741 |
in_uint8(in, pser_inf->parity); |
in_uint8(in, pser_inf->parity); |
742 |
in_uint8(in, pser_inf->word_length); |
in_uint8(in, pser_inf->word_length); |
743 |
set_termios(pser_inf, handle); |
set_termios(pser_inf, handle); |
744 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_LINE_CONTROL stop %d parity %d word %d\n", |
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)); |
|
pser_inf->stop_bits, pser_inf->parity, pser_inf->word_length)); |
|
745 |
break; |
break; |
746 |
case SERIAL_GET_LINE_CONTROL: |
case SERIAL_GET_LINE_CONTROL: |
747 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_LINE_CONTROL\n")); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_LINE_CONTROL\n")); |
784 |
in_uint32_le(in, pser_inf->onlimit); |
in_uint32_le(in, pser_inf->onlimit); |
785 |
in_uint32_le(in, pser_inf->offlimit); |
in_uint32_le(in, pser_inf->offlimit); |
786 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_HANDFLOW %x %x %x %x\n", |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_HANDFLOW %x %x %x %x\n", |
787 |
pser_inf->control, pser_inf->xonoff, pser_inf->onlimit, |
pser_inf->control, pser_inf->xonoff, pser_inf->onlimit, |
788 |
pser_inf->onlimit)); |
pser_inf->onlimit)); |
789 |
set_termios(pser_inf, handle); |
set_termios(pser_inf, handle); |
790 |
break; |
break; |
791 |
case SERIAL_SET_TIMEOUTS: |
case SERIAL_SET_TIMEOUTS: |
795 |
in_uint32(in, pser_inf->write_total_timeout_multiplier); |
in_uint32(in, pser_inf->write_total_timeout_multiplier); |
796 |
in_uint32(in, pser_inf->write_total_timeout_constant); |
in_uint32(in, pser_inf->write_total_timeout_constant); |
797 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_TIMEOUTS read timeout %d %d %d\n", |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_TIMEOUTS read timeout %d %d %d\n", |
798 |
pser_inf->read_interval_timeout, |
pser_inf->read_interval_timeout, |
799 |
pser_inf->read_total_timeout_multiplier, |
pser_inf->read_total_timeout_multiplier, |
800 |
pser_inf->read_total_timeout_constant)); |
pser_inf->read_total_timeout_constant)); |
801 |
break; |
break; |
802 |
case SERIAL_GET_TIMEOUTS: |
case SERIAL_GET_TIMEOUTS: |
803 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_TIMEOUTS read timeout %d %d %d\n", |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_TIMEOUTS read timeout %d %d %d\n", |
804 |
pser_inf->read_interval_timeout, |
pser_inf->read_interval_timeout, |
805 |
pser_inf->read_total_timeout_multiplier, |
pser_inf->read_total_timeout_multiplier, |
806 |
pser_inf->read_total_timeout_constant)); |
pser_inf->read_total_timeout_constant)); |
807 |
|
|
808 |
out_uint32(out, pser_inf->read_interval_timeout); |
out_uint32(out, pser_inf->read_interval_timeout); |
809 |
out_uint32(out, pser_inf->read_total_timeout_multiplier); |
out_uint32(out, pser_inf->read_total_timeout_multiplier); |
812 |
out_uint32(out, pser_inf->write_total_timeout_constant); |
out_uint32(out, pser_inf->write_total_timeout_constant); |
813 |
break; |
break; |
814 |
case SERIAL_GET_WAIT_MASK: |
case SERIAL_GET_WAIT_MASK: |
815 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_WAIT_MASK %X\n", pser_inf->wait_mask); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_WAIT_MASK %X\n", |
816 |
out_uint32(out, pser_inf->wait_mask)); |
pser_inf->wait_mask)); |
817 |
|
out_uint32(out, pser_inf->wait_mask); |
818 |
break; |
break; |
819 |
case SERIAL_SET_WAIT_MASK: |
case SERIAL_SET_WAIT_MASK: |
820 |
in_uint32(in, pser_inf->wait_mask); |
in_uint32(in, pser_inf->wait_mask); |
821 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_WAIT_MASK %X\n", pser_inf->wait_mask)); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_WAIT_MASK %X\n", |
822 |
|
pser_inf->wait_mask)); |
823 |
break; |
break; |
824 |
case SERIAL_SET_DTR: |
case SERIAL_SET_DTR: |
825 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_DTR\n")); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_SET_DTR\n")); |
874 |
out_uint32_le(out, 0); /* Hold reasons */ |
out_uint32_le(out, 0); /* Hold reasons */ |
875 |
|
|
876 |
result = 0; |
result = 0; |
877 |
|
#ifdef TIOCINQ |
878 |
ioctl(handle, TIOCINQ, &result); |
ioctl(handle, TIOCINQ, &result); |
879 |
|
#endif |
880 |
out_uint32_le(out, result); /* Amount in in queue */ |
out_uint32_le(out, result); /* Amount in in queue */ |
881 |
if (result) |
if (result) |
882 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_COMMSTATUS in queue %d\n", result)); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_COMMSTATUS in queue %d\n", |
883 |
|
result)); |
884 |
|
|
885 |
result = 0; |
result = 0; |
886 |
|
#ifdef TIOCOUTQ |
887 |
ioctl(handle, TIOCOUTQ, &result); |
ioctl(handle, TIOCOUTQ, &result); |
888 |
|
#endif |
889 |
out_uint32_le(out, result); /* Amount in out queue */ |
out_uint32_le(out, result); /* Amount in out queue */ |
890 |
if (result) |
if (result) |
891 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_COMMSTATUS out queue %d\n", result)); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_GET_COMMSTATUS out queue %d\n", result)); |
909 |
rdpdr_abort_io(handle, 3, STATUS_CANCELLED); |
rdpdr_abort_io(handle, 3, STATUS_CANCELLED); |
910 |
break; |
break; |
911 |
case SERIAL_WAIT_ON_MASK: |
case SERIAL_WAIT_ON_MASK: |
912 |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_WAIT_ON_MASK %X\n", pser_inf->wait_mask)); |
DEBUG_SERIAL(("serial_ioctl -> SERIAL_WAIT_ON_MASK %X\n", |
913 |
|
pser_inf->wait_mask)); |
914 |
pser_inf->event_pending = 1; |
pser_inf->event_pending = 1; |
915 |
if (serial_get_event(handle, &result)) |
if (serial_get_event(handle, &result)) |
916 |
{ |
{ |
958 |
if (index < 0) |
if (index < 0) |
959 |
return False; |
return False; |
960 |
|
|
961 |
|
#ifdef TIOCINQ |
962 |
pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data; |
pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data; |
963 |
|
|
|
|
|
964 |
ioctl(handle, TIOCINQ, &bytes); |
ioctl(handle, TIOCINQ, &bytes); |
965 |
|
|
966 |
if (bytes > 0) |
if (bytes > 0) |
996 |
{ |
{ |
997 |
pser_inf->event_rlsd = 0; |
pser_inf->event_rlsd = 0; |
998 |
} |
} |
999 |
|
#endif |
1000 |
|
|
1001 |
|
#ifdef TIOCOUTQ |
1002 |
ioctl(handle, TIOCOUTQ, &bytes); |
ioctl(handle, TIOCOUTQ, &bytes); |
1003 |
if ((bytes == 0) |
if ((bytes == 0) |
1004 |
&& (pser_inf->event_txempty > 0) && (pser_inf->wait_mask & SERIAL_EV_TXEMPTY)) |
&& (pser_inf->event_txempty > 0) && (pser_inf->wait_mask & SERIAL_EV_TXEMPTY)) |
1009 |
ret = True; |
ret = True; |
1010 |
} |
} |
1011 |
pser_inf->event_txempty = bytes; |
pser_inf->event_txempty = bytes; |
1012 |
|
#endif |
1013 |
|
|
1014 |
ioctl(handle, TIOCMGET, &bytes); |
ioctl(handle, TIOCMGET, &bytes); |
1015 |
if ((bytes & TIOCM_DSR) != pser_inf->event_dsr) |
if ((bytes & TIOCM_DSR) != pser_inf->event_dsr) |
1017 |
pser_inf->event_dsr = bytes & TIOCM_DSR; |
pser_inf->event_dsr = bytes & TIOCM_DSR; |
1018 |
if (pser_inf->wait_mask & SERIAL_EV_DSR) |
if (pser_inf->wait_mask & SERIAL_EV_DSR) |
1019 |
{ |
{ |
1020 |
DEBUG_SERIAL(("event -> SERIAL_EV_DSR %s\n", (bytes & TIOCM_DSR) ? "ON" : "OFF")); |
DEBUG_SERIAL(("event -> SERIAL_EV_DSR %s\n", |
1021 |
|
(bytes & TIOCM_DSR) ? "ON" : "OFF")); |
1022 |
*result |= SERIAL_EV_DSR; |
*result |= SERIAL_EV_DSR; |
1023 |
ret = True; |
ret = True; |
1024 |
} |
} |
1029 |
pser_inf->event_cts = bytes & TIOCM_CTS; |
pser_inf->event_cts = bytes & TIOCM_CTS; |
1030 |
if (pser_inf->wait_mask & SERIAL_EV_CTS) |
if (pser_inf->wait_mask & SERIAL_EV_CTS) |
1031 |
{ |
{ |
1032 |
DEBUG_SERIAL((" EVENT-> SERIAL_EV_CTS %s\n", (bytes & TIOCM_CTS) ? "ON" : "OFF")); |
DEBUG_SERIAL((" EVENT-> SERIAL_EV_CTS %s\n", |
1033 |
|
(bytes & TIOCM_CTS) ? "ON" : "OFF")); |
1034 |
*result |= SERIAL_EV_CTS; |
*result |= SERIAL_EV_CTS; |
1035 |
ret = True; |
ret = True; |
1036 |
} |
} |