1 package com.neuronrobotics.sdk.javaxusb;
3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
9 import javax.usb.UsbClaimException;
10 import javax.usb.UsbConfiguration;
11 import javax.usb.UsbConst;
12 import javax.usb.UsbControlIrp;
13 import javax.usb.UsbDevice;
14 import javax.usb.UsbDisconnectedException;
15 import javax.usb.UsbEndpoint;
16 import javax.usb.UsbException;
17 import javax.usb.UsbHostManager;
18 import javax.usb.UsbHub;
19 import javax.usb.UsbInterface;
20 import javax.usb.UsbInterfaceDescriptor;
21 import javax.usb.UsbIrp;
22 import javax.usb.UsbNotActiveException;
23 import javax.usb.UsbNotOpenException;
24 import javax.usb.UsbPipe;
25 import javax.usb.UsbServices;
26 import javax.usb.event.UsbDeviceDataEvent;
27 import javax.usb.event.UsbDeviceErrorEvent;
28 import javax.usb.event.UsbDeviceEvent;
29 import javax.usb.event.UsbDeviceListener;
30 import javax.usb.util.DefaultUsbIrp;
32 import org.usb4java.Context;
33 import org.usb4java.Device;
34 import org.usb4java.DeviceDescriptor;
35 import org.usb4java.DeviceHandle;
36 import org.usb4java.DeviceList;
37 import org.usb4java.HotplugCallback;
38 import org.usb4java.HotplugCallbackHandle;
39 import org.usb4java.LibUsb;
40 import org.usb4java.LibUsbException;
42 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
43 import com.neuronrobotics.sdk.common.BowlerDatagram;
44 import com.neuronrobotics.sdk.common.BowlerDatagramFactory;
45 import com.neuronrobotics.sdk.common.BowlerRuntimeException;
46 import com.neuronrobotics.sdk.common.ByteList;
47 import com.neuronrobotics.sdk.common.Log;
48 import com.neuronrobotics.sdk.util.OsInfoUtil;
50 import com.neuronrobotics.sdk.util.ThreadUtil;
60 static UsbServices services =
null;
89 private UsbIrp
read =
new DefaultUsbIrp();
95 private UsbIrp
write =
new DefaultUsbIrp();
104 private static EventHandlingThread
thread;
107 byte[] data =
new byte[64];
127 throw new NullPointerException(
128 "A valid USB device is needed to regester this connection.");
133 }
catch (UnsupportedEncodingException e) {
136 }
catch (UsbDisconnectedException e) {
139 }
catch (UsbException e) {
152 static class EventHandlingThread
extends Thread {
154 private volatile boolean abort;
159 public void abort() {
169 setName(
"Bowler Platform USB Events thread");
170 while (!this.abort) {
176 int result = LibUsb.handleEventsTimeoutCompleted(
null, 0,
178 }
catch (Exception e) {
181 ThreadUtil.wait(100);
197 services = UsbHostManager.getUsbServices();
200 int result = LibUsb.hotplugRegisterCallback(
null,
201 LibUsb.HOTPLUG_EVENT_DEVICE_ARRIVED
202 | LibUsb.HOTPLUG_EVENT_DEVICE_LEFT,
203 LibUsb.HOTPLUG_ENUMERATE, LibUsb.HOTPLUG_MATCH_ANY,
204 LibUsb.HOTPLUG_MATCH_ANY, LibUsb.HOTPLUG_MATCH_ANY,
205 new HotplugCallback() {
208 public int processEvent(Context arg0, Device arg1,
209 int arg2, Object arg3) {
210 DeviceDescriptor descriptor =
new DeviceDescriptor();
211 int result = LibUsb.getDeviceDescriptor(arg1,
213 if (result != LibUsb.SUCCESS)
214 throw new LibUsbException(
215 "Unable to read device descriptor",
217 if (0x04d8 == descriptor.idVendor()) {
225 if (result != LibUsb.SUCCESS) {
226 throw new LibUsbException(
"Unable to register hotplug callback", result);
229 }
catch (SecurityException e) {
232 }
catch (UsbException e) {
240 thread =
new EventHandlingThread();
272 DeviceDescriptor descriptor =
new DeviceDescriptor();
273 LibUsb.getDeviceDescriptor(device, descriptor);
275 for (UsbDevice d : javaxDev) {
276 if (descriptor.iSerialNumber() == d.getUsbDeviceDescriptor()
278 && descriptor.idProduct() == d.getUsbDeviceDescriptor()
280 && descriptor.idVendor() == d.getUsbDeviceDescriptor()
285 }
catch (UnsupportedEncodingException e) {
288 }
catch (UsbDisconnectedException e) {
291 }
catch (SecurityException e) {
294 }
catch (UsbException e) {
311 @SuppressWarnings(
"unchecked")
313 ArrayList<UsbDevice> addrs) throws UnsupportedEncodingException,
314 UsbDisconnectedException, UsbException {
316 if (device.getUsbDeviceDescriptor().idVendor() == 0x04d8 &&
317 ( device.getUsbDeviceDescriptor().idProduct() == 0x0001||
318 device.getUsbDeviceDescriptor().idProduct() == 0x3742
334 if (device.isUsbHub()) {
335 final UsbHub hub = (UsbHub) device;
336 for (UsbDevice child : (List<UsbDevice>) hub
337 .getAttachedUsbDevices()) {
341 }
catch (Exception e) {
356 throws UnsupportedEncodingException, UsbDisconnectedException,
357 SecurityException, UsbException {
358 ArrayList<UsbDevice> addrs =
null;
360 addrs =
new ArrayList<UsbDevice>();
377 throws UnsupportedEncodingException, UsbDisconnectedException,
379 return d.getProductString().trim() +
" "
380 + d.getSerialNumberString().trim();
390 ArrayList<UsbDevice> devices;
395 for (UsbDevice d : devices) {
400 }
catch (UnsupportedEncodingException e) {
403 }
catch (UsbDisconnectedException e) {
406 }
catch (SecurityException e) {
409 }
catch (UsbException e) {
415 throw new NullPointerException(
416 "A valid USB device is needed to regester this connection.");
426 @SuppressWarnings(
"unchecked")
442 for (UsbConfiguration configuration : (List<UsbConfiguration>)
mDevice
443 .getUsbConfigurations()) {
445 for (UsbInterface iface : (List<UsbInterface>) configuration
446 .getUsbInterfaces()) {
450 if (iface.getUsbInterfaceDescriptor().bInterfaceClass() == 2) {
455 if (iface.getUsbInterfaceDescriptor().bInterfaceClass() == 10) {
460 }
catch (Exception e1) {
462 e1.printStackTrace();
469 for (UsbEndpoint endpoint : (List<UsbEndpoint>)
dataInterface
470 .getUsbEndpoints()) {
471 if (endpoint.getUsbEndpointDescriptor()
472 .bEndpointAddress() == 0x03) {
481 }
catch (UsbClaimException e) {
484 }
catch (UsbNotActiveException e) {
487 }
catch (UsbDisconnectedException e) {
490 }
catch (UsbException e) {
495 Log.
error(
"Interface is already climed");
506 mDevice.addUsbDeviceListener(
this);
523 DeviceList list =
new DeviceList();
524 int result = LibUsb.getDeviceList(
null, list);
526 throw new LibUsbException(
"Unable to get device list", result);
530 for (Device device : list) {
532 DeviceDescriptor descriptor =
new DeviceDescriptor();
533 result = LibUsb.getDeviceDescriptor(device, descriptor);
534 if (result != LibUsb.SUCCESS)
535 throw new LibUsbException(
536 "Unable to read device descriptor", result);
537 DeviceHandle handle =
new DeviceHandle();
538 result = LibUsb.open(device, handle);
539 if (result == LibUsb.SUCCESS) {
540 String sn = LibUsb.getStringDescriptor(handle,
541 descriptor.iSerialNumber()).trim();
542 LibUsb.close(handle);
543 if (sn.contains(seriualNumber.trim())) {
551 LibUsb.freeDeviceList(list,
true);
568 }
catch (UnsupportedEncodingException e) {
571 }
catch (UsbDisconnectedException e) {
574 }
catch (UsbException e) {
586 if (result != LibUsb.SUCCESS)
587 throw new LibUsbException(
"Unable to open USB device", result);
590 if (r != LibUsb.SUCCESS && r != LibUsb.ERROR_NOT_SUPPORTED
591 && r != LibUsb.ERROR_NOT_FOUND)
592 throw new LibUsbException(
"Unable to detach kernel driver", r);
601 mDevice.removeUsbDeviceListener(
this);
609 }
catch (UsbDisconnectedException e) {
612 }
catch (UsbException e) {
621 }
catch (UsbDisconnectedException e) {
624 }
catch (UsbException e) {
636 }
catch(IllegalStateException e){
663 private void prepIrp(UsbIrp irp,
byte[] data) {
666 irp.setLength(data.length);
668 irp.setAcceptShortPacket(
true);
669 irp.setComplete(
false);
670 irp.setUsbException(
null);
680 public void write(
byte[] src)
throws IOException {
696 write.waitUntilComplete();
698 while (!
write.isComplete()) {
702 }
catch (Exception e) {
706 "Connection is no longer availible "
707 + e.getLocalizedMessage());
716 enum usbControlState{
729 usbControlState usbReadState = usbControlState.init;
736 throws NullPointerException, IOException {
743 switch (usbReadState){
759 read.waitUntilComplete();
761 usbReadState = usbControlState.submitted;
763 }
catch ( IllegalArgumentException
768 }
catch (UsbNotActiveException e) {
771 }
catch (UsbNotOpenException e) {
774 }
catch (UsbDisconnectedException e) {
777 }
catch (UsbException e) {
783 if(
read.isComplete()){
784 got =
read.getActualLength();
786 bytesToPacketBuffer.add(Arrays.copyOfRange(data, 0, got));
788 usbReadState = usbControlState.init;
796 .build(bytesToPacketBuffer);
837 if(arg0.getUsbDevice() ==
mDevice){
838 new RuntimeException(
"Disconnect in USB called").printStackTrace();
850 if(arg0.getUsbDevice() ==
mDevice){
void setLastWrite(long lastWrite)
synchronized void setConnected(boolean c)
void waitForConnectioToBeReady()
static void error(String message)
Device findDevice(String seriualNumber)
static void dumpDevice(final UsbDevice device, ArrayList< UsbDevice > addrs)
static EventHandlingThread thread
BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer)
void usbDeviceDetached(UsbDeviceEvent arg0)
static void resetUsbSystem()
void errorEventOccurred(UsbDeviceErrorEvent arg0)
UsbEndpoint dataOutEndpoint
static HotplugCallbackHandle callbackHandle
UsbCDCSerialConnection(String deviceString)
void dataEventOccurred(UsbDeviceDataEvent arg0)
static ArrayList< IUsbDeviceEventListener > usbDeviceEventListeners
DeviceHandle deviceHandle
UsbInterface dataInterface
UsbEndpoint dataInEndpoint
static UsbDevice mapLibUsbDevicetoJavaxDevice(Device device)
boolean waitingForConnection()
void onDeviceEvent(UsbDevice device)
static ArrayList< UsbDevice > getAllUsbBowlerDevices()
void prepIrp(UsbIrp irp, byte[] data)
static void removeUsbDeviceEventListener(IUsbDeviceEventListener l)
void kernelDetatch(UsbDevice mDevice)
UsbCDCSerialConnection(UsbDevice device)
static String getUniqueID(UsbDevice d)
static void addUsbDeviceEventListener(IUsbDeviceEventListener l)
static boolean isWindows()
static void wait(int time)