1 |
#include <unistd.h> |
#include <unistd.h> |
2 |
#include <fcntl.h> |
#include <fcntl.h> |
3 |
#include <termios.h> |
#include <termios.h> |
4 |
|
#include <strings.h> |
5 |
#include "rdesktop.h" |
#include "rdesktop.h" |
6 |
|
|
7 |
#define FILE_DEVICE_SERIAL_PORT 0x1b |
#define FILE_DEVICE_SERIAL_PORT 0x1b |
53 |
#define ODD_PARITY 1 |
#define ODD_PARITY 1 |
54 |
#define EVEN_PARITY 2 |
#define EVEN_PARITY 2 |
55 |
|
|
56 |
|
extern RDPDR_DEVICE g_rdpdr_device[]; |
57 |
|
|
58 |
int serial_fd; |
int serial_fd; |
59 |
struct termios termios; |
struct termios termios; |
60 |
|
|
64 |
uint32 wait_mask; |
uint32 wait_mask; |
65 |
uint8 stop_bits, parity, word_length; |
uint8 stop_bits, parity, word_length; |
66 |
|
|
67 |
static BOOL |
SERIAL_DEVICE |
68 |
get_termios(void) |
*get_serial_info(HANDLE handle) |
69 |
{ |
{ |
70 |
speed_t speed; |
int index; |
71 |
|
|
72 |
if (tcgetattr(serial_fd, &termios) == -1) |
for (index = 0; index < RDPDR_MAX_DEVICES; index++) |
73 |
return False; |
{ |
74 |
|
if (handle == g_rdpdr_device[index].handle) |
75 |
|
return (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data; |
76 |
|
} |
77 |
|
return NULL; |
78 |
|
} |
79 |
|
|
80 |
speed = cfgetispeed(&termios); |
BOOL |
81 |
switch (speed) |
get_termios(SERIAL_DEVICE *pser_inf, HANDLE serial_fd) |
82 |
{ |
{ |
83 |
case B75: baud_rate = 75; break; |
speed_t speed; |
84 |
case B110: baud_rate = 110; break; |
struct termios *ptermios; |
|
case B134: baud_rate = 134; break; |
|
|
case B150: baud_rate = 150; break; |
|
|
case B300: baud_rate = 300; break; |
|
|
case B600: baud_rate = 600; break; |
|
|
case B1200: baud_rate = 1200; break; |
|
|
case B1800: baud_rate = 1800; break; |
|
|
case B2400: baud_rate = 2400; break; |
|
|
case B4800: baud_rate = 4800; break; |
|
|
case B9600: baud_rate = 9600; break; |
|
|
case B19200: baud_rate = 19200; break; |
|
|
case B38400: baud_rate = 38400; break; |
|
|
case B57600: baud_rate = 57600; break; |
|
|
case B115200: baud_rate = 115200; break; |
|
|
default: baud_rate = 0; break; |
|
|
} |
|
85 |
|
|
86 |
speed = cfgetospeed(&termios); |
ptermios = pser_inf->ptermios; |
|
dtr = (speed == B0) ? 0 : 1; |
|
87 |
|
|
88 |
stop_bits = (termios.c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BITS_1; |
if (tcgetattr(serial_fd, ptermios) == -1) |
89 |
parity = (termios.c_cflag & PARENB) ? ((termios.c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY; |
return False; |
90 |
switch (termios.c_cflag & CSIZE) |
|
91 |
{ |
speed = cfgetispeed(ptermios); |
92 |
case CS5: word_length = 5; break; |
switch (speed) |
93 |
case CS6: word_length = 6; break; |
{ |
94 |
case CS7: word_length = 7; break; |
#ifdef B75 |
95 |
default: word_length = 8; break; |
case B75: pser_inf->baud_rate = 75; break; |
96 |
} |
#endif |
97 |
|
#ifdef B110 |
98 |
|
case B110: pser_inf->baud_rate = 110; break; |
99 |
|
#endif |
100 |
|
#ifdef B134 |
101 |
|
case B134: pser_inf->baud_rate = 134; break; |
102 |
|
#endif |
103 |
|
#ifdef B150 |
104 |
|
case B150: pser_inf->baud_rate = 150; break; |
105 |
|
#endif |
106 |
|
#ifdef B300 |
107 |
|
case B300: pser_inf->baud_rate = 300; break; |
108 |
|
#endif |
109 |
|
#ifdef B600 |
110 |
|
case B600: pser_inf->baud_rate = 600; break; |
111 |
|
#endif |
112 |
|
#ifdef B1200 |
113 |
|
case B1200: pser_inf->baud_rate = 1200; break; |
114 |
|
#endif |
115 |
|
#ifdef B1800 |
116 |
|
case B1800: pser_inf->baud_rate = 1800; break; |
117 |
|
#endif |
118 |
|
#ifdef B2400 |
119 |
|
case B2400: pser_inf->baud_rate = 2400; break; |
120 |
|
#endif |
121 |
|
#ifdef B4800 |
122 |
|
case B4800: pser_inf->baud_rate = 4800; break; |
123 |
|
#endif |
124 |
|
#ifdef B9600 |
125 |
|
case B9600: pser_inf->baud_rate = 9600; break; |
126 |
|
#endif |
127 |
|
#ifdef B19200 |
128 |
|
case B19200: pser_inf->baud_rate = 19200; break; |
129 |
|
#endif |
130 |
|
#ifdef B38400 |
131 |
|
case B38400: pser_inf->baud_rate = 38400; break; |
132 |
|
#endif |
133 |
|
#ifdef B57600 |
134 |
|
case B57600: pser_inf->baud_rate = 57600; break; |
135 |
|
#endif |
136 |
|
#ifdef B115200 |
137 |
|
case B115200: pser_inf->baud_rate = 115200; break; |
138 |
|
#endif |
139 |
|
default: pser_inf->baud_rate = 0; break; |
140 |
|
} |
141 |
|
|
142 |
return True; |
speed = cfgetospeed(ptermios); |
143 |
|
pser_inf->dtr = (speed == B0) ? 0 : 1; |
144 |
|
|
145 |
|
pser_inf->stop_bits = (ptermios->c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BITS_1; |
146 |
|
pser_inf->parity = (ptermios->c_cflag & PARENB) ? ((ptermios->c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY; |
147 |
|
switch (ptermios->c_cflag & CSIZE) |
148 |
|
{ |
149 |
|
case CS5: pser_inf->word_length = 5; break; |
150 |
|
case CS6: pser_inf->word_length = 6; break; |
151 |
|
case CS7: pser_inf->word_length = 7; break; |
152 |
|
default: pser_inf->word_length = 8; break; |
153 |
|
} |
154 |
|
|
155 |
|
return True; |
156 |
} |
} |
157 |
|
|
158 |
static void |
static void |
162 |
|
|
163 |
switch (baud_rate) |
switch (baud_rate) |
164 |
{ |
{ |
165 |
case 75: speed = B75; break; |
#ifdef B75 |
166 |
case 110: speed = B110; break; |
case 75: speed = B75;break; |
167 |
case 134: speed = B134; break; |
#endif |
168 |
case 150: speed = B150; break; |
#ifdef B110 |
169 |
case 300: speed = B300; break; |
case 110: speed = B110;break; |
170 |
case 600: speed = B600; break; |
#endif |
171 |
case 1200: speed = B1200; break; |
#ifdef B134 |
172 |
case 1800: speed = B1800; break; |
case 134: speed = B134;break; |
173 |
case 2400: speed = B2400; break; |
#endif |
174 |
case 4800: speed = B4800; break; |
#ifdef B150 |
175 |
case 9600: speed = B9600; break; |
case 150: speed = B150;break; |
176 |
case 19200: speed = B19200; break; |
#endif |
177 |
case 38400: speed = B38400; break; |
#ifdef B300 |
178 |
case 57600: speed = B57600; break; |
case 300: speed = B300;break; |
179 |
case 115200: speed = B115200; break; |
#endif |
180 |
default: speed = B0; break; |
#ifdef B600 |
181 |
|
case 600: speed = B600;break; |
182 |
|
#endif |
183 |
|
#ifdef B1200 |
184 |
|
case 1200: speed = B1200;break; |
185 |
|
#endif |
186 |
|
#ifdef B1800 |
187 |
|
case 1800: speed = B1800;break; |
188 |
|
#endif |
189 |
|
#ifdef B2400 |
190 |
|
case 2400: speed = B2400;break; |
191 |
|
#endif |
192 |
|
#ifdef B4800 |
193 |
|
case 4800: speed = B4800;break; |
194 |
|
#endif |
195 |
|
#ifdef B9600 |
196 |
|
case 9600: speed = B9600;break; |
197 |
|
#endif |
198 |
|
#ifdef B19200 |
199 |
|
case 19200: speed = B19200;break; |
200 |
|
#endif |
201 |
|
#ifdef B38400 |
202 |
|
case 38400: speed = B38400;break; |
203 |
|
#endif |
204 |
|
#ifdef B57600 |
205 |
|
case 57600: speed = B57600;break; |
206 |
|
#endif |
207 |
|
#ifdef B115200 |
208 |
|
case 115200: speed = B115200;break; |
209 |
|
#endif |
210 |
|
default: speed = B0;break; |
211 |
} |
} |
212 |
|
|
213 |
/* on systems with separate ispeed and ospeed, we can remember the speed |
/* on systems with separate ispeed and ospeed, we can remember the speed |
215 |
cfsetispeed(&termios, speed); |
cfsetispeed(&termios, speed); |
216 |
cfsetospeed(&termios, dtr ? speed : 0); |
cfsetospeed(&termios, dtr ? speed : 0); |
217 |
|
|
218 |
termios.c_cflag &= ~(CSTOPB|PARENB|PARODD|CSIZE); |
termios.c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE); |
219 |
switch (stop_bits) |
switch (stop_bits) |
220 |
{ |
{ |
221 |
case STOP_BITS_2: |
case STOP_BITS_2: |
228 |
termios.c_cflag |= PARENB; |
termios.c_cflag |= PARENB; |
229 |
break; |
break; |
230 |
case ODD_PARITY: |
case ODD_PARITY: |
231 |
termios.c_cflag |= PARENB|PARODD; |
termios.c_cflag |= PARENB | PARODD; |
232 |
break; |
break; |
233 |
} |
} |
234 |
switch (word_length) |
switch (word_length) |
235 |
{ |
{ |
236 |
case 5: termios.c_cflag |= CS5; break; |
case 5: |
237 |
case 6: termios.c_cflag |= CS6; break; |
termios.c_cflag |= CS5; |
238 |
case 7: termios.c_cflag |= CS7; break; |
break; |
239 |
default: termios.c_cflag |= CS8; break; |
case 6: |
240 |
|
termios.c_cflag |= CS6; |
241 |
|
break; |
242 |
|
case 7: |
243 |
|
termios.c_cflag |= CS7; |
244 |
|
break; |
245 |
|
default: |
246 |
|
termios.c_cflag |= CS8; |
247 |
|
break; |
248 |
} |
} |
249 |
|
|
250 |
tcsetattr(serial_fd, TCSANOW, &termios); |
tcsetattr(serial_fd, TCSANOW, &termios); |
251 |
} |
} |
252 |
|
|
253 |
static NTSTATUS |
/* Enumeration of devices from rdesktop.c */ |
254 |
serial_create(HANDLE *handle) |
/* returns numer of units found and initialized. */ |
255 |
|
/* optarg looks like ':com1=/dev/ttyS0' */ |
256 |
|
/* when it arrives to this function. */ |
257 |
|
/* :com1=/dev/ttyS0,com2=/dev/ttyS1 */ |
258 |
|
int |
259 |
|
serial_enum_devices(int *id, char* optarg) |
260 |
{ |
{ |
261 |
/* XXX do we have to handle concurrent open attempts? */ |
SERIAL_DEVICE* pser_inf; |
|
serial_fd = open("/dev/ttyS0", O_RDWR); |
|
|
if (serial_fd == -1) |
|
|
return STATUS_ACCESS_DENIED; |
|
262 |
|
|
263 |
if (!get_termios()) |
char *pos = optarg; |
264 |
return STATUS_ACCESS_DENIED; |
char *pos2; |
265 |
|
int count = 0; |
266 |
|
|
267 |
|
// skip the first colon |
268 |
|
optarg++; |
269 |
|
while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) |
270 |
|
{ |
271 |
|
// Init data structures for device |
272 |
|
pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE)); |
273 |
|
pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios)); |
274 |
|
pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios)); |
275 |
|
|
276 |
|
pos2 = next_arg(optarg, '='); |
277 |
|
strcpy(g_rdpdr_device[*id].name, optarg); |
278 |
|
|
279 |
|
toupper_str(g_rdpdr_device[*id].name); |
280 |
|
|
281 |
|
g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1); |
282 |
|
strcpy(g_rdpdr_device[*id].local_path, pos2); |
283 |
|
printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name, g_rdpdr_device[*id].local_path); |
284 |
|
// set device type |
285 |
|
g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL; |
286 |
|
g_rdpdr_device[*id].pdevice_data = (void *) pser_inf; |
287 |
|
count++; |
288 |
|
(*id)++; |
289 |
|
|
290 |
*handle = 0; |
optarg = pos; |
291 |
return STATUS_SUCCESS; |
} |
292 |
|
return count; |
293 |
|
} |
294 |
|
|
295 |
|
NTSTATUS |
296 |
|
serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition, uint32 flags_and_attributes, char *filename, HANDLE *handle) |
297 |
|
{ |
298 |
|
HANDLE serial_fd; |
299 |
|
SERIAL_DEVICE *pser_inf; |
300 |
|
struct termios *ptermios; |
301 |
|
|
302 |
|
pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data; |
303 |
|
ptermios = pser_inf->ptermios; |
304 |
|
serial_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR | O_NOCTTY); |
305 |
|
|
306 |
|
if (serial_fd == -1) |
307 |
|
return STATUS_ACCESS_DENIED; |
308 |
|
|
309 |
|
if (!get_termios(pser_inf, serial_fd )) |
310 |
|
return STATUS_ACCESS_DENIED; |
311 |
|
|
312 |
|
// Store handle for later use |
313 |
|
g_rdpdr_device[device_id].handle = serial_fd; |
314 |
|
|
315 |
|
/* some sane information */ |
316 |
|
printf("INFO: SERIAL %s to %s\nINFO: speed %u baud, stop bits %u, parity %u, word length %u bits, dtr %u\n", |
317 |
|
g_rdpdr_device[device_id].name, g_rdpdr_device[device_id].local_path, |
318 |
|
pser_inf->baud_rate, pser_inf->stop_bits, pser_inf->parity, pser_inf->word_length, pser_inf->dtr ); |
319 |
|
printf("INFO: use stty to change settings\n" ); |
320 |
|
|
321 |
|
//tcgetattr(serial_fd, pser_inf->ptermios); |
322 |
|
|
323 |
|
*handle = serial_fd; |
324 |
|
return STATUS_SUCCESS; |
325 |
} |
} |
326 |
|
|
327 |
static NTSTATUS |
static NTSTATUS |
331 |
return STATUS_SUCCESS; |
return STATUS_SUCCESS; |
332 |
} |
} |
333 |
|
|
334 |
static NTSTATUS |
NTSTATUS |
335 |
serial_read(HANDLE handle, uint8 *data, uint32 length, uint32 *result) |
serial_read(HANDLE handle, uint8 *data, uint32 length, uint32 offset, uint32 *result) |
336 |
{ |
{ |
337 |
*result = read(serial_fd, data, length); |
long timeout; |
338 |
return STATUS_SUCCESS; |
SERIAL_DEVICE *pser_inf; |
339 |
|
struct termios *ptermios; |
340 |
|
|
341 |
|
timeout = 0; |
342 |
|
pser_inf = get_serial_info(handle); |
343 |
|
ptermios = pser_inf->ptermios; |
344 |
|
|
345 |
|
// Set timeouts kind of like the windows serial timeout parameters. Multiply timeout |
346 |
|
// with requested read size |
347 |
|
if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant) |
348 |
|
{ |
349 |
|
timeout = (pser_inf->read_total_timeout_multiplier * length + pser_inf->read_total_timeout_constant + 99) / 100; |
350 |
|
} |
351 |
|
else if (pser_inf->read_interval_timeout) |
352 |
|
{ |
353 |
|
timeout = (pser_inf->read_interval_timeout * length + 99) / 100; |
354 |
|
} |
355 |
|
|
356 |
|
// If a timeout is set, do a blocking read, which times out after some time. |
357 |
|
// It will make rdesktop less responsive, but it will improve serial performance, by not |
358 |
|
// reading one character at a time. |
359 |
|
if (timeout == 0) |
360 |
|
{ |
361 |
|
ptermios->c_cc[VTIME] = 0; |
362 |
|
ptermios->c_cc[VMIN] = 0; |
363 |
|
} |
364 |
|
else |
365 |
|
{ |
366 |
|
ptermios->c_cc[VTIME] = timeout; |
367 |
|
ptermios->c_cc[VMIN] = 1; |
368 |
|
} |
369 |
|
tcsetattr(handle, TCSANOW, ptermios); |
370 |
|
|
371 |
|
*result = read(handle, data, length); |
372 |
|
return STATUS_SUCCESS; |
373 |
} |
} |
374 |
|
|
375 |
static NTSTATUS |
NTSTATUS |
376 |
serial_write(HANDLE handle, uint8 *data, uint32 length, uint32 *result) |
serial_write(HANDLE handle, uint8 *data, uint32 length, uint32 offset, uint32 *result) |
377 |
{ |
{ |
378 |
*result = write(serial_fd, data, length); |
*result = write(handle, data, length); |
379 |
return STATUS_SUCCESS; |
return STATUS_SUCCESS; |
380 |
} |
} |
381 |
|
|
382 |
static NTSTATUS |
static NTSTATUS |
420 |
break; |
break; |
421 |
case SERIAL_IMMEDIATE_CHAR: |
case SERIAL_IMMEDIATE_CHAR: |
422 |
in_uint8(in, immediate); |
in_uint8(in, immediate); |
423 |
serial_write(handle, &immediate, 1, &result); |
serial_write(handle, &immediate, 1, 0, &result); |
424 |
break; |
break; |
425 |
case SERIAL_CONFIG_SIZE: |
case SERIAL_CONFIG_SIZE: |
426 |
out_uint32_le(out, 0); |
out_uint32_le(out, 0); |
433 |
break; |
break; |
434 |
case SERIAL_GET_HANDFLOW: |
case SERIAL_GET_HANDFLOW: |
435 |
out_uint32_le(out, 0); |
out_uint32_le(out, 0); |
436 |
out_uint32_le(out, 3); /* Xon/Xoff */ |
out_uint32_le(out, 3); /* Xon/Xoff */ |
437 |
out_uint32_le(out, 0); |
out_uint32_le(out, 0); |
438 |
out_uint32_le(out, 0); |
out_uint32_le(out, 0); |
439 |
break; |
break; |
468 |
tcsendbreak(serial_fd, 0); |
tcsendbreak(serial_fd, 0); |
469 |
break; |
break; |
470 |
case SERIAL_PURGE: |
case SERIAL_PURGE: |
471 |
in_uint32(purge_mask); |
|
472 |
/* tcflush */ |
printf("SERIAL_PURGE\n"); |
473 |
break; |
in_uint32(in, purge_mask); |
474 |
|
if (purge_mask & 0x04) flush_mask |= TCOFLUSH; |
475 |
|
if (purge_mask & 0x08) flush_mask |= TCIFLUSH; |
476 |
|
if (flush_mask != 0) tcflush(handle, flush_mask); |
477 |
|
if (purge_mask & 0x01) rdpdr_abort_io(handle, 4, STATUS_CANCELLED); |
478 |
|
if (purge_mask & 0x02) rdpdr_abort_io(handle, 3, STATUS_CANCELLED); |
479 |
|
break; |
480 |
|
|
481 |
case SERIAL_RESET_DEVICE: |
case SERIAL_RESET_DEVICE: |
482 |
case SERIAL_SET_BREAK_OFF: |
case SERIAL_SET_BREAK_OFF: |
483 |
case SERIAL_SET_RTS: |
case SERIAL_SET_RTS: |
496 |
return STATUS_SUCCESS; |
return STATUS_SUCCESS; |
497 |
} |
} |
498 |
|
|
499 |
DEVICE_FNS serial_fns = |
/* Read timeout for a given file descripter (device) when adding fd's to select() */ |
500 |
|
BOOL |
501 |
|
serial_get_timeout(uint32 handle, uint32 length, uint32 *timeout, uint32 *itv_timeout) |
502 |
{ |
{ |
503 |
|
int index; |
504 |
|
SERIAL_DEVICE *pser_inf; |
505 |
|
|
506 |
|
index = get_device_index(handle); |
507 |
|
|
508 |
|
if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL) |
509 |
|
{ |
510 |
|
return False; |
511 |
|
} |
512 |
|
|
513 |
|
pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data; |
514 |
|
|
515 |
|
*timeout = pser_inf->read_total_timeout_multiplier * length + pser_inf->read_total_timeout_constant; |
516 |
|
*itv_timeout = pser_inf->read_interval_timeout; |
517 |
|
return True; |
518 |
|
} |
519 |
|
|
520 |
|
DEVICE_FNS serial_fns = { |
521 |
serial_create, |
serial_create, |
522 |
serial_close, |
serial_close, |
523 |
serial_read, |
serial_read, |
524 |
serial_write, |
serial_write, |
525 |
serial_device_control |
serial_device_control |
526 |
}; |
}; |
|
|
|