Package: jssc / 2.6.0-5

0002-Make-it-possible-to-retrieve-information-about-the-U.patch Patch series | download
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
From: gohai <gottfried.haider@gmail.com>
Date: Wed, 11 Sep 2013 00:07:55 -0700
Subject: Make it possible to retrieve information about the USB device that
 provides the serial port

getPortProperties() returns a map with keys and values as strings. The currently available keys are: idProduct (lowercase hexadecimal zero-padded to four digits), idVendor (as idProduct), manufacturer, product, serial. This is currently implemented for Linux and Mac OS X.

Origin: other, https://github.com/gohai/java-simple-serial-connector/commit/5c61d622b71ee5ece5ab3b31397e2cca34eed8e9
Forwarded: https://github.com/scream3r/java-simple-serial-connector/pull/33
Bug-Debian: http://bugs.debian.org/734820
---
 src/cpp/_nix_based/jssc.cpp              | 135 +++++++++++++++++++++++++++++++
 src/cpp/jssc_SerialNativeInterface.h     |   8 ++
 src/java/jssc/SerialNativeInterface.java |   2 +
 src/java/jssc/SerialPortList.java        |  75 +++++++++++++++++
 4 files changed, 220 insertions(+)

diff --git a/src/cpp/_nix_based/jssc.cpp b/src/cpp/_nix_based/jssc.cpp
index 230e248..9468bc0 100644
--- a/src/cpp/_nix_based/jssc.cpp
+++ b/src/cpp/_nix_based/jssc.cpp
@@ -40,7 +40,12 @@
     #include <string.h>//Needed for select() function
 #endif
 #ifdef __APPLE__
+    #include <CoreFoundation/CoreFoundation.h>
+    #include <IOKit/IOKitLib.h>
+    #include <IOKit/serial/IOSerialKeys.h>
+    #include <IOKit/usb/USBSpec.h>
     #include <serial/ioss.h>//Needed for IOSSIOSPEED in Mac OS X (Non standard baudrate)
+    #include <sys/param.h> // Needed for MAXPATHLEN
 #endif
 
 #include <jni.h>
@@ -865,3 +870,133 @@ JNIEXPORT jintArray JNICALL Java_jssc_SerialNativeInterface_getLinesStatus
     env->SetIntArrayRegion(returnArray, 0, 4, returnValues);
     return returnArray;
 }
+
+JNIEXPORT jobjectArray JNICALL Java_jssc_SerialNativeInterface_getPortProperties
+  (JNIEnv *env, jclass cls, jstring portName) {
+    const char* portNameChar = (const char*)env->GetStringUTFChars(portName, NULL);
+    jclass stringClass = env->FindClass("Ljava/lang/String;");
+    jobjectArray ret = env->NewObjectArray(5, stringClass, NULL);
+
+#ifdef __APPLE__
+
+    // this code is based on QtSerialPort
+    CFMutableDictionaryRef matching = IOServiceMatching(kIOSerialBSDServiceValue);
+    io_iterator_t iter = 0;
+    kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &iter);
+    if (kr != kIOReturnSuccess) {
+        env->ReleaseStringUTFChars(portName, portNameChar);
+        return ret;
+    }
+
+    io_registry_entry_t service;
+    while ((service = IOIteratorNext(iter))) {
+
+        // compare portName against cu and tty devices
+        bool found = false;
+
+        CFTypeRef cu = 0;
+        cu = IORegistryEntrySearchCFProperty(service, kIOServicePlane, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
+        if (cu) {
+            char buffer[MAXPATHLEN];
+            CFStringGetCString(CFStringRef(cu), buffer, sizeof(buffer), kCFStringEncodingUTF8);
+            //fprintf(stdout, "getPortProperties: %s\n", buffer);
+            //fflush(stdout);
+            if (strcmp(portNameChar, buffer) == 0) {
+                found = true;
+            }
+            CFRelease(cu);
+        }
+
+        CFTypeRef tty = 0;
+        tty = IORegistryEntrySearchCFProperty(service, kIOServicePlane, CFSTR(kIODialinDeviceKey), kCFAllocatorDefault, 0);
+        if (tty) {
+            char buffer[MAXPATHLEN];
+            CFStringGetCString(CFStringRef(tty), buffer, sizeof(buffer), kCFStringEncodingUTF8);
+            //fprintf(stdout, "getPortProperties: %s\n", buffer);
+            //fflush(stdout);
+            if (strcmp(portNameChar, buffer) == 0) {
+                found = true;
+            }
+            CFRelease(tty);
+        }
+
+        if (!found) {
+            // not port we're looking for
+            //fprintf(stderr, "getPortProperties: %s not found", portNameChar);
+            //fflush(stderr);
+            IOObjectRelease(service);
+            continue;
+        }
+
+        io_registry_entry_t entry = service;
+        do {
+            int val = 0;
+            char buffer[255];
+
+            CFTypeRef idProduct = 0;
+            idProduct = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBProductID), kCFAllocatorDefault, 0);
+            if (idProduct && !env->GetObjectArrayElement(ret, 0)) {
+                CFNumberGetValue(CFNumberRef(idProduct), kCFNumberIntType, &val);
+                sprintf(buffer, "%04x", val);
+                jstring tmp = env->NewStringUTF(buffer);
+                env->SetObjectArrayElement(ret, 0, tmp);
+                env->DeleteLocalRef(tmp);
+                CFRelease(idProduct);
+            }
+
+            CFTypeRef idVendor = 0;
+            idVendor = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBVendorID), kCFAllocatorDefault, 0);
+            if (idVendor && !env->GetObjectArrayElement(ret, 1)) {
+                CFNumberGetValue(CFNumberRef(idVendor), kCFNumberIntType, &val);
+                sprintf(buffer, "%04x", val);
+                jstring tmp = env->NewStringUTF(buffer);
+                env->SetObjectArrayElement(ret, 1, tmp);
+                env->DeleteLocalRef(tmp);
+                CFRelease(idVendor);
+            }
+
+            CFTypeRef manufacturer = 0;
+            manufacturer = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBVendorString), kCFAllocatorDefault, 0);
+            if (manufacturer && !env->GetObjectArrayElement(ret, 2)) {
+                CFStringGetCString(CFStringRef(manufacturer), buffer, sizeof(buffer), kCFStringEncodingUTF8);
+                jstring tmp = env->NewStringUTF(buffer);
+                env->SetObjectArrayElement(ret, 2, tmp);
+                env->DeleteLocalRef(tmp);
+                CFRelease(manufacturer);
+            }
+
+            CFTypeRef product = 0;
+            product = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBProductString), kCFAllocatorDefault, 0);
+            if (product && !env->GetObjectArrayElement(ret, 3)) {
+                CFStringGetCString(CFStringRef(product), buffer, sizeof(buffer), kCFStringEncodingUTF8);
+                jstring tmp = env->NewStringUTF(buffer);
+                env->SetObjectArrayElement(ret, 3, tmp);
+                env->DeleteLocalRef(tmp);
+                CFRelease(product);
+            }
+
+            CFTypeRef serial = 0;
+            serial = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBSerialNumberString), kCFAllocatorDefault, 0);
+            if (serial && !env->GetObjectArrayElement(ret, 4)) {
+                CFStringGetCString(CFStringRef(serial), buffer, sizeof(buffer), kCFStringEncodingUTF8);
+                jstring tmp = env->NewStringUTF(buffer);
+                env->SetObjectArrayElement(ret, 4, tmp);
+                env->DeleteLocalRef(tmp);
+                CFRelease(serial);
+            }
+
+            kr = IORegistryEntryGetParentEntry(entry, kIOServicePlane, &entry);
+        } while (kr == kIOReturnSuccess);
+
+        IOObjectRelease(entry);
+
+        IOObjectRelease(service);
+    }
+
+    IOObjectRelease(iter);
+
+#endif  // __APPLE__
+
+    env->ReleaseStringUTFChars(portName, portNameChar);
+    return ret;
+}
diff --git a/src/cpp/jssc_SerialNativeInterface.h b/src/cpp/jssc_SerialNativeInterface.h
index e3182e1..3d64631 100644
--- a/src/cpp/jssc_SerialNativeInterface.h
+++ b/src/cpp/jssc_SerialNativeInterface.h
@@ -183,6 +183,14 @@ JNIEXPORT jintArray JNICALL Java_jssc_SerialNativeInterface_getLinesStatus
 JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_sendBreak
   (JNIEnv *, jobject, jlong, jint);
 
+/*
+ * Class:     jssc_SerialNativeInterface
+ * Method:    getPortIdProduct
+ * Signature: (Ljava/lang/String;)Ljava/lang/String;
+ */
+JNIEXPORT jobjectArray JNICALL Java_jssc_SerialNativeInterface_getPortProperties
+  (JNIEnv *, jclass, jstring);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/java/jssc/SerialNativeInterface.java b/src/java/jssc/SerialNativeInterface.java
index 50eec84..443b791 100644
--- a/src/java/jssc/SerialNativeInterface.java
+++ b/src/java/jssc/SerialNativeInterface.java
@@ -469,4 +469,6 @@ public class SerialNativeInterface {
      * @since 0.8
      */
     public native boolean sendBreak(long handle, int duration);
+
+    public static native String[] getPortProperties(String portName);
 }
diff --git a/src/java/jssc/SerialPortList.java b/src/java/jssc/SerialPortList.java
index e65df18..0e4b238 100644
--- a/src/java/jssc/SerialPortList.java
+++ b/src/java/jssc/SerialPortList.java
@@ -25,7 +25,11 @@
 package jssc;
 
 import java.io.File;
+import java.io.FileReader;
 import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Scanner;
 import java.util.TreeSet;
 import java.util.regex.Pattern;
 
@@ -296,6 +300,77 @@ public class SerialPortList {
         return getUnixBasedPortNames(searchPath, pattern, comparator);
     }
 
+    public static Map<String, String> getPortProperties(String portName) {
+        if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX) {
+            return getLinuxPortProperties(portName);
+        } else if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X) {
+            return getNativePortProperties(portName);
+        } else if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS){
+            // TODO
+            return new HashMap<String, String>();
+        } else {
+            return new HashMap<String, String>();
+        }
+    }
+
+    public static Map<String, String> getLinuxPortProperties(String portName) {
+        Map<String, String> props = new HashMap<String, String>();
+        try {
+            // portName has the format /dev/ttyUSB0
+            String dev = portName.split("/")[2];
+            File sysfsNode = new File("/sys/bus/usb-serial/devices/"+dev);
+
+            // resolve the symbolic link and store the resulting components in an array
+            String[] sysfsPath = sysfsNode.getCanonicalPath().split("/");
+
+            // walk the tree to the root
+            for (int i=sysfsPath.length-2; 0 < i; i--) {
+                String curPath = "/";
+                for (int j=1; j <= i; j++) {
+                    curPath += sysfsPath[j]+"/";
+                }
+
+                // look for specific attributes
+                String[] attribs = { "idProduct", "idVendor", "manufacturer", "product", "serial" };
+                for (int j=0; j < attribs.length; j++) {
+                    try {
+                        Scanner in = new Scanner(new FileReader(curPath+attribs[j]));
+                        // we treat the values just as strings
+                        props.put(attribs[j], in.next());
+                    } catch (Exception e) {
+                        // ignore the attribute
+                    }
+                }
+
+                // stop once we have at least one attribute
+                if (0 < props.size()) {
+                    break;
+                }
+            }
+        } catch (Exception e) {
+            // nothing to do, return what we have so far
+        }
+        return props;
+    }
+
+    public static Map<String, String> getNativePortProperties(String portName) {
+        Map<String, String> props = new HashMap<String, String>();
+        try {
+            // use JNI functions to read those properties
+            String[] names = { "idProduct", "idVendor", "manufacturer", "product", "serial" };
+            String[] values = SerialNativeInterface.getPortProperties(portName);
+
+            for (int i=0; i < names.length; i++) {
+                if (values[i] != null) {
+                    props.put(names[i], values[i]);
+                }
+            }
+        } catch (Exception e) {
+            // nothing to do, return what we have so far
+        }
+        return props;
+    }
+
     /**
      * Get serial port names in Windows
      *