/[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 580 by astrand, Fri Jan 23 08:35:52 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    extern RDPDR_DEVICE g_rdpdr_device[];
57    
58  int serial_fd;  int serial_fd;
59  struct termios termios;  struct termios termios;
60    
# Line 61  uint32 queue_in_size, queue_out_size; Line 64  uint32 queue_in_size, queue_out_size;
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 * get_serial_info(HANDLE handle)
68  get_termios(void)  {
69            int index;
70    
71            for (index = 0; index < RDPDR_MAX_DEVICES; index++)
72            {
73                    if (handle == g_rdpdr_device[index].handle)
74                            return (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
75            }
76            return NULL;
77    }
78    
79    BOOL
80    get_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd)
81  {  {
82          speed_t speed;          speed_t speed;
83            struct termios *ptermios;
84    
85            ptermios = pser_inf->ptermios;
86    
87          if (tcgetattr(serial_fd, &termios) == -1)          if (tcgetattr(serial_fd, ptermios) == -1)
88                  return False;                  return False;
89    
90          speed = cfgetispeed(&termios);          speed = cfgetispeed(ptermios);
91          switch (speed)          switch (speed)
92          {          {
93  #ifdef B75  #ifdef B75
94                  case B75:                  case B75:
95                          baud_rate = 75;                          pser_inf->baud_rate = 75;
96                          break;                          break;
97  #endif  #endif
98  #ifdef B110  #ifdef B110
99                  case B110:                  case B110:
100                          baud_rate = 110;                          pser_inf->baud_rate = 110;
101                          break;                          break;
102  #endif  #endif
103  #ifdef B134  #ifdef B134
104                  case B134:                  case B134:
105                          baud_rate = 134;                          pser_inf->baud_rate = 134;
106                          break;                          break;
107  #endif  #endif
108  #ifdef B150  #ifdef B150
109                  case B150:                  case B150:
110                          baud_rate = 150;                          pser_inf->baud_rate = 150;
111                          break;                          break;
112  #endif  #endif
113  #ifdef B300  #ifdef B300
114                  case B300:                  case B300:
115                          baud_rate = 300;                          pser_inf->baud_rate = 300;
116                          break;                          break;
117  #endif  #endif
118  #ifdef B600  #ifdef B600
119                  case B600:                  case B600:
120                          baud_rate = 600;                          pser_inf->baud_rate = 600;
121                          break;                          break;
122  #endif  #endif
123  #ifdef B1200  #ifdef B1200
124                  case B1200:                  case B1200:
125                          baud_rate = 1200;                          pser_inf->baud_rate = 1200;
126                          break;                          break;
127  #endif  #endif
128  #ifdef B1800  #ifdef B1800
129                  case B1800:                  case B1800:
130                          baud_rate = 1800;                          pser_inf->baud_rate = 1800;
131                          break;                          break;
132  #endif  #endif
133  #ifdef B2400  #ifdef B2400
134                  case B2400:                  case B2400:
135                          baud_rate = 2400;                          pser_inf->baud_rate = 2400;
136                          break;                          break;
137  #endif  #endif
138  #ifdef B4800  #ifdef B4800
139                  case B4800:                  case B4800:
140                          baud_rate = 4800;                          pser_inf->baud_rate = 4800;
141                          break;                          break;
142  #endif  #endif
143  #ifdef B9600  #ifdef B9600
144                  case B9600:                  case B9600:
145                          baud_rate = 9600;                          pser_inf->baud_rate = 9600;
146                          break;                          break;
147  #endif  #endif
148  #ifdef B19200  #ifdef B19200
149                  case B19200:                  case B19200:
150                          baud_rate = 19200;                          pser_inf->baud_rate = 19200;
151                          break;                          break;
152  #endif  #endif
153  #ifdef B38400  #ifdef B38400
154                  case B38400:                  case B38400:
155                          baud_rate = 38400;                          pser_inf->baud_rate = 38400;
156                          break;                          break;
157  #endif  #endif
158  #ifdef B57600  #ifdef B57600
159                  case B57600:                  case B57600:
160                          baud_rate = 57600;                          pser_inf->baud_rate = 57600;
161                          break;                          break;
162  #endif  #endif
163  #ifdef B115200  #ifdef B115200
164                  case B115200:                  case B115200:
165                          baud_rate = 115200;                          pser_inf->baud_rate = 115200;
166                          break;                          break;
167  #endif  #endif
168                  default:                  default:
169                          baud_rate = 0;                          pser_inf->baud_rate = 0;
170                          break;                          break;
171          }          }
172    
173          speed = cfgetospeed(&termios);          speed = cfgetospeed(ptermios);
174          dtr = (speed == B0) ? 0 : 1;          pser_inf->dtr = (speed == B0) ? 0 : 1;
175    
176          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;
177          parity = (termios.          pser_inf->parity =
178                    c_cflag & PARENB) ? ((termios.                  (ptermios->
179                                          c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY;                   c_cflag & PARENB) ? ((ptermios->
180          switch (termios.c_cflag & CSIZE)                                         c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY;
181            switch (ptermios->c_cflag & CSIZE)
182          {          {
183                  case CS5:                  case CS5:
184                          word_length = 5;                          pser_inf->word_length = 5;
185                          break;                          break;
186                  case CS6:                  case CS6:
187                          word_length = 6;                          pser_inf->word_length = 6;
188                          break;                          break;
189                  case CS7:                  case CS7:
190                          word_length = 7;                          pser_inf->word_length = 7;
191                          break;                          break;
192                  default:                  default:
193                          word_length = 8;                          pser_inf->word_length = 8;
194                          break;                          break;
195          }          }
196    
# Line 305  set_termios(void) Line 324  set_termios(void)
324          tcsetattr(serial_fd, TCSANOW, &termios);          tcsetattr(serial_fd, TCSANOW, &termios);
325  }  }
326    
327  static NTSTATUS  /* Enumeration of devices from rdesktop.c        */
328  serial_create(HANDLE * handle)  /* returns numer of units found and initialized. */
329    /* optarg looks like ':com1=/dev/ttyS0'           */
330    /* when it arrives to this function.              */
331    /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */
332    int
333    serial_enum_devices(int *id, char *optarg)
334  {  {
335          /* XXX do we have to handle concurrent open attempts? */          SERIAL_DEVICE *pser_inf;
336          serial_fd = open("/dev/ttyS0", O_RDWR);  
337            char *pos = optarg;
338            char *pos2;
339            int count = 0;
340    
341            // skip the first colon
342            optarg++;
343            while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
344            {
345                    // Init data structures for device
346                    pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE));
347                    pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios));
348                    pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios));
349    
350                    pos2 = next_arg(optarg, '=');
351                    strcpy(g_rdpdr_device[*id].name, optarg);
352    
353                    toupper_str(g_rdpdr_device[*id].name);
354    
355                    g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);
356                    strcpy(g_rdpdr_device[*id].local_path, pos2);
357                    printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name,
358                           g_rdpdr_device[*id].local_path);
359                    // set device type
360                    g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL;
361                    g_rdpdr_device[*id].pdevice_data = (void *) pser_inf;
362                    count++;
363                    (*id)++;
364    
365                    optarg = pos;
366            }
367            return count;
368    }
369    
370    NTSTATUS
371    serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition,
372                  uint32 flags_and_attributes, char *filename, HANDLE * handle)
373    {
374            HANDLE serial_fd;
375            SERIAL_DEVICE *pser_inf;
376            struct termios *ptermios;
377    
378            pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;
379            ptermios = pser_inf->ptermios;
380            serial_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR | O_NOCTTY);
381    
382          if (serial_fd == -1)          if (serial_fd == -1)
383                  return STATUS_ACCESS_DENIED;                  return STATUS_ACCESS_DENIED;
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            //tcgetattr(serial_fd, pser_inf->ptermios);
396    
397            *handle = serial_fd;
398          return STATUS_SUCCESS;          return STATUS_SUCCESS;
399  }  }
400    
# Line 327  serial_close(HANDLE handle) Line 405  serial_close(HANDLE handle)
405          return STATUS_SUCCESS;          return STATUS_SUCCESS;
406  }  }
407    
408  static NTSTATUS  NTSTATUS
409  serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 * result)  serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
410  {  {
411          *result = read(serial_fd, data, length);          long timeout;
412            SERIAL_DEVICE *pser_inf;
413            struct termios *ptermios;
414    
415            timeout = 0;
416            pser_inf = get_serial_info(handle);
417            ptermios = pser_inf->ptermios;
418    
419            // Set timeouts kind of like the windows serial timeout parameters. Multiply timeout
420            // with requested read size
421            if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant)
422            {
423                    timeout =
424                            (pser_inf->read_total_timeout_multiplier * length +
425                             pser_inf->read_total_timeout_constant + 99) / 100;
426            }
427            else if (pser_inf->read_interval_timeout)
428            {
429                    timeout = (pser_inf->read_interval_timeout * length + 99) / 100;
430            }
431    
432            // If a timeout is set, do a blocking read, which times out after some time.
433            // It will make rdesktop less responsive, but it will improve serial performance, by not
434            // reading one character at a time.
435            if (timeout == 0)
436            {
437                    ptermios->c_cc[VTIME] = 0;
438                    ptermios->c_cc[VMIN] = 0;
439            }
440            else
441            {
442                    ptermios->c_cc[VTIME] = timeout;
443                    ptermios->c_cc[VMIN] = 1;
444            }
445            tcsetattr(handle, TCSANOW, ptermios);
446    
447            *result = read(handle, data, length);
448          return STATUS_SUCCESS;          return STATUS_SUCCESS;
449  }  }
450    
451  static NTSTATUS  NTSTATUS
452  serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 * result)  serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
453  {  {
454          *result = write(serial_fd, data, length);          *result = write(handle, data, length);
455          return STATUS_SUCCESS;          return STATUS_SUCCESS;
456  }  }
457    
# Line 382  serial_device_control(HANDLE handle, uin Line 496  serial_device_control(HANDLE handle, uin
496                          break;                          break;
497                  case SERIAL_IMMEDIATE_CHAR:                  case SERIAL_IMMEDIATE_CHAR:
498                          in_uint8(in, immediate);                          in_uint8(in, immediate);
499                          serial_write(handle, &immediate, 1, &result);                          serial_write(handle, &immediate, 1, 0, &result);
500                          break;                          break;
501                  case SERIAL_CONFIG_SIZE:                  case SERIAL_CONFIG_SIZE:
502                          out_uint32_le(out, 0);                          out_uint32_le(out, 0);
# Line 430  serial_device_control(HANDLE handle, uin Line 544  serial_device_control(HANDLE handle, uin
544                          tcsendbreak(serial_fd, 0);                          tcsendbreak(serial_fd, 0);
545                          break;                          break;
546                  case SERIAL_PURGE:                  case SERIAL_PURGE:
547                          in_uint32(purge_mask);  
548                          /* tcflush */                          printf("SERIAL_PURGE\n");
549                            in_uint32(in, purge_mask);
550                            if (purge_mask & 0x04)
551                                    flush_mask |= TCOFLUSH;
552                            if (purge_mask & 0x08)
553                                    flush_mask |= TCIFLUSH;
554                            if (flush_mask != 0)
555                                    tcflush(handle, flush_mask);
556                            if (purge_mask & 0x01)
557                                    rdpdr_abort_io(handle, 4, STATUS_CANCELLED);
558                            if (purge_mask & 0x02)
559                                    rdpdr_abort_io(handle, 3, STATUS_CANCELLED);
560                          break;                          break;
561    
562                  case SERIAL_RESET_DEVICE:                  case SERIAL_RESET_DEVICE:
563                  case SERIAL_SET_BREAK_OFF:                  case SERIAL_SET_BREAK_OFF:
564                  case SERIAL_SET_RTS:                  case SERIAL_SET_RTS:
# Line 451  serial_device_control(HANDLE handle, uin Line 577  serial_device_control(HANDLE handle, uin
577          return STATUS_SUCCESS;          return STATUS_SUCCESS;
578  }  }
579    
580    /* Read timeout for a given file descripter (device) when adding fd's to select() */
581    BOOL
582    serial_get_timeout(uint32 handle, uint32 length, uint32 * timeout, uint32 * itv_timeout)
583    {
584            int index;
585            SERIAL_DEVICE *pser_inf;
586    
587            index = get_device_index(handle);
588    
589            if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)
590            {
591                    return False;
592            }
593    
594            pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
595    
596            *timeout =
597                    pser_inf->read_total_timeout_multiplier * length +
598                    pser_inf->read_total_timeout_constant;
599            *itv_timeout = pser_inf->read_interval_timeout;
600            return True;
601    }
602    
603  DEVICE_FNS serial_fns = {  DEVICE_FNS serial_fns = {
604          serial_create,          serial_create,
605          serial_close,          serial_close,

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

  ViewVC Help
Powered by ViewVC 1.1.26