/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 545 by stargo, Mon Nov 3 20:19:01 2003 UTC revision 588 by n-ki, Thu Jan 29 11:47:14 2004 UTC
# Line 1  Line 1 
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
# Line 52  Line 53 
53  #define ODD_PARITY                      1  #define ODD_PARITY                      1
54  #define EVEN_PARITY                     2  #define EVEN_PARITY                     2
55    
56  int serial_fd;  extern RDPDR_DEVICE g_rdpdr_device[];
 struct termios termios;  
57    
58  int dtr;  SERIAL_DEVICE *
59  uint32 baud_rate;  get_serial_info(HANDLE handle)
60  uint32 queue_in_size, queue_out_size;  {
61  uint32 wait_mask;          int index;
 uint8 stop_bits, parity, word_length;  
62    
63  static BOOL          for (index = 0; index < RDPDR_MAX_DEVICES; index++)
64  get_termios(void)          {
65                    if (handle == g_rdpdr_device[index].handle)
66                            return (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
67            }
68            return NULL;
69    }
70    
71    BOOL
72    get_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd)
73  {  {
74          speed_t speed;          speed_t speed;
75            struct termios *ptermios;
76    
77            ptermios = pser_inf->ptermios;
78    
79          if (tcgetattr(serial_fd, &termios) == -1)          if (tcgetattr(serial_fd, ptermios) == -1)
80                  return False;                  return False;
81    
82          speed = cfgetispeed(&termios);          speed = cfgetispeed(ptermios);
83          switch (speed)          switch (speed)
84          {          {
85  #ifdef B75  #ifdef B75
86                  case B75:                  case B75:
87                          baud_rate = 75;                          pser_inf->baud_rate = 75;
88                          break;                          break;
89  #endif  #endif
90  #ifdef B110  #ifdef B110
91                  case B110:                  case B110:
92                          baud_rate = 110;                          pser_inf->baud_rate = 110;
93                          break;                          break;
94  #endif  #endif
95  #ifdef B134  #ifdef B134
96                  case B134:                  case B134:
97                          baud_rate = 134;                          pser_inf->baud_rate = 134;
98                          break;                          break;
99  #endif  #endif
100  #ifdef B150  #ifdef B150
101                  case B150:                  case B150:
102                          baud_rate = 150;                          pser_inf->baud_rate = 150;
103                          break;                          break;
104  #endif  #endif
105  #ifdef B300  #ifdef B300
106                  case B300:                  case B300:
107                          baud_rate = 300;                          pser_inf->baud_rate = 300;
108                          break;                          break;
109  #endif  #endif
110  #ifdef B600  #ifdef B600
111                  case B600:                  case B600:
112                          baud_rate = 600;                          pser_inf->baud_rate = 600;
113                          break;                          break;
114  #endif  #endif
115  #ifdef B1200  #ifdef B1200
116                  case B1200:                  case B1200:
117                          baud_rate = 1200;                          pser_inf->baud_rate = 1200;
118                          break;                          break;
119  #endif  #endif
120  #ifdef B1800  #ifdef B1800
121                  case B1800:                  case B1800:
122                          baud_rate = 1800;                          pser_inf->baud_rate = 1800;
123                          break;                          break;
124  #endif  #endif
125  #ifdef B2400  #ifdef B2400
126                  case B2400:                  case B2400:
127                          baud_rate = 2400;                          pser_inf->baud_rate = 2400;
128                          break;                          break;
129  #endif  #endif
130  #ifdef B4800  #ifdef B4800
131                  case B4800:                  case B4800:
132                          baud_rate = 4800;                          pser_inf->baud_rate = 4800;
133                          break;                          break;
134  #endif  #endif
135  #ifdef B9600  #ifdef B9600
136                  case B9600:                  case B9600:
137                          baud_rate = 9600;                          pser_inf->baud_rate = 9600;
138                          break;                          break;
139  #endif  #endif
140  #ifdef B19200  #ifdef B19200
141                  case B19200:                  case B19200:
142                          baud_rate = 19200;                          pser_inf->baud_rate = 19200;
143                          break;                          break;
144  #endif  #endif
145  #ifdef B38400  #ifdef B38400
146                  case B38400:                  case B38400:
147                          baud_rate = 38400;                          pser_inf->baud_rate = 38400;
148                          break;                          break;
149  #endif  #endif
150  #ifdef B57600  #ifdef B57600
151                  case B57600:                  case B57600:
152                          baud_rate = 57600;                          pser_inf->baud_rate = 57600;
153                          break;                          break;
154  #endif  #endif
155  #ifdef B115200  #ifdef B115200
156                  case B115200:                  case B115200:
157                          baud_rate = 115200;                          pser_inf->baud_rate = 115200;
158                          break;                          break;
159  #endif  #endif
160                  default:                  default:
161                          baud_rate = 0;                          pser_inf->baud_rate = 0;
162                          break;                          break;
163          }          }
164    
165          speed = cfgetospeed(&termios);          speed = cfgetospeed(ptermios);
166          dtr = (speed == B0) ? 0 : 1;          pser_inf->dtr = (speed == B0) ? 0 : 1;
167    
168          stop_bits = (termios.c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BITS_1;          pser_inf->stop_bits = (ptermios->c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BITS_1;
169          parity = (termios.          pser_inf->parity =
170                    c_cflag & PARENB) ? ((termios.                  (ptermios->
171                                          c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY;                   c_cflag & PARENB) ? ((ptermios->
172          switch (termios.c_cflag & CSIZE)                                         c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY;
173            switch (ptermios->c_cflag & CSIZE)
174          {          {
175                  case CS5:                  case CS5:
176                          word_length = 5;                          pser_inf->word_length = 5;
177                          break;                          break;
178                  case CS6:                  case CS6:
179                          word_length = 6;                          pser_inf->word_length = 6;
180                          break;                          break;
181                  case CS7:                  case CS7:
182                          word_length = 7;                          pser_inf->word_length = 7;
183                          break;                          break;
184                  default:                  default:
185                          word_length = 8;                          pser_inf->word_length = 8;
186                          break;                          break;
187          }          }
188    
# Line 179  get_termios(void) Line 190  get_termios(void)
190  }  }
191    
192  static void  static void
193  set_termios(void)  set_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd)
194  {  {
195          speed_t speed;          speed_t speed;
196    
197          switch (baud_rate)          struct termios *ptermios;
198    
199            ptermios = pser_inf->ptermios;
200    
201    
202            switch (pser_inf->baud_rate)
203          {          {
204  #ifdef B75  #ifdef B75
205                  case 75:                  case 75:
# Line 267  set_termios(void) Line 283  set_termios(void)
283    
284          /* on systems with separate ispeed and ospeed, we can remember the speed          /* on systems with separate ispeed and ospeed, we can remember the speed
285             in ispeed while changing DTR with ospeed */             in ispeed while changing DTR with ospeed */
286          cfsetispeed(&termios, speed);          cfsetispeed(pser_inf->ptermios, speed);
287          cfsetospeed(&termios, dtr ? speed : 0);          cfsetospeed(pser_inf->ptermios, pser_inf->dtr ? speed : 0);
288    
289          termios.c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE);          ptermios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE);
290          switch (stop_bits)          switch (pser_inf->stop_bits)
291          {          {
292                  case STOP_BITS_2:                  case STOP_BITS_2:
293                          termios.c_cflag |= CSTOPB;                          ptermios->c_cflag |= CSTOPB;
294                          break;                          break;
295          }          }
296          switch (parity)          switch (pser_inf->parity)
297          {          {
298                  case EVEN_PARITY:                  case EVEN_PARITY:
299                          termios.c_cflag |= PARENB;                          ptermios->c_cflag |= PARENB;
300                          break;                          break;
301                  case ODD_PARITY:                  case ODD_PARITY:
302                          termios.c_cflag |= PARENB | PARODD;                          ptermios->c_cflag |= PARENB | PARODD;
303                          break;                          break;
304          }          }
305          switch (word_length)          switch (pser_inf->word_length)
306          {          {
307                  case 5:                  case 5:
308                          termios.c_cflag |= CS5;                          ptermios->c_cflag |= CS5;
309                          break;                          break;
310                  case 6:                  case 6:
311                          termios.c_cflag |= CS6;                          ptermios->c_cflag |= CS6;
312                          break;                          break;
313                  case 7:                  case 7:
314                          termios.c_cflag |= CS7;                          ptermios->c_cflag |= CS7;
315                          break;                          break;
316                  default:                  default:
317                          termios.c_cflag |= CS8;                          ptermios->c_cflag |= CS8;
318                          break;                          break;
319          }          }
320    
321          tcsetattr(serial_fd, TCSANOW, &termios);          tcsetattr(serial_fd, TCSANOW, ptermios);
322  }  }
323    
324  static NTSTATUS  /* Enumeration of devices from rdesktop.c        */
325  serial_create(HANDLE * handle)  /* returns numer of units found and initialized. */
326    /* optarg looks like ':com1=/dev/ttyS0'           */
327    /* when it arrives to this function.              */
328    /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */
329    int
330    serial_enum_devices(int *id, char *optarg)
331  {  {
332          /* XXX do we have to handle concurrent open attempts? */          SERIAL_DEVICE *pser_inf;
333          serial_fd = open("/dev/ttyS0", O_RDWR);  
334            char *pos = optarg;
335            char *pos2;
336            int count = 0;
337    
338            // skip the first colon
339            optarg++;
340            while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
341            {
342                    // Init data structures for device
343                    pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE));
344                    pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios));
345                    pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios));
346    
347                    pos2 = next_arg(optarg, '=');
348                    strcpy(g_rdpdr_device[*id].name, optarg);
349    
350                    toupper_str(g_rdpdr_device[*id].name);
351    
352                    g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);
353                    strcpy(g_rdpdr_device[*id].local_path, pos2);
354                    printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name,
355                           g_rdpdr_device[*id].local_path);
356                    // set device type
357                    g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL;
358                    g_rdpdr_device[*id].pdevice_data = (void *) pser_inf;
359                    count++;
360                    (*id)++;
361    
362                    optarg = pos;
363            }
364            return count;
365    }
366    
367    NTSTATUS
368    serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition,
369                  uint32 flags_and_attributes, char *filename, HANDLE * handle)
370    {
371            HANDLE serial_fd;
372            SERIAL_DEVICE *pser_inf;
373            struct termios *ptermios;
374    
375            pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;
376            ptermios = pser_inf->ptermios;
377            serial_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR | O_NOCTTY);
378    
379          if (serial_fd == -1)          if (serial_fd == -1)
380            {
381                    perror("open");
382                  return STATUS_ACCESS_DENIED;                  return STATUS_ACCESS_DENIED;
383            }
384    
385          if (!get_termios())          if (!get_termios(pser_inf, serial_fd))
386                  return STATUS_ACCESS_DENIED;                  return STATUS_ACCESS_DENIED;
387    
388          *handle = 0;          // Store handle for later use
389            g_rdpdr_device[device_id].handle = serial_fd;
390    
391            /* some sane information */
392            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);
393            printf("INFO: use stty to change settings\n");
394    
395    /*      ptermios->c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
396            ptermios->c_cflag |= CREAD;
397            ptermios->c_lflag |= ICANON;
398            ptermios->c_iflag = IGNPAR | ICRNL;
399    
400            tcsetattr(serial_fd, TCSANOW, ptermios);
401    */
402            *handle = serial_fd;
403          return STATUS_SUCCESS;          return STATUS_SUCCESS;
404  }  }
405    
406  static NTSTATUS  static NTSTATUS
407  serial_close(HANDLE handle)  serial_close(HANDLE handle)
408  {  {
409          close(serial_fd);          g_rdpdr_device[get_device_index(handle)].handle = 0;
410            close(handle);
411          return STATUS_SUCCESS;          return STATUS_SUCCESS;
412  }  }
413    
414  static NTSTATUS  NTSTATUS
415  serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 * result)  serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
416  {  {
417          *result = read(serial_fd, data, length);          long timeout;
418            SERIAL_DEVICE *pser_inf;
419            struct termios *ptermios;
420    
421    //      timeout = 90;
422            pser_inf = get_serial_info(handle);
423            ptermios = pser_inf->ptermios;
424    
425    #if 0
426            // Set timeouts kind of like the windows serial timeout parameters. Multiply timeout
427            // with requested read size
428            if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant)
429            {
430                    timeout =
431                            (pser_inf->read_total_timeout_multiplier * length +
432                             pser_inf->read_total_timeout_constant + 99) / 100;
433            }
434            else if (pser_inf->read_interval_timeout)
435            {
436                    timeout = (pser_inf->read_interval_timeout * length + 99) / 100;
437            }
438    
439            // If a timeout is set, do a blocking read, which times out after some time.
440            // It will make rdesktop less responsive, but it will improve serial performance, by not
441            // reading one character at a time.
442            if (timeout == 0)
443            {
444                    ptermios->c_cc[VTIME] = 0;
445                    ptermios->c_cc[VMIN] = 0;
446            }
447            else
448            {
449                    ptermios->c_cc[VTIME] = timeout;
450                    ptermios->c_cc[VMIN] = 1;
451            }
452            tcsetattr(handle, TCSANOW, ptermios);
453    #endif
454            *result = read(handle, data, length);
455          return STATUS_SUCCESS;          return STATUS_SUCCESS;
456  }  }
457    
458  static NTSTATUS  NTSTATUS
459  serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 * result)  serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
460  {  {
461          *result = write(serial_fd, data, length);          *result = write(handle, data, length);
462          return STATUS_SUCCESS;          return STATUS_SUCCESS;
463  }  }
464    
# Line 346  serial_device_control(HANDLE handle, uin Line 467  serial_device_control(HANDLE handle, uin
467  {  {
468          uint32 result;          uint32 result;
469          uint8 immediate;          uint8 immediate;
470            SERIAL_DEVICE *pser_inf;
471            struct termios *ptermios;
472    
473          if ((request >> 16) != FILE_DEVICE_SERIAL_PORT)          if ((request >> 16) != FILE_DEVICE_SERIAL_PORT)
474                  return STATUS_INVALID_PARAMETER;                  return STATUS_INVALID_PARAMETER;
475    
476            pser_inf = get_serial_info(handle);
477            ptermios = pser_inf->ptermios;
478    
479          /* extract operation */          /* extract operation */
480          request >>= 2;          request >>= 2;
481          request &= 0xfff;          request &= 0xfff;
# Line 359  serial_device_control(HANDLE handle, uin Line 485  serial_device_control(HANDLE handle, uin
485          switch (request)          switch (request)
486          {          {
487                  case SERIAL_SET_BAUD_RATE:                  case SERIAL_SET_BAUD_RATE:
488                          in_uint32_le(in, baud_rate);                          in_uint32_le(in, pser_inf->baud_rate);
489                          set_termios();                          set_termios(pser_inf, handle);
490                          break;                          break;
491                  case SERIAL_GET_BAUD_RATE:                  case SERIAL_GET_BAUD_RATE:
492                          out_uint32_le(out, baud_rate);                          out_uint32_le(out, pser_inf->baud_rate);
493                          break;                          break;
494                  case SERIAL_SET_QUEUE_SIZE:                  case SERIAL_SET_QUEUE_SIZE:
495                          in_uint32_le(in, queue_in_size);                          in_uint32_le(in, pser_inf->queue_in_size);
496                          in_uint32_le(in, queue_out_size);                          in_uint32_le(in, pser_inf->queue_out_size);
497                          break;                          break;
498                  case SERIAL_SET_LINE_CONTROL:                  case SERIAL_SET_LINE_CONTROL:
499                          in_uint8(in, stop_bits);                          in_uint8(in, pser_inf->stop_bits);
500                          in_uint8(in, parity);                          in_uint8(in, pser_inf->parity);
501                          in_uint8(in, word_length);                          in_uint8(in, pser_inf->word_length);
502                          set_termios();                          set_termios(pser_inf, handle);
503                          break;                          break;
504                  case SERIAL_GET_LINE_CONTROL:                  case SERIAL_GET_LINE_CONTROL:
505                          out_uint8(out, stop_bits);                          out_uint8(out, pser_inf->stop_bits);
506                          out_uint8(out, parity);                          out_uint8(out, pser_inf->parity);
507                          out_uint8(out, word_length);                          out_uint8(out, pser_inf->word_length);
508                          break;                          break;
509                  case SERIAL_IMMEDIATE_CHAR:                  case SERIAL_IMMEDIATE_CHAR:
510                          in_uint8(in, immediate);                          in_uint8(in, immediate);
511                          serial_write(handle, &immediate, 1, &result);                          serial_write(handle, &immediate, 1, 0, &result);
512                          break;                          break;
513                  case SERIAL_CONFIG_SIZE:                  case SERIAL_CONFIG_SIZE:
514                          out_uint32_le(out, 0);                          out_uint32_le(out, 0);
# Line 409  serial_device_control(HANDLE handle, uin Line 535  serial_device_control(HANDLE handle, uin
535                          out_uint8s(out, 20);                          out_uint8s(out, 20);
536                          break;                          break;
537                  case SERIAL_GET_WAIT_MASK:                  case SERIAL_GET_WAIT_MASK:
538                          out_uint32(out, wait_mask);                          out_uint32(out, pser_inf->wait_mask);
539                          break;                          break;
540                  case SERIAL_SET_WAIT_MASK:                  case SERIAL_SET_WAIT_MASK:
541                          in_uint32(in, wait_mask);                          in_uint32(in, pser_inf->wait_mask);
542                          break;                          break;
543                  case SERIAL_SET_DTR:                  case SERIAL_SET_DTR:
544                          dtr = 1;                          pser_inf->dtr = 1;
545                          set_termios();                          set_termios(pser_inf, handle);
546                          break;                          break;
547                  case SERIAL_CLR_DTR:                  case SERIAL_CLR_DTR:
548                          dtr = 0;                          pser_inf->dtr = 0;
549                          set_termios();                          set_termios(pser_inf, handle);
550                          break;                          break;
551  #if 0  #if 0
552                  case SERIAL_WAIT_ON_MASK:                  case SERIAL_WAIT_ON_MASK:
# Line 430  serial_device_control(HANDLE handle, uin Line 556  serial_device_control(HANDLE handle, uin
556                          tcsendbreak(serial_fd, 0);                          tcsendbreak(serial_fd, 0);
557                          break;                          break;
558                  case SERIAL_PURGE:                  case SERIAL_PURGE:
559                          in_uint32(purge_mask);  
560                          /* tcflush */                          printf("SERIAL_PURGE\n");
561                            in_uint32(in, purge_mask);
562                            if (purge_mask & 0x04)
563                                    flush_mask |= TCOFLUSH;
564                            if (purge_mask & 0x08)
565                                    flush_mask |= TCIFLUSH;
566                            if (flush_mask != 0)
567                                    tcflush(handle, flush_mask);
568                            if (purge_mask & 0x01)
569                                    rdpdr_abort_io(handle, 4, STATUS_CANCELLED);
570                            if (purge_mask & 0x02)
571                                    rdpdr_abort_io(handle, 3, STATUS_CANCELLED);
572                          break;                          break;
573    
574                  case SERIAL_RESET_DEVICE:                  case SERIAL_RESET_DEVICE:
575                  case SERIAL_SET_BREAK_OFF:                  case SERIAL_SET_BREAK_OFF:
576                  case SERIAL_SET_RTS:                  case SERIAL_SET_RTS:
# Line 451  serial_device_control(HANDLE handle, uin Line 589  serial_device_control(HANDLE handle, uin
589          return STATUS_SUCCESS;          return STATUS_SUCCESS;
590  }  }
591    
592    /* Read timeout for a given file descripter (device) when adding fd's to select() */
593    BOOL
594    serial_get_timeout(HANDLE handle, uint32 length, uint32 * timeout, uint32 * itv_timeout)
595    {
596            int index;
597            SERIAL_DEVICE *pser_inf;
598    
599            index = get_device_index(handle);
600    
601            if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)
602            {
603                    return False;
604            }
605    
606            pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
607    
608            *timeout =
609                    pser_inf->read_total_timeout_multiplier * length +
610                    pser_inf->read_total_timeout_constant;
611            *itv_timeout = pser_inf->read_interval_timeout;
612            return True;
613    }
614    
615  DEVICE_FNS serial_fns = {  DEVICE_FNS serial_fns = {
616          serial_create,          serial_create,
617          serial_close,          serial_close,

Legend:
Removed from v.545  
changed lines
  Added in v.588

  ViewVC Help
Powered by ViewVC 1.1.26