Debian Codegraph @

src/SerialImp.h

raw symbols ◀ ▶ SCIP
Source reconstructed at patch state 2 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
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-2007 by Trent Jarvi [email protected] 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 /* gnu.io.SerialPort constants */
59 #ifdef WIN32
60 #include "win32termios.h"
61 #endif /* WIN32 */
62 #define JDATABITS_5 5
63 #define JDATABITS_6 6
64 #define JDATABITS_7 7
65 #define JDATABITS_8 8
66 #define JPARITY_NONE 0
67 #define JPARITY_ODD 1
68 #define JPARITY_EVEN 2
69 #define JPARITY_MARK 3
70 #define JPARITY_SPACE 4
71 #define STOPBITS_1 1
72 #define STOPBITS_2 2
73 #define STOPBITS_1_5 3
74 #define FLOWCONTROL_NONE 0
75 #define FLOWCONTROL_RTSCTS_IN 1
76 #define FLOWCONTROL_RTSCTS_OUT 2
77 #define FLOWCONTROL_XONXOFF_IN 4
78 #define FLOWCONTROL_XONXOFF_OUT 8
79
80 /* gnu.io.SerialPortEvent constants */
81 #define SPE_DATA_AVAILABLE 1
82 #define SPE_OUTPUT_BUFFER_EMPTY 2
83 #define SPE_CTS 3
84 #define SPE_DSR 4
85 #define SPE_RI 5
86 #define SPE_CD 6
87 #define SPE_OE 7
88 #define SPE_PE 8
89 #define SPE_FE 9
90 #define SPE_BI 10
91
92 #define PORT_SERIAL 1
93 #define PORT_PARALLEL 2
94 #define PORT_I2C 3
95 #define PORT_RS485 4
96 #define PORT_RAW 5
97
98 /* glue for unsupported linux speeds see also win32termios.h */
99
100 #if !defined(__APPLE__) && !defined(__FreeBSD_kernel__) /* dima */
101 /* this is now handled in SerialImp.c
102 #define B14400 1010001
103 #define B28800 1010002
104 #define B128000 1010003
105 #define B256000 1010004
106 */
107 #endif /* dima */
108
109 struct preopened
110 {
111 char filename[40];
112 int fd;
113 struct preopened *next;
114 struct preopened *prev;
115 };
116
117 struct event_info_struct
118 {
119 int fd;
120 /* flags for events */
121 int eventflags[11];
122
123 int initialised;
124 int ret, change;
125 unsigned int omflags;
126 char message[80];
127 int has_tiocsergetlsr;
128 int has_tiocgicount;
129 int eventloop_interrupted;
130 JNIEnv *env;
131 jobject *jobj;
132 jclass jclazz;
133 jmethodID send_event;
134 jmethodID checkMonitorThread;
135 struct event_info_struct *next, *prev;
136 fd_set rfds;
137 struct timeval tv_sleep;
138 int closing;
139 #if !defined(TIOCSERGETLSR) && !defined(WIN32)
140 int writing;
141 int output_buffer_empty_flag;
142 pthread_t drain_tid;
143 #endif /* !TIOCSERGETLSR !WIN32 */
144 # if defined(TIOCGICOUNT)
145 struct serial_icounter_struct osis;
146 #endif /* TIOCGICOUNT */
147 };
148
149 /* Ports known on the OS */
150 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__GNU__)
151 /*
152 This is a small hack to get mark and space parity working on older systems
153 https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=147533
154 */
155 # if !defined(CMSPAR)
156 # define CMSPAR 010000000000
157 # endif /* CMSPAR */
158 #
159 # define DEVICEDIR "/dev/"
160 # define LOCKDIR "/var/lock"
161 # define LOCKFILEPREFIX "LCK.."
162 # define FHS
163 #endif /* __linux__ */
164 #if defined(__QNX__)
165 # define DEVICEDIR "/dev/"
166 # define LOCKDIR ""
167 # define LOCKFILEPREFIX ""
168 #endif /* qnx */
169 #if defined(__sgi__) || defined(sgi)
170 # define DEVICEDIR "/dev/"
171 # define LOCKDIR "/usr/spool/uucp"
172 # define LOCKFILEPREFIX "LK."
173 # define UUCP
174 #endif /* __sgi__ || sgi */
175 #if defined(__FreeBSD__)
176 # define DEVICEDIR "/dev/"
177 # define LOCKDIR "/var/spool/lock"
178 # define LOCKFILEPREFIX "LK.."
179 # define UUCP
180 #endif /* __FreeBSD__ */
181 #if defined(__APPLE__)
182 # define DEVICEDIR "/dev/"
183 /*# define LOCKDIR "/var/spool/uucp"*/
184 # define LOCKDIR "/var/lock"
185 # define LOCKFILEPREFIX "LK."
186 /*# define UUCP*/
187 # define OPEN_EXCL
188 #endif /* __APPLE__ */
189 #if defined(__NetBSD__)
190 # define DEVICEDIR "/dev/"
191 # define LOCKDIR "/var/lock"
192 /*# define LOCKDIR "/usr/spool/uucp"*/
193 # define LOCKFILEPREFIX "LK."
194 # define UUCP
195 #endif /* __NetBSD__ */
196 #if defined(__unixware__)
197 # define DEVICEDIR "/dev/"
198 /* really this only fully works for OpenServer */
199 # define LOCKDIR "/var/spool/uucp/"
200 # define LOCKFILEPREFIX "LK."
201 /*
202 this needs work....
203
204 Jonathan Schilling <[email protected]> writes:
205
206 This is complicated because as I said in my previous mail, there are
207 two kinds of SCO operating systems.
208
209 The one that most people want gnu.io for, including the guy who
210 asked the mailing list about SCO support a few days ago, is Open Server
211 (a/k/a "SCO UNIX"), which is SVR3-based. This uses old-style uucp locks,
212 of the form LCK..tty0a. That's what I implemented in the RXTX port I did,
213 and it works correctly.
214
215 The other SCO/Caldera OS, UnixWare/Open UNIX, uses the new-style
216 SVR4 locks, of the form LK.123.123.123. These OSes are a lot like
217 Solaris (UnixWare/Open UNIX come from AT&T SVR4 which had a joint
218
219 The other SCO/Caldera OS, UnixWare/Open UNIX, uses the new-style
220 SVR4 locks, of the form LK.123.123.123. These OSes are a lot like
221 Solaris (UnixWare/Open UNIX come from AT&T SVR4 which had a joint
222 heritage with Sun way back when). I saw that you added support
223 for this form of lock by RXTX 1.4-10 ... but it gets messy because,
224 as I said before, we use the same binary gnu.io files for both
225 UnixWare/Open UNIX and OpenServer. Thus we can't #ifdef one or the
226 other; it would have to be a runtime test. Your code and your macros
227 aren't set up for doing this (understandably!). So I didn't implement
228 these; the gnu.io locks won't fully work on UnixWare/Open UNIX
229 as a result, which I mentioned in the Release Notes.
230
231
232 What I would suggest is that you merge in the old-style LCK..tty0a lock
233 code that I used, since this will satisfy 90% of the SCO users. Then
234 I'll work on some way of getting UnixWare/Open UNIX locking to work
235 correctly, and give you those changes at a later date.
236
237 Jonathan
238
239 FIXME The lock type could be passed with -DOLDUUCP or -DUUCP based on
240 os.name in configure.in or perhaps system defines could determine the lock
241 type.
242
243 Trent
244 */
245 # define OLDUUCP
246 #endif
247 #if defined(__hpux__)
248 /* modif cath */
249 # define DEVICEDIR "/dev/"
250 # define LOCKDIR "/var/spool/uucp"
251 # define LOCKFILEPREFIX "LCK."
252 # define UUCP
253 #endif /* __hpux__ */
254 #if defined(__osf__) /* Digital Unix */
255 # define DEVICEDIR "/dev/"
256 # define LOCKDIR ""
257 # define LOCKFILEPREFIX "LK."
258 # define UUCP
259 #endif /* __osf__ */
260 #if defined(__sun__) /* Solaris */
261 # define DEVICEDIR "/dev/"
262 # define LOCKDIR "/var/spool/locks"
263 # define LOCKFILEPREFIX "LK."
264 /*
265 # define UUCP
266 */
267 #endif /* __sun__ */
268 #if defined(__BEOS__)
269 # define DEVICEDIR "/dev/ports/"
270 # define LOCKDIR ""
271 # define LOCKFILEPREFIX ""
272 # define UUCP
273 #endif /* __BEOS__ */
274 #if defined(WIN32)
275 # define DEVICEDIR "//./"
276 # define LOCKDIR ""
277 # define LOCKFILEPREFIX ""
278 # define OPEN serial_open
279 # define CLOSE serial_close
280 # define WRITE serial_write
281 # define READ serial_read
282 # define SELECT serial_select
283 #else /* use the system calls for Unix */
284 # define OPEN open
285 # define CLOSE close
286 # define WRITE write
287 # define READ read
288 # define SELECT select
289 struct timeval snow, enow, seloop, eeloop;
290 #define report_time_start_rs( ) \
291 { \
292 gettimeofday(&snow, NULL); \
293 }
294 #define report_time_end_rs( ) \
295 { \
296 gettimeofday(&enow, NULL); \
297 mexPrintf("%8i sec : %8i usec\n", enow.tv_sec - snow.tv_sec, enow.tv_sec - snow.tv_sec?snow.tv_usec-enow.tv_usec:enow.tv_usec - snow.tv_usec); \
298 }
299 #ifdef DEBUG_TIMING
300 struct timeval snow, enow, seloop, eeloop;
301 #define report_time_eventLoop( ) { \
302 if ( seloop.tv_sec == eeloop.tv_sec && seloop.tv_usec == eeloop.tv_usec ) \
303 { \
304 gettimeofday(&eeloop, NULL); \
305 seloop.tv_sec = eeloop.tv_sec; \
306 seloop.tv_usec = eeloop.tv_usec; \
307 mexPrintf("%8i sec : %8i usec\n", eeloop.tv_sec - seloop.tv_sec, eeloop.tv_usec - seloop.tv_usec); \
308 } \
309 }
310 #define report_time( ) \
311 { \
312 struct timeval now; \
313 gettimeofday(&now, NULL); \
314 mexPrintf("%8s : %5i : %8i sec : %8i usec\n", __TIME__, __LINE__, now.tv_sec, now.tv_usec); \
315 }
316 #define report_time_start( ) \
317 { \
318 gettimeofday(&snow, NULL); \
319 mexPrintf("%8s : %5i : %8i sec : %8i usec", __TIME__, __LINE__, snow.tv_sec, snow.tv_usec); \
320 }
321 #define report_time_end( ) \
322 { \
323 gettimeofday(&enow, NULL); \
324 mexPrintf("%8i sec : %8i usec\n", enow.tv_sec - snow.tv_sec, enow.tv_sec - snow.tv_sec?snow.tv_usec-enow.tv_usec:enow.tv_usec - snow.tv_usec); \
325 }
326 #else
327 #define report_time_eventLoop( ){};
328 #define report_time( ) {}
329 #define report_time_start( ) {}
330 #define report_time_end( ) {}
331
332 #endif /* DEBUG_TIMING */
333
334 /* #define TRACE */
335 #ifdef TRACE
336 #define ENTER(x) report("entering "x" \n");
337 #define LEAVE(x) report("leaving "x" \n");
338 #else
339 #define ENTER(x)
340 #define LEAVE(x)
341 #endif /* TRACE */
342
343 #endif /* WIN32 */
344
345 /* allow people to override the directories */
346 /* #define USER_LOCK_DIRECTORY "/home/tjarvi/1.5/build" */
347 #ifdef USER_LOCK_DIRECTORY
348 # define LOCKDIR USER_LOCK_DIRECTORY
349 #endif /* USER_LOCK_DIRECTORY */
350
351 #ifdef DISABLE_LOCKFILES
352 #undef UUCP
353 #undef FHS
354 #undef OLDUUCP
355 #endif /* DISABLE_LOCKFILES */
356
357 /* That should be all you need to look at in this file for porting */
358 #ifdef LFS /* Use a Lock File Server */
359 # define LOCK lfs_lock
360 # define UNLOCK lfs_unlock
361 #elif defined(UUCP)
362 # define LOCK uucp_lock
363 # define UNLOCK uucp_unlock
364 #elif defined(OLDUUCP)
365 /*
366 We can handle the old style if needed here see __unixware__ above.
367 defaulting to rxtx-1.4-8 behavior for now.
368
369 see also __sco__ in SerialImp.c when changing this for a possible
370 bug
371
372 FIXME
373 */
374 # define LOCK fhs_lock
375 # define UNLOCK fhs_unlock
376 #elif defined(FHS)
377 #ifdef LIBLOCKDEV
378 # define LOCK lib_lock_dev_lock
379 # define UNLOCK lib_lock_dev_unlock
380 #else
381 # define LOCK fhs_lock
382 # define UNLOCK fhs_unlock
383 #endif /* LIBLOCKDEV */
384 #else
385 # define LOCK system_does_not_lock
386 # define UNLOCK system_does_not_unlock
387 #endif /* UUCP */
388
389 /* java exception class names */
390 /* define UNSUPPORTED_COMM_OPERATION "gnu/io/UnsupportedCommOperationException" */
391 #define UNSUPPORTED_COMM_OPERATION "UnsupportedCommOperationException"
392 #define ARRAY_INDEX_OUT_OF_BOUNDS "java/lang/ArrayIndexOutOfBoundsException"
393 #define OUT_OF_MEMORY "java/lang/OutOfMemoryError"
394 #define IO_EXCEPTION "java/io/IOException"
395 #define PORT_IN_USE_EXCEPTION "gnu/io/PortInUseException"
396
397 /* some popular releases of Slackware do not have SSIZE_MAX */
398
399 #ifndef SSIZE_MAX
400 # if defined(INT_MAX)
401 # define SSIZE_MAX INT_MAX
402 # elif defined(MAXINT)
403 # define SSIZE_MAX MAXINT
404 # else
405 # define SSIZE_MAX 2147483647 /* ugh */
406 # endif
407 #endif
408
409 /*
410 Flow Control defines inspired by reading how mgetty by Gert Doering does it
411 */
412
413 #ifdef CRTSCTS
414 #define HARDWARE_FLOW_CONTROL CRTSCTS
415 #else
416 # ifdef CCTS_OFLOW
417 # define HARDWARE_FLOW_CONTROL CCTS_OFLOW|CRST_IFLOW
418 # else
419 # ifdef RTSFLOW
420 # define HARDWARE_FLOW_CONTROL RTSFLOW|CTSFLOW
421 # else
422 # ifdef CRTSFL
423 # define HARDWARE_FLOW_CONTROL CRTSFL
424 # else
425 # ifdef CTSCD
426 # define HARDWARE_FLOW_CONTROL CTSCD
427 # else
428 # define HARDWARE_FLOW_CONTROL 0
429 # endif
430 # endif
431 # endif
432 # endif
433 #endif
434
435 /* PROTOTYPES */
436 #ifdef DEBUG_MW
437 extern void mexWarnMsgTxt( const char * );
438 extern void mexErrMsgTxt( const char * );
439 #ifndef __APPLE__
440 extern int mexPrintf( const char *, ... );
441 # define printf mexPrintf
442 #endif
443 #endif /* DEBUG_MW */
444
445 #ifdef __APPLE__
446 # define mexPrintf printf
447 #endif
448
449 #ifdef __BEOS__
450 struct tpid_info_struct *add_tpid( struct tpid_info_struct * );
451 data_rate translate_speed( JNIEnv*, jint );
452 int translate_data_bits( JNIEnv *, data_bits *, jint );
453 int translate_stop_bits( JNIEnv *, stop_bits *, jint );
454 int translate_parity( JNIEnv *, parity_mode *, jint );
455 #else
456 int spawn_write_thread( int, char *, int, JNIEnv *, jobject *);
457 int translate_speed( JNIEnv*, jint );
458 int translate_data_bits( JNIEnv *, tcflag_t *, jint );
459 int translate_stop_bits( JNIEnv *, tcflag_t *, jint );
460 int translate_parity( JNIEnv *, tcflag_t *, jint );
461 #endif
462 void system_wait();
463 void finalize_event_info_struct( struct event_info_struct * );
464 int read_byte_array( JNIEnv *, jobject *, int, unsigned char *, int, int );
465 size_t get_java_var( JNIEnv *, jobject, char *, char * );
466 jboolean is_interrupted( struct event_info_struct * );
467 int send_event(struct event_info_struct *, jint, int );
468 void dump_termios(char *,struct termios *);
469 void report_verbose(const char *);
470 void report_error(const char *);
471 void report_warning(const char *);
472 void report(const char *);
473 void throw_java_exception( JNIEnv *, char *, char *, char * );
474 int lock_device( const char * );
475 void unlock_device( const char * );
476 int is_device_locked( const char * );
477 int check_lock_status( const char * );
478 int lfs_unlock(const char *, int );
479 int lfs_lock( const char *, int);
480 int lib_lock_dev_unlock(const char *, int );
481 int lib_lock_dev_lock( const char *, int);
482 void fhs_unlock(const char *, int );
483 int fhs_lock( const char *, int);
484 void uucp_unlock( const char *, int );
485 int uucp_lock( const char *, int );
486 int system_does_not_lock( const char *, int );
487 void system_does_not_unlock( const char *, int );
488 int check_group_uucp();
489 int check_lock_pid( const char *, int );
490 int printj(JNIEnv *env, wchar_t *fmt, ...);
491
492 #define UNEXPECTED_LOCK_FILE "RXTX Error: Unexpected lock file: %s\n Please report to the RXTX developers\n"
493 #define LINUX_KERNEL_VERSION_ERROR "\n\n\nRXTX WARNING: This library was compiled to run with OS release %s and you are currently running OS release %s. In some cases this can be a problem. Try recompiling RXTX if you notice strange behavior. If you just compiled RXTX make sure /usr/include/linux is a symbolic link to the include files that came with the kernel source and not an older copy.\n\n\npress enter to continue\n"
494 #define UUCP_ERROR "\n\n\nRXTX WARNING: This library requires the user running applications to be in\ngroup uucp. Please consult the INSTALL documentation. More information is\navaiable under the topic 'How can I use Lock Files with rxtx?'\n"