--- sourceforge.net/trunk/rdesktop/serial.c 2004/01/23 08:35:52 580 +++ sourceforge.net/trunk/rdesktop/serial.c 2004/08/25 00:43:48 757 @@ -2,6 +2,7 @@ #include #include #include +#include #include "rdesktop.h" #define FILE_DEVICE_SERIAL_PORT 0x1b @@ -53,18 +54,41 @@ #define ODD_PARITY 1 #define EVEN_PARITY 2 -extern RDPDR_DEVICE g_rdpdr_device[]; +#define SERIAL_PURGE_TXABORT 0x00000001 +#define SERIAL_PURGE_RXABORT 0x00000002 +#define SERIAL_PURGE_TXCLEAR 0x00000004 +#define SERIAL_PURGE_RXCLEAR 0x00000008 + +/* SERIAL_WAIT_ON_MASK */ +#define SERIAL_EV_RXCHAR 0x0001 // Any Character received +#define SERIAL_EV_RXFLAG 0x0002 // Received certain character +#define SERIAL_EV_TXEMPTY 0x0004 // Transmitt Queue Empty +#define SERIAL_EV_CTS 0x0008 // CTS changed state +#define SERIAL_EV_DSR 0x0010 // DSR changed state +#define SERIAL_EV_RLSD 0x0020 // RLSD changed state +#define SERIAL_EV_BREAK 0x0040 // BREAK received +#define SERIAL_EV_ERR 0x0080 // Line status error occurred +#define SERIAL_EV_RING 0x0100 // Ring signal detected +#define SERIAL_EV_PERR 0x0200 // Printer error occured +#define SERIAL_EV_RX80FULL 0x0400 // Receive buffer is 80 percent full +#define SERIAL_EV_EVENT1 0x0800 // Provider specific event 1 +#define SERIAL_EV_EVENT2 0x1000 // Provider specific event 2 + +/* Modem Status */ +#define SERIAL_MS_CTS 0x10 +#define SERIAL_MS_DSR 0x20 +#define SERIAL_MS_RNG 0x40 +#define SERIAL_MS_CAR 0x80 + +#ifndef CRTSCTS +#define CRTSCTS 0 +#endif -int serial_fd; -struct termios termios; -int dtr; -uint32 baud_rate; -uint32 queue_in_size, queue_out_size; -uint32 wait_mask; -uint8 stop_bits, parity, word_length; +extern RDPDR_DEVICE g_rdpdr_device[]; -SERIAL_DEVICE * get_serial_info(HANDLE handle) +static SERIAL_DEVICE * +get_serial_info(HANDLE handle) { int index; @@ -76,7 +100,7 @@ return NULL; } -BOOL +static BOOL get_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd) { speed_t speed; @@ -194,15 +218,22 @@ break; } + pser_inf->rts = (ptermios->c_cflag & CRTSCTS) ? 1 : 0; + return True; } static void -set_termios(void) +set_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd) { speed_t speed; - switch (baud_rate) + struct termios *ptermios; + + ptermios = pser_inf->ptermios; + + + switch (pser_inf->baud_rate) { #ifdef B75 case 75: @@ -286,42 +317,47 @@ /* on systems with separate ispeed and ospeed, we can remember the speed in ispeed while changing DTR with ospeed */ - cfsetispeed(&termios, speed); - cfsetospeed(&termios, dtr ? speed : 0); + cfsetispeed(pser_inf->ptermios, speed); + cfsetospeed(pser_inf->ptermios, pser_inf->dtr ? speed : 0); - termios.c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE); - switch (stop_bits) + ptermios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE | CRTSCTS); + switch (pser_inf->stop_bits) { case STOP_BITS_2: - termios.c_cflag |= CSTOPB; + ptermios->c_cflag |= CSTOPB; break; } - switch (parity) + + switch (pser_inf->parity) { case EVEN_PARITY: - termios.c_cflag |= PARENB; + ptermios->c_cflag |= PARENB; break; case ODD_PARITY: - termios.c_cflag |= PARENB | PARODD; + ptermios->c_cflag |= PARENB | PARODD; break; } - switch (word_length) + + switch (pser_inf->word_length) { case 5: - termios.c_cflag |= CS5; + ptermios->c_cflag |= CS5; break; case 6: - termios.c_cflag |= CS6; + ptermios->c_cflag |= CS6; break; case 7: - termios.c_cflag |= CS7; + ptermios->c_cflag |= CS7; break; default: - termios.c_cflag |= CS8; + ptermios->c_cflag |= CS8; break; } - tcsetattr(serial_fd, TCSANOW, &termios); + if (pser_inf->rts) + ptermios->c_cflag |= CRTSCTS; + + tcsetattr(serial_fd, TCSANOW, ptermios); } /* Enumeration of devices from rdesktop.c */ @@ -330,7 +366,7 @@ /* when it arrives to this function. */ /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */ int -serial_enum_devices(int *id, char *optarg) +serial_enum_devices(uint32 * id, char *optarg) { SERIAL_DEVICE *pser_inf; @@ -345,7 +381,9 @@ // Init data structures for device pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE)); pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios)); + memset(pser_inf->ptermios, 0, sizeof(struct termios)); pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios)); + memset(pser_inf->pold_termios, 0, sizeof(struct termios)); pos2 = next_arg(optarg, '='); strcpy(g_rdpdr_device[*id].name, optarg); @@ -367,7 +405,7 @@ return count; } -NTSTATUS +static NTSTATUS serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition, uint32 flags_and_attributes, char *filename, HANDLE * handle) { @@ -377,42 +415,65 @@ pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data; ptermios = pser_inf->ptermios; - serial_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR | O_NOCTTY); + serial_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR | O_NOCTTY | O_NONBLOCK); if (serial_fd == -1) + { + perror("open"); return STATUS_ACCESS_DENIED; + } if (!get_termios(pser_inf, serial_fd)) + { + printf("INFO: SERIAL %s access denied\n", g_rdpdr_device[device_id].name); + fflush(stdout); return STATUS_ACCESS_DENIED; + } // Store handle for later use g_rdpdr_device[device_id].handle = serial_fd; /* some sane information */ - printf("INFO: SERIAL %s to %s\nINFO: speed %u baud, stop bits %u, parity %u, word length %u bits, dtr %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); + printf("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); + printf("INFO: use stty to change settings\n"); - //tcgetattr(serial_fd, pser_inf->ptermios); +/* ptermios->c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD; + ptermios->c_cflag |= CREAD; + ptermios->c_lflag |= ICANON; + ptermios->c_iflag = IGNPAR | ICRNL; + + tcsetattr(serial_fd, TCSANOW, ptermios); +*/ + cfmakeraw(pser_inf->ptermios); *handle = serial_fd; + + /* all read and writes should be non blocking */ + if (fcntl(*handle, F_SETFL, O_NONBLOCK) == -1) + perror("fcntl"); + return STATUS_SUCCESS; } static NTSTATUS serial_close(HANDLE handle) { - close(serial_fd); + int i = get_device_index(handle); + if (i >= 0) + g_rdpdr_device[i].handle = 0; + close(handle); return STATUS_SUCCESS; } -NTSTATUS +static NTSTATUS serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { long timeout; SERIAL_DEVICE *pser_inf; struct termios *ptermios; - timeout = 0; + timeout = 90; pser_inf = get_serial_info(handle); ptermios = pser_inf->ptermios; @@ -444,11 +505,15 @@ } tcsetattr(handle, TCSANOW, ptermios); + *result = read(handle, data, length); + + //hexdump(data, *read); + return STATUS_SUCCESS; } -NTSTATUS +static NTSTATUS serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { *result = write(handle, data, length); @@ -458,12 +523,20 @@ static NTSTATUS serial_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out) { - uint32 result; +#if 0 + int flush_mask, purge_mask; +#endif + uint32 result, modemstate; uint8 immediate; + SERIAL_DEVICE *pser_inf; + struct termios *ptermios; if ((request >> 16) != FILE_DEVICE_SERIAL_PORT) return STATUS_INVALID_PARAMETER; + pser_inf = get_serial_info(handle); + ptermios = pser_inf->ptermios; + /* extract operation */ request >>= 2; request &= 0xfff; @@ -473,26 +546,26 @@ switch (request) { case SERIAL_SET_BAUD_RATE: - in_uint32_le(in, baud_rate); - set_termios(); + in_uint32_le(in, pser_inf->baud_rate); + set_termios(pser_inf, handle); break; case SERIAL_GET_BAUD_RATE: - out_uint32_le(out, baud_rate); + out_uint32_le(out, pser_inf->baud_rate); break; case SERIAL_SET_QUEUE_SIZE: - in_uint32_le(in, queue_in_size); - in_uint32_le(in, queue_out_size); + in_uint32_le(in, pser_inf->queue_in_size); + in_uint32_le(in, pser_inf->queue_out_size); break; case SERIAL_SET_LINE_CONTROL: - in_uint8(in, stop_bits); - in_uint8(in, parity); - in_uint8(in, word_length); - set_termios(); + in_uint8(in, pser_inf->stop_bits); + in_uint8(in, pser_inf->parity); + in_uint8(in, pser_inf->word_length); + set_termios(pser_inf, handle); break; case SERIAL_GET_LINE_CONTROL: - out_uint8(out, stop_bits); - out_uint8(out, parity); - out_uint8(out, word_length); + out_uint8(out, pser_inf->stop_bits); + out_uint8(out, pser_inf->parity); + out_uint8(out, pser_inf->word_length); break; case SERIAL_IMMEDIATE_CHAR: in_uint8(in, immediate); @@ -523,28 +596,52 @@ out_uint8s(out, 20); break; case SERIAL_GET_WAIT_MASK: - out_uint32(out, wait_mask); + out_uint32(out, pser_inf->wait_mask); break; case SERIAL_SET_WAIT_MASK: - in_uint32(in, wait_mask); + in_uint32(in, pser_inf->wait_mask); break; case SERIAL_SET_DTR: - dtr = 1; - set_termios(); + pser_inf->dtr = 1; + set_termios(pser_inf, handle); break; case SERIAL_CLR_DTR: - dtr = 0; - set_termios(); + pser_inf->dtr = 0; + set_termios(pser_inf, handle); break; -#if 0 - case SERIAL_WAIT_ON_MASK: - /* XXX implement me */ + case SERIAL_SET_RTS: + pser_inf->rts = 1; + set_termios(pser_inf, handle); break; - case SERIAL_SET_BREAK_ON: - tcsendbreak(serial_fd, 0); + case SERIAL_CLR_RTS: + pser_inf->rts = 0; + set_termios(pser_inf, handle); break; + case SERIAL_GET_MODEMSTATUS: + modemstate = 0; +#ifdef TIOCMGET + ioctl(handle, TIOCMGET, &result); + if (result & TIOCM_CTS) + modemstate |= SERIAL_MS_CTS; + if (result & TIOCM_DSR) + modemstate |= SERIAL_MS_DSR; + if (result & TIOCM_RNG) + modemstate |= SERIAL_MS_RNG; + if (result & TIOCM_CAR) + modemstate |= SERIAL_MS_CAR; +#endif + out_uint32_le(out, modemstate); + break; + case SERIAL_GET_COMMSTATUS: + out_uint32_le(out, 0); /* Errors */ + out_uint32_le(out, 0); /* Hold reasons */ + out_uint32_le(out, 0); /* Amount in in queue */ + out_uint32_le(out, 0); /* Amount in out queue */ + out_uint8(out, 0); /* EofReceived */ + out_uint8(out, 0); /* WaitForImmediate */ + break; +#if 0 case SERIAL_PURGE: - printf("SERIAL_PURGE\n"); in_uint32(in, purge_mask); if (purge_mask & 0x04) @@ -558,17 +655,20 @@ if (purge_mask & 0x02) rdpdr_abort_io(handle, 3, STATUS_CANCELLED); break; - + case SERIAL_WAIT_ON_MASK: + /* XXX implement me */ + out_uint32_le(out, pser_inf->wait_mask); + break; + case SERIAL_SET_BREAK_ON: + tcsendbreak(serial_fd, 0); + break; case SERIAL_RESET_DEVICE: case SERIAL_SET_BREAK_OFF: - case SERIAL_SET_RTS: - case SERIAL_CLR_RTS: case SERIAL_SET_XOFF: case SERIAL_SET_XON: /* ignore */ break; #endif - default: unimpl("SERIAL IOCTL %d\n", request); return STATUS_INVALID_PARAMETER; @@ -579,12 +679,14 @@ /* Read timeout for a given file descripter (device) when adding fd's to select() */ BOOL -serial_get_timeout(uint32 handle, uint32 length, uint32 * timeout, uint32 * itv_timeout) +serial_get_timeout(HANDLE handle, uint32 length, uint32 * timeout, uint32 * itv_timeout) { int index; SERIAL_DEVICE *pser_inf; index = get_device_index(handle); + if (index < 0) + return True; if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL) {