Skip to content

Commit 15805ac

Browse files
committed
adding osutil and windows lock out on the USB stac
1 parent 29d9a59 commit 15805ac

File tree

3 files changed

+168
-94
lines changed

3 files changed

+168
-94
lines changed

javasdk/NRSDK/addons/com/neuronrobotics/sdk/javaxusb/UsbCDCSerialConnection.java

Lines changed: 100 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.neuronrobotics.sdk.common.BowlerRuntimeException;
4646
import com.neuronrobotics.sdk.common.ByteList;
4747
import com.neuronrobotics.sdk.common.Log;
48+
import com.neuronrobotics.sdk.util.OsInfoUtil;
4849
import com.neuronrobotics.sdk.util.ThreadUtil;
4950

5051
public class UsbCDCSerialConnection extends BowlerAbstractConnection implements
@@ -89,55 +90,58 @@ public void abort() {
8990

9091
@Override
9192
public void run() {
92-
setName("Bowler Platform USB Events thread");
93-
while (!this.abort) {
94-
// Let libusb handle pending events. This blocks until events
95-
// have been handled, a hotplug callback has been deregistered
96-
// or the specified time of .1 second (Specified in
97-
// Microseconds) has passed.
98-
try {
99-
int result = LibUsb.handleEventsTimeoutCompleted(null, 0,
100-
null);
101-
} catch (Exception e) {
102-
e.printStackTrace();
93+
if(!OsInfoUtil.isWindows()){
94+
setName("Bowler Platform USB Events thread");
95+
while (!this.abort) {
96+
// Let libusb handle pending events. This blocks until events
97+
// have been handled, a hotplug callback has been deregistered
98+
// or the specified time of .1 second (Specified in
99+
// Microseconds) has passed.
100+
try {
101+
int result = LibUsb.handleEventsTimeoutCompleted(null, 0,
102+
null);
103+
} catch (Exception e) {
104+
e.printStackTrace();
105+
}
106+
ThreadUtil.wait(100);
103107
}
104-
ThreadUtil.wait(100);
105108
}
106109
}
107110
}
108111

109112
static {
110113
try {
111114
services = UsbHostManager.getUsbServices();
112-
callbackHandle = new HotplugCallbackHandle();
113-
int result = LibUsb.hotplugRegisterCallback(null,
114-
LibUsb.HOTPLUG_EVENT_DEVICE_ARRIVED
115-
| LibUsb.HOTPLUG_EVENT_DEVICE_LEFT,
116-
LibUsb.HOTPLUG_ENUMERATE, LibUsb.HOTPLUG_MATCH_ANY,
117-
LibUsb.HOTPLUG_MATCH_ANY, LibUsb.HOTPLUG_MATCH_ANY,
118-
new HotplugCallback() {
119-
120-
@Override
121-
public int processEvent(Context arg0, Device arg1,
122-
int arg2, Object arg3) {
123-
DeviceDescriptor descriptor = new DeviceDescriptor();
124-
int result = LibUsb.getDeviceDescriptor(arg1,
125-
descriptor);
126-
if (result != LibUsb.SUCCESS)
127-
throw new LibUsbException(
128-
"Unable to read device descriptor",
129-
result);
130-
if (0x04d8 == descriptor.idVendor()) {
131-
for (IUsbDeviceEventListener d : usbDeviceEventListeners) {
132-
d.onDeviceEvent(mapLibUsbDevicetoJavaxDevice(arg1));
115+
if(!OsInfoUtil.isWindows()){
116+
callbackHandle = new HotplugCallbackHandle();
117+
int result = LibUsb.hotplugRegisterCallback(null,
118+
LibUsb.HOTPLUG_EVENT_DEVICE_ARRIVED
119+
| LibUsb.HOTPLUG_EVENT_DEVICE_LEFT,
120+
LibUsb.HOTPLUG_ENUMERATE, LibUsb.HOTPLUG_MATCH_ANY,
121+
LibUsb.HOTPLUG_MATCH_ANY, LibUsb.HOTPLUG_MATCH_ANY,
122+
new HotplugCallback() {
123+
124+
@Override
125+
public int processEvent(Context arg0, Device arg1,
126+
int arg2, Object arg3) {
127+
DeviceDescriptor descriptor = new DeviceDescriptor();
128+
int result = LibUsb.getDeviceDescriptor(arg1,
129+
descriptor);
130+
if (result != LibUsb.SUCCESS)
131+
throw new LibUsbException(
132+
"Unable to read device descriptor",
133+
result);
134+
if (0x04d8 == descriptor.idVendor()) {
135+
for (IUsbDeviceEventListener d : usbDeviceEventListeners) {
136+
d.onDeviceEvent(mapLibUsbDevicetoJavaxDevice(arg1));
137+
}
133138
}
139+
return 0;
134140
}
135-
return 0;
136-
}
137-
}, null, callbackHandle);
138-
if (result != LibUsb.SUCCESS) {
139-
throw new LibUsbException(
140-
"Unable to register hotplug callback", result);
141+
}, null, callbackHandle);
142+
if (result != LibUsb.SUCCESS) {
143+
throw new LibUsbException("Unable to register hotplug callback", result);
144+
}
141145
}
142146
} catch (SecurityException e) {
143147
// TODO Auto-generated catch block
@@ -148,7 +152,7 @@ public int processEvent(Context arg0, Device arg1,
148152
}
149153

150154
// Start the event handling thread
151-
thread = new EventHandlingThread();
155+
thread = new EventHandlingThread();https://github.com/NeuronRobotics/NrConsole.git
152156
thread.start();
153157
}
154158

@@ -371,63 +375,65 @@ public boolean connect() {
371375
}
372376

373377
public Device findDevice(String seriualNumber) {
374-
// Read the USB device list
375-
DeviceList list = new DeviceList();
376-
int result = LibUsb.getDeviceList(null, list);
377-
if (result < 0)
378-
throw new LibUsbException("Unable to get device list", result);
379-
380-
try {
381-
// Iterate over all devices and scan for the right one
382-
for (Device device : list) {
383-
384-
DeviceDescriptor descriptor = new DeviceDescriptor();
385-
result = LibUsb.getDeviceDescriptor(device, descriptor);
386-
if (result != LibUsb.SUCCESS)
387-
throw new LibUsbException(
388-
"Unable to read device descriptor", result);
389-
DeviceHandle handle = new DeviceHandle();
390-
result = LibUsb.open(device, handle);
391-
if (result == LibUsb.SUCCESS) {
392-
String sn = LibUsb.getStringDescriptor(handle,
393-
descriptor.iSerialNumber()).trim();
394-
LibUsb.close(handle);
395-
if (sn.contains(seriualNumber.trim())) {
396-
397-
return device;
378+
if(!OsInfoUtil.isWindows()){
379+
// Read the USB device list
380+
DeviceList list = new DeviceList();
381+
int result = LibUsb.getDeviceList(null, list);
382+
if (result < 0)
383+
throw new LibUsbException("Unable to get device list", result);
384+
385+
try {
386+
// Iterate over all devices and scan for the right one
387+
for (Device device : list) {
388+
389+
DeviceDescriptor descriptor = new DeviceDescriptor();
390+
result = LibUsb.getDeviceDescriptor(device, descriptor);
391+
if (result != LibUsb.SUCCESS)
392+
throw new LibUsbException(
393+
"Unable to read device descriptor", result);
394+
DeviceHandle handle = new DeviceHandle();
395+
result = LibUsb.open(device, handle);
396+
if (result == LibUsb.SUCCESS) {
397+
String sn = LibUsb.getStringDescriptor(handle,
398+
descriptor.iSerialNumber()).trim();
399+
LibUsb.close(handle);
400+
if (sn.contains(seriualNumber.trim())) {
401+
402+
return device;
403+
}
398404
}
399405
}
406+
} finally {
407+
// Ensure the allocated device list is freed
408+
LibUsb.freeDeviceList(list, true);
400409
}
401-
} finally {
402-
// Ensure the allocated device list is freed
403-
LibUsb.freeDeviceList(list, true);
404410
}
405-
406411
// Device not found
407412
return null;
408413
}
409414

410415
private void kernelDetatch(UsbDevice mDevice)
411416
throws UnsupportedEncodingException, UsbDisconnectedException,
412417
UsbException {
413-
Device kDev = findDevice(mDevice.getSerialNumberString());
414-
if (kDev == null)
415-
return;
416-
417-
deviceHandle = new DeviceHandle();
418-
interfaceNumber = dataInterface.getUsbInterfaceDescriptor()
419-
.bInterfaceNumber();
420-
421-
int result = LibUsb.open(kDev, deviceHandle);
422-
if (result != LibUsb.SUCCESS)
423-
throw new LibUsbException("Unable to open USB device", result);
424-
425-
int r = LibUsb.detachKernelDriver(deviceHandle, interfaceNumber);
426-
if (r != LibUsb.SUCCESS && r != LibUsb.ERROR_NOT_SUPPORTED
427-
&& r != LibUsb.ERROR_NOT_FOUND)
428-
throw new LibUsbException("Unable to detach kernel driver", r);
429-
// System.out.println("Kernel detatched for device "+mDevice);
430-
418+
if(!OsInfoUtil.isWindows()){
419+
Device kDev = findDevice(mDevice.getSerialNumberString());
420+
if (kDev == null)
421+
return;
422+
423+
deviceHandle = new DeviceHandle();
424+
interfaceNumber = dataInterface.getUsbInterfaceDescriptor()
425+
.bInterfaceNumber();
426+
427+
int result = LibUsb.open(kDev, deviceHandle);
428+
if (result != LibUsb.SUCCESS)
429+
throw new LibUsbException("Unable to open USB device", result);
430+
431+
int r = LibUsb.detachKernelDriver(deviceHandle, interfaceNumber);
432+
if (r != LibUsb.SUCCESS && r != LibUsb.ERROR_NOT_SUPPORTED
433+
&& r != LibUsb.ERROR_NOT_FOUND)
434+
throw new LibUsbException("Unable to detach kernel driver", r);
435+
// System.out.println("Kernel detatched for device "+mDevice);
436+
}
431437
}
432438

433439
/*
@@ -446,13 +452,14 @@ public void disconnect() {
446452
camOutpipe.close();
447453
} catch (Exception e1) {
448454
}
449-
450-
if (deviceHandle != null) {
451-
try{
452-
LibUsb.attachKernelDriver(deviceHandle, interfaceNumber);
453-
LibUsb.close(deviceHandle);
454-
}catch (Exception e){
455-
455+
if(!OsInfoUtil.isWindows()){
456+
if (deviceHandle != null) {
457+
try{
458+
LibUsb.attachKernelDriver(deviceHandle, interfaceNumber);
459+
LibUsb.close(deviceHandle);
460+
}catch (Exception e){
461+
462+
}
456463
}
457464
}
458465
try {

javasdk/NRSDK/addons/com/neuronrobotics/sdk/ui/ConnectionDialog.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ private void loadDefaultConnections() {
111111
addConnectionPanel(new BluetoothConnectionPanel());
112112
addConnectionPanel(new SerialConnectionPanel());
113113
}catch(Error e){
114-
Log.error("This is not a java 7 compliant system, removing the serial, bluetooth and usb connections");
114+
e.printStackTrace();
115+
Log.error("This is not a java 8 compliant system, removing the serial, bluetooth and usb connections");
115116
}
116117
addConnectionPanel(new UDPConnectionPanel());
117118
addConnectionPanel(new TCPConnectionPanel());
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.neuronrobotics.sdk.util;
2+
3+
public class OsInfoUtil {
4+
public static boolean is64Bit() {
5+
// //System.out.println("Arch: "+getOsArch());
6+
return getOsArch().startsWith("x86_64")
7+
|| getOsArch().startsWith("amd64");
8+
}
9+
10+
public static boolean isARM() {
11+
return getOsArch().startsWith("arm");
12+
}
13+
14+
public static boolean isPPC() {
15+
return getOsArch().toLowerCase().contains("ppc");
16+
}
17+
18+
public static boolean isCortexA8() {
19+
if (isARM()) {
20+
// TODO check for cortex a8 vs arm9 generic
21+
return true;
22+
}
23+
return false;
24+
}
25+
26+
public static boolean isWindows() {
27+
// //System.out.println("OS name: "+getOsName());
28+
return getOsName().toLowerCase().startsWith("windows")
29+
|| getOsName().toLowerCase().startsWith("microsoft")
30+
|| getOsName().toLowerCase().startsWith("ms");
31+
}
32+
33+
public static boolean isLinux() {
34+
return getOsName().toLowerCase().startsWith("linux");
35+
}
36+
37+
public static boolean isOSX() {
38+
return getOsName().toLowerCase().startsWith("mac");
39+
}
40+
41+
public static String getExtension() {
42+
if (isWindows()) {
43+
return ".zip";
44+
}
45+
if (isLinux()) {
46+
return ".zip";
47+
}
48+
if (isOSX()) {
49+
return ".dmg";
50+
}
51+
return "";
52+
}
53+
54+
public static String getOsName() {
55+
return System.getProperty("os.name");
56+
}
57+
58+
public static String getOsArch() {
59+
return System.getProperty("os.arch");
60+
}
61+
62+
@SuppressWarnings("unused")
63+
public static String getIdentifier() {
64+
return getOsName() + " : " + getOsArch();
65+
}
66+
}

0 commit comments

Comments
 (0)