2 |
#include <fcntl.h> |
#include <fcntl.h> |
3 |
#include <termios.h> |
#include <termios.h> |
4 |
#include <strings.h> |
#include <strings.h> |
5 |
|
#include <sys/ioctl.h> |
6 |
#include "rdesktop.h" |
#include "rdesktop.h" |
7 |
|
|
8 |
#define FILE_DEVICE_SERIAL_PORT 0x1b |
#define FILE_DEVICE_SERIAL_PORT 0x1b |
54 |
#define ODD_PARITY 1 |
#define ODD_PARITY 1 |
55 |
#define EVEN_PARITY 2 |
#define EVEN_PARITY 2 |
56 |
|
|
57 |
extern RDPDR_DEVICE g_rdpdr_device[]; |
#define SERIAL_PURGE_TXABORT 0x00000001 |
58 |
|
#define SERIAL_PURGE_RXABORT 0x00000002 |
59 |
|
#define SERIAL_PURGE_TXCLEAR 0x00000004 |
60 |
|
#define SERIAL_PURGE_RXCLEAR 0x00000008 |
61 |
|
|
62 |
|
/* SERIAL_WAIT_ON_MASK */ |
63 |
|
#define SERIAL_EV_RXCHAR 0x0001 // Any Character received |
64 |
|
#define SERIAL_EV_RXFLAG 0x0002 // Received certain character |
65 |
|
#define SERIAL_EV_TXEMPTY 0x0004 // Transmitt Queue Empty |
66 |
|
#define SERIAL_EV_CTS 0x0008 // CTS changed state |
67 |
|
#define SERIAL_EV_DSR 0x0010 // DSR changed state |
68 |
|
#define SERIAL_EV_RLSD 0x0020 // RLSD changed state |
69 |
|
#define SERIAL_EV_BREAK 0x0040 // BREAK received |
70 |
|
#define SERIAL_EV_ERR 0x0080 // Line status error occurred |
71 |
|
#define SERIAL_EV_RING 0x0100 // Ring signal detected |
72 |
|
#define SERIAL_EV_PERR 0x0200 // Printer error occured |
73 |
|
#define SERIAL_EV_RX80FULL 0x0400 // Receive buffer is 80 percent full |
74 |
|
#define SERIAL_EV_EVENT1 0x0800 // Provider specific event 1 |
75 |
|
#define SERIAL_EV_EVENT2 0x1000 // Provider specific event 2 |
76 |
|
|
77 |
|
/* Modem Status */ |
78 |
|
#define SERIAL_MS_CTS 0x10 |
79 |
|
#define SERIAL_MS_DSR 0x20 |
80 |
|
#define SERIAL_MS_RNG 0x40 |
81 |
|
#define SERIAL_MS_CAR 0x80 |
82 |
|
|
83 |
|
#ifndef CRTSCTS |
84 |
|
#define CRTSCTS 0 |
85 |
|
#endif |
86 |
|
|
|
int serial_fd; |
|
|
struct termios termios; |
|
87 |
|
|
88 |
int dtr; |
extern RDPDR_DEVICE g_rdpdr_device[]; |
|
uint32 baud_rate; |
|
|
uint32 queue_in_size, queue_out_size; |
|
|
uint32 wait_mask; |
|
|
uint8 stop_bits, parity, word_length; |
|
89 |
|
|
90 |
SERIAL_DEVICE * get_serial_info(HANDLE handle) |
static SERIAL_DEVICE * |
91 |
|
get_serial_info(HANDLE handle) |
92 |
{ |
{ |
93 |
int index; |
int index; |
94 |
|
|
100 |
return NULL; |
return NULL; |
101 |
} |
} |
102 |
|
|
103 |
BOOL |
static BOOL |
104 |
get_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd) |
get_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd) |
105 |
{ |
{ |
106 |
speed_t speed; |
speed_t speed; |
218 |
break; |
break; |
219 |
} |
} |
220 |
|
|
221 |
|
pser_inf->rts = (ptermios->c_cflag & CRTSCTS) ? 1 : 0; |
222 |
|
|
223 |
return True; |
return True; |
224 |
} |
} |
225 |
|
|
226 |
static void |
static void |
227 |
set_termios(void) |
set_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd) |
228 |
{ |
{ |
229 |
speed_t speed; |
speed_t speed; |
230 |
|
|
231 |
switch (baud_rate) |
struct termios *ptermios; |
232 |
|
|
233 |
|
ptermios = pser_inf->ptermios; |
234 |
|
|
235 |
|
|
236 |
|
switch (pser_inf->baud_rate) |
237 |
{ |
{ |
238 |
#ifdef B75 |
#ifdef B75 |
239 |
case 75: |
case 75: |
317 |
|
|
318 |
/* on systems with separate ispeed and ospeed, we can remember the speed |
/* on systems with separate ispeed and ospeed, we can remember the speed |
319 |
in ispeed while changing DTR with ospeed */ |
in ispeed while changing DTR with ospeed */ |
320 |
cfsetispeed(&termios, speed); |
cfsetispeed(pser_inf->ptermios, speed); |
321 |
cfsetospeed(&termios, dtr ? speed : 0); |
cfsetospeed(pser_inf->ptermios, pser_inf->dtr ? speed : 0); |
322 |
|
|
323 |
termios.c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE); |
ptermios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE | CRTSCTS); |
324 |
switch (stop_bits) |
switch (pser_inf->stop_bits) |
325 |
{ |
{ |
326 |
case STOP_BITS_2: |
case STOP_BITS_2: |
327 |
termios.c_cflag |= CSTOPB; |
ptermios->c_cflag |= CSTOPB; |
328 |
break; |
break; |
329 |
} |
} |
330 |
switch (parity) |
|
331 |
|
switch (pser_inf->parity) |
332 |
{ |
{ |
333 |
case EVEN_PARITY: |
case EVEN_PARITY: |
334 |
termios.c_cflag |= PARENB; |
ptermios->c_cflag |= PARENB; |
335 |
break; |
break; |
336 |
case ODD_PARITY: |
case ODD_PARITY: |
337 |
termios.c_cflag |= PARENB | PARODD; |
ptermios->c_cflag |= PARENB | PARODD; |
338 |
break; |
break; |
339 |
} |
} |
340 |
switch (word_length) |
|
341 |
|
switch (pser_inf->word_length) |
342 |
{ |
{ |
343 |
case 5: |
case 5: |
344 |
termios.c_cflag |= CS5; |
ptermios->c_cflag |= CS5; |
345 |
break; |
break; |
346 |
case 6: |
case 6: |
347 |
termios.c_cflag |= CS6; |
ptermios->c_cflag |= CS6; |
348 |
break; |
break; |
349 |
case 7: |
case 7: |
350 |
termios.c_cflag |= CS7; |
ptermios->c_cflag |= CS7; |
351 |
break; |
break; |
352 |
default: |
default: |
353 |
termios.c_cflag |= CS8; |
ptermios->c_cflag |= CS8; |
354 |
break; |
break; |
355 |
} |
} |
356 |
|
|
357 |
tcsetattr(serial_fd, TCSANOW, &termios); |
if (pser_inf->rts) |
358 |
|
ptermios->c_cflag |= CRTSCTS; |
359 |
|
|
360 |
|
tcsetattr(serial_fd, TCSANOW, ptermios); |
361 |
} |
} |
362 |
|
|
363 |
/* Enumeration of devices from rdesktop.c */ |
/* Enumeration of devices from rdesktop.c */ |
366 |
/* when it arrives to this function. */ |
/* when it arrives to this function. */ |
367 |
/* :com1=/dev/ttyS0,com2=/dev/ttyS1 */ |
/* :com1=/dev/ttyS0,com2=/dev/ttyS1 */ |
368 |
int |
int |
369 |
serial_enum_devices(int *id, char *optarg) |
serial_enum_devices(uint32 * id, char *optarg) |
370 |
{ |
{ |
371 |
SERIAL_DEVICE *pser_inf; |
SERIAL_DEVICE *pser_inf; |
372 |
|
|
381 |
// Init data structures for device |
// Init data structures for device |
382 |
pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE)); |
pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE)); |
383 |
pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios)); |
pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios)); |
384 |
|
memset(pser_inf->ptermios, 0, sizeof(struct termios)); |
385 |
pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios)); |
pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios)); |
386 |
|
memset(pser_inf->pold_termios, 0, sizeof(struct termios)); |
387 |
|
|
388 |
pos2 = next_arg(optarg, '='); |
pos2 = next_arg(optarg, '='); |
389 |
strcpy(g_rdpdr_device[*id].name, optarg); |
strcpy(g_rdpdr_device[*id].name, optarg); |
405 |
return count; |
return count; |
406 |
} |
} |
407 |
|
|
408 |
NTSTATUS |
static NTSTATUS |
409 |
serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition, |
serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition, |
410 |
uint32 flags_and_attributes, char *filename, HANDLE * handle) |
uint32 flags_and_attributes, char *filename, HANDLE * handle) |
411 |
{ |
{ |
415 |
|
|
416 |
pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data; |
pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data; |
417 |
ptermios = pser_inf->ptermios; |
ptermios = pser_inf->ptermios; |
418 |
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); |
419 |
|
|
420 |
if (serial_fd == -1) |
if (serial_fd == -1) |
421 |
|
{ |
422 |
|
perror("open"); |
423 |
return STATUS_ACCESS_DENIED; |
return STATUS_ACCESS_DENIED; |
424 |
|
} |
425 |
|
|
426 |
if (!get_termios(pser_inf, serial_fd)) |
if (!get_termios(pser_inf, serial_fd)) |
427 |
|
{ |
428 |
|
printf("INFO: SERIAL %s access denied\n", g_rdpdr_device[device_id].name); |
429 |
|
fflush(stdout); |
430 |
return STATUS_ACCESS_DENIED; |
return STATUS_ACCESS_DENIED; |
431 |
|
} |
432 |
|
|
433 |
// Store handle for later use |
// Store handle for later use |
434 |
g_rdpdr_device[device_id].handle = serial_fd; |
g_rdpdr_device[device_id].handle = serial_fd; |
435 |
|
|
436 |
/* some sane information */ |
/* some sane information */ |
437 |
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); |
438 |
|
|
439 |
printf("INFO: use stty to change settings\n"); |
printf("INFO: use stty to change settings\n"); |
440 |
|
|
441 |
//tcgetattr(serial_fd, pser_inf->ptermios); |
/* ptermios->c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD; |
442 |
|
ptermios->c_cflag |= CREAD; |
443 |
|
ptermios->c_lflag |= ICANON; |
444 |
|
ptermios->c_iflag = IGNPAR | ICRNL; |
445 |
|
|
446 |
|
tcsetattr(serial_fd, TCSANOW, ptermios); |
447 |
|
*/ |
448 |
|
|
449 |
|
cfmakeraw(pser_inf->ptermios); |
450 |
*handle = serial_fd; |
*handle = serial_fd; |
451 |
|
|
452 |
|
/* all read and writes should be non blocking */ |
453 |
|
if (fcntl(*handle, F_SETFL, O_NONBLOCK) == -1) |
454 |
|
perror("fcntl"); |
455 |
|
|
456 |
return STATUS_SUCCESS; |
return STATUS_SUCCESS; |
457 |
} |
} |
458 |
|
|
459 |
static NTSTATUS |
static NTSTATUS |
460 |
serial_close(HANDLE handle) |
serial_close(HANDLE handle) |
461 |
{ |
{ |
462 |
close(serial_fd); |
int i = get_device_index(handle); |
463 |
|
if (i >= 0) |
464 |
|
g_rdpdr_device[i].handle = 0; |
465 |
|
close(handle); |
466 |
return STATUS_SUCCESS; |
return STATUS_SUCCESS; |
467 |
} |
} |
468 |
|
|
469 |
NTSTATUS |
static NTSTATUS |
470 |
serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
471 |
{ |
{ |
472 |
long timeout; |
long timeout; |
473 |
SERIAL_DEVICE *pser_inf; |
SERIAL_DEVICE *pser_inf; |
474 |
struct termios *ptermios; |
struct termios *ptermios; |
475 |
|
|
476 |
timeout = 0; |
timeout = 90; |
477 |
pser_inf = get_serial_info(handle); |
pser_inf = get_serial_info(handle); |
478 |
ptermios = pser_inf->ptermios; |
ptermios = pser_inf->ptermios; |
479 |
|
|
505 |
} |
} |
506 |
tcsetattr(handle, TCSANOW, ptermios); |
tcsetattr(handle, TCSANOW, ptermios); |
507 |
|
|
508 |
|
|
509 |
*result = read(handle, data, length); |
*result = read(handle, data, length); |
510 |
|
|
511 |
|
//hexdump(data, *read); |
512 |
|
|
513 |
return STATUS_SUCCESS; |
return STATUS_SUCCESS; |
514 |
} |
} |
515 |
|
|
516 |
NTSTATUS |
static NTSTATUS |
517 |
serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
518 |
{ |
{ |
519 |
*result = write(handle, data, length); |
*result = write(handle, data, length); |
523 |
static NTSTATUS |
static NTSTATUS |
524 |
serial_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out) |
serial_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out) |
525 |
{ |
{ |
526 |
uint32 result; |
#if 0 |
527 |
|
int flush_mask, purge_mask; |
528 |
|
#endif |
529 |
|
uint32 result, modemstate; |
530 |
uint8 immediate; |
uint8 immediate; |
531 |
|
SERIAL_DEVICE *pser_inf; |
532 |
|
struct termios *ptermios; |
533 |
|
|
534 |
if ((request >> 16) != FILE_DEVICE_SERIAL_PORT) |
if ((request >> 16) != FILE_DEVICE_SERIAL_PORT) |
535 |
return STATUS_INVALID_PARAMETER; |
return STATUS_INVALID_PARAMETER; |
536 |
|
|
537 |
|
pser_inf = get_serial_info(handle); |
538 |
|
ptermios = pser_inf->ptermios; |
539 |
|
|
540 |
/* extract operation */ |
/* extract operation */ |
541 |
request >>= 2; |
request >>= 2; |
542 |
request &= 0xfff; |
request &= 0xfff; |
546 |
switch (request) |
switch (request) |
547 |
{ |
{ |
548 |
case SERIAL_SET_BAUD_RATE: |
case SERIAL_SET_BAUD_RATE: |
549 |
in_uint32_le(in, baud_rate); |
in_uint32_le(in, pser_inf->baud_rate); |
550 |
set_termios(); |
set_termios(pser_inf, handle); |
551 |
break; |
break; |
552 |
case SERIAL_GET_BAUD_RATE: |
case SERIAL_GET_BAUD_RATE: |
553 |
out_uint32_le(out, baud_rate); |
out_uint32_le(out, pser_inf->baud_rate); |
554 |
break; |
break; |
555 |
case SERIAL_SET_QUEUE_SIZE: |
case SERIAL_SET_QUEUE_SIZE: |
556 |
in_uint32_le(in, queue_in_size); |
in_uint32_le(in, pser_inf->queue_in_size); |
557 |
in_uint32_le(in, queue_out_size); |
in_uint32_le(in, pser_inf->queue_out_size); |
558 |
break; |
break; |
559 |
case SERIAL_SET_LINE_CONTROL: |
case SERIAL_SET_LINE_CONTROL: |
560 |
in_uint8(in, stop_bits); |
in_uint8(in, pser_inf->stop_bits); |
561 |
in_uint8(in, parity); |
in_uint8(in, pser_inf->parity); |
562 |
in_uint8(in, word_length); |
in_uint8(in, pser_inf->word_length); |
563 |
set_termios(); |
set_termios(pser_inf, handle); |
564 |
break; |
break; |
565 |
case SERIAL_GET_LINE_CONTROL: |
case SERIAL_GET_LINE_CONTROL: |
566 |
out_uint8(out, stop_bits); |
out_uint8(out, pser_inf->stop_bits); |
567 |
out_uint8(out, parity); |
out_uint8(out, pser_inf->parity); |
568 |
out_uint8(out, word_length); |
out_uint8(out, pser_inf->word_length); |
569 |
break; |
break; |
570 |
case SERIAL_IMMEDIATE_CHAR: |
case SERIAL_IMMEDIATE_CHAR: |
571 |
in_uint8(in, immediate); |
in_uint8(in, immediate); |
596 |
out_uint8s(out, 20); |
out_uint8s(out, 20); |
597 |
break; |
break; |
598 |
case SERIAL_GET_WAIT_MASK: |
case SERIAL_GET_WAIT_MASK: |
599 |
out_uint32(out, wait_mask); |
out_uint32(out, pser_inf->wait_mask); |
600 |
break; |
break; |
601 |
case SERIAL_SET_WAIT_MASK: |
case SERIAL_SET_WAIT_MASK: |
602 |
in_uint32(in, wait_mask); |
in_uint32(in, pser_inf->wait_mask); |
603 |
break; |
break; |
604 |
case SERIAL_SET_DTR: |
case SERIAL_SET_DTR: |
605 |
dtr = 1; |
pser_inf->dtr = 1; |
606 |
set_termios(); |
set_termios(pser_inf, handle); |
607 |
break; |
break; |
608 |
case SERIAL_CLR_DTR: |
case SERIAL_CLR_DTR: |
609 |
dtr = 0; |
pser_inf->dtr = 0; |
610 |
set_termios(); |
set_termios(pser_inf, handle); |
611 |
break; |
break; |
612 |
#if 0 |
case SERIAL_SET_RTS: |
613 |
case SERIAL_WAIT_ON_MASK: |
pser_inf->rts = 1; |
614 |
/* XXX implement me */ |
set_termios(pser_inf, handle); |
615 |
break; |
break; |
616 |
case SERIAL_SET_BREAK_ON: |
case SERIAL_CLR_RTS: |
617 |
tcsendbreak(serial_fd, 0); |
pser_inf->rts = 0; |
618 |
|
set_termios(pser_inf, handle); |
619 |
break; |
break; |
620 |
|
case SERIAL_GET_MODEMSTATUS: |
621 |
|
modemstate = 0; |
622 |
|
#ifdef TIOCMGET |
623 |
|
ioctl(handle, TIOCMGET, &result); |
624 |
|
if (result & TIOCM_CTS) |
625 |
|
modemstate |= SERIAL_MS_CTS; |
626 |
|
if (result & TIOCM_DSR) |
627 |
|
modemstate |= SERIAL_MS_DSR; |
628 |
|
if (result & TIOCM_RNG) |
629 |
|
modemstate |= SERIAL_MS_RNG; |
630 |
|
if (result & TIOCM_CAR) |
631 |
|
modemstate |= SERIAL_MS_CAR; |
632 |
|
#endif |
633 |
|
out_uint32_le(out, modemstate); |
634 |
|
break; |
635 |
|
case SERIAL_GET_COMMSTATUS: |
636 |
|
out_uint32_le(out, 0); /* Errors */ |
637 |
|
out_uint32_le(out, 0); /* Hold reasons */ |
638 |
|
out_uint32_le(out, 0); /* Amount in in queue */ |
639 |
|
out_uint32_le(out, 0); /* Amount in out queue */ |
640 |
|
out_uint8(out, 0); /* EofReceived */ |
641 |
|
out_uint8(out, 0); /* WaitForImmediate */ |
642 |
|
break; |
643 |
|
#if 0 |
644 |
case SERIAL_PURGE: |
case SERIAL_PURGE: |
|
|
|
645 |
printf("SERIAL_PURGE\n"); |
printf("SERIAL_PURGE\n"); |
646 |
in_uint32(in, purge_mask); |
in_uint32(in, purge_mask); |
647 |
if (purge_mask & 0x04) |
if (purge_mask & 0x04) |
655 |
if (purge_mask & 0x02) |
if (purge_mask & 0x02) |
656 |
rdpdr_abort_io(handle, 3, STATUS_CANCELLED); |
rdpdr_abort_io(handle, 3, STATUS_CANCELLED); |
657 |
break; |
break; |
658 |
|
case SERIAL_WAIT_ON_MASK: |
659 |
|
/* XXX implement me */ |
660 |
|
out_uint32_le(out, pser_inf->wait_mask); |
661 |
|
break; |
662 |
|
case SERIAL_SET_BREAK_ON: |
663 |
|
tcsendbreak(serial_fd, 0); |
664 |
|
break; |
665 |
case SERIAL_RESET_DEVICE: |
case SERIAL_RESET_DEVICE: |
666 |
case SERIAL_SET_BREAK_OFF: |
case SERIAL_SET_BREAK_OFF: |
|
case SERIAL_SET_RTS: |
|
|
case SERIAL_CLR_RTS: |
|
667 |
case SERIAL_SET_XOFF: |
case SERIAL_SET_XOFF: |
668 |
case SERIAL_SET_XON: |
case SERIAL_SET_XON: |
669 |
/* ignore */ |
/* ignore */ |
670 |
break; |
break; |
671 |
#endif |
#endif |
|
|
|
672 |
default: |
default: |
673 |
unimpl("SERIAL IOCTL %d\n", request); |
unimpl("SERIAL IOCTL %d\n", request); |
674 |
return STATUS_INVALID_PARAMETER; |
return STATUS_INVALID_PARAMETER; |
679 |
|
|
680 |
/* Read timeout for a given file descripter (device) when adding fd's to select() */ |
/* Read timeout for a given file descripter (device) when adding fd's to select() */ |
681 |
BOOL |
BOOL |
682 |
serial_get_timeout(uint32 handle, uint32 length, uint32 * timeout, uint32 * itv_timeout) |
serial_get_timeout(HANDLE handle, uint32 length, uint32 * timeout, uint32 * itv_timeout) |
683 |
{ |
{ |
684 |
int index; |
int index; |
685 |
SERIAL_DEVICE *pser_inf; |
SERIAL_DEVICE *pser_inf; |
686 |
|
|
687 |
index = get_device_index(handle); |
index = get_device_index(handle); |
688 |
|
if (index < 0) |
689 |
|
return True; |
690 |
|
|
691 |
if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL) |
if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL) |
692 |
{ |
{ |