Update of /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows In directory sc8-pr-cvs17.sourceforge.net:/tmp/cvs-serv25963 Modified Files: JavaxUsb.java WindowsDeviceOsImp.java WindowsInterfaceOsImp.java WindowsPipeOsImp.java WindowsUsbServices.java Log Message: Added better exception throwing when libusb returns an error, as getSerialNumber was failing silently and returning bad values. Package now appears to receive connect and disconnect notices from libusb properly. Cleaned up some log messages. Rearranged some functions in a more logical order. Removed dead code Index: WindowsUsbServices.java =================================================================== RCS file: /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows/WindowsUsbServices.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** WindowsUsbServices.java 18 Aug 2008 21:31:07 -0000 1.5 --- WindowsUsbServices.java 28 Aug 2008 21:22:03 -0000 1.6 *************** *** 310,316 **** int updates = JavaxUsb.nativeTopologyUpdater(this, connectedDevices, disconnectedDevices); if(updates == 0) return; // if there are no changes go home early - // connectedDevices contains all new devices found - // disconnectedDevices contains all devices removed Iterator iterator = disconnectedDevices.iterator(); while(iterator.hasNext()) --- 310,317 ---- int updates = JavaxUsb.nativeTopologyUpdater(this, connectedDevices, disconnectedDevices); if(updates == 0) return; // if there are no changes go home early + // if something has changed continue on + + // disconnectedDevices contains all devices removed Iterator iterator = disconnectedDevices.iterator(); while(iterator.hasNext()) *************** *** 320,345 **** device.disconnect(); listenerImp.usbDeviceDetached(new UsbServicesEvent(this, (UsbDevice)device )); - } iterator = connectedDevices.iterator(); while(iterator.hasNext()) { UsbDeviceImp device = (UsbDeviceImp) iterator.next(); ! // fixme: setActiveConfig... is omitted to find out, whether it // is really needed in libusb implementation // setActiveConfigAndInterfaceSettings(device); - // attachUsbDeviceImp now occures in JavaUsb.java - // try - // { - // device.getParentUsbPortImp().attachUsbDeviceImp(device); - // } - // catch (IllegalArgumentException iae) - // { - // if(log.isDebugEnabled()) log.debug( "updateTopology() while attaching UsbDeviceImp "+iae ); - // // device is already attached - // continue; - // } // Let's wait a bit before each new device's event, so its driver can have some time to --- 321,335 ---- device.disconnect(); listenerImp.usbDeviceDetached(new UsbServicesEvent(this, (UsbDevice)device )); } + // connectedDevices contains all new devices found iterator = connectedDevices.iterator(); while(iterator.hasNext()) { UsbDeviceImp device = (UsbDeviceImp) iterator.next(); ! // fixme: setActiveConfig... is omitted to find out, whether it // is really needed in libusb implementation // setActiveConfigAndInterfaceSettings(device); // Let's wait a bit before each new device's event, so its driver can have some time to *************** *** 347,351 **** try { - // if(!device.isUsbHub()) if( !(device instanceof WindowsHubOsImp) ) { --- 337,340 ---- *************** *** 357,361 **** { } ! if(log.isDebugEnabled()) log.debug( "updateTopology() connected device: "+device ); listenerImp.usbDeviceAttached(new UsbServicesEvent(this, (UsbDevice) device)); --- 346,357 ---- { } ! try ! { ! if(log.isDebugEnabled()) log.debug( "updateTopology() found device: "+device.getSerialNumberString() ); ! } ! catch(Exception e) ! { ! } ! if(log.isDebugEnabled()) log.debug( "updateTopology() connecting device: "+device ); listenerImp.usbDeviceAttached(new UsbServicesEvent(this, (UsbDevice) device)); Index: WindowsPipeOsImp.java =================================================================== RCS file: /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows/WindowsPipeOsImp.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** WindowsPipeOsImp.java 18 Aug 2008 18:14:52 -0000 1.3 --- WindowsPipeOsImp.java 28 Aug 2008 21:22:03 -0000 1.4 *************** *** 223,227 **** int epDir = ep.getDirection(); int result = 0; ! String action = ""; // lock the access to the libusb --- 223,227 ---- int epDir = ep.getDirection(); int result = 0; ! // String action = ""; // lock the access to the libusb *************** *** 235,239 **** if (epDir == UsbConst.ENDPOINT_DIRECTION_OUT) { ! action = "bulk write"; int chunkSize = 4096; --- 235,239 ---- if (epDir == UsbConst.ENDPOINT_DIRECTION_OUT) { ! // action = "bulk write"; int chunkSize = 4096; *************** *** 254,258 **** res = Libusb.usb_bulk_write(handle,getEndpointAddress(),buf,timeout); JavaxUsb.getMutex().release(); //FIXME not sure if this is required - if (log.isDebugEnabled()) log.debug("Called bulk_write with buffer of size "+ buf.length + ", res = " + res); --- 254,257 ---- *************** *** 267,271 **** else if (epDir == UsbConst.ENDPOINT_DIRECTION_IN) { ! action = "bulk read"; JavaxUsb.getMutex().acquire(); //FIXME not sure if this is required result = Libusb.usb_bulk_read(handle,getEndpointAddress(),irp.getData(),timeout); --- 266,270 ---- else if (epDir == UsbConst.ENDPOINT_DIRECTION_IN) { ! // action = "bulk read"; JavaxUsb.getMutex().acquire(); //FIXME not sure if this is required result = Libusb.usb_bulk_read(handle,getEndpointAddress(),irp.getData(),timeout); *************** *** 277,281 **** if (epDir == UsbConst.ENDPOINT_DIRECTION_OUT) { ! action = "interrupt write"; JavaxUsb.getMutex().acquire(); //FIXME not sure if this is required result = Libusb.usb_interrupt_write(handle,getEndpointAddress(),irp.getData(),timeout); --- 276,280 ---- if (epDir == UsbConst.ENDPOINT_DIRECTION_OUT) { ! // action = "interrupt write"; JavaxUsb.getMutex().acquire(); //FIXME not sure if this is required result = Libusb.usb_interrupt_write(handle,getEndpointAddress(),irp.getData(),timeout); *************** *** 284,288 **** else if (epDir == UsbConst.ENDPOINT_DIRECTION_IN) { ! action = "interrupt read"; // don't use a mutex here because a long time may elapse before getting a response // and FIXME, not sure a mutex is required anyway for this action --- 283,287 ---- else if (epDir == UsbConst.ENDPOINT_DIRECTION_IN) { ! // action = "interrupt read"; // don't use a mutex here because a long time may elapse before getting a response // and FIXME, not sure a mutex is required anyway for this action *************** *** 294,307 **** } ! if (result < 0) ! { ! String msg = "submitIrp: Error during " + action + ", error code " + result + ": "+ Libusb.usb_strerror(); ! log.debug(msg); ! throw new UsbException(msg); ! } ! else ! { ! irp.setActualLength(result); ! } // if (log.isDebugEnabled()) --- 293,298 ---- } ! JavaxUsb.isReturnCodeError(result); // throws an exception if retval is less than 0 ! irp.setActualLength(result); // if (log.isDebugEnabled()) Index: WindowsInterfaceOsImp.java =================================================================== RCS file: /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows/WindowsInterfaceOsImp.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** WindowsInterfaceOsImp.java 5 Aug 2008 20:53:24 -0000 1.3 --- WindowsInterfaceOsImp.java 28 Aug 2008 21:22:03 -0000 1.4 *************** *** 188,195 **** log.debug("Setting libusb to configuration number: "+ucd.getIConfiguration()); ! Libusb.usb_set_configuration(getWindowsDeviceOsImp().getHandle(),ucd.getIConfiguration()); ! int result = Libusb.usb_claim_interface(getWindowsDeviceOsImp().getHandle(),getInterfaceNumber()); ! if (result != 0) { String msg = "Couldn't claim interface. usb error: "+ Libusb.usb_strerror(); --- 188,196 ---- log.debug("Setting libusb to configuration number: "+ucd.getIConfiguration()); ! int retval = Libusb.usb_set_configuration(getWindowsDeviceOsImp().getHandle(),ucd.getIConfiguration()); ! JavaxUsb.isReturnCodeError(retval); // throws an exception if retval is less than 0 ! retval = Libusb.usb_claim_interface(getWindowsDeviceOsImp().getHandle(),getInterfaceNumber()); ! if (retval != 0) { String msg = "Couldn't claim interface. usb error: "+ Libusb.usb_strerror(); Index: WindowsDeviceOsImp.java =================================================================== RCS file: /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows/WindowsDeviceOsImp.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** WindowsDeviceOsImp.java 7 Aug 2008 14:35:10 -0000 1.3 --- WindowsDeviceOsImp.java 28 Aug 2008 21:22:03 -0000 1.4 *************** *** 164,168 **** if( (cIrp.bRequest() == UsbConst.REQUEST_SET_CONFIGURATION) && (cIrp.bmRequestType() ==0) ) { ! Libusb.usb_set_configuration( getHandle(),cIrp.wValue()); return; } --- 164,169 ---- if( (cIrp.bRequest() == UsbConst.REQUEST_SET_CONFIGURATION) && (cIrp.bmRequestType() ==0) ) { ! int retval = Libusb.usb_set_configuration( getHandle(),cIrp.wValue()); ! JavaxUsb.isReturnCodeError(retval); return; } *************** *** 189,201 **** " wLength: "+UsbUtil.toHexString(cIrp.wLength())); ! int result = Libusb.usb_control_msg(handle,cIrp.bmRequestType(),cIrp.bRequest(), cIrp.wValue(), cIrp.wIndex(),data ,JavaxUsb.getIoTimeout()); ! if(result >=0) ! cIrp.setActualLength(result); ! else ! { ! String msg = "usb_control_msg: " + Libusb.usb_strerror() +" errorno: " + result; ! log.debug(msg); ! throw new UsbException(msg); ! } } finally --- 190,196 ---- " wLength: "+UsbUtil.toHexString(cIrp.wLength())); ! int retval = Libusb.usb_control_msg(handle,cIrp.bmRequestType(),cIrp.bRequest(), cIrp.wValue(), cIrp.wIndex(),data ,JavaxUsb.getIoTimeout()); ! JavaxUsb.isReturnCodeError(retval); // throws an exception if retval is less than 0 ! cIrp.setActualLength(retval); } finally *************** *** 260,264 **** try { ! Libusb.usb_get_string_simple(handle, index, buffer); } finally --- 255,260 ---- try { ! int retval = Libusb.usb_get_string_simple(handle, index, buffer); ! JavaxUsb.isReturnCodeError(retval); // throws an exception if retval is less than 0 } finally *************** *** 293,299 **** if (handle == null) { ! String msg = ! "Couldn't open device " + device.getFilename() ! + " due to error: " + Libusb.usb_strerror(); log.debug(msg); throw new UsbException(msg); --- 289,293 ---- if (handle == null) { ! String msg = "Couldn't open device " + device.getFilename() + " due to error: " + Libusb.usb_strerror(); log.debug(msg); throw new UsbException(msg); *************** *** 303,307 **** { int bufSize = 256; ! int ret; byte[] buf = new byte[bufSize]; int manu = device.getDescriptor().getIManufacturer(); --- 297,301 ---- { int bufSize = 256; ! int retval; byte[] buf = new byte[bufSize]; int manu = device.getDescriptor().getIManufacturer(); *************** *** 309,324 **** if (manu > 0) { ! ret = Libusb.usb_get_string_simple(handle, manu, buf); ! ! if (ret > 0) ! { ! log.debug( ! "Manufacturer : " + JavaxUsb.bytes2String(buf)); ! } ! else ! { ! log.debug( ! "Unable to fetch manufacturer string\r\n"); ! } } --- 303,309 ---- if (manu > 0) { ! retval = Libusb.usb_get_string_simple(handle, manu, buf); ! JavaxUsb.isReturnCodeError(retval); // throws an exception if retval is less than 0 ! log.debug("Manufacturer : <" + JavaxUsb.bytes2String(buf)+">"); } *************** *** 327,355 **** if (prod > 0) { ! ret = Libusb.usb_get_string_simple(handle, prod, buf); ! ! if (ret > 0) ! log.debug( ! "Product : " + JavaxUsb.bytes2String(buf)); ! else ! log.debug("Unable to fetch product string\r\n"); } int serial = device.getDescriptor().getISerialNumber(); - if (serial > 0) { ! ret = Libusb.usb_get_string_simple(handle, serial, buf); ! ! if (ret > 0) ! { ! log.debug( ! "Serial Number: " + JavaxUsb.bytes2String(buf)); ! } ! else ! { ! log.debug( ! "Unable to fetch serial number string\r\n"); ! } } } // end if isDebugEnabled() --- 312,326 ---- if (prod > 0) { ! retval = Libusb.usb_get_string_simple(handle, prod, buf); ! JavaxUsb.isReturnCodeError(retval); // throws an exception if retval is less than 0 ! log.debug("Product : <" + JavaxUsb.bytes2String(buf)+">"); } int serial = device.getDescriptor().getISerialNumber(); if (serial > 0) { ! retval = Libusb.usb_get_string_simple(handle, serial, buf); ! JavaxUsb.isReturnCodeError(retval); // throws an exception if retval is less than 0 ! log.debug("Serial Number: <" + JavaxUsb.bytes2String(buf)+">"); } } // end if isDebugEnabled() *************** *** 360,364 **** } } - return handle; } --- 331,334 ---- *************** *** 379,392 **** try { ! int result = Libusb.usb_close(handle); ! if (result != 0) { ! String msg = ! "Couldn't close device " + device.getFilename() ! + " due to error: " + Libusb.usb_strerror(); log.debug(msg); throw new UsbException(msg); } } finally --- 349,362 ---- try { ! int retval = Libusb.usb_close(handle); ! if (retval != 0) { ! String msg = "Couldn't close device " + device.getFilename()+ " due to error: " + Libusb.usb_strerror(); log.debug(msg); throw new UsbException(msg); } + JavaxUsb.isReturnCodeError(retval); // throws an exception if retval is less than 0 + } finally Index: JavaxUsb.java =================================================================== RCS file: /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows/JavaxUsb.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** JavaxUsb.java 18 Aug 2008 21:31:07 -0000 1.7 --- JavaxUsb.java 28 Aug 2008 21:22:03 -0000 1.8 *************** *** 42,45 **** --- 42,46 ---- import javax.usb.UsbDeviceDescriptor; import javax.usb.UsbException; + import javax.usb.UsbDisconnectedException; import java.io.UnsupportedEncodingException; *************** *** 394,398 **** } ! /** * Convert the error code to a UsbException. --- 395,406 ---- } ! public static void isReturnCodeError(int error) throws UsbException, UsbDisconnectedException ! { ! if( error >=0) return; ! // log.debug("isReturnCodeError() throwing an exception on "+error); ! if(error == -19) throw new UsbDisconnectedException("libusb reports device has been disconnected: "+error+", "+Libusb.usb_strerror()); ! throw errorToUsbException(error); ! } ! /** * Convert the error code to a UsbException. *************** *** 442,446 **** * @return the message associated with the specified error number */ ! static native String nativeGetErrorMessage(int error); /** --- 450,458 ---- * @return the message associated with the specified error number */ ! // static native String nativeGetErrorMessage(int error); ! static String nativeGetErrorMessage(int error) ! { ! return(Libusb.usb_strerror()); ! } /** *************** *** 460,463 **** --- 472,504 ---- // int i); + + private static void attachParentUsbPort(UsbHubImp hub, UsbDeviceImp device) + { + List ports = hub.getUsbPorts(); + Iterator iterator = ports.iterator(); + while(iterator.hasNext()) + { + UsbPortImp usbPortImp = (UsbPortImp)iterator.next(); + if(usbPortImp.isUsbDeviceAttached()) continue; + if(log.isDebugEnabled()) log.debug("attachParentUsbPort(), found empty port"); + device.setParentUsbPortImp(usbPortImp); + usbPortImp.attachUsbDeviceImp(device); + return; + } + + byte port = hub.getNumberOfPorts(); + port++; + UsbPortImp usbPortImp = hub.getUsbPortImp((byte)port); + if( usbPortImp == null) + { + log.debug("attachParentUsbPort(), WARNING resizing hub to "+port+" ports"); + hub.resize((byte)port); + usbPortImp = hub.getUsbPortImp((byte)port); + } + usbPortImp.attachUsbDeviceImp(device); + device.setParentUsbPortImp(usbPortImp); + } + + /** * Return if the compared devices are the same device. *************** *** 478,482 **** UsbDeviceDescriptor desc2 = dev2.getUsbDeviceDescriptor(); ! if( dev1.isUsbHub() != dev1.isUsbHub() ) return(false); if( dev1.getSpeed() != dev2.getSpeed() ) return(false); if( !desc1.equals(desc2) ) return(false); --- 519,523 ---- UsbDeviceDescriptor desc2 = dev2.getUsbDeviceDescriptor(); ! if( dev1.isUsbHub() != dev2.isUsbHub() ) return(false); if( dev1.getSpeed() != dev2.getSpeed() ) return(false); if( !desc1.equals(desc2) ) return(false); *************** *** 492,495 **** --- 533,541 ---- return(false); } + catch(UsbDisconnectedException ude) + { + log.debug("isUsbDevicesEqual() device no longer exists"); + return(false); + } catch (UsbException ue) { *************** *** 519,543 **** private static void checkUsbDeviceImp(UsbHubImp hub, UsbDeviceImp device, List currentDevices, List connected) { ! String meth = "checkUsbDeviceImp"; ! // byte port = (byte)p; ! ! // if(log.isDebugEnabled()) log.debug( meth +" Entered with port" + p+" our port num "+port ); ! // UsbPortImp usbPortImp = hub.getUsbPortImp((byte)port); ! // if (null == usbPortImp) ! // { ! // log.debug(meth+" WARNING resizing port"); ! // hub.resize((byte)port); ! // usbPortImp = hub.getUsbPortImp((byte)port); ! // } ! ! if(log.isDebugEnabled()) log.debug( meth+ " Hub now has " + hub.getNumberOfPorts() + " ports"); // look for this devices in the current device list, if found remove from the list // because later, the remaining items in current device list will be removed. - boolean found = false; Iterator iterator = currentDevices.iterator(); while( iterator.hasNext()) { ! if( isUsbDevicesEqual((UsbDeviceImp)iterator.next(), device)) { iterator.remove(); --- 565,578 ---- private static void checkUsbDeviceImp(UsbHubImp hub, UsbDeviceImp device, List currentDevices, List connected) { ! boolean found = false; ! String method = "checkUsbDeviceImp"; // look for this devices in the current device list, if found remove from the list // because later, the remaining items in current device list will be removed. Iterator iterator = currentDevices.iterator(); while( iterator.hasNext()) { ! UsbDeviceImp usbDeviceFromList = (UsbDeviceImp)iterator.next(); ! if( isUsbDevicesEqual(usbDeviceFromList, device)) { iterator.remove(); *************** *** 546,692 **** } ! // if the device was not found in the list, it must be new so add it to the connected list if(!found) { ! if(log.isDebugEnabled()) log.debug(meth+" adding new device"); connected.add(device); - // device.setParentUsbPortImp(usbPortImp); attachParentUsbPort(hub,device); } - - if(log.isDebugEnabled()) log.debug(meth+ " Leaving with device " + device); } - - private static void attachParentUsbPort(UsbHubImp hub, UsbDeviceImp device) - { - if(log.isDebugEnabled()) log.debug("attachParentUsbPort"); - List ports = hub.getUsbPorts(); - Iterator iterator = ports.iterator(); - while(iterator.hasNext()) - { - UsbPortImp usbPortImp = (UsbPortImp)iterator.next(); - if(usbPortImp.isUsbDeviceAttached()) continue; - if(log.isDebugEnabled()) log.debug("attachParentUsbPort(), found empty port"); - device.setParentUsbPortImp(usbPortImp); - usbPortImp.attachUsbDeviceImp(device); - return; - } - byte port = hub.getNumberOfPorts(); - log.debug("attachParentUsbPort(), no empty ports found, out of "+ port+" ports"); - port++; - UsbPortImp usbPortImp = hub.getUsbPortImp((byte)port); - if (null == usbPortImp) - { - log.debug("attachParentUsbPort(), WARNING resizing port"); - hub.resize((byte)port); - usbPortImp = hub.getUsbPortImp((byte)port); - } - device.setParentUsbPortImp(usbPortImp); - usbPortImp.attachUsbDeviceImp(device); - } ! private static void buildConfig(UsbDeviceImp usbDev,usb_config_descriptor config) { ! UsbConfigurationDescriptorImp desc = new UsbConfigurationDescriptorImp((byte) config.getBLength(), ! (byte) config.getBDescriptorType(),(short) config.getWTotalLength(), ! (byte) config.getBNumInterfaces(),(byte) config.getBConfigurationValue(), ! (byte) config.getIConfiguration(),(byte) config.getBmAttributes(), ! (byte) config.getMaxPower()); ! ! UsbConfigurationImp usbConfig = new UsbConfigurationImp(usbDev, desc); ! usbDev.addUsbConfigurationImp(usbConfig); ! ! // FIXME: since most devices only support one configuration we will use the first one as default ! // this is probably ok for devices that support more than one configuration as well ! // unless it has been somehow previously set ! if(config.getBConfigurationValue() == 1) ! { ! // log(LOG_HOTPLUG, FUNC, CLASS, "buildConfiguration","WARNING Using config " + config.getBConfigurationValue()+" as active; no checking."); ! config.setIConfiguration((byte)1); ! usbDev.setActiveUsbConfigurationNumber((byte)config.getBConfigurationValue()); ! } ! ! for (int i = 0; i < config.getBNumInterfaces(); i++) ! { ! usb_interface iface; ! iface = Libusb.usb_interface_index(config.get_interface(),i); ! ! for (int j = 0; j < iface.getNum_altsetting(); j++) ! { ! usb_interface_descriptor ifaceDesc; ! ifaceDesc = Libusb.usb_interface_descriptor_index(iface.getAltsetting(),j); ! buildInterface(usbConfig, ifaceDesc); ! } } ! } ! ! /** ! * @param usbConfig ! * @param ifaceDesc ! */ ! private static void buildInterface(UsbConfigurationImp usbConfig, usb_interface_descriptor ifaceDesc) ! { ! // log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","Entering with config " + usbConfig); ! UsbInterfaceDescriptorImp desc = new UsbInterfaceDescriptorImp((byte) ifaceDesc.getBLength(), ! (byte) ifaceDesc.getBDescriptorType(),(byte) ifaceDesc.getBInterfaceNumber(), ! (byte) ifaceDesc.getBAlternateSetting(),(byte) ifaceDesc.getBNumEndpoints(), ! (byte) ifaceDesc.getBInterfaceClass(),(byte) ifaceDesc.getBInterfaceSubClass(), ! (byte) ifaceDesc.getBInterfaceProtocol(),(byte) ifaceDesc.getIInterface()); ! // log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","new interface descriptor " + desc); ! ! UsbInterfaceImp iface = new UsbInterfaceImp(usbConfig, desc); ! boolean active = ((0 == ifaceDesc.getBAlternateSetting())? true : false); ! /* If the config is not active, neither are its interface settings */ ! if(usbConfig.isActive() && active) ! { ! // log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","inteface is active"); ! iface.setActiveSettingNumber(iface.getUsbInterfaceDescriptor().bAlternateSetting()); } ! else { ! log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","inteface not active"); ! } ! WindowsDeviceOsImp windowsDeviceOsImp = (WindowsDeviceOsImp) iface.getUsbConfigurationImp().getUsbDeviceImp(); ! WindowsInterfaceOsImp windowsInterfaceOsImp = new WindowsInterfaceOsImp(iface, windowsDeviceOsImp); ! iface.setUsbInterfaceOsImp(windowsInterfaceOsImp); - for (int i = 0; i < ifaceDesc.getBNumEndpoints(); i++) - { - buildEndpoint(iface, Libusb.usb_endpoint_descriptor_index(ifaceDesc.getEndpoint(),i)); - } - // log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","Leaving with interface " + iface); - } /** ! * @param iface ! * @param usb_endpoint_descriptor ! */ private static void buildEndpoint(UsbInterfaceImp iface,usb_endpoint_descriptor endPointDesc) { ! UsbEndpointDescriptorImp desc = ! new UsbEndpointDescriptorImp( ! (byte) endPointDesc.getBLength(), ! (byte) endPointDesc.getBDescriptorType(), ! (byte) endPointDesc.getBEndpointAddress(), ! (byte) endPointDesc.getBmAttributes(), ! (byte) endPointDesc.getBInterval(), ! (short) endPointDesc.getWMaxPacketSize()); UsbEndpointImp ep = new UsbEndpointImp(iface, desc); UsbPipeImp pipe = null; ! WindowsInterfaceOsImp windowsInterfaceOsImp = ! (WindowsInterfaceOsImp) iface.getUsbInterfaceOsImp(); switch ((int) ep.getType()) --- 581,681 ---- } ! // If the device was not found in the list, it must be new. Add it to the connected list if(!found) { ! if(log.isDebugEnabled()) log.debug(method+" found new device"); connected.add(device); attachParentUsbPort(hub,device); } } ! /** ! * looks at the bus through libusb and attempts to update our knowlege of the system ! * by putting new devices into the connectedDevices list and removing still present items from the ! * disconnected List ! * @param services ! * @param connectedDevices - at entry contains no devices, at exit contains newly found devices ! * @param disconnectedDevices -at entry contains all known devices, at exit contains newly removed devices ! * @return 0 if no change, else -1 ! */ ! static int nativeTopologyUpdater(WindowsUsbServices services, List connectedDevices, List disconnectedDevices) { ! String method = "nativeTopologyUpdater()"; ! UsbHubImp rootHub = services.getRootUsbHubImp(); ! if (rootHub == null) ! { // this shouldn't happen, as the root hub is setup during initialisation ! throw new RuntimeException("The (virtual) root hub couldn't be retrieved."); } ! // acquire a lock, so we don't interfere with others trying to access libusb ! mutex.acquire(); ! try ! { ! int busCount = Libusb.usb_find_busses(); ! int deviceCount = Libusb.usb_find_devices(); ! if(log.isDebugEnabled()) log.debug(method + " Found " + busCount + " new busses. and " + deviceCount + " new devices."); ! // if no devices have changed do a quick exit as the calling method looks at the return value ! // to determin if more work has to be done ! if (deviceCount == 0) ! return( 0 ); ! // if we get here a change has been detected by libusb, examine the structure of the bus as presented by libusb to ! // what has changed. To do this we build all the devices listed in libusb and compare them to our internal list ! usb_bus libusb_bus = Libusb.usb_get_busses(); ! while (libusb_bus != null) ! {// for all busses ! log.debug(""); ! log.debug( method +" Scanning bus " + libusb_bus.getDirname()); ! usb_device libusb_dev = libusb_bus.getDevices(); ! while (libusb_dev != null) ! { // for all devices on this bus ! if(log.isDebugEnabled()) log.debug(method + " Device: " + libusb_dev.getFilename()); ! if (libusb_dev.getConfig() == null) ! { ! log(LOG_HOTPLUG, ERROR, CLASS, method,"Couldn't retrieve descriptors for device '"+ libusb_bus.getDirname() + "/" + libusb_dev.getFilename() + "'"); ! libusb_dev = libusb_dev.getNext(); ! continue; ! } ! UsbDeviceImp usbDev = buildDevice(libusb_dev, libusb_bus ); ! // usbDev is a device that is being reported as existing by libusb ! // if it is found in our list of disconnectedDevices, remove it and add it to ! // connected devices. ! checkUsbDeviceImp(rootHub, usbDev, disconnectedDevices, connectedDevices); ! libusb_dev = libusb_dev.getNext(); ! } // end for all devices on this bus ! libusb_bus = libusb_bus.getNext(); ! } // end for all busses } ! finally { ! mutex.release(); } ! return -1; ! } /** ! * @param iface ! * @param usb_endpoint_descriptor ! */ private static void buildEndpoint(UsbInterfaceImp iface,usb_endpoint_descriptor endPointDesc) { ! UsbEndpointDescriptorImp desc = ! new UsbEndpointDescriptorImp((byte) endPointDesc.getBLength(),(byte) endPointDesc.getBDescriptorType(), ! (byte) endPointDesc.getBEndpointAddress(),(byte) endPointDesc.getBmAttributes(), ! (byte) endPointDesc.getBInterval(),(short) endPointDesc.getWMaxPacketSize()); UsbEndpointImp ep = new UsbEndpointImp(iface, desc); UsbPipeImp pipe = null; ! WindowsInterfaceOsImp windowsInterfaceOsImp = (WindowsInterfaceOsImp) iface.getUsbInterfaceOsImp(); switch ((int) ep.getType()) *************** *** 702,738 **** // pipe.setUsbPipeOsImp( new WindowsIsochronousPipeImp( pipe, windowsInterfaceOsImp ) ); // break; ! case UsbConst.ENDPOINT_TYPE_CONTROL : ! pipe = new UsbControlPipeImp(ep, null); ! pipe.setUsbPipeOsImp( ! new WindowsPipeOsImp( ! (UsbControlPipeImp) pipe, windowsInterfaceOsImp)); ! ! break; ! ! case UsbConst.ENDPOINT_TYPE_ISOCHRONOUS : ! pipe = new UsbPipeImp(ep, null); ! pipe.setUsbPipeOsImp( ! new WindowsPipeOsImp(pipe, windowsInterfaceOsImp)); ! ! break; ! ! case UsbConst.ENDPOINT_TYPE_BULK : ! pipe = new UsbPipeImp(ep, null); ! pipe.setUsbPipeOsImp( ! new WindowsPipeOsImp(pipe, windowsInterfaceOsImp)); ! break; ! case UsbConst.ENDPOINT_TYPE_INTERRUPT : ! pipe = new UsbPipeImp(ep, null); ! pipe.setUsbPipeOsImp( ! new WindowsPipeOsImp(pipe, windowsInterfaceOsImp)); ! break; ! default : ! String emsg = "Invalid UsbEndpoint type " + ep.getType(); ! log.error("buildEndpoint() "+emsg); ! throw new RuntimeException(emsg); } } --- 691,718 ---- // pipe.setUsbPipeOsImp( new WindowsIsochronousPipeImp( pipe, windowsInterfaceOsImp ) ); // break; ! case UsbConst.ENDPOINT_TYPE_CONTROL : ! pipe = new UsbControlPipeImp(ep, null); ! pipe.setUsbPipeOsImp(new WindowsPipeOsImp((UsbControlPipeImp) pipe, windowsInterfaceOsImp)); ! break; ! case UsbConst.ENDPOINT_TYPE_ISOCHRONOUS : ! pipe = new UsbPipeImp(ep, null); ! pipe.setUsbPipeOsImp(new WindowsPipeOsImp(pipe, windowsInterfaceOsImp)); ! break; ! case UsbConst.ENDPOINT_TYPE_BULK : ! pipe = new UsbPipeImp(ep, null); ! pipe.setUsbPipeOsImp(new WindowsPipeOsImp(pipe, windowsInterfaceOsImp)); ! break; ! case UsbConst.ENDPOINT_TYPE_INTERRUPT : ! pipe = new UsbPipeImp(ep, null); ! pipe.setUsbPipeOsImp(new WindowsPipeOsImp(pipe, windowsInterfaceOsImp)); ! break; ! default : ! String emsg = "Invalid UsbEndpoint type " + ep.getType(); ! log.error("buildEndpoint() "+emsg); ! throw new RuntimeException(emsg); } } *************** *** 740,898 **** /** ! * Creates a UsbDevice or UsbHub, depending on the type of ! * argument dev, fills in the values from the config of dev ! * and checks, whether this device ! * @param dev ! * @param bus ! */ ! static UsbDeviceImp buildDevice( usb_device dev, usb_bus bus) { ! String meth = "buildDevice"; ! ! log(LOG_HOTPLUG, FUNC, CLASS, meth,"Entered buildDevice with device " + dev.getFilename()); ! ! UsbDeviceImp usbDev; ! if( dev.getDescriptor().getBDeviceClass() == Libusb.USB_CLASS_HUB) { ! log(LOG_HOTPLUG, DEBUG, CLASS, meth, "Device is a hub."); ! ! // note that libusb doesn't give us information about ! // the number of ports ! usbDev = new WindowsHubOsImp(dev); } else { ! log(LOG_HOTPLUG, DEBUG, CLASS, meth, "Device is NOT a hub."); ! usbDev = new WindowsDeviceOsImp(dev); ! ! // libusb doesn't tell the speed ! usbDev.setSpeed(UsbConst.DEVICE_SPEED_UNKNOWN); ! // now build all configurations ! for (int i = 0; i < dev.getDescriptor().getBNumConfigurations(); i++) ! { ! buildConfig(usbDev,Libusb.usb_config_descriptor_index(dev.getConfig(),i)); ! } ! } ! if(log.isDebugEnabled()) { ! log.debug("buildDevice() Leaving buildDevice with device " + dev.getFilename()); } - return(usbDev); } ! /** ! * looks at the bus through libusb and attempts to update our knowlege of the system ! * by putting new devices into the connectedDevices list and removing still present items from the ! * disconnected List ! * @param services ! * @param connectedDevices - at entry contains no devices, at exit contains newly found devices ! * @param disconnectedDevices -at entry contains all known devices, at exit contains newly removed devices ! * @return 0 if no change, else -1 ! */ ! static int nativeTopologyUpdater(WindowsUsbServices services, List connectedDevices, List disconnectedDevices) { ! String method = "topologyUpdater"; ! log(LOG_DEFAULT, FUNC, CLASS, method, "Entering topologyUpdater."); ! UsbHubImp rootHub = services.getRootUsbHubImp(); ! if (rootHub == null) ! { ! // this shouldn't happen, as the root hub is setup during initialisation ! throw new RuntimeException("The (virtual) root hub couldn't be retrieved."); ! } ! // acquire a lock, so we don't interfere with others trying to access libusb ! mutex.acquire(); ! try { ! int busCount = Libusb.usb_find_busses(); ! int deviceCount = Libusb.usb_find_devices(); ! ! String msg = "Found " + busCount + " new busses. and " + deviceCount + " new devices."; ! log(LOG_HOTPLUG, DEBUG, CLASS, method, msg); ! ! if (deviceCount == 0) ! { ! // nothing has changed, so all devices which ! // were present before (they are in disconnectedDevices) ! // are still present, now ! Iterator iterator = disconnectedDevices.iterator(); ! while(iterator.hasNext()) ! { ! connectedDevices.add(iterator.next()); ! iterator.remove(); ! } ! return 0; ! } ! ! usb_bus libusb_bus = Libusb.usb_get_busses(); ! log.debug(" disconnectedDevices size: "+disconnectedDevices.size()); ! ! ! while (libusb_bus != null) ! { ! msg = "Scanning bus " + libusb_bus.getDirname(); ! log.debug(""); ! log.debug(""); ! log(LOG_HOTPLUG, DEBUG, CLASS, method, msg); ! ! usb_device libusb_dev = libusb_bus.getDevices(); - while (libusb_dev != null) - { - // usb_device_descriptor devDesc = libusb_dev.getDescriptor(); - if(log.isDebugEnabled()) - { - log.debug(""); - msg = "Device: " + libusb_dev.getFilename(); - log(LOG_HOTPLUG, DEBUG, CLASS, method, msg); - } ! if (libusb_dev.getConfig() == null) ! { ! log(LOG_HOTPLUG, ERROR, CLASS, method,"Couldn't retrieve descriptors for device '"+ libusb_bus.getDirname() + "/" + libusb_dev.getFilename() + "'"); ! libusb_dev = libusb_dev.getNext(); ! continue; ! } ! // for( int i = 0; i < libusb_dev.getDescriptor().getBNumConfigurations();i++) ! // { ! // usb_config_descriptor config = Libusb.usb_config_descriptor_index(libusb_dev.getConfig(),i); ! // if(log.isDebugEnabled()) ! // printConfiguration(config); ! // } ! // if( devDesc.getBDeviceClass() == Libusb.USB_CLASS_HUB) ! // { ! // UsbDeviceImp usbDev = buildDevice(libusb_dev, libusb_bus ); ! // log.debug(method+" device is Hub, skipping"); ! // } ! // else ! { ! UsbDeviceImp usbDev = buildDevice(libusb_dev, libusb_bus ); ! // usbDev is a device that is being reported as existing by libusb ! // if it is found in our list of disconnectedDevices, remove it and add it to ! // connected devices. ! checkUsbDeviceImp(rootHub, usbDev, disconnectedDevices, connectedDevices); ! } ! libusb_dev = libusb_dev.getNext(); ! } ! libusb_bus = libusb_bus.getNext(); ! } ! log.debug(" disconnectedDevices size: "+disconnectedDevices.size()); ! log.debug(" connectedDevices size: "+connectedDevices.size()); ! } ! finally { ! mutex.release(); } ! ! return -1; } /** * Converts the bytes in argument buf to a String --- 720,824 ---- /** ! * @param usbConfig ! * @param ifaceDesc ! */ ! private static void buildInterface(UsbConfigurationImp usbConfig, usb_interface_descriptor ifaceDesc) { ! UsbInterfaceDescriptorImp desc = new UsbInterfaceDescriptorImp((byte) ifaceDesc.getBLength(), ! (byte) ifaceDesc.getBDescriptorType(),(byte) ifaceDesc.getBInterfaceNumber(), ! (byte) ifaceDesc.getBAlternateSetting(),(byte) ifaceDesc.getBNumEndpoints(), ! (byte) ifaceDesc.getBInterfaceClass(),(byte) ifaceDesc.getBInterfaceSubClass(), ! (byte) ifaceDesc.getBInterfaceProtocol(),(byte) ifaceDesc.getIInterface()); ! UsbInterfaceImp iface = new UsbInterfaceImp(usbConfig, desc); ! boolean active = ((0 == ifaceDesc.getBAlternateSetting())? true : false); ! /* If the config is not active, neither are its interface settings */ ! if(usbConfig.isActive() && active) { ! iface.setActiveSettingNumber(iface.getUsbInterfaceDescriptor().bAlternateSetting()); } else { ! log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","inteface not active"); ! ! } ! WindowsDeviceOsImp windowsDeviceOsImp = (WindowsDeviceOsImp) iface.getUsbConfigurationImp().getUsbDeviceImp(); ! WindowsInterfaceOsImp windowsInterfaceOsImp = new WindowsInterfaceOsImp(iface, windowsDeviceOsImp); ! iface.setUsbInterfaceOsImp(windowsInterfaceOsImp); ! for (int i = 0; i < ifaceDesc.getBNumEndpoints(); i++) { ! buildEndpoint(iface, Libusb.usb_endpoint_descriptor_index(ifaceDesc.getEndpoint(),i)); } } ! private static void buildConfig(UsbDeviceImp usbDev,usb_config_descriptor config) { ! UsbConfigurationDescriptorImp desc = new UsbConfigurationDescriptorImp((byte) config.getBLength(), ! (byte) config.getBDescriptorType(),(short) config.getWTotalLength(), ! (byte) config.getBNumInterfaces(),(byte) config.getBConfigurationValue(), ! (byte) config.getIConfiguration(),(byte) config.getBmAttributes(), ! (byte) config.getMaxPower()); ! UsbConfigurationImp usbConfig = new UsbConfigurationImp(usbDev, desc); ! usbDev.addUsbConfigurationImp(usbConfig); ! // FIXME: since most devices only support one configuration we will use the first one as default ! // this is probably ok for devices that support more than one configuration as well ! // unless it has been somehow previously set ! if(config.getBConfigurationValue() == 1) ! { ! // log(LOG_HOTPLUG, FUNC, CLASS, "buildConfiguration","WARNING Using config " + config.getBConfigurationValue()+" as active; no checking."); ! config.setIConfiguration((byte)1); ! usbDev.setActiveUsbConfigurationNumber((byte)config.getBConfigurationValue()); ! } ! ! for (int i = 0; i < config.getBNumInterfaces(); i++) ! { ! usb_interface iface; ! iface = Libusb.usb_interface_index(config.get_interface(),i); ! for (int j = 0; j < iface.getNum_altsetting(); j++) { ! usb_interface_descriptor ifaceDesc; ! ifaceDesc = Libusb.usb_interface_descriptor_index(iface.getAltsetting(),j); ! buildInterface(usbConfig, ifaceDesc); ! } ! } ! } ! /** ! * Creates a UsbDevice or UsbHub, depending on the type of ! * argument dev, fills in the values from the config of dev ! * and checks, whether this device ! * @param dev ! * @param bus ! */ ! static UsbDeviceImp buildDevice( usb_device dev, usb_bus bus) ! { ! UsbDeviceImp usbDev; ! if( dev.getDescriptor().getBDeviceClass() == Libusb.USB_CLASS_HUB) ! { ! usbDev = new WindowsHubOsImp(dev); ! // note that libusb doesn't give us information about the number of ports ! } ! else ! { ! usbDev = new WindowsDeviceOsImp(dev); ! usbDev.setSpeed(UsbConst.DEVICE_SPEED_UNKNOWN); // libusb doesn't tell the speed ! // now build all configurations ! for (int i = 0; i < dev.getDescriptor().getBNumConfigurations(); i++) { ! buildConfig(usbDev,Libusb.usb_config_descriptor_index(dev.getConfig(),i)); } ! } ! return(usbDev); } + /** * Converts the bytes in argument buf to a String *************** *** 913,916 **** --- 839,843 ---- } + //************************************************************************* // Creation methods |