/[rdesktop]/sourceforge.net/trunk/rdesktop/serial.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sourceforge.net/trunk/rdesktop/serial.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 578 - (hide annotations)
Fri Jan 23 06:56:42 2004 UTC (20 years, 5 months ago) by n-ki
File MIME type: text/plain
File size: 13757 byte(s)
removing the code that set the serial io stuff, and the extra options to comport....

1 matthewc 432 #include <unistd.h>
2     #include <fcntl.h>
3     #include <termios.h>
4 stargo 570 #include <strings.h>
5 matthewc 432 #include "rdesktop.h"
6    
7     #define FILE_DEVICE_SERIAL_PORT 0x1b
8    
9     #define SERIAL_SET_BAUD_RATE 1
10     #define SERIAL_SET_QUEUE_SIZE 2
11     #define SERIAL_SET_LINE_CONTROL 3
12     #define SERIAL_SET_BREAK_ON 4
13     #define SERIAL_SET_BREAK_OFF 5
14     #define SERIAL_IMMEDIATE_CHAR 6
15     #define SERIAL_SET_TIMEOUTS 7
16     #define SERIAL_GET_TIMEOUTS 8
17     #define SERIAL_SET_DTR 9
18     #define SERIAL_CLR_DTR 10
19     #define SERIAL_RESET_DEVICE 11
20     #define SERIAL_SET_RTS 12
21     #define SERIAL_CLR_RTS 13
22     #define SERIAL_SET_XOFF 14
23     #define SERIAL_SET_XON 15
24     #define SERIAL_GET_WAIT_MASK 16
25     #define SERIAL_SET_WAIT_MASK 17
26     #define SERIAL_WAIT_ON_MASK 18
27     #define SERIAL_PURGE 19
28     #define SERIAL_GET_BAUD_RATE 20
29     #define SERIAL_GET_LINE_CONTROL 21
30     #define SERIAL_GET_CHARS 22
31     #define SERIAL_SET_CHARS 23
32     #define SERIAL_GET_HANDFLOW 24
33     #define SERIAL_SET_HANDFLOW 25
34     #define SERIAL_GET_MODEMSTATUS 26
35     #define SERIAL_GET_COMMSTATUS 27
36     #define SERIAL_XOFF_COUNTER 28
37     #define SERIAL_GET_PROPERTIES 29
38     #define SERIAL_GET_DTRRTS 30
39     #define SERIAL_LSRMST_INSERT 31
40     #define SERIAL_CONFIG_SIZE 32
41     #define SERIAL_GET_COMMCONFIG 33
42     #define SERIAL_SET_COMMCONFIG 34
43     #define SERIAL_GET_STATS 35
44     #define SERIAL_CLEAR_STATS 36
45     #define SERIAL_GET_MODEM_CONTROL 37
46     #define SERIAL_SET_MODEM_CONTROL 38
47     #define SERIAL_SET_FIFO_CONTROL 39
48    
49     #define STOP_BITS_1 0
50     #define STOP_BITS_2 2
51    
52     #define NO_PARITY 0
53     #define ODD_PARITY 1
54     #define EVEN_PARITY 2
55    
56 n-ki 569 extern RDPDR_DEVICE g_rdpdr_device[];
57    
58 matthewc 432 int serial_fd;
59     struct termios termios;
60    
61     int dtr;
62     uint32 baud_rate;
63     uint32 queue_in_size, queue_out_size;
64     uint32 wait_mask;
65     uint8 stop_bits, parity, word_length;
66    
67 n-ki 569 SERIAL_DEVICE
68     *get_serial_info(HANDLE handle)
69 matthewc 432 {
70 n-ki 569 int index;
71 matthewc 432
72 n-ki 569 for (index = 0; index < RDPDR_MAX_DEVICES; index++)
73     {
74     if (handle == g_rdpdr_device[index].handle)
75     return (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
76     }
77     return NULL;
78     }
79 matthewc 432
80 n-ki 569 BOOL
81     get_termios(SERIAL_DEVICE *pser_inf, HANDLE serial_fd)
82     {
83     speed_t speed;
84     struct termios *ptermios;
85    
86     ptermios = pser_inf->ptermios;
87    
88     if (tcgetattr(serial_fd, ptermios) == -1)
89     return False;
90    
91     speed = cfgetispeed(ptermios);
92     switch (speed)
93     {
94 stargo 545 #ifdef B75
95 n-ki 569 case B75: pser_inf->baud_rate = 75; break;
96 stargo 545 #endif
97     #ifdef B110
98 n-ki 569 case B110: pser_inf->baud_rate = 110; break;
99 stargo 545 #endif
100     #ifdef B134
101 n-ki 569 case B134: pser_inf->baud_rate = 134; break;
102 stargo 545 #endif
103     #ifdef B150
104 n-ki 569 case B150: pser_inf->baud_rate = 150; break;
105 stargo 545 #endif
106     #ifdef B300
107 n-ki 569 case B300: pser_inf->baud_rate = 300; break;
108 stargo 545 #endif
109     #ifdef B600
110 n-ki 569 case B600: pser_inf->baud_rate = 600; break;
111 stargo 545 #endif
112     #ifdef B1200
113 n-ki 569 case B1200: pser_inf->baud_rate = 1200; break;
114 stargo 545 #endif
115     #ifdef B1800
116 n-ki 569 case B1800: pser_inf->baud_rate = 1800; break;
117 stargo 545 #endif
118     #ifdef B2400
119 n-ki 569 case B2400: pser_inf->baud_rate = 2400; break;
120 stargo 545 #endif
121     #ifdef B4800
122 n-ki 569 case B4800: pser_inf->baud_rate = 4800; break;
123 stargo 545 #endif
124     #ifdef B9600
125 n-ki 569 case B9600: pser_inf->baud_rate = 9600; break;
126 stargo 545 #endif
127     #ifdef B19200
128 n-ki 569 case B19200: pser_inf->baud_rate = 19200; break;
129 stargo 545 #endif
130     #ifdef B38400
131 n-ki 569 case B38400: pser_inf->baud_rate = 38400; break;
132 stargo 545 #endif
133     #ifdef B57600
134 n-ki 569 case B57600: pser_inf->baud_rate = 57600; break;
135 stargo 545 #endif
136     #ifdef B115200
137 n-ki 569 case B115200: pser_inf->baud_rate = 115200; break;
138 stargo 545 #endif
139 n-ki 569 default: pser_inf->baud_rate = 0; break;
140     }
141 matthewc 432
142 n-ki 569 speed = cfgetospeed(ptermios);
143     pser_inf->dtr = (speed == B0) ? 0 : 1;
144 matthewc 432
145 n-ki 569 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 matthewc 432
155 n-ki 569 return True;
156 matthewc 432 }
157    
158     static void
159     set_termios(void)
160     {
161     speed_t speed;
162    
163     switch (baud_rate)
164     {
165 stargo 545 #ifdef B75
166 n-ki 569 case 75: speed = B75;break;
167 stargo 545 #endif
168     #ifdef B110
169 n-ki 569 case 110: speed = B110;break;
170 stargo 545 #endif
171     #ifdef B134
172 n-ki 569 case 134: speed = B134;break;
173 stargo 545 #endif
174     #ifdef B150
175 n-ki 569 case 150: speed = B150;break;
176 stargo 545 #endif
177     #ifdef B300
178 n-ki 569 case 300: speed = B300;break;
179 stargo 545 #endif
180     #ifdef B600
181 n-ki 569 case 600: speed = B600;break;
182 stargo 545 #endif
183     #ifdef B1200
184 n-ki 569 case 1200: speed = B1200;break;
185 stargo 545 #endif
186     #ifdef B1800
187 n-ki 569 case 1800: speed = B1800;break;
188 stargo 545 #endif
189     #ifdef B2400
190 n-ki 569 case 2400: speed = B2400;break;
191 stargo 545 #endif
192     #ifdef B4800
193 n-ki 569 case 4800: speed = B4800;break;
194 stargo 545 #endif
195     #ifdef B9600
196 n-ki 569 case 9600: speed = B9600;break;
197 stargo 545 #endif
198     #ifdef B19200
199 n-ki 569 case 19200: speed = B19200;break;
200 stargo 545 #endif
201     #ifdef B38400
202 n-ki 569 case 38400: speed = B38400;break;
203 stargo 545 #endif
204     #ifdef B57600
205 n-ki 569 case 57600: speed = B57600;break;
206 stargo 545 #endif
207     #ifdef B115200
208 n-ki 569 case 115200: speed = B115200;break;
209 stargo 545 #endif
210 n-ki 569 default: speed = B0;break;
211 matthewc 432 }
212    
213     /* on systems with separate ispeed and ospeed, we can remember the speed
214     in ispeed while changing DTR with ospeed */
215     cfsetispeed(&termios, speed);
216     cfsetospeed(&termios, dtr ? speed : 0);
217    
218 astrand 435 termios.c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE);
219 matthewc 432 switch (stop_bits)
220     {
221     case STOP_BITS_2:
222     termios.c_cflag |= CSTOPB;
223     break;
224     }
225     switch (parity)
226     {
227     case EVEN_PARITY:
228     termios.c_cflag |= PARENB;
229     break;
230     case ODD_PARITY:
231 astrand 435 termios.c_cflag |= PARENB | PARODD;
232 matthewc 432 break;
233     }
234     switch (word_length)
235     {
236 astrand 435 case 5:
237     termios.c_cflag |= CS5;
238     break;
239     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 matthewc 432 }
249    
250     tcsetattr(serial_fd, TCSANOW, &termios);
251     }
252    
253 n-ki 569 /* Enumeration of devices from rdesktop.c */
254     /* returns numer of units found and initialized. */
255     /* optarg looks like ':com1=/dev/ttyS0' */
256     /* when it arrives to this function. */
257 n-ki 578 /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */
258 n-ki 569 int
259     serial_enum_devices(int *id, char* optarg)
260 matthewc 432 {
261 n-ki 569 SERIAL_DEVICE* pser_inf;
262 matthewc 432
263 n-ki 578 char *pos = optarg;
264     char *pos2;
265     int count = 0;
266 matthewc 432
267 n-ki 578 // skip the first colon
268     optarg++;
269     while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
270     {
271 n-ki 569 // 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 n-ki 578 pos2 = next_arg(optarg, '=');
277     strcpy(g_rdpdr_device[*id].name, optarg);
278 n-ki 569
279 n-ki 578 toupper_str(g_rdpdr_device[*id].name);
280 n-ki 569
281 n-ki 578 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 n-ki 569 // set device type
285     g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL;
286     g_rdpdr_device[*id].pdevice_data = (void *) pser_inf;
287 n-ki 578 count++;
288 n-ki 569 (*id)++;
289    
290 n-ki 578 optarg = pos;
291 n-ki 569 }
292 n-ki 578 return count;
293 matthewc 432 }
294    
295 n-ki 569 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 n-ki 578 if (!get_termios(pser_inf, serial_fd ))
310 n-ki 569 return STATUS_ACCESS_DENIED;
311    
312 n-ki 578 // Store handle for later use
313 n-ki 569 g_rdpdr_device[device_id].handle = serial_fd;
314    
315 n-ki 578 /* 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 n-ki 569
321 n-ki 578 //tcgetattr(serial_fd, pser_inf->ptermios);
322 n-ki 569
323 n-ki 578 *handle = serial_fd;
324 n-ki 569 return STATUS_SUCCESS;
325     }
326    
327 matthewc 432 static NTSTATUS
328     serial_close(HANDLE handle)
329     {
330     close(serial_fd);
331     return STATUS_SUCCESS;
332     }
333    
334 n-ki 569 NTSTATUS
335     serial_read(HANDLE handle, uint8 *data, uint32 length, uint32 offset, uint32 *result)
336 matthewc 432 {
337 n-ki 569 long timeout;
338     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 matthewc 432 }
374    
375 n-ki 569 NTSTATUS
376     serial_write(HANDLE handle, uint8 *data, uint32 length, uint32 offset, uint32 *result)
377 matthewc 432 {
378 n-ki 569 *result = write(handle, data, length);
379     return STATUS_SUCCESS;
380 matthewc 432 }
381    
382     static NTSTATUS
383     serial_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out)
384     {
385     uint32 result;
386     uint8 immediate;
387    
388     if ((request >> 16) != FILE_DEVICE_SERIAL_PORT)
389     return STATUS_INVALID_PARAMETER;
390    
391     /* extract operation */
392     request >>= 2;
393     request &= 0xfff;
394    
395     printf("SERIAL IOCTL %d\n", request);
396    
397     switch (request)
398     {
399     case SERIAL_SET_BAUD_RATE:
400     in_uint32_le(in, baud_rate);
401     set_termios();
402     break;
403     case SERIAL_GET_BAUD_RATE:
404     out_uint32_le(out, baud_rate);
405     break;
406     case SERIAL_SET_QUEUE_SIZE:
407     in_uint32_le(in, queue_in_size);
408     in_uint32_le(in, queue_out_size);
409     break;
410     case SERIAL_SET_LINE_CONTROL:
411     in_uint8(in, stop_bits);
412     in_uint8(in, parity);
413     in_uint8(in, word_length);
414     set_termios();
415     break;
416     case SERIAL_GET_LINE_CONTROL:
417     out_uint8(out, stop_bits);
418     out_uint8(out, parity);
419     out_uint8(out, word_length);
420     break;
421     case SERIAL_IMMEDIATE_CHAR:
422     in_uint8(in, immediate);
423 n-ki 569 serial_write(handle, &immediate, 1, 0, &result);
424 matthewc 432 break;
425     case SERIAL_CONFIG_SIZE:
426     out_uint32_le(out, 0);
427     break;
428     case SERIAL_GET_CHARS:
429     out_uint8s(out, 6);
430     break;
431     case SERIAL_SET_CHARS:
432     in_uint8s(in, 6);
433     break;
434     case SERIAL_GET_HANDFLOW:
435     out_uint32_le(out, 0);
436 astrand 435 out_uint32_le(out, 3); /* Xon/Xoff */
437 matthewc 432 out_uint32_le(out, 0);
438     out_uint32_le(out, 0);
439     break;
440     case SERIAL_SET_HANDFLOW:
441     in_uint8s(in, 16);
442     break;
443     case SERIAL_SET_TIMEOUTS:
444     in_uint8s(in, 20);
445     break;
446     case SERIAL_GET_TIMEOUTS:
447     out_uint8s(out, 20);
448     break;
449     case SERIAL_GET_WAIT_MASK:
450     out_uint32(out, wait_mask);
451     break;
452     case SERIAL_SET_WAIT_MASK:
453     in_uint32(in, wait_mask);
454     break;
455     case SERIAL_SET_DTR:
456     dtr = 1;
457     set_termios();
458     break;
459     case SERIAL_CLR_DTR:
460     dtr = 0;
461     set_termios();
462     break;
463     #if 0
464     case SERIAL_WAIT_ON_MASK:
465     /* XXX implement me */
466     break;
467     case SERIAL_SET_BREAK_ON:
468     tcsendbreak(serial_fd, 0);
469     break;
470     case SERIAL_PURGE:
471 n-ki 569
472     printf("SERIAL_PURGE\n");
473     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 matthewc 432 case SERIAL_RESET_DEVICE:
482     case SERIAL_SET_BREAK_OFF:
483     case SERIAL_SET_RTS:
484     case SERIAL_CLR_RTS:
485     case SERIAL_SET_XOFF:
486     case SERIAL_SET_XON:
487     /* ignore */
488     break;
489     #endif
490    
491     default:
492     unimpl("SERIAL IOCTL %d\n", request);
493     return STATUS_INVALID_PARAMETER;
494     }
495    
496     return STATUS_SUCCESS;
497     }
498    
499 n-ki 569 /* 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 astrand 435 DEVICE_FNS serial_fns = {
521 matthewc 432 serial_create,
522     serial_close,
523     serial_read,
524     serial_write,
525     serial_device_control
526     };

  ViewVC Help
Powered by ViewVC 1.1.26