Testing out the PPD42 Air Quality Sensor, with an MSP430 Launchpad and graphing the data with GNUplot.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
14 KiB

5 years ago
  1. /*-------------------------------------------------------------------------
  2. | rxtx is a native interface to serial ports in java.
  3. | Copyright 1997-2004 by Trent Jarvi taj@www.linux.org.uk
  4. |
  5. | This library is free software; you can redistribute it and/or
  6. | modify it under the terms of the GNU Library General Public
  7. | License as published by the Free Software Foundation; either
  8. | version 2 of the License, or (at your option) any later version.
  9. |
  10. | This library is distributed in the hope that it will be useful,
  11. | but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. | Library General Public License for more details.
  14. |
  15. | You should have received a copy of the GNU Library General Public
  16. | License along with this library; if not, write to the Free
  17. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. --------------------------------------------------------------------------*/
  19. /* gnu.io.SerialPort constants */
  20. #ifdef WIN32
  21. #include "win32termios.h"
  22. #endif /* WIN32 */
  23. #define JDATABITS_5 5
  24. #define JDATABITS_6 6
  25. #define JDATABITS_7 7
  26. #define JDATABITS_8 8
  27. #define JPARITY_NONE 0
  28. #define JPARITY_ODD 1
  29. #define JPARITY_EVEN 2
  30. #define JPARITY_MARK 3
  31. #define JPARITY_SPACE 4
  32. #define STOPBITS_1 1
  33. #define STOPBITS_2 2
  34. #define STOPBITS_1_5 3
  35. #define FLOWCONTROL_NONE 0
  36. #define FLOWCONTROL_RTSCTS_IN 1
  37. #define FLOWCONTROL_RTSCTS_OUT 2
  38. #define FLOWCONTROL_XONXOFF_IN 4
  39. #define FLOWCONTROL_XONXOFF_OUT 8
  40. /* gnu.io.SerialPortEvent constants */
  41. #define SPE_DATA_AVAILABLE 1
  42. #define SPE_OUTPUT_BUFFER_EMPTY 2
  43. #define SPE_CTS 3
  44. #define SPE_DSR 4
  45. #define SPE_RI 5
  46. #define SPE_CD 6
  47. #define SPE_OE 7
  48. #define SPE_PE 8
  49. #define SPE_FE 9
  50. #define SPE_BI 10
  51. #define PORT_SERIAL 1
  52. #define PORT_PARALLEL 2
  53. #define PORT_I2C 3
  54. #define PORT_RS485 4
  55. #define PORT_RAW 5
  56. /* glue for unsupported linux speeds see also win32termios.h */
  57. #if !defined(__APPLE__) && !defined(__FreeBSD__) /* dima */
  58. /* this is now handled in SerialImp.c
  59. #define B14400 1010001
  60. #define B28800 1010002
  61. #define B128000 1010003
  62. #define B256000 1010004
  63. */
  64. #endif /* dima */
  65. struct preopened
  66. {
  67. char filename[40];
  68. int fd;
  69. struct preopened *next;
  70. struct preopened *prev;
  71. };
  72. struct event_info_struct
  73. {
  74. int fd;
  75. /* flags for events */
  76. int eventflags[11];
  77. int initialised;
  78. int ret, change;
  79. unsigned int omflags;
  80. char message[80];
  81. int has_tiocsergetlsr;
  82. int has_tiocgicount;
  83. int eventloop_interrupted;
  84. /*
  85. JNIEnv *env;
  86. jobject *jobj;
  87. jclass jclazz;
  88. jmethodID send_event;
  89. jmethodID checkMonitorThread;
  90. */
  91. struct event_info_struct *next, *prev;
  92. fd_set rfds;
  93. struct timeval tv_sleep;
  94. int closing;
  95. #if !defined(TIOCSERGETLSR) && !defined(WIN32)
  96. int writing;
  97. int output_buffer_empty_flag;
  98. #endif /* !TIOCSERGETLSR !WIN32 */
  99. # if defined(TIOCGICOUNT)
  100. struct serial_icounter_struct osis;
  101. #endif /* TIOCGICOUNT */
  102. };
  103. /* Ports known on the OS */
  104. #if defined(__linux__)
  105. # define DEVICEDIR "/dev/"
  106. # define LOCKDIR "/var/lock"
  107. # define LOCKFILEPREFIX "LCK.."
  108. # define FHS
  109. #endif /* __linux__ */
  110. #if defined(__QNX__)
  111. # define DEVICEDIR "/dev/"
  112. # define LOCKDIR ""
  113. # define LOCKFILEPREFIX ""
  114. #endif /* qnx */
  115. #if defined(__sgi__) || defined(sgi)
  116. # define DEVICEDIR "/dev/"
  117. # define LOCKDIR "/usr/spool/uucp"
  118. # define LOCKFILEPREFIX "LK."
  119. # define UUCP
  120. #endif /* __sgi__ || sgi */
  121. #if defined(__FreeBSD__)
  122. # define DEVICEDIR "/dev/"
  123. # define LOCKDIR "/var/spool/uucp"
  124. # define LOCKFILEPREFIX "LK."
  125. # define UUCP
  126. #endif
  127. #if defined(__APPLE__)
  128. # define DEVICEDIR "/dev/"
  129. # define LOCKDIR "/var/spool/uucp"
  130. # define LOCKFILEPREFIX "LK."
  131. # define UUCP
  132. #endif /* __FreeBSD__ */
  133. #if defined(__NetBSD__)
  134. # define DEVICEDIR "/dev/"
  135. # define LOCKDIR "/usr/spool/uucp"
  136. # define LOCKFILEPREFIX "LK."
  137. # define UUCP
  138. #endif /* __NetBSD__ */
  139. #if defined(__unixware__)
  140. # define DEVICEDIR "/dev/"
  141. /* really this only fully works for OpenServer */
  142. # define LOCKDIR "/var/spool/uucp/"
  143. # define LOCKFILEPREFIX "LK."
  144. /*
  145. this needs work....
  146. Jonathan Schilling <jls@caldera.com> writes:
  147. This is complicated because as I said in my previous mail, there are
  148. two kinds of SCO operating systems.
  149. The one that most people want gnu.io for, including the guy who
  150. asked the mailing list about SCO support a few days ago, is Open Server
  151. (a/k/a "SCO UNIX"), which is SVR3-based. This uses old-style uucp locks,
  152. of the form LCK..tty0a. That's what I implemented in the RXTX port I did,
  153. and it works correctly.
  154. The other SCO/Caldera OS, UnixWare/Open UNIX, uses the new-style
  155. SVR4 locks, of the form LK.123.123.123. These OSes are a lot like
  156. Solaris (UnixWare/Open UNIX come from AT&T SVR4 which had a joint
  157. The other SCO/Caldera OS, UnixWare/Open UNIX, uses the new-style
  158. SVR4 locks, of the form LK.123.123.123. These OSes are a lot like
  159. Solaris (UnixWare/Open UNIX come from AT&T SVR4 which had a joint
  160. heritage with Sun way back when). I saw that you added support
  161. for this form of lock by RXTX 1.4-10 ... but it gets messy because,
  162. as I said before, we use the same binary gnu.io files for both
  163. UnixWare/Open UNIX and OpenServer. Thus we can't #ifdef one or the
  164. other; it would have to be a runtime test. Your code and your macros
  165. aren't set up for doing this (understandably!). So I didn't implement
  166. these; the gnu.io locks won't fully work on UnixWare/Open UNIX
  167. as a result, which I mentioned in the Release Notes.
  168. What I would suggest is that you merge in the old-style LCK..tty0a lock
  169. code that I used, since this will satisfy 90% of the SCO users. Then
  170. I'll work on some way of getting UnixWare/Open UNIX locking to work
  171. correctly, and give you those changes at a later date.
  172. Jonathan
  173. FIXME The lock type could be passed with -DOLDUUCP or -DUUCP based on
  174. os.name in configure.in or perhaps system defines could determine the lock
  175. type.
  176. Trent
  177. */
  178. # define OLDUUCP
  179. #endif
  180. #if defined(__hpux__)
  181. /* modif cath */
  182. # define DEVICEDIR "/dev/"
  183. # define LOCKDIR "/var/spool/uucp"
  184. # define LOCKFILEPREFIX "LCK."
  185. # define UUCP
  186. #endif /* __hpux__ */
  187. #if defined(__osf__) /* Digital Unix */
  188. # define DEVICEDIR "/dev/"
  189. # define LOCKDIR ""
  190. # define LOCKFILEPREFIX "LK."
  191. # define UUCP
  192. #endif /* __osf__ */
  193. #if defined(__sun__) /* Solaris */
  194. # define DEVICEDIR "/dev/"
  195. # define LOCKDIR "/var/spool/locks"
  196. # define LOCKFILEPREFIX "LK."
  197. /*
  198. # define UUCP
  199. */
  200. #endif /* __sun__ */
  201. #if defined(__BEOS__)
  202. # define DEVICEDIR "/dev/ports/"
  203. # define LOCKDIR ""
  204. # define LOCKFILEPREFIX ""
  205. # define UUCP
  206. #endif /* __BEOS__ */
  207. #if defined(WIN32)
  208. # define DEVICEDIR "//./"
  209. # define LOCKDIR ""
  210. # define LOCKFILEPREFIX ""
  211. # define OPEN serial_open
  212. # define CLOSE serial_close
  213. # define WRITE serial_write
  214. # define READ serial_read
  215. # define SELECT serial_select
  216. #else /* use the system calls for Unix */
  217. # define OPEN open
  218. # define CLOSE close
  219. # define WRITE write
  220. # define READ read
  221. # define SELECT select
  222. struct timeval snow, enow, seloop, eeloop;
  223. #define report_time_start_rs( ) \
  224. { \
  225. gettimeofday(&snow, NULL); \
  226. }
  227. #define report_time_end_rs( ) \
  228. { \
  229. gettimeofday(&enow, NULL); \
  230. 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); \
  231. }
  232. #ifdef DEBUG_TIMING
  233. struct timeval snow, enow, seloop, eeloop;
  234. #define report_time_eventLoop( ) { \
  235. if ( seloop.tv_sec == eeloop.tv_sec && seloop.tv_usec == eeloop.tv_usec ) \
  236. { \
  237. gettimeofday(&eeloop, NULL); \
  238. seloop.tv_sec = eeloop.tv_sec; \
  239. seloop.tv_usec = eeloop.tv_usec; \
  240. mexPrintf("%8i sec : %8i usec\n", eeloop.tv_sec - seloop.tv_sec, eeloop.tv_usec - seloop.tv_usec); \
  241. } \
  242. }
  243. #define report_time( ) \
  244. { \
  245. struct timeval now; \
  246. gettimeofday(&now, NULL); \
  247. mexPrintf("%8s : %5i : %8i sec : %8i usec\n", __TIME__, __LINE__, now.tv_sec, now.tv_usec); \
  248. }
  249. #define report_time_start( ) \
  250. { \
  251. gettimeofday(&snow, NULL); \
  252. mexPrintf("%8s : %5i : %8i sec : %8i usec", __TIME__, __LINE__, snow.tv_sec, snow.tv_usec); \
  253. }
  254. #define report_time_end( ) \
  255. { \
  256. gettimeofday(&enow, NULL); \
  257. 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); \
  258. }
  259. #else
  260. #define report_time_eventLoop( ){};
  261. #define report_time( ) {}
  262. #define report_time_start( ) {}
  263. #define report_time_end( ) {}
  264. #endif /* DEBUG_TIMING */
  265. /* #define TRACE */
  266. #ifdef TRACE
  267. #define ENTER(x) report("entering "x" \n");
  268. #define LEAVE(x) report("leaving "x" \n");
  269. #else
  270. #define ENTER(x)
  271. #define LEAVE(x)
  272. #endif /* TRACE */
  273. #endif /* WIN32 */
  274. /* allow people to override the directories */
  275. /* #define USER_LOCK_DIRECTORY "/home/tjarvi/1.5/build" */
  276. #ifdef USER_LOCK_DIRECTORY
  277. # define LOCKDIR USER_LOCK_DIRECTORY
  278. #endif /* USER_LOCK_DIRECTORY */
  279. #ifdef DISABLE_LOCKFILES
  280. #undef UUCP
  281. #undef FHS
  282. #undef OLDUUCP
  283. #endif /* DISABLE_LOCKFILES */
  284. /* That should be all you need to look at in this file for porting */
  285. #ifdef LFS /* Use a Lock File Server */
  286. # define LOCK lfs_lock
  287. # define UNLOCK lfs_unlock
  288. #elif defined(UUCP)
  289. # define LOCK uucp_lock
  290. # define UNLOCK uucp_unlock
  291. #elif defined(OLDUUCP)
  292. /*
  293. We can handle the old style if needed here see __unixware__ above.
  294. defaulting to rxtx-1.4-8 behavior for now.
  295. see also __sco__ in SerialImp.c when changing this for a possible
  296. bug
  297. FIXME
  298. */
  299. # define LOCK fhs_lock
  300. # define UNLOCK fhs_unlock
  301. #elif defined(FHS)
  302. # define LOCK fhs_lock
  303. # define UNLOCK fhs_unlock
  304. #else
  305. # define LOCK system_does_not_lock
  306. # define UNLOCK system_does_not_unlock
  307. #endif /* UUCP */
  308. /* java exception class names */
  309. //#define UNSUPPORTED_COMM_OPERATION "gnu/io/UnsupportedCommOperationException"
  310. #define UNSUPPORTED_COMM_OPERATION "UnsupportedCommOperationException"
  311. #define ARRAY_INDEX_OUT_OF_BOUNDS "java/lang/ArrayIndexOutOfBoundsException"
  312. #define OUT_OF_MEMORY "java/lang/OutOfMemoryError"
  313. #define IO_EXCEPTION "java/io/IOException"
  314. #define PORT_IN_USE_EXCEPTION "gnu/io/PortInUseException"
  315. /* some popular releases of Slackware do not have SSIZE_MAX */
  316. #ifndef SSIZE_MAX
  317. # if defined(INT_MAX)
  318. # define SSIZE_MAX INT_MAX
  319. # elif defined(MAXINT)
  320. # define SSIZE_MAX MAXINT
  321. # else
  322. # define SSIZE_MAX 2147483647 /* ugh */
  323. # endif
  324. #endif
  325. /*
  326. Flow Control defines inspired by reading how mgetty by Gert Doering does it
  327. */
  328. #ifdef CRTSCTS
  329. #define HARDWARE_FLOW_CONTROL CRTSCTS
  330. #else
  331. # ifdef CCTS_OFLOW
  332. # define HARDWARE_FLOW_CONTROL CCTS_OFLOW|CRST_IFLOW
  333. # else
  334. # ifdef RTSFLOW
  335. # define HARDWARE_FLOW_CONTROL RTSFLOW|CTSFLOW
  336. # else
  337. # ifdef CRTSFL
  338. # define HARDWARE_FLOW_CONTROL CRTSFL
  339. # else
  340. # ifdef CTSCD
  341. # define HARDWARE_FLOW_CONTROL CTSCD
  342. # else
  343. # define HARDWARE_FLOW_CONTROL 0
  344. # endif
  345. # endif
  346. # endif
  347. # endif
  348. #endif
  349. /* PROTOTYPES */
  350. #ifdef DEBUG_MW
  351. extern void mexWarnMsgTxt( const char * );
  352. extern void mexErrMsgTxt( const char * );
  353. #ifndef __APPLE__
  354. extern int mexPrintf( const char *, ... );
  355. # define printf mexPrintf
  356. #endif
  357. #endif /* DEBUG_MW */
  358. #ifdef __APPLE__
  359. # define mexPrintf printf
  360. #endif
  361. #ifdef __BEOS__
  362. struct tpid_info_struct *add_tpid( struct tpid_info_struct * );
  363. data_rate translate_speed( jint );
  364. int translate_data_bits( data_bits *, jint );
  365. int translate_stop_bits( stop_bits *, jint );
  366. int translate_parity( parity_mode *, jint );
  367. #else
  368. int spawn_write_thread( int, char *, int );
  369. int translate_speed( jint );
  370. int translate_data_bits( tcflag_t *, jint );
  371. int translate_stop_bits( tcflag_t *, jint );
  372. int translate_parity( tcflag_t *, jint );
  373. #endif
  374. void system_wait();
  375. void finalize_event_info_struct( struct event_info_struct * );
  376. int read_byte_array( int, unsigned char *, int, int );
  377. int get_java_var( char *, char * );
  378. jboolean is_interrupted( struct event_info_struct * );
  379. int send_event(gnu::io::RXTXPort *, struct event_info_struct *, jint, int );
  380. void dump_termios(char *,struct termios *);
  381. void report_verbose(char *);
  382. void report_error(char *);
  383. void report_warning(char *);
  384. void report(char *);
  385. void throw_java_exception( char *, char *, char * );
  386. int lock_device( const char * );
  387. void unlock_device( const char * );
  388. int is_device_locked( const char * );
  389. int check_lock_status( const char * );
  390. int lfs_unlock(const char *, int );
  391. int lfs_lock( const char *, int);
  392. void fhs_unlock(const char *, int );
  393. int fhs_lock( const char *, int);
  394. void uucp_unlock( const char *, int );
  395. int uucp_lock( const char *, int );
  396. int system_does_not_lock( const char *, int );
  397. void system_does_not_unlock( const char *, int );
  398. int check_group_uucp();
  399. int check_lock_pid( const char *, int );
  400. int printj(wchar_t *fmt, ...);
  401. #define UNEXPECTED_LOCK_FILE "RXTX Error: Unexpected lock file: %s\n Please report to the RXTX developers\n"
  402. #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"
  403. #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"