4545import com .neuronrobotics .sdk .common .BowlerRuntimeException ;
4646import com .neuronrobotics .sdk .common .ByteList ;
4747import com .neuronrobotics .sdk .common .Log ;
48+ import com .neuronrobotics .sdk .util .OsInfoUtil ;
4849import com .neuronrobotics .sdk .util .ThreadUtil ;
4950
5051public 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 {
0 commit comments