-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathSerialNativeInterface.java
More file actions
487 lines (444 loc) · 15 KB
/
SerialNativeInterface.java
File metadata and controls
487 lines (444 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* jSSC (Java Simple Serial Connector) - serial port communication library.
* © Alexey Sokolov (scream3r), 2010-2014.
*
* This file is part of jSSC.
*
* jSSC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* jSSC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with jSSC. If not, see <http://www.gnu.org/licenses/>.
*
* If you use jSSC in public project you can inform me about this by e-mail,
* of course if you want it.
*
* e-mail: scream3r.org@gmail.com
* web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/
*/
package jssc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
*
* @author scream3r
*/
public class SerialNativeInterface {
private static final String libVersion = "2.8"; //jSSC-2.8.0 Release from 24.01.2014
private static final String libMinorSuffix = "0"; //since 0.9.0
public static final int OS_LINUX = 0;
public static final int OS_WINDOWS = 1;
public static final int OS_SOLARIS = 2;//since 0.9.0
public static final int OS_MAC_OS_X = 3;//since 0.9.0
private static int osType = -1;
/**
* @since 2.3.0
*/
public static final long ERR_PORT_BUSY = -1;
/**
* @since 2.3.0
*/
public static final long ERR_PORT_NOT_FOUND = -2;
/**
* @since 2.3.0
*/
public static final long ERR_PERMISSION_DENIED = -3;
/**
* @since 2.3.0
*/
public static final long ERR_INCORRECT_SERIAL_PORT = -4;
/**
* @since 2.6.0
*/
public static final String PROPERTY_JSSC_NO_TIOCEXCL = "JSSC_NO_TIOCEXCL";
/**
* @since 2.6.0
*/
public static final String PROPERTY_JSSC_IGNPAR = "JSSC_IGNPAR";
/**
* @since 2.6.0
*/
public static final String PROPERTY_JSSC_PARMRK = "JSSC_PARMRK";
static {
String libFolderPath;
String libName;
String osName = System.getProperty("os.name");
String architecture = System.getProperty("os.arch");
String userHome = System.getProperty("user.home");
String fileSeparator = System.getProperty("file.separator");
String tmpFolder = System.getProperty("java.io.tmpdir");
//since 2.3.0 ->
String libRootFolder = new File(userHome).canWrite() ? userHome : tmpFolder;
//<- since 2.3.0
String javaLibPath = System.getProperty("java.library.path");//since 2.1.0
if(osName.equals("Linux")){
osName = "linux";
osType = OS_LINUX;
}
else if(osName.startsWith("Win")){
osName = "windows";
osType = OS_WINDOWS;
}//since 0.9.0 ->
else if(osName.equals("SunOS")){
osName = "solaris";
osType = OS_SOLARIS;
}
else if(osName.equals("Mac OS X") || osName.equals("Darwin")){//os.name "Darwin" since 2.6.0
osName = "mac_os_x";
osType = OS_MAC_OS_X;
}//<- since 0.9.0
if(architecture.equals("i386") || architecture.equals("i686")){
architecture = "x86";
}
else if(architecture.equals("amd64") || architecture.equals("universal")){//os.arch "universal" since 2.6.0
architecture = "x86_64";
}
else if(architecture.equals("arm")) {//since 2.1.0
String floatStr = "sf";
if(javaLibPath.toLowerCase().contains("gnueabihf") || javaLibPath.toLowerCase().contains("armhf")){
floatStr = "hf";
}
else {
try {
// get the ELF format of the JVM binary to know which library should be loaded (soft-float or hard-float)
Process readelfProcess = Runtime.getRuntime().exec("readelf -A " + System.getProperty("java.home") + "/bin/java");
BufferedReader reader = new BufferedReader(new InputStreamReader(readelfProcess.getInputStream()));
String buffer = "";
while((buffer = reader.readLine()) != null && !buffer.isEmpty()){
if(buffer.toLowerCase().contains("Tag_ABI_VFP_args".toLowerCase())){
floatStr = "hf";
break;
}
}
reader.close();
}
catch (Exception ex) {
//Do nothing
}
}
architecture = "arm" + floatStr;
}
libFolderPath = libRootFolder + fileSeparator + ".jssc" + fileSeparator + osName;
libName = "jSSC-" + libVersion + "_" + architecture;
libName = System.mapLibraryName(libName);
if(libName.endsWith(".dylib")){//Since 2.1.0 MacOSX 10.8 fix
libName = libName.replace(".dylib", ".jnilib");
}
boolean loadLib = false;
if(isLibFolderExist(libFolderPath)){
if(isLibFileExist(libFolderPath + fileSeparator + libName)){
loadLib = true;
}
else {
if(extractLib((libFolderPath + fileSeparator + libName), osName, libName)){
loadLib = true;
}
}
}
else {
if(new File(libFolderPath).mkdirs()){
if(extractLib((libFolderPath + fileSeparator + libName), osName, libName)){
loadLib = true;
}
}
}
if (loadLib) {
System.load(libFolderPath + fileSeparator + libName);
String versionBase = getLibraryBaseVersion();
String versionNative = getNativeLibraryVersion();
if (!versionBase.equals(versionNative)) {
System.err.println("Warning! jSSC Java and Native versions mismatch (Java: " + versionBase + ", Native: " + versionNative + ")");
}
}
}
/**
* Is library folder exists
*
* @param libFolderPath
*
* @since 0.8
*/
private static boolean isLibFolderExist(String libFolderPath) {
boolean returnValue = false;
File folder = new File(libFolderPath);
if(folder.exists() && folder.isDirectory()){
returnValue = true;
}
return returnValue;
}
/**
* Is library file exists
*
* @param libFilePath
*
* @since 0.8
*/
private static boolean isLibFileExist(String libFilePath) {
boolean returnValue = false;
File folder = new File(libFilePath);
if(folder.exists() && folder.isFile()){
returnValue = true;
}
return returnValue;
}
/**
* Extract lib to lib folder
*
* @param libFilePath
* @param osName
* @param libName
*
* @since 0.8
*/
private static boolean extractLib(String libFilePath, String osName, String libName) {
boolean returnValue = false;
File libFile = new File(libFilePath);
InputStream input = null;
FileOutputStream output = null;
input = SerialNativeInterface.class.getResourceAsStream("/libs/" + osName + "/" + libName);
if(input != null){
int read;
byte[] buffer = new byte[4096];
try {
output = new FileOutputStream(libFilePath);
while((read = input.read(buffer)) != -1){
output.write(buffer, 0, read);
}
output.close();
input.close();
returnValue = true;
}
catch (Exception ex) {
try {
output.close();
if(libFile.exists()){
libFile.delete();
}
}
catch (Exception ex_out) {
//Do nothing
}
try {
input.close();
}
catch (Exception ex_in) {
//Do nothing
}
}
}
return returnValue;
}
/**
* Get OS type (OS_LINUX || OS_WINDOWS || OS_SOLARIS)
*
* @since 0.8
*/
public static int getOsType() {
return osType;
}
/**
* Get jSSC version. The version of library is <b>Base Version</b> + <b>Minor Suffix</b>
*
* @since 0.8
*/
public static String getLibraryVersion() {
return libVersion + "." + libMinorSuffix;
}
/**
* Get jSSC Base Version
*
* @since 0.9.0
*/
public static String getLibraryBaseVersion() {
return libVersion;
}
/**
* Get jSSC minor suffix. For example in version 0.8.1 - <b>1</b> is a minor suffix
*
* @since 0.9.0
*/
public static String getLibraryMinorSuffix() {
return libMinorSuffix;
}
/**
* Get jSSC native library version
*
* @return native lib version (for jSSC-2.8.0 should be 2.8 for example)
*
* @since 2.8.0
*/
public static native String getNativeLibraryVersion();
/**
* Open port
*
* @param portName name of port for opening
* @param useTIOCEXCL enable/disable using of <b>TIOCEXCL</b>. Take effect only on *nix based systems
*
* @return handle of opened port or -1 if opening of the port was unsuccessful
*/
public native long openPort(String portName, boolean useTIOCEXCL);
/**
* Setting the parameters of opened port
*
* @param handle handle of opened port
* @param baudRate data transfer rate
* @param dataBits number of data bits
* @param stopBits number of stop bits
* @param parity parity
* @param setRTS initial state of RTS line (ON/OFF)
* @param setDTR initial state of DTR line (ON/OFF)
* @param flags additional Native settings. Take effect only on *nix based systems
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean setParams(long handle, int baudRate, int dataBits, int stopBits, int parity, boolean setRTS, boolean setDTR, int flags);
/**
* Purge of input and output buffer
*
* @param handle handle of opened port
* @param flags flags specifying required actions for purgePort method
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean purgePort(long handle, int flags);
/**
* Close port
*
* @param handle handle of opened port
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean closePort(long handle);
/**
* Set events mask
*
* @param handle handle of opened port
* @param mask events mask
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean setEventsMask(long handle, int mask);
/**
* Get events mask
*
* @param handle handle of opened port
*
* @return Method returns event mask as a variable of <b>int</b> type
*/
public native int getEventsMask(long handle);
/**
* Wait events
*
* @param handle handle of opened port
*
* @return Method returns two-dimensional array containing event types and their values
* (<b>events[i][0] - event type</b>, <b>events[i][1] - event value</b>).
*/
public native int[][] waitEvents(long handle);
/**
* Change RTS line state
*
* @param handle handle of opened port
* @param value <b>true - ON</b>, <b>false - OFF</b>
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean setRTS(long handle, boolean value);
/**
* Change DTR line state
*
* @param handle handle of opened port
* @param value <b>true - ON</b>, <b>false - OFF</b>
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean setDTR(long handle, boolean value);
/**
* Read data from port
*
* @param handle handle of opened port
* @param byteCount count of bytes required to read
*
* @return Method returns the array of read bytes
*/
public native byte[] readBytes(long handle, int byteCount);
/**
* Write data to port
*
* @param handle handle of opened port
* @param buffer array of bytes to write
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean writeBytes(long handle, byte[] buffer);
/**
* Get bytes count in buffers of port
*
* @param handle handle of opened port
*
* @return Method returns the array that contains info about bytes count in buffers:
* <br><b>element 0</b> - input buffer</br>
* <br><b>element 1</b> - output buffer</br>
*
* @since 0.8
*/
public native int[] getBuffersBytesCount(long handle);
/**
* Set flow control mode
*
* @param handle handle of opened port
* @param mask mask of flow control mode
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @since 0.8
*/
public native boolean setFlowControlMode(long handle, int mask);
/**
* Get flow control mode
*
* @param handle handle of opened port
*
* @return Mask of setted flow control mode
*
* @since 0.8
*/
public native int getFlowControlMode(long handle);
/**
* Get serial port names like an array of String
*
* @return unsorted array of String with port names
*/
public native String[] getSerialPortNames();
/**
* Getting lines states
*
* @param handle handle of opened port
*
* @return Method returns the array containing information about lines in following order:
* <br><b>element 0</b> - <b>CTS</b> line state</br>
* <br><b>element 1</b> - <b>DSR</b> line state</br>
* <br><b>element 2</b> - <b>RING</b> line state</br>
* <br><b>element 3</b> - <b>RLSD</b> line state</br>
*/
public native int[] getLinesStatus(long handle);
/**
* Send Break singnal for setted duration
*
* @param handle handle of opened port
* @param duration duration of Break signal
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @since 0.8
*/
public native boolean sendBreak(long handle, int duration);
}