/[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 602 by stargo, Sat Feb 7 17:32:21 2004 UTC revision 755 by stargo, Tue Aug 24 21:37:25 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    #define SERIAL_PURGE_TXABORT 0x00000001
57    #define SERIAL_PURGE_RXABORT 0x00000002
58    #define SERIAL_PURGE_TXCLEAR 0x00000004
59    #define SERIAL_PURGE_RXCLEAR 0x00000008
60    
61    /* SERIAL_WAIT_ON_MASK */
62    #define SERIAL_EV_RXCHAR           0x0001       // Any Character received
63    #define SERIAL_EV_RXFLAG           0x0002       // Received certain character
64    #define SERIAL_EV_TXEMPTY          0x0004       // Transmitt Queue Empty
65    #define SERIAL_EV_CTS              0x0008       // CTS changed state
66    #define SERIAL_EV_DSR              0x0010       // DSR changed state
67    #define SERIAL_EV_RLSD             0x0020       // RLSD changed state
68    #define SERIAL_EV_BREAK            0x0040       // BREAK received
69    #define SERIAL_EV_ERR              0x0080       // Line status error occurred
70    #define SERIAL_EV_RING             0x0100       // Ring signal detected
71    #define SERIAL_EV_PERR             0x0200       // Printer error occured
72    #define SERIAL_EV_RX80FULL         0x0400       // Receive buffer is 80 percent full
73    #define SERIAL_EV_EVENT1           0x0800       // Provider specific event 1
74    #define SERIAL_EV_EVENT2           0x1000       // Provider specific event 2
75    
76    #ifndef CRTSCTS
77    #define CRTSCTS 0
78    #endif
79    
80    
81  extern RDPDR_DEVICE g_rdpdr_device[];  extern RDPDR_DEVICE g_rdpdr_device[];
82    
83  SERIAL_DEVICE *  static SERIAL_DEVICE *
84  get_serial_info(HANDLE handle)  get_serial_info(HANDLE handle)
85  {  {
86          int index;          int index;
# Line 68  get_serial_info(HANDLE handle) Line 93  get_serial_info(HANDLE handle)
93          return NULL;          return NULL;
94  }  }
95    
96  BOOL  static BOOL
97  get_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd)  get_termios(SERIAL_DEVICE * pser_inf, HANDLE serial_fd)
98  {  {
99          speed_t speed;          speed_t speed;
# Line 186  get_termios(SERIAL_DEVICE * pser_inf, HA Line 211  get_termios(SERIAL_DEVICE * pser_inf, HA
211                          break;                          break;
212          }          }
213    
214            pser_inf->rts = (ptermios->c_cflag & CRTSCTS) ? 1 : 0;
215    
216          return True;          return True;
217  }  }
218    
# Line 286  set_termios(SERIAL_DEVICE * pser_inf, HA Line 313  set_termios(SERIAL_DEVICE * pser_inf, HA
313          cfsetispeed(pser_inf->ptermios, speed);          cfsetispeed(pser_inf->ptermios, speed);
314          cfsetospeed(pser_inf->ptermios, pser_inf->dtr ? speed : 0);          cfsetospeed(pser_inf->ptermios, pser_inf->dtr ? speed : 0);
315    
316          ptermios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE);          ptermios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CSIZE | CRTSCTS);
317          switch (pser_inf->stop_bits)          switch (pser_inf->stop_bits)
318          {          {
319                  case STOP_BITS_2:                  case STOP_BITS_2:
320                          ptermios->c_cflag |= CSTOPB;                          ptermios->c_cflag |= CSTOPB;
321                          break;                          break;
322          }          }
323    
324          switch (pser_inf->parity)          switch (pser_inf->parity)
325          {          {
326                  case EVEN_PARITY:                  case EVEN_PARITY:
# Line 302  set_termios(SERIAL_DEVICE * pser_inf, HA Line 330  set_termios(SERIAL_DEVICE * pser_inf, HA
330                          ptermios->c_cflag |= PARENB | PARODD;                          ptermios->c_cflag |= PARENB | PARODD;
331                          break;                          break;
332          }          }
333    
334          switch (pser_inf->word_length)          switch (pser_inf->word_length)
335          {          {
336                  case 5:                  case 5:
# Line 318  set_termios(SERIAL_DEVICE * pser_inf, HA Line 347  set_termios(SERIAL_DEVICE * pser_inf, HA
347                          break;                          break;
348          }          }
349    
350            if (pser_inf->rts)
351                    ptermios->c_cflag |= CRTSCTS;
352    
353          tcsetattr(serial_fd, TCSANOW, ptermios);          tcsetattr(serial_fd, TCSANOW, ptermios);
354  }  }
355    
# Line 327  set_termios(SERIAL_DEVICE * pser_inf, HA Line 359  set_termios(SERIAL_DEVICE * pser_inf, HA
359  /* when it arrives to this function.              */  /* when it arrives to this function.              */
360  /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */  /* :com1=/dev/ttyS0,com2=/dev/ttyS1 */
361  int  int
362  serial_enum_devices(uint32 *id, char *optarg)  serial_enum_devices(uint32 * id, char *optarg)
363  {  {
364          SERIAL_DEVICE *pser_inf;          SERIAL_DEVICE *pser_inf;
365    
# Line 342  serial_enum_devices(uint32 *id, char *op Line 374  serial_enum_devices(uint32 *id, char *op
374                  // Init data structures for device                  // Init data structures for device
375                  pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE));                  pser_inf = (SERIAL_DEVICE *) xmalloc(sizeof(SERIAL_DEVICE));
376                  pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios));                  pser_inf->ptermios = (struct termios *) xmalloc(sizeof(struct termios));
377                    memset(pser_inf->ptermios, 0, sizeof(struct termios));
378                  pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios));                  pser_inf->pold_termios = (struct termios *) xmalloc(sizeof(struct termios));
379                    memset(pser_inf->pold_termios, 0, sizeof(struct termios));
380    
381                  pos2 = next_arg(optarg, '=');                  pos2 = next_arg(optarg, '=');
382                  strcpy(g_rdpdr_device[*id].name, optarg);                  strcpy(g_rdpdr_device[*id].name, optarg);
# Line 364  serial_enum_devices(uint32 *id, char *op Line 398  serial_enum_devices(uint32 *id, char *op
398          return count;          return count;
399  }  }
400    
401  NTSTATUS  static NTSTATUS
402  serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition,  serial_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition,
403                uint32 flags_and_attributes, char *filename, HANDLE * handle)                uint32 flags_and_attributes, char *filename, HANDLE * handle)
404  {  {
# Line 374  serial_create(uint32 device_id, uint32 a Line 408  serial_create(uint32 device_id, uint32 a
408    
409          pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;          pser_inf = (SERIAL_DEVICE *) g_rdpdr_device[device_id].pdevice_data;
410          ptermios = pser_inf->ptermios;          ptermios = pser_inf->ptermios;
411          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 | O_NONBLOCK);
412    
413          if (serial_fd == -1)          if (serial_fd == -1)
414          {          {
# Line 383  serial_create(uint32 device_id, uint32 a Line 417  serial_create(uint32 device_id, uint32 a
417          }          }
418    
419          if (!get_termios(pser_inf, serial_fd))          if (!get_termios(pser_inf, serial_fd))
420            {
421                    printf("INFO: SERIAL %s access denied\n", g_rdpdr_device[device_id].name);
422                    fflush(stdout);
423                  return STATUS_ACCESS_DENIED;                  return STATUS_ACCESS_DENIED;
424            }
425    
426          // Store handle for later use          // Store handle for later use
427          g_rdpdr_device[device_id].handle = serial_fd;          g_rdpdr_device[device_id].handle = serial_fd;
428    
429          /* some sane information */          /* some sane information */
430          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);          printf("INFO: SERIAL %s to %s\nINFO: speed %u baud, stop bits %u, parity %u, word length %u bits, dtr %u, rts %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, pser_inf->rts);
431    
432          printf("INFO: use stty to change settings\n");          printf("INFO: use stty to change settings\n");
433    
434  /*      ptermios->c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;  /*      ptermios->c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
# Line 400  serial_create(uint32 device_id, uint32 a Line 439  serial_create(uint32 device_id, uint32 a
439          tcsetattr(serial_fd, TCSANOW, ptermios);          tcsetattr(serial_fd, TCSANOW, ptermios);
440  */  */
441    
442            cfmakeraw(pser_inf->ptermios);
443          *handle = serial_fd;          *handle = serial_fd;
444    
445          /* all read and writes should be non blocking */          /* all read and writes should be non blocking */
# Line 412  serial_create(uint32 device_id, uint32 a Line 452  serial_create(uint32 device_id, uint32 a
452  static NTSTATUS  static NTSTATUS
453  serial_close(HANDLE handle)  serial_close(HANDLE handle)
454  {  {
455          g_rdpdr_device[get_device_index(handle)].handle = 0;          int i = get_device_index(handle);
456            if (i >= 0)
457                    g_rdpdr_device[i].handle = 0;
458          close(handle);          close(handle);
459          return STATUS_SUCCESS;          return STATUS_SUCCESS;
460  }  }
461    
462  NTSTATUS  static NTSTATUS
463  serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)  serial_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
464  {  {
465          long timeout;          long timeout;
# Line 459  serial_read(HANDLE handle, uint8 * data, Line 501  serial_read(HANDLE handle, uint8 * data,
501    
502          *result = read(handle, data, length);          *result = read(handle, data, length);
503    
504            //hexdump(data, *read);
505    
506          return STATUS_SUCCESS;          return STATUS_SUCCESS;
507  }  }
508    
509  NTSTATUS  static NTSTATUS
510  serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)  serial_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
511  {  {
512          *result = write(handle, data, length);          *result = write(handle, data, length);
# Line 472  serial_write(HANDLE handle, uint8 * data Line 516  serial_write(HANDLE handle, uint8 * data
516  static NTSTATUS  static NTSTATUS
517  serial_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out)  serial_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out)
518  {  {
519    #if 0
520            int flush_mask, purge_mask;
521    #endif
522          uint32 result;          uint32 result;
523          uint8 immediate;          uint8 immediate;
524          SERIAL_DEVICE *pser_inf;          SERIAL_DEVICE *pser_inf;
# Line 555  serial_device_control(HANDLE handle, uin Line 602  serial_device_control(HANDLE handle, uin
602                          pser_inf->dtr = 0;                          pser_inf->dtr = 0;
603                          set_termios(pser_inf, handle);                          set_termios(pser_inf, handle);
604                          break;                          break;
605  #if 0                  case SERIAL_SET_RTS:
606                  case SERIAL_WAIT_ON_MASK:                          pser_inf->rts = 1;
607                          /* XXX implement me */                          set_termios(pser_inf, handle);
608                          break;                          break;
609                  case SERIAL_SET_BREAK_ON:                  case SERIAL_CLR_RTS:
610                          tcsendbreak(serial_fd, 0);                          pser_inf->rts = 0;
611                            set_termios(pser_inf, handle);
612                            break;
613                    case SERIAL_GET_MODEMSTATUS:
614                            out_uint32_le(out, 0);  /* Errors */
615                            break;
616                    case SERIAL_GET_COMMSTATUS:
617                            out_uint32_le(out, 0);  /* Errors */
618                            out_uint32_le(out, 0);  /* Hold reasons */
619                            out_uint32_le(out, 0);  /* Amount in in queue */
620                            out_uint32_le(out, 0);  /* Amount in out queue */
621                            out_uint8(out, 0);      /* EofReceived */
622                            out_uint8(out, 0);      /* WaitForImmediate */
623                          break;                          break;
624    #if 0
625                  case SERIAL_PURGE:                  case SERIAL_PURGE:
   
626                          printf("SERIAL_PURGE\n");                          printf("SERIAL_PURGE\n");
627                          in_uint32(in, purge_mask);                          in_uint32(in, purge_mask);
628                          if (purge_mask & 0x04)                          if (purge_mask & 0x04)
# Line 577  serial_device_control(HANDLE handle, uin Line 636  serial_device_control(HANDLE handle, uin
636                          if (purge_mask & 0x02)                          if (purge_mask & 0x02)
637                                  rdpdr_abort_io(handle, 3, STATUS_CANCELLED);                                  rdpdr_abort_io(handle, 3, STATUS_CANCELLED);
638                          break;                          break;
639                    case SERIAL_WAIT_ON_MASK:
640                            /* XXX implement me */
641                            out_uint32_le(out, pser_inf->wait_mask);
642                            break;
643                    case SERIAL_SET_BREAK_ON:
644                            tcsendbreak(serial_fd, 0);
645                            break;
646                  case SERIAL_RESET_DEVICE:                  case SERIAL_RESET_DEVICE:
647                  case SERIAL_SET_BREAK_OFF:                  case SERIAL_SET_BREAK_OFF:
                 case SERIAL_SET_RTS:  
                 case SERIAL_CLR_RTS:  
648                  case SERIAL_SET_XOFF:                  case SERIAL_SET_XOFF:
649                  case SERIAL_SET_XON:                  case SERIAL_SET_XON:
650                          /* ignore */                          /* ignore */
651                          break;                          break;
652  #endif  #endif
   
653                  default:                  default:
654                          unimpl("SERIAL IOCTL %d\n", request);                          unimpl("SERIAL IOCTL %d\n", request);
655                          return STATUS_INVALID_PARAMETER;                          return STATUS_INVALID_PARAMETER;
# Line 604  serial_get_timeout(HANDLE handle, uint32 Line 666  serial_get_timeout(HANDLE handle, uint32
666          SERIAL_DEVICE *pser_inf;          SERIAL_DEVICE *pser_inf;
667    
668          index = get_device_index(handle);          index = get_device_index(handle);
669            if (index < 0)
670                    return True;
671    
672          if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)          if (g_rdpdr_device[index].device_type != DEVICE_TYPE_SERIAL)
673          {          {

Legend:
Removed from v.602  
changed lines
  Added in v.755

  ViewVC Help
Powered by ViewVC 1.1.26