/[rdesktop]/jpeg/rdesktop/trunk/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 /jpeg/rdesktop/trunk/serial.c

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

revision 578 by n-ki, Fri Jan 23 06:56:42 2004 UTC revision 580 by astrand, Fri Jan 23 08:35:52 2004 UTC
# Line 53  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[];  extern RDPDR_DEVICE g_rdpdr_device[];
57    
58  int serial_fd;  int serial_fd;
59  struct termios termios;  struct termios termios;
# Line 64  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  SERIAL_DEVICE  SERIAL_DEVICE * get_serial_info(HANDLE handle)
 *get_serial_info(HANDLE handle)  
68  {  {
69          int     index;          int index;
70    
71          for (index = 0; index < RDPDR_MAX_DEVICES; index++)          for (index = 0; index < RDPDR_MAX_DEVICES; index++)
72          {          {
73                  if (handle == g_rdpdr_device[index].handle)                  if (handle == g_rdpdr_device[index].handle)
74                          return (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;                          return (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
75          }          }
76          return NULL;          return NULL;
77  }  }
78    
79  BOOL  BOOL
80  get_termios(SERIAL_DEVICE *pser_inf, HANDLE serial_fd)  get_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd)
81  {  {
82          speed_t         speed;          speed_t speed;
83          struct termios  *ptermios;          struct termios *ptermios;
84    
85          ptermios        = pser_inf->ptermios;          ptermios = pser_inf->ptermios;
86    
87          if (tcgetattr(serial_fd, ptermios) == -1)          if (tcgetattr(serial_fd, ptermios) == -1)
88                  return False;                  return False;
89    
90          speed = cfgetispeed(ptermios);          speed = cfgetispeed(ptermios);
91          switch (speed)          switch (speed)
92          {          {
93  #ifdef B75  #ifdef B75
94                  case B75:       pser_inf->baud_rate = 75; break;                  case B75:
95                            pser_inf->baud_rate = 75;
96                            break;
97  #endif  #endif
98  #ifdef B110  #ifdef B110
99                  case B110:      pser_inf->baud_rate = 110; break;                  case B110:
100                            pser_inf->baud_rate = 110;
101                            break;
102  #endif  #endif
103  #ifdef B134  #ifdef B134
104                  case B134:      pser_inf->baud_rate = 134; break;                  case B134:
105                            pser_inf->baud_rate = 134;
106                            break;
107  #endif  #endif
108  #ifdef B150  #ifdef B150
109                  case B150:      pser_inf->baud_rate = 150; break;                  case B150:
110                            pser_inf->baud_rate = 150;
111                            break;
112  #endif  #endif
113  #ifdef B300  #ifdef B300
114                  case B300:      pser_inf->baud_rate = 300; break;                  case B300:
115                            pser_inf->baud_rate = 300;
116                            break;
117  #endif  #endif
118  #ifdef B600  #ifdef B600
119                  case B600:      pser_inf->baud_rate = 600; break;                  case B600:
120                            pser_inf->baud_rate = 600;
121                            break;
122  #endif  #endif
123  #ifdef B1200  #ifdef B1200
124                  case B1200:     pser_inf->baud_rate = 1200; break;                  case B1200:
125                            pser_inf->baud_rate = 1200;
126                            break;
127  #endif  #endif
128  #ifdef B1800  #ifdef B1800
129                  case B1800:     pser_inf->baud_rate = 1800; break;                  case B1800:
130                            pser_inf->baud_rate = 1800;
131                            break;
132  #endif  #endif
133  #ifdef B2400  #ifdef B2400
134                  case B2400:     pser_inf->baud_rate = 2400; break;                  case B2400:
135                            pser_inf->baud_rate = 2400;
136                            break;
137  #endif  #endif
138  #ifdef B4800  #ifdef B4800
139                  case B4800:     pser_inf->baud_rate = 4800; break;                  case B4800:
140                            pser_inf->baud_rate = 4800;
141                            break;
142  #endif  #endif
143  #ifdef B9600  #ifdef B9600
144                  case B9600:     pser_inf->baud_rate = 9600; break;                  case B9600:
145                            pser_inf->baud_rate = 9600;
146                            break;
147  #endif  #endif
148  #ifdef B19200  #ifdef B19200
149                  case B19200:    pser_inf->baud_rate = 19200; break;                  case B19200:
150                            pser_inf->baud_rate = 19200;
151                            break;
152  #endif  #endif
153  #ifdef B38400  #ifdef B38400
154                  case B38400:    pser_inf->baud_rate = 38400; break;                  case B38400:
155                            pser_inf->baud_rate = 38400;
156                            break;
157  #endif  #endif
158  #ifdef B57600  #ifdef B57600
159                  case B57600:    pser_inf->baud_rate = 57600; break;                  case B57600:
160                            pser_inf->baud_rate = 57600;
161                            break;
162  #endif  #endif
163  #ifdef B115200  #ifdef B115200
164                  case B115200:   pser_inf->baud_rate = 115200; break;                  case B115200:
165                            pser_inf->baud_rate = 115200;
166                            break;
167  #endif  #endif
168                  default:        pser_inf->baud_rate = 0; break;                  default:
169          }                          pser_inf->baud_rate = 0;
170                            break;
171            }
172    
173          speed = cfgetospeed(ptermios);          speed = cfgetospeed(ptermios);
174          pser_inf->dtr = (speed == B0) ? 0 : 1;          pser_inf->dtr = (speed == B0) ? 0 : 1;
175    
176          pser_inf->stop_bits = (ptermios->c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BITS_1;          pser_inf->stop_bits = (ptermios->c_cflag & CSTOPB) ? STOP_BITS_2 : STOP_BITS_1;
177          pser_inf->parity = (ptermios->c_cflag & PARENB) ? ((ptermios->c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY;          pser_inf->parity =
178          switch (ptermios->c_cflag & CSIZE)                  (ptermios->
179          {                   c_cflag & PARENB) ? ((ptermios->
180                  case CS5: pser_inf->word_length = 5; break;                                         c_cflag & PARODD) ? ODD_PARITY : EVEN_PARITY) : NO_PARITY;
181                  case CS6: pser_inf->word_length = 6; break;          switch (ptermios->c_cflag & CSIZE)
182                  case CS7: pser_inf->word_length = 7; break;          {
183                  default:  pser_inf->word_length = 8; break;                  case CS5:
184          }                          pser_inf->word_length = 5;
185                            break;
186                    case CS6:
187                            pser_inf->word_length = 6;
188                            break;
189                    case CS7:
190                            pser_inf->word_length = 7;
191                            break;
192                    default:
193                            pser_inf->word_length = 8;
194                            break;
195            }
196    
197          return True;          return True;
198  }  }
199    
200  static void  static void
# Line 163  set_termios(void) Line 205  set_termios(void)
205          switch (baud_rate)          switch (baud_rate)
206          {          {
207  #ifdef B75  #ifdef B75
208                  case 75:        speed = B75;break;                  case 75:
209                            speed = B75;
210                            break;
211  #endif  #endif
212  #ifdef B110  #ifdef B110
213                  case 110:       speed = B110;break;                  case 110:
214                            speed = B110;
215                            break;
216  #endif  #endif
217  #ifdef B134  #ifdef B134
218                  case 134:       speed = B134;break;                  case 134:
219                            speed = B134;
220                            break;
221  #endif  #endif
222  #ifdef B150  #ifdef B150
223                  case 150:       speed = B150;break;                  case 150:
224                            speed = B150;
225                            break;
226  #endif  #endif
227  #ifdef B300  #ifdef B300
228                  case 300:       speed = B300;break;                  case 300:
229                            speed = B300;
230                            break;
231  #endif  #endif
232  #ifdef B600  #ifdef B600
233                  case 600:       speed = B600;break;                  case 600:
234                            speed = B600;
235                            break;
236  #endif  #endif
237  #ifdef B1200  #ifdef B1200
238                  case 1200:      speed = B1200;break;                  case 1200:
239                            speed = B1200;
240                            break;
241  #endif  #endif
242  #ifdef B1800  #ifdef B1800
243                  case 1800:      speed = B1800;break;                  case 1800:
244                            speed = B1800;
245                            break;
246  #endif  #endif
247  #ifdef B2400  #ifdef B2400
248                  case 2400:      speed = B2400;break;                  case 2400:
249                            speed = B2400;
250                            break;
251  #endif  #endif
252  #ifdef B4800  #ifdef B4800
253                  case 4800:      speed = B4800;break;                  case 4800:
254                            speed = B4800;
255                            break;
256  #endif  #endif
257  #ifdef B9600  #ifdef B9600
258                  case 9600:      speed = B9600;break;                  case 9600:
259                            speed = B9600;
260                            break;
261  #endif  #endif
262  #ifdef B19200  #ifdef B19200
263                  case 19200:     speed = B19200;break;                  case 19200:
264                            speed = B19200;
265                            break;
266  #endif  #endif
267  #ifdef B38400  #ifdef B38400
268                  case 38400:     speed = B38400;break;                  case 38400:
269                            speed = B38400;
270                            break;
271  #endif  #endif
272  #ifdef B57600  #ifdef B57600
273                  case 57600:     speed = B57600;break;                  case 57600:
274                            speed = B57600;
275                            break;
276  #endif  #endif
277  #ifdef B115200  #ifdef B115200
278                  case 115200:    speed = B115200;break;                  case 115200:
279                            speed = B115200;
280                            break;
281  #endif  #endif
282                  default:        speed = B0;break;                  default:
283                            speed = B0;
284                            break;
285          }          }
286    
287          /* on systems with separate ispeed and ospeed, we can remember the speed          /* on systems with separate ispeed and ospeed, we can remember the speed
# Line 256  set_termios(void) Line 330  set_termios(void)
330  /* when it arrives to this function.              */  /* when it arrives to this function.              */
331  /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */  /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */
332  int  int
333  serial_enum_devices(int *id, char* optarg)  serial_enum_devices(int *id, char *optarg)
334  {  {
335          SERIAL_DEVICE* pser_inf;          SERIAL_DEVICE *pser_inf;
336    
337          char *pos = optarg;          char *pos = optarg;
338          char *pos2;          char *pos2;
# Line 280  serial_enum_devices(int *id, char* optar Line 354  serial_enum_devices(int *id, char* optar
354    
355                  g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);                  g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);
356                  strcpy(g_rdpdr_device[*id].local_path, pos2);                  strcpy(g_rdpdr_device[*id].local_path, pos2);
357                  printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name, g_rdpdr_device[*id].local_path);                  printf("SERIAL %s to %s\n", g_rdpdr_device[*id].name,
358                           g_rdpdr_device[*id].local_path);
359                  // set device type                  // set device type
360                  g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL;                  g_rdpdr_device[*id].device_type = DEVICE_TYPE_SERIAL;
361                  g_rdpdr_device[*id].pdevice_data = (void *) pser_inf;                  g_rdpdr_device[*id].pdevice_data = (void *) pser_inf;
# Line 293  serial_enum_devices(int *id, char* optar Line 368  serial_enum_devices(int *id, char* optar
368  }  }
369    
370  NTSTATUS  NTSTATUS
371  serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition, uint32 flags_and_attributes, char *filename, HANDLE *handle)  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;          HANDLE serial_fd;
375          SERIAL_DEVICE   *pser_inf;          SERIAL_DEVICE *pser_inf;
376          struct termios  *ptermios;          struct termios *ptermios;
377    
378          pser_inf        = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;          pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;
379          ptermios        = pser_inf->ptermios;          ptermios = pser_inf->ptermios;
380          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);
381    
382          if (serial_fd == -1)          if (serial_fd == -1)
383                  return STATUS_ACCESS_DENIED;                  return STATUS_ACCESS_DENIED;
384    
385          if (!get_termios(pser_inf, serial_fd ))          if (!get_termios(pser_inf, serial_fd))
386                  return STATUS_ACCESS_DENIED;                  return STATUS_ACCESS_DENIED;
387    
388          // Store handle for later use          // Store handle for later use
389          g_rdpdr_device[device_id].handle = serial_fd;          g_rdpdr_device[device_id].handle = serial_fd;
390    
391          /* some sane information */          /* 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",          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                  g_rdpdr_device[device_id].name, g_rdpdr_device[device_id].local_path,          printf("INFO: use stty to change settings\n");
                 pser_inf->baud_rate, pser_inf->stop_bits, pser_inf->parity, pser_inf->word_length, pser_inf->dtr );  
         printf("INFO: use stty to change settings\n" );  
394    
395          //tcgetattr(serial_fd, pser_inf->ptermios);          //tcgetattr(serial_fd, pser_inf->ptermios);
396    
397           *handle = serial_fd;          *handle = serial_fd;
398          return STATUS_SUCCESS;          return STATUS_SUCCESS;
399  }  }
400    
401  static NTSTATUS  static NTSTATUS
# Line 332  serial_close(HANDLE handle) Line 406  serial_close(HANDLE handle)
406  }  }
407    
408  NTSTATUS  NTSTATUS
409  serial_read(HANDLE handle, uint8 *data, uint32 length, uint32 offset, uint32 *result)  serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
410  {  {
411          long            timeout;          long timeout;
412          SERIAL_DEVICE   *pser_inf;          SERIAL_DEVICE *pser_inf;
413          struct termios  *ptermios;          struct termios *ptermios;
414    
415          timeout         = 0;          timeout = 0;
416          pser_inf        = get_serial_info(handle);          pser_inf = get_serial_info(handle);
417          ptermios        = pser_inf->ptermios;          ptermios = pser_inf->ptermios;
418    
419          // Set timeouts kind of like the windows serial timeout parameters. Multiply timeout          // Set timeouts kind of like the windows serial timeout parameters. Multiply timeout
420          // with requested read size          // with requested read size
421          if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant)          if (pser_inf->read_total_timeout_multiplier | pser_inf->read_total_timeout_constant)
422          {          {
423                  timeout = (pser_inf->read_total_timeout_multiplier * length + pser_inf->read_total_timeout_constant + 99) / 100;                  timeout =
424          }                          (pser_inf->read_total_timeout_multiplier * length +
425          else if (pser_inf->read_interval_timeout)                           pser_inf->read_total_timeout_constant + 99) / 100;
426          {          }
427                  timeout = (pser_inf->read_interval_timeout * length + 99) / 100;          else if (pser_inf->read_interval_timeout)
428          }          {
429                    timeout = (pser_inf->read_interval_timeout * length + 99) / 100;
430          // If a timeout is set, do a blocking read, which times out after some time.          }
         // It will make rdesktop less responsive, but it will improve serial performance, by not  
         // reading one character at a time.  
         if (timeout == 0)  
         {  
                 ptermios->c_cc[VTIME] = 0;  
                 ptermios->c_cc[VMIN] = 0;  
         }  
         else  
         {  
                 ptermios->c_cc[VTIME] = timeout;  
                 ptermios->c_cc[VMIN] = 1;  
         }  
         tcsetattr(handle, TCSANOW, ptermios);  
431    
432          *result = read(handle, data, length);          // If a timeout is set, do a blocking read, which times out after some time.
433          return STATUS_SUCCESS;          // 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;
449  }  }
450    
451  NTSTATUS  NTSTATUS
452  serial_write(HANDLE handle, uint8 *data, uint32 length, uint32 offset, uint32 *result)  serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
453  {  {
454          *result = write(handle, data, length);          *result = write(handle, data, length);
455          return STATUS_SUCCESS;          return STATUS_SUCCESS;
456  }  }
457    
458  static NTSTATUS  static NTSTATUS
# Line 469  serial_device_control(HANDLE handle, uin Line 545  serial_device_control(HANDLE handle, uin
545                          break;                          break;
546                  case SERIAL_PURGE:                  case SERIAL_PURGE:
547    
548                          printf("SERIAL_PURGE\n");                          printf("SERIAL_PURGE\n");
549                          in_uint32(in, purge_mask);                          in_uint32(in, purge_mask);
550                          if (purge_mask & 0x04) flush_mask |= TCOFLUSH;                          if (purge_mask & 0x04)
551                          if (purge_mask & 0x08) flush_mask |= TCIFLUSH;                                  flush_mask |= TCOFLUSH;
552                          if (flush_mask != 0) tcflush(handle, flush_mask);                          if (purge_mask & 0x08)
553                          if (purge_mask & 0x01) rdpdr_abort_io(handle, 4, STATUS_CANCELLED);                                  flush_mask |= TCIFLUSH;
554                          if (purge_mask & 0x02) rdpdr_abort_io(handle, 3, STATUS_CANCELLED);                          if (flush_mask != 0)
555                          break;                                  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;
561    
562                  case SERIAL_RESET_DEVICE:                  case SERIAL_RESET_DEVICE:
563                  case SERIAL_SET_BREAK_OFF:                  case SERIAL_SET_BREAK_OFF:
# Line 498  serial_device_control(HANDLE handle, uin Line 579  serial_device_control(HANDLE handle, uin
579    
580  /* 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() */
581  BOOL  BOOL
582  serial_get_timeout(uint32 handle, uint32 length, uint32 *timeout, uint32 *itv_timeout)  serial_get_timeout(uint32 handle, uint32 length, uint32 * timeout, uint32 * itv_timeout)
583  {  {
584          int             index;          int index;
585          SERIAL_DEVICE   *pser_inf;          SERIAL_DEVICE *pser_inf;
586    
587          index = get_device_index(handle);          index = get_device_index(handle);
588    
589          if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)          if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)
590          {          {
591                  return False;                  return False;
592          }          }
593    
594          pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;          pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[index].pdevice_data;
595    
596          *timeout = pser_inf->read_total_timeout_multiplier * length + pser_inf->read_total_timeout_constant;          *timeout =
597          *itv_timeout = pser_inf->read_interval_timeout;                  pser_inf->read_total_timeout_multiplier * length +
598          return True;                  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 = {

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

  ViewVC Help
Powered by ViewVC 1.1.26