2 * "$Id: serial.c 181 2006-06-22 20:01:18Z jlovell $"
4 * Serial port backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the printer or server.
29 * list_devices() - List all serial devices.
33 * Include necessary headers.
36 #include "backend-private.h"
39 # include <sys/modem.h>
49 # include <sys/time.h>
51 # include <sys/select.h>
53 # ifdef HAVE_SYS_IOCTL_H
54 # include <sys/ioctl.h>
55 # endif /* HAVE_SYS_IOCTL_H */
60 # ifndef INV_EPP_ECP_PLP
61 # define INV_EPP_ECP_PLP 6 /* From 6.3/6.4/6.5 sys/invent.h */
62 # define INV_ASO_SERIAL 14 /* serial portion of SGI ASO board */
63 # define INV_IOC3_DMA 16 /* DMA mode IOC3 serial */
64 # define INV_IOC3_PIO 17 /* PIO mode IOC3 serial */
65 # define INV_ISA_DMA 19 /* DMA mode ISA serial -- O2 */
66 # endif /* !INV_EPP_ECP_PLP */
71 # define CRTSCTS CNEW_RTSCTS
74 # endif /* CNEW_RTSCTS */
77 #if defined(__APPLE__)
78 # include <CoreFoundation/CoreFoundation.h>
79 # include <IOKit/IOKitLib.h>
80 # include <IOKit/serial/IOSerialKeys.h>
81 # include <IOKit/IOBSD.h>
82 #endif /* __APPLE__ */
84 #if defined(__linux) && defined(TIOCGSERIAL)
85 # include <linux/serial.h>
86 # include <linux/ioctl.h>
87 #endif /* __linux && TIOCGSERIAL */
94 void list_devices(void);
98 * 'main()' - Send a file to the printer or server.
102 * printer-uri job-id user title copies options [file]
105 int /* O - Exit status */
106 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
107 char *argv
[]) /* I - Command-line arguments */
109 char method
[255], /* Method in URI */
110 hostname
[1024], /* Hostname */
111 username
[255], /* Username info (not used) */
112 resource
[1024], /* Resource info (device and options) */
113 *options
, /* Pointer to options */
114 name
[255], /* Name of option */
115 value
[255], /* Value of option */
116 *ptr
; /* Pointer into name or value */
117 int port
; /* Port number (not used) */
118 int copies
; /* Number of copies to print */
119 int print_fd
, /* Print file */
120 device_fd
; /* Serial device */
121 int nfds
; /* Maximum file descriptor value + 1 */
122 fd_set input
, /* Input set for reading */
123 output
; /* Output set for writing */
124 ssize_t print_bytes
, /* Print bytes read */
125 bc_bytes
, /* Backchannel bytes read */
126 total_bytes
, /* Total bytes written */
127 bytes
; /* Bytes written */
128 int dtrdsr
; /* Do dtr/dsr flow control? */
129 int print_size
; /* Size of output buffer for writes */
130 char print_buffer
[8192], /* Print data buffer */
131 *print_ptr
, /* Pointer into print data buffer */
132 bc_buffer
[1024]; /* Back-channel data buffer */
133 struct termios opts
; /* Serial port options */
134 struct termios origopts
; /* Original port options */
135 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
136 struct sigaction action
; /* Actions for POSIX signals */
137 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
141 * Make sure status messages are not buffered...
144 setbuf(stderr
, NULL
);
147 * Ignore SIGPIPE signals...
151 sigset(SIGPIPE
, SIG_IGN
);
152 #elif defined(HAVE_SIGACTION)
153 memset(&action
, 0, sizeof(action
));
154 action
.sa_handler
= SIG_IGN
;
155 sigaction(SIGPIPE
, &action
, NULL
);
157 signal(SIGPIPE
, SIG_IGN
);
158 #endif /* HAVE_SIGSET */
161 * Check command-line...
167 return (CUPS_BACKEND_OK
);
169 else if (argc
< 6 || argc
> 7)
171 fputs("Usage: serial job-id user title copies options [file]\n", stderr
);
172 return (CUPS_BACKEND_FAILED
);
176 * If we have 7 arguments, print the file named on the command-line.
177 * Otherwise, send stdin instead...
188 * Try to open the print file...
191 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
193 perror("ERROR: unable to open print file");
194 return (CUPS_BACKEND_FAILED
);
197 copies
= atoi(argv
[4]);
201 * Extract the device name and options from the URI...
204 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
205 method
, sizeof(method
), username
, sizeof(username
),
206 hostname
, sizeof(hostname
), &port
,
207 resource
, sizeof(resource
));
210 * See if there are any options...
213 if ((options
= strchr(resource
, '?')) != NULL
)
216 * Yup, terminate the device name string and move to the first
217 * character of the options...
224 * Open the serial port device...
227 fputs("STATE: +connecting-to-device\n", stderr
);
231 if ((device_fd
= open(resource
, O_RDWR
| O_NOCTTY
| O_EXCL
|
234 if (getenv("CLASS") != NULL
)
237 * If the CLASS environment variable is set, the job was submitted
238 * to a class and not to a specific queue. In this case, we want
239 * to abort immediately so that the job can be requeued on the next
240 * available printer in the class.
243 fputs("INFO: Unable to open serial port, queuing on next printer in class...\n",
247 * Sleep 5 seconds to keep the job from requeuing too rapidly...
252 return (CUPS_BACKEND_FAILED
);
257 fputs("INFO: Serial port busy; will retry in 30 seconds...\n", stderr
);
262 fprintf(stderr
, "ERROR: Unable to open serial port device file \"%s\": %s\n",
263 resource
, strerror(errno
));
264 return (CUPS_BACKEND_FAILED
);
268 while (device_fd
< 0);
270 fputs("STATE: -connecting-to-device\n", stderr
);
273 * Set any options provided...
276 tcgetattr(device_fd
, &origopts
);
277 tcgetattr(device_fd
, &opts
);
279 opts
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
);
281 opts
.c_oflag
&= ~OPOST
; /* Don't post-process */
283 print_size
= 96; /* 9600 baud / 10 bits/char / 10Hz */
284 dtrdsr
= 0; /* No dtr/dsr flow control */
294 for (ptr
= name
; *options
&& *options
!= '=';)
295 if (ptr
< (name
+ sizeof(name
) - 1))
307 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
308 if (ptr
< (value
+ sizeof(value
) - 1))
312 if (*options
== '+' || *options
== '&')
319 * Process the option...
322 if (!strcasecmp(name
, "baud"))
325 * Set the baud rate...
328 print_size
= atoi(value
) / 100;
331 cfsetispeed(&opts
, atoi(value
));
332 cfsetospeed(&opts
, atoi(value
));
337 cfsetispeed(&opts
, B1200
);
338 cfsetospeed(&opts
, B1200
);
341 cfsetispeed(&opts
, B2400
);
342 cfsetospeed(&opts
, B2400
);
345 cfsetispeed(&opts
, B4800
);
346 cfsetospeed(&opts
, B4800
);
349 cfsetispeed(&opts
, B9600
);
350 cfsetospeed(&opts
, B9600
);
353 cfsetispeed(&opts
, B19200
);
354 cfsetospeed(&opts
, B19200
);
357 cfsetispeed(&opts
, B38400
);
358 cfsetospeed(&opts
, B38400
);
362 cfsetispeed(&opts
, B57600
);
363 cfsetospeed(&opts
, B57600
);
368 cfsetispeed(&opts
, B115200
);
369 cfsetospeed(&opts
, B115200
);
371 # endif /* B115200 */
374 cfsetispeed(&opts
, B230400
);
375 cfsetospeed(&opts
, B230400
);
377 # endif /* B230400 */
379 fprintf(stderr
, "WARNING: Unsupported baud rate %s!\n", value
);
382 #endif /* B19200 == 19200 */
384 else if (!strcasecmp(name
, "bits"))
387 * Set number of data bits...
393 opts
.c_cflag
&= ~CSIZE
;
395 opts
.c_cflag
|= PARENB
;
396 opts
.c_cflag
&= ~PARODD
;
399 opts
.c_cflag
&= ~CSIZE
;
401 opts
.c_cflag
&= ~PARENB
;
405 else if (!strcasecmp(name
, "parity"))
408 * Set parity checking...
411 if (!strcasecmp(value
, "even"))
413 opts
.c_cflag
|= PARENB
;
414 opts
.c_cflag
&= ~PARODD
;
416 else if (!strcasecmp(value
, "odd"))
418 opts
.c_cflag
|= PARENB
;
419 opts
.c_cflag
|= PARODD
;
421 else if (!strcasecmp(value
, "none"))
422 opts
.c_cflag
&= ~PARENB
;
423 else if (!strcasecmp(value
, "space"))
426 * Note: we only support space parity with 7 bits per character...
429 opts
.c_cflag
&= ~CSIZE
;
431 opts
.c_cflag
&= ~PARENB
;
433 else if (!strcasecmp(value
, "mark"))
436 * Note: we only support mark parity with 7 bits per character
440 opts
.c_cflag
&= ~CSIZE
;
442 opts
.c_cflag
&= ~PARENB
;
443 opts
.c_cflag
|= CSTOPB
;
446 else if (!strcasecmp(name
, "flow"))
449 * Set flow control...
452 if (!strcasecmp(value
, "none"))
454 opts
.c_iflag
&= ~(IXON
| IXOFF
);
455 opts
.c_cflag
&= ~CRTSCTS
;
457 else if (!strcasecmp(value
, "soft"))
459 opts
.c_iflag
|= IXON
| IXOFF
;
460 opts
.c_cflag
&= ~CRTSCTS
;
462 else if (!strcasecmp(value
, "hard") ||
463 !strcasecmp(value
, "rtscts"))
465 opts
.c_iflag
&= ~(IXON
| IXOFF
);
466 opts
.c_cflag
|= CRTSCTS
;
468 else if (!strcasecmp(value
, "dtrdsr"))
470 opts
.c_iflag
&= ~(IXON
| IXOFF
);
471 opts
.c_cflag
&= ~CRTSCTS
;
476 else if (!strcasecmp(name
, "stop"))
481 opts
.c_cflag
&= ~CSTOPB
;
485 opts
.c_cflag
|= CSTOPB
;
492 tcsetattr(device_fd
, TCSANOW
, &opts
);
493 fcntl(device_fd
, F_SETFL
, 0);
496 * Now that we are "connected" to the port, ignore SIGTERM so that we
497 * can finish out any page data the driver sends (e.g. to eject the
498 * current page... Only ignore SIGTERM if we are printing data from
499 * stdin (otherwise you can't cancel raw jobs...)
504 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
505 sigset(SIGTERM
, SIG_IGN
);
506 #elif defined(HAVE_SIGACTION)
507 memset(&action
, 0, sizeof(action
));
509 sigemptyset(&action
.sa_mask
);
510 action
.sa_handler
= SIG_IGN
;
511 sigaction(SIGTERM
, &action
, NULL
);
513 signal(SIGTERM
, SIG_IGN
);
514 #endif /* HAVE_SIGSET */
518 * Figure out the maximum file descriptor value to use with select()...
521 nfds
= (print_fd
> device_fd
? print_fd
: device_fd
) + 1;
524 * Finally, send the print file. Ordinarily we would just use the
525 * backendRunLoop() function, however since we need to use smaller
526 * writes and may need to do DSR/DTR flow control, we duplicate much
527 * of the code here instead...
530 if (print_size
> sizeof(print_buffer
))
531 print_size
= sizeof(print_buffer
);
541 fputs("PAGE: 1 1\n", stderr
);
542 lseek(print_fd
, 0, SEEK_SET
);
546 * Now loop until we are out of data from print_fd...
549 for (print_bytes
= 0, print_ptr
= print_buffer
;;)
552 * Use select() to determine whether we have data to copy around...
557 FD_SET(print_fd
, &input
);
558 FD_SET(device_fd
, &input
);
562 FD_SET(device_fd
, &output
);
564 if (select(nfds
, &input
, &output
, NULL
, NULL
) < 0)
565 continue; /* Ignore errors here */
568 * Check if we have back-channel data ready...
571 if (FD_ISSET(device_fd
, &input
))
573 if ((bc_bytes
= read(device_fd
, bc_buffer
, sizeof(bc_buffer
))) > 0)
576 "DEBUG: Received " CUPS_LLFMT
" bytes of back-channel data!\n",
577 CUPS_LLCAST bc_bytes
);
578 cupsBackChannelWrite(bc_buffer
, bc_bytes
, 1.0);
583 * Check if we have print data ready...
586 if (FD_ISSET(print_fd
, &input
))
588 if ((print_bytes
= read(print_fd
, print_buffer
, print_size
)) < 0)
591 * Read error - bail if we don't see EAGAIN or EINTR...
594 if (errno
!= EAGAIN
|| errno
!= EINTR
)
596 perror("ERROR: Unable to read print data");
598 tcsetattr(device_fd
, TCSADRAIN
, &origopts
);
605 return (CUPS_BACKEND_FAILED
);
610 else if (print_bytes
== 0)
613 * End of file, break out of the loop...
619 print_ptr
= print_buffer
;
623 * Check if the device is ready to receive data and we have data to
627 if (print_bytes
&& FD_ISSET(device_fd
, &output
))
632 * Check the port and sleep until DSR is set...
638 if (!ioctl(device_fd
, TIOCMGET
, &status
))
639 if (!(status
& TIOCM_DSR
))
642 * Wait for DSR to go high...
645 fputs("DEBUG: DSR is low; waiting for device...\n", stderr
);
650 * Poll every 100ms...
655 if (ioctl(device_fd
, TIOCMGET
, &status
))
658 while (!(status
& TIOCM_DSR
));
660 fputs("DEBUG: DSR is high; writing to device...\n", stderr
);
664 if ((bytes
= write(device_fd
, print_ptr
, print_bytes
)) < 0)
667 * Write error - bail if we don't see an error we can retry...
670 if (errno
!= EAGAIN
&& errno
!= EINTR
&& errno
!= ENOTTY
)
672 perror("ERROR: Unable to write print data");
674 tcsetattr(device_fd
, TCSADRAIN
, &origopts
);
681 return (CUPS_BACKEND_FAILED
);
686 fprintf(stderr
, "DEBUG: Wrote %d bytes...\n", (int)bytes
);
688 print_bytes
-= bytes
;
690 total_bytes
+= bytes
;
697 * Close the serial port and input file and return...
700 tcsetattr(device_fd
, TCSADRAIN
, &origopts
);
707 return (total_bytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
712 * 'list_devices()' - List all serial devices.
718 #if defined(__hpux) || defined(__sgi) || defined(__sun) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
719 static char *funky_hex
= "0123456789abcdefghijklmnopqrstuvwxyz";
720 /* Funky hex numbering used for some *
722 #endif /* __hpux || __sgi || __sun || __FreeBSD__ || __OpenBSD__ */
725 int i
, j
; /* Looping vars */
726 int fd
; /* File descriptor */
727 char device
[255]; /* Device filename */
729 struct serial_struct serinfo
; /* serial port info */
730 # endif /* TIOCGSERIAL */
733 for (i
= 0; i
< 100; i
++)
735 sprintf(device
, "/dev/ttyS%d", i
);
737 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
741 * See if this port exists...
744 serinfo
.reserved_char
[0] = 0;
746 if (!ioctl(fd
, TIOCGSERIAL
, &serinfo
))
748 if (serinfo
.type
== PORT_UNKNOWN
)
758 # endif /* TIOCGSERIAL */
762 # if defined(_ARCH_PPC) || defined(powerpc) || defined(__powerpc)
763 printf("serial serial:%s?baud=230400 \"Unknown\" \"Serial Port #%d\"\n",
766 printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
768 # endif /* _ARCH_PPC || powerpc || __powerpc */
772 for (i
= 0; i
< 16; i
++)
774 sprintf(device
, "/dev/usb/ttyUSB%d", i
);
775 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
778 printf("serial serial:%s?baud=230400 \"Unknown\" \"USB Serial Port #%d\"\n",
783 for (i
= 0; i
< 64; i
++)
785 for (j
= 0; j
< 8; j
++)
787 sprintf(device
, "/dev/ttyQ%02de%d", i
, j
);
788 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
791 printf("serial serial:%s?baud=115200 \"Unknown\" "
792 "\"Equinox ESP %d Port #%d\"\n",
798 int i
, j
, n
; /* Looping vars */
799 char device
[255]; /* Device filename */
800 inventory_t
*inv
; /* Hardware inventory info */
804 * IRIX maintains a hardware inventory of most devices...
809 while ((inv
= getinvent()) != NULL
)
811 if (inv
->inv_class
== INV_SERIAL
)
814 * Some sort of serial port...
817 if (inv
->inv_type
== INV_CDSIO
|| inv
->inv_type
== INV_CDSIO_E
)
823 for (n
= 0; n
< 6; n
++)
824 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"CDSIO Board %d Serial Port #%d\"\n",
825 n
+ 5 + 8 * inv
->inv_controller
, inv
->inv_controller
, n
+ 1);
827 else if (inv
->inv_type
== INV_EPC_SERIAL
)
830 * Everest serial port...
833 if (inv
->inv_unit
== 0)
836 i
= 41 + 4 * (int)inv
->inv_controller
;
838 for (n
= 0; n
< (int)inv
->inv_state
; n
++)
839 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"EPC Serial Port %d, Ebus slot %d\"\n",
840 n
+ i
, n
+ 1, (int)inv
->inv_controller
);
842 else if (inv
->inv_state
> 1)
845 * Standard serial port under IRIX 6.4 and earlier...
848 for (n
= 0; n
< (int)inv
->inv_state
; n
++)
849 printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"Onboard Serial Port %d\"\n",
850 n
+ (int)inv
->inv_unit
+ 1, n
+ (int)inv
->inv_unit
+ 1);
855 * Standard serial port under IRIX 6.5 and beyond...
858 printf("serial serial:/dev/ttyd%d?baud=115200 \"Unknown\" \"Onboard Serial Port %d\"\n",
859 (int)inv
->inv_controller
, (int)inv
->inv_controller
);
867 * Central Data makes serial and parallel "servers" that can be
868 * connected in a number of ways. Look for ports...
871 for (i
= 0; i
< 10; i
++)
872 for (j
= 0; j
< 8; j
++)
873 for (n
= 0; n
< 32; n
++)
875 if (i
== 8) /* EtherLite */
876 sprintf(device
, "/dev/ttydn%d%c", j
, funky_hex
[n
]);
877 else if (i
== 9) /* PCI */
878 sprintf(device
, "/dev/ttydp%d%c", j
, funky_hex
[n
]);
880 sprintf(device
, "/dev/ttyd%d%d%c", i
, j
, funky_hex
[n
]);
882 if (access(device
, 0) == 0)
885 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
888 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data PCI Serial Port, ID %d, port %d\"\n",
891 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
896 int i
, j
, n
; /* Looping vars */
897 char device
[255]; /* Device filename */
901 * Standard serial ports...
904 for (i
= 0; i
< 26; i
++)
906 sprintf(device
, "/dev/cua/%c", 'a' + i
);
907 if (access(device
, 0) == 0)
909 printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
912 printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
914 # endif /* B115200 */
918 * MAGMA serial ports...
921 for (i
= 0; i
< 40; i
++)
923 sprintf(device
, "/dev/term/%02d", i
);
924 if (access(device
, 0) == 0)
925 printf("serial serial:%s?baud=38400 \"Unknown\" \"MAGMA Serial Board #%d Port #%d\"\n",
926 device
, (i
/ 10) + 1, (i
% 10) + 1);
930 * Central Data serial ports...
933 for (i
= 0; i
< 9; i
++)
934 for (j
= 0; j
< 8; j
++)
935 for (n
= 0; n
< 32; n
++)
937 if (i
== 8) /* EtherLite */
938 sprintf(device
, "/dev/sts/ttyN%d%c", j
, funky_hex
[n
]);
940 sprintf(device
, "/dev/sts/tty%c%d%c", i
+ 'C', j
,
943 if (access(device
, 0) == 0)
946 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
949 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
953 #elif defined(__hpux)
954 int i
, j
, n
; /* Looping vars */
955 char device
[255]; /* Device filename */
959 * Standard serial ports...
962 for (i
= 0; i
< 10; i
++)
964 sprintf(device
, "/dev/tty%dp0", i
);
965 if (access(device
, 0) == 0)
966 printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
971 * Central Data serial ports...
974 for (i
= 0; i
< 9; i
++)
975 for (j
= 0; j
< 8; j
++)
976 for (n
= 0; n
< 32; n
++)
978 if (i
== 8) /* EtherLite */
979 sprintf(device
, "/dev/ttyN%d%c", j
, funky_hex
[n
]);
981 sprintf(device
, "/dev/tty%c%d%c", i
+ 'C', j
,
984 if (access(device
, 0) == 0)
987 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
990 printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
994 #elif defined(__osf__)
995 int i
; /* Looping var */
996 char device
[255]; /* Device filename */
1000 * Standard serial ports...
1003 for (i
= 0; i
< 100; i
++)
1005 sprintf(device
, "/dev/tty%02d", i
);
1006 if (access(device
, 0) == 0)
1007 printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
1010 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
1011 int i
, j
; /* Looping vars */
1012 int fd
; /* File descriptor */
1013 char device
[255]; /* Device filename */
1020 for (i
= 0; i
< 32; i
++)
1022 sprintf(device
, "/dev/ttyd%c", funky_hex
[i
]);
1023 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1026 printf("serial serial:%s?baud=115200 \"Unknown\" \"Standard Serial Port #%d\"\n",
1035 for (i
= 0; i
< 16; i
++) /* Should be up to 65536 boards... */
1036 for (j
= 0; j
< 32; j
++)
1038 sprintf(device
, "/dev/ttyc%d%c", i
, funky_hex
[j
]);
1039 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1042 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Port #%d\"\n",
1046 sprintf(device
, "/dev/ttyC%d%c", i
, funky_hex
[j
]);
1047 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1050 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Port #%d\"\n",
1056 * Digiboard ports...
1059 for (i
= 0; i
< 16; i
++) /* Should be up to 65536 boards... */
1060 for (j
= 0; j
< 32; j
++)
1062 sprintf(device
, "/dev/ttyD%d%c", i
, funky_hex
[j
]);
1063 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1066 printf("serial serial:%s?baud=115200 \"Unknown\" \"Digiboard #%d Serial Port #%d\"\n",
1075 for (i
= 0; i
< 32; i
++)
1077 sprintf(device
, "/dev/ttyE%c", funky_hex
[i
]);
1078 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1081 printf("serial serial:%s?baud=115200 \"Unknown\" \"Stallion Serial Port #%d\"\n",
1090 for (i
= 0; i
< 128; i
++)
1092 sprintf(device
, "/dev/ttyA%d", i
+ 1);
1093 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1096 printf("serial serial:%s?baud=115200 \"Unknown\" \"SX Serial Port #%d\"\n",
1100 #elif defined(__NetBSD__)
1101 int i
, j
; /* Looping vars */
1102 int fd
; /* File descriptor */
1103 char device
[255]; /* Device filename */
1107 * Standard serial ports...
1110 for (i
= 0; i
< 4; i
++)
1112 sprintf(device
, "/dev/tty%02d", i
);
1113 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1116 printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
1122 * Cyclades-Z ports...
1125 for (i
= 0; i
< 16; i
++) /* Should be up to 65536 boards... */
1126 for (j
= 0; j
< 64; j
++)
1128 sprintf(device
, "/dev/ttyCZ%02d%02d", i
, j
);
1129 if ((fd
= open(device
, O_WRONLY
| O_NOCTTY
| O_NDELAY
)) >= 0)
1132 printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Prt #%d\"\n",
1136 #elif defined(__APPLE__)
1138 * Standard serial ports on MacOS X...
1141 kern_return_t kernResult
;
1142 mach_port_t masterPort
;
1143 io_iterator_t serialPortIterator
;
1144 CFMutableDictionaryRef classesToMatch
;
1145 io_object_t serialService
;
1148 kernResult
= IOMasterPort(MACH_PORT_NULL
, &masterPort
);
1149 if (KERN_SUCCESS
!= kernResult
)
1153 * Serial devices are instances of class IOSerialBSDClient.
1156 classesToMatch
= IOServiceMatching(kIOSerialBSDServiceValue
);
1157 if (classesToMatch
!= NULL
)
1159 CFDictionarySetValue(classesToMatch
, CFSTR(kIOSerialBSDTypeKey
),
1160 CFSTR(kIOSerialBSDRS232Type
));
1162 kernResult
= IOServiceGetMatchingServices(masterPort
, classesToMatch
,
1163 &serialPortIterator
);
1164 if (kernResult
== KERN_SUCCESS
)
1166 while ((serialService
= IOIteratorNext(serialPortIterator
)))
1168 CFTypeRef serialNameAsCFString
;
1169 CFTypeRef bsdPathAsCFString
;
1170 char serialName
[128];
1175 serialNameAsCFString
=
1176 IORegistryEntryCreateCFProperty(serialService
,
1177 CFSTR(kIOTTYDeviceKey
),
1178 kCFAllocatorDefault
, 0);
1179 if (serialNameAsCFString
)
1181 result
= CFStringGetCString(serialNameAsCFString
, serialName
,
1183 kCFStringEncodingASCII
);
1184 CFRelease(serialNameAsCFString
);
1189 IORegistryEntryCreateCFProperty(serialService
,
1190 CFSTR(kIOCalloutDeviceKey
),
1191 kCFAllocatorDefault
, 0);
1192 if (bsdPathAsCFString
)
1194 result
= CFStringGetCString(bsdPathAsCFString
, bsdPath
,
1196 kCFStringEncodingASCII
);
1197 CFRelease(bsdPathAsCFString
);
1200 printf("serial serial:%s?baud=115200 \"Unknown\" \"%s\"\n",
1201 bsdPath
, serialName
);
1206 IOObjectRelease(serialService
);
1210 * Release the iterator.
1213 IOObjectRelease(serialPortIterator
);
1221 * End of "$Id: serial.c 181 2006-06-22 20:01:18Z jlovell $".