1 /*-------------------------------------------------------------------------
2 |   RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface.
3 |   RXTX is a native interface to serial ports in java.
4 |   Copyright 1997-2009 by Trent Jarvi tjarvi@qbang.org and others who
5 |   actually wrote it.  See individual source files for more information.
6 |
7 |   A copy of the LGPL v 2.1 may be found at
8 |   http://www.gnu.org/licenses/lgpl.txt on March 4th 2007.  A copy is
9 |   here for your convenience.
10 |
11 |   This library is free software; you can redistribute it and/or
12 |   modify it under the terms of the GNU Lesser General Public
13 |   License as published by the Free Software Foundation; either
14 |   version 2.1 of the License, or (at your option) any later version.
15 |
16 |   This library is distributed in the hope that it will be useful,
17 |   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 |   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 |   Lesser General Public License for more details.
20 |
21 |   An executable that contains no derivative of any portion of RXTX, but
22 |   is designed to work with RXTX by being dynamically linked with it,
23 |   is considered a "work that uses the Library" subject to the terms and
24 |   conditions of the GNU Lesser General Public License.
25 |
26 |   The following has been added to the RXTX License to remove
27 |   any confusion about linking to RXTX.   We want to allow in part what
28 |   section 5, paragraph 2 of the LGPL does not permit in the special
29 |   case of linking over a controlled interface.  The intent is to add a
30 |   Java Specification Request or standards body defined interface in the
31 |   future as another exception but one is not currently available.
32 |
33 |   http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface
34 |
35 |   As a special exception, the copyright holders of RXTX give you
36 |   permission to link RXTX with independent modules that communicate with
37 |   RXTX solely through the Sun Microsytems CommAPI interface version 2,
38 |   regardless of the license terms of these independent modules, and to copy
39 |   and distribute the resulting combined work under terms of your choice,
40 |   provided that every copy of the combined work is accompanied by a complete
41 |   copy of the source code of RXTX (the version of RXTX used to produce the
42 |   combined work), being distributed under the terms of the GNU Lesser General
43 |   Public License plus this exception.  An independent module is a
44 |   module which is not derived from or based on RXTX.
45 |
46 |   Note that people who make modified versions of RXTX are not obligated
47 |   to grant this special exception for their modified versions; it is
48 |   their choice whether to do so.  The GNU Lesser General Public License
49 |   gives permission to release a modified version without this exception; this
50 |   exception also makes it possible to release a modified version which
51 |   carries forward this exception.
52 |
53 |   You should have received a copy of the GNU Lesser General Public
54 |   License along with this library; if not, write to the Free
55 |   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
56 |   All trademarks belong to their respective owners.
57 --------------------------------------------------------------------------*/
58 #ifndef _WIN32S_H_
59 #define _WIN32S_H_
60 #include <windows.h>
61 #include <sys/types.h>
62 #include <io.h>
63 #ifdef TRACE
64 #define ENTER(x) report("entering "x" \n");
65 #define LEAVE(x) report("leaving "x" \n");
66 #else
67 #define ENTER(x)
68 #define LEAVE(x)
69 #endif /* TRACE */
70 #if defined(_MSC_VER)
71 #define YACK() \
72 { \
73 	char *allocTextBuf, message[80]; \
74 	unsigned long nChars; \
75 	unsigned int errorCode = GetLastError(); \
76 	nChars = FormatMessage ( \
77 		FORMAT_MESSAGE_ALLOCATE_BUFFER | \
78 		FORMAT_MESSAGE_FROM_SYSTEM, \
79 		NULL, \
80 		errorCode, \
81 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
82 		(LPSTR)&allocTextBuf, \
83 		16, \
84 		NULL ); \
85 	_snprintf_s( message, 80, 80, "Error 0x%x at %s(%d): %s\n", errorCode, __FILE__, __LINE__, allocTextBuf); \
86 	report_error( message ); \
87 	LocalFree(allocTextBuf); \
88 	Sleep(1); \
89 }
90 #else
91 
92 #define YACK() \
93 { \
94 	char *allocTextBuf, message[80]; \
95 	unsigned long nChars; \
96 	unsigned int errorCode = GetLastError(); \
97 	nChars = FormatMessage ( \
98 		FORMAT_MESSAGE_ALLOCATE_BUFFER | \
99 		FORMAT_MESSAGE_FROM_SYSTEM, \
100 		NULL, \
101 		errorCode, \
102 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
103 		(LPSTR)&allocTextBuf, \
104 		16, \
105 		NULL ); \
106 	snprintf( message, 80, "Error 0x%x at %s(%d): %s\n", errorCode, __FILE__, __LINE__, allocTextBuf); \
107 	report_error( message ); \
108 	LocalFree(allocTextBuf); \
109 	Sleep(1); \
110 }
111 #endif
112 typedef unsigned char   cc_t;
113 typedef unsigned int    speed_t;
114 typedef unsigned int    tcflag_t;
115 
116 /* structs are from linux includes or linux man pages to match
117    interfaces.
118 */
119 
120 struct timespec
121 {
122 	time_t	tv_sec;
123 	long	tv_nsec;
124 };
125 
126 #define NCCS 32
127 struct termios
128   {
129     tcflag_t c_iflag;           /* input mode flags */
130     tcflag_t c_oflag;           /* output mode flags */
131     tcflag_t c_cflag;           /* control mode flags */
132     tcflag_t c_lflag;           /* local mode flags */
133     cc_t c_cc[NCCS];            /* control characters */
134     cc_t c_line;                /* line discipline (== c_cc[33]) */
135     speed_t c_ispeed;           /* input speed */
136     speed_t c_ospeed;           /* output speed */
137   };
138 
139 /*  for TIOCGSERIAL and TIOCSSERIAL of interest are baud_base and
140  *  custom_divisor
141  *  --- NOTE:  This is not used.  Win32 sets custom speeds on the
142  *             kernel side.
143  */
144 struct serial_struct {
145 /*
146 	Mainly we are after baud_base/custom_diviser to match
147 	the ioctl() in SerialImp.c
148 */
149 	int custom_divisor;   /* use to set unsupported speeds */
150 	int baud_base;        /* use to set unsupported speeds */
151 
152 	unsigned short	close_delay, closing_wait, iomem_reg_shift;
153 	int type, line, irq, flags, xmit_fifo_size, hub6;
154 	unsigned int	port, port_high;
155 	char		io_type;
156 	unsigned char	*iomem_base;
157 };
158 struct serial_icounter_struct {
159 	int cts;		/* clear to send count */
160 	int dsr;		/* data set ready count */
161 	int rng;		/* ring count */
162 	int dcd;		/* carrier detect count */
163 	int rx;			/* recieved byte count */
164 	int tx;			/* transmitted byte count */
165 	int frame;		/* frame error count */
166 	int overrun;		/* hardware overrun error count */
167 	int parity;		/* parity error count */
168 	int brk;		/* break count */
169 	int buf_overrun;	/* buffer overrun count */
170 	int reserved[9]; 	/* unused */
171 };
172 
173 int serial_test( char * );
174 int serial_open(const char *File, int flags, ... );
175 int serial_close(int fd);
176 int serial_read(int fd, void *b, int size);
177 int serial_write(int fd, char *Str, int length);
178 /*
179  * lcc winsock.h conflicts
180  */
181 #ifndef __LCC__
182 int serial_select(int, struct fd_set *, struct fd_set *, struct fd_set *, struct timeval *);
183 #define select serial_select
184 #endif
185 
186 #define OPEN serial_open
187 #define CLOSE serial_close
188 #define READ serial_read
189 #define WRITE serial_write
190 
191 void termios_interrupt_event_loop( int , int );
192 void termios_setflags( int , int[] );
193 struct termios_list *find_port( int );
194 void usleep(unsigned long usec);
195 int fcntl(int fd, int command, ...);
196 const char *get_dos_port(const char *);
197 void set_errno(int);
198 char *sterror(int);
199 int B_to_CBR(int);
200 int CBR_to_B(int);
201 int termios_to_bytesize(int);
202 int bytesize_to_termios(int);
203 int tcgetattr(int Fd, struct termios *s_termios);
204 int tcsetattr(int Fd, int when, struct termios *);
205 int serial_close(int );
206 speed_t cfgetospeed(struct termios *s_termios);
207 speed_t cfgetispeed(struct termios *s_termios);
208 int cfsetspeed(struct termios *, speed_t speed);
209 int cfsetospeed(struct termios *, speed_t speed);
210 int cfsetispeed ( struct termios *, speed_t speed);
211 int tcflush ( int , int );
212 int tcgetpgrp ( int );
213 int tcsetpgrp ( int , int );
214 int tcdrain ( int );
215 int tcflow ( int , int );
216 int tcsendbreak ( int , int );
217 int ioctl(int fd, int request, ... );
218 /*
219 int fstat(int fd, ... );
220 */
221 void cfmakeraw(struct termios *s_termios);
222 int termiosGetParityErrorChar( int );
223 void termiosSetParityError( int, char );
224 
225 #define O_NOCTTY	0400	/* not for fcntl */
226 #define O_NONBLOCK	 00004
227 #define O_NDELAY	O_NONBLOCK
228 #define O_SYNC		040000
229 #define O_FSYNC		O_SYNC
230 #define O_ASYNC		020000	/* fcntl, for BSD compatibility */
231 
232 #define F_DUPFD		0	/* dup */
233 #define F_GETFD		1	/* get f_flags */
234 #define F_SETFD		2	/* set f_flags */
235 #define F_GETFL		3	/* more flags (cloexec) */
236 #define F_SETFL		4
237 #define F_GETLK		7
238 #define F_SETLK		8
239 #define F_SETLKW	9
240 
241 #define F_SETOWN	5	/*  for sockets. */
242 #define F_GETOWN	6	/*  for sockets. */
243 
244 /* for F_[GET|SET]FL */
245 #define FD_CLOEXEC	1	/* actually anything with low bit set goes */
246 
247 /* for posix fcntl() and lockf() */
248 #define F_RDLCK		1
249 #define F_WRLCK		2
250 #define F_UNLCK		8
251 
252 /* for old implementation of bsd flock () */
253 #define F_EXLCK		16	/* or 3 */
254 #define F_SHLCK		32	/* or 4 */
255 
256 /* operations for bsd flock(), also used by the kernel implementation */
257 #define LOCK_SH		1	/* shared lock */
258 #define LOCK_EX		2	/* exclusive lock */
259 #define LOCK_NB		4	/* or'd with one of the above to prevent
260 				   blocking */
261 #define LOCK_UN		8	/* remove lock */
262 
263 /* c_cc characters */
264 #define VINTR 0
265 #define VQUIT 1
266 #define VERASE 2
267 #define VKILL 3
268 #define VEOF 4
269 #define VTIME 5
270 #define VMIN 6
271 #define VSWTC 7
272 #define VSTART 8
273 #define VSTOP 9
274 #define VSUSP 10
275 #define VEOL 11
276 #define VREPRINT 12
277 #define VDISCARD 13
278 #define VWERASE 14
279 #define VLNEXT 15
280 #define VEOL2 16
281 
282 /* c_iflag bits */
283 #define IGNBRK	0000001
284 #define BRKINT	0000002
285 #define IGNPAR	0000004
286 #define PARMRK	0000010
287 #define INPCK	0000020
288 #define ISTRIP	0000040
289 #define INLCR	0000100
290 #define IGNCR	0000200
291 #define ICRNL	0000400
292 #define IXON	0002000
293 #define IXANY	0004000
294 #define IXOFF	0010000
295 #define IMAXBEL	   0020000
296 #define CRTS_IFLOW 0040000
297 #define CCTS_OFLOW	0100000
298 #define CIGNORE		0400000
299 #define CRTSCTS	  020000000000		/* flow control */
300 #define HARDWARE_FLOW_CONTROL CRTSCTS
301 #define CRTSXOFF        010000000000
302 /* c_oflag bits */
303 #define OPOST	0000001
304 #define ONLCR	0000002
305 #define OLCUC	0000004
306 
307 #define OCRNL	0000010
308 #define ONOCR	0000020
309 #define ONLRET	0000040
310 
311 #define OFILL	00000100
312 #define OFDEL	00000200
313 #define NLDLY	00001400
314 #define   NL0	00000000
315 #define   NL1	00000400
316 #define   NL2	00001000
317 #define   NL3	00001400
318 #define TABDLY	00006000
319 #define   TAB0	00000000
320 #define   TAB1	00002000
321 #define   TAB2	00004000
322 #define   TAB3	00006000
323 #define CRDLY	00030000
324 #define   CR0	00000000
325 #define   CR1	00010000
326 #define   CR2	00020000
327 #define   CR3	00030000
328 #define FFDLY	00040000
329 #define   FF0	00000000
330 #define   FF1	00040000
331 #define BSDLY	00100000
332 #define   BS0	00000000
333 #define   BS1	00100000
334 #define VTDLY	00200000
335 #define   VT0	00000000
336 #define   VT1	00200000
337 #define XTABS	01000000 /* Hmm.. Linux/i386 considers this part of TABDLY.. */
338 
339 /* c_cflag bit meaning */
340 #define  CBAUD  0030017
341 #define  B0	0000000		/* hang up */
342 #define  B50	0000001
343 #define  B75	0000002
344 #define  B110	0000003
345 #define  B134	0000004
346 #define  B150	0000005
347 #define  B200	0000006
348 #define  B300	0000007
349 #define  B600	0000010
350 #define  B1200	0000011
351 #define  B1800	0000012
352 #define  B2400	0000013
353 #define  B4800	0000014
354 #define  B9600	0000015
355 #define  B19200	0000016
356 #define  B38400	0000017
357 #define  B57600	  0010001
358 #define  B115200  0010002
359 #define  B230400  0010003
360 #define  B460800  0010004
361 #define  B500000  0010005
362 #define  B576000  0010006
363 #define  B921600  0010007
364 #define  B1000000 0010010
365 #define  B1152000 0010011
366 #define  B1500000 0010012
367 #define  B2000000 0010013
368 #define  B2500000 0010014
369 #define  B3000000 0010015
370 #define  B3500000 0010016
371 #define  B4000000 0010017
372 
373 /*
374 	glue for unsupported linux speeds see also SerialImp.h
375 	custom baud rates around 8192-9000 will not work because
376 	of these.
377 */
378 
379 #define B14400		0020001
380 #define B28800		0020002
381 #define B128000		0020003
382 #define B256000		0020004
383 
384 #define EXTA B19200
385 #define EXTB B38400
386 #define CSIZE	0000060
387 #define   CS5	0000000
388 #define   CS6	0000020
389 #define   CS7	0000040
390 #define   CS8	0000060
391 #define CSTOPB	0000100
392 #define CREAD	0000200
393 #define PARENB	0000400
394 #define PARODD	0001000
395 #define HUPCL	0002000
396 #define CLOCAL	0004000
397 # define CBAUDEX 0010000
398 # define CIBAUD	  002003600000		/* input baud rate (not used) */
399 # define CRTSCTS  020000000000		/* flow control */
400 
401 /* c_l flag */
402 #define ISIG    0000001
403 #define ICANON  0000002
404 #define XCASE   0000004
405 #define ECHO    0000010
406 #define ECHOE   0000020
407 #define ECHOK   0000040
408 #define ECHONL  0000100
409 #define NOFLSH  0000200
410 #define TOSTOP  0000400
411 #define ECHOCTL 0001000
412 #define ECHOPRT 0002000
413 #define ECHOKE  0004000
414 #define FLUSHO  0010000
415 #define PENDIN  0040000
416 #define IEXTEN  0100000
417 
418 /* glue for unsupported windows speeds */
419 
420 #define CBR_28800	28800
421 #define CBR_128000	128000
422 #define CBR_230400	230400
423 #define CBR_256000	256000
424 #define CBR_460800	460800
425 #define CBR_500000	500000
426 #define CBR_576000	576000
427 #define CBR_921600	921600
428 #define CBR_1000000	1000000
429 #define CBR_1152000	1152000
430 #define CBR_1500000	1500000
431 #define CBR_2000000	2000000
432 #define CBR_2500000	2500000
433 #define CBR_3000000	3000000
434 #define CBR_3500000	3500000
435 #define CBR_4000000	4000000
436 
437 
438 /* Values for the ACTION argument to `tcflow'.  */
439 #define	TCOOFF		0
440 #define	TCOON		1
441 #define	TCIOFF		2
442 #define	TCION		3
443 
444 /* Values for the QUEUE_SELECTOR argument to `tcflush'.  */
445 #define	TCIFLUSH	0
446 #define	TCOFLUSH	1
447 #define	TCIOFLUSH	2
448 
449 /* Values for the OPTIONAL_ACTIONS argument to `tcsetattr'.  */
450 #define	TCSANOW		0
451 #define	TCSADRAIN	1
452 #define	TCSAFLUSH	2
453 
454 /* ioctls */
455 #define TIOCSERGETLSR	0x5459
456 
457 #endif /*_WIN32S_H_*/
458 
459 /* unused ioctls */
460 #define TCSBRK		0x5409
461 #define TIOCOUTQ	0x5411
462 #define TIOCMGET	0x5415
463 #define TIOCMBIS	0x5416
464 #define TIOCMBIC	0x5417
465 #define TIOCMSET	0x5418
466 #define TIOCGSOFTCAR	0x5419
467 #define TIOCSSOFTCAR	0x541a
468 #define TIOCSER_TEMP	0x01
469 /*
470 #define FIONREAD	0x541b
471 TIOC[GS]SERIAL is not used on win32.  It was dropped after we could not
472 find a way to get/set buad_base and divisor directly.
473 #define TIOCGSERIAL	0x541e
474 #define TIOCSSERIAL	0x541f
475 */
476 #define TCSBRKP		0x5425
477 #define TIOCSERCONFIG	0x5453
478 #define TIOCSERGWILD	0x5454
479 #define TIOCSERSWILD	0x5455
480 #define TIOCSERGSTRUCT	0x5458
481 #define TIOCSERGETMULTI	0x545a
482 #define TIOCSERSETMULTI	0x545b
483 #define TIOCMIWAIT	0x545c
484 /* this would require being able to get the number of overruns ... */
485 /*
486 	FIXME
487 	frame and parity errors caused crashes in testing BlackBox
488 */
489 #define TIOCGICOUNT	0x545d
490 
491 /* ioctl errors */
492 #define ENOIOCTLCMD	515
493 #define EBADFD		 77
494 /* modem lines */
495 #define TIOCM_LE    0x001
496 #define TIOCM_DTR   0x002
497 #define TIOCM_RTS   0x004
498 #define TIOCM_ST    0x008
499 #define TIOCM_SR    0x010
500 #define TIOCM_CTS   0x020
501 #define TIOCM_CAR   0x040
502 #define TIOCM_RNG   0x080
503 #define TIOCM_DSR   0x100
504 #define TIOCM_CD    TIOCM_CAR
505 #define TIOCM_RI    TIOCM_RNG
506 
507 
508 #define CMSPAR      010000000000  /* mark or space parity */
509