|
From: Mike C. <mc...@us...> - 2008-08-18 18:52:11
|
Update of /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows In directory sc8-pr-cvs17.sourceforge.net:/tmp/cvs-serv4307 Modified Files: JavaxUsb.java WindowsUsbServices.java Log Message: Some improvements to hot plugging. Better testing of devices when creating connected/disconnected lists. Still needs better testing Index: WindowsUsbServices.java =================================================================== RCS file: /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows/WindowsUsbServices.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** WindowsUsbServices.java 13 Aug 2008 21:08:39 -0000 1.3 --- WindowsUsbServices.java 18 Aug 2008 18:52:04 -0000 1.4 *************** *** 304,312 **** List connectedDevices = new ArrayList(); List disconnectedDevices = new ArrayList(); ! fillDeviceList(getRootUsbHubImp(),disconnectedDevices); while(disconnectedDevices.remove(getRootUsbHubImp())); ! JavaxUsb.nativeTopologyUpdater(this, connectedDevices, disconnectedDevices); Iterator iterator = disconnectedDevices.iterator(); --- 304,315 ---- List connectedDevices = new ArrayList(); List disconnectedDevices = new ArrayList(); ! fillDeviceList(getRootUsbHubImp(),disconnectedDevices); while(disconnectedDevices.remove(getRootUsbHubImp())); ! 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(); *************** *** 341,345 **** try { ! Thread.sleep(topologyUpdateNewDeviceDelay); } catch (InterruptedException iE) --- 344,348 ---- try { ! if(!device.isUsbHub()) Thread.sleep(topologyUpdateNewDeviceDelay); } catch (InterruptedException iE) Index: JavaxUsb.java =================================================================== RCS file: /cvsroot/javax-usb/javax-usb-libusb/src/com/mcreations/usb/windows/JavaxUsb.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** JavaxUsb.java 13 Aug 2008 21:08:39 -0000 1.5 --- JavaxUsb.java 18 Aug 2008 18:52:04 -0000 1.6 *************** *** 27,31 **** import net.sf.libusb.usb_config_descriptor; import net.sf.libusb.usb_device; ! import net.sf.libusb.usb_device_descriptor; import net.sf.libusb.usb_endpoint_descriptor; import net.sf.libusb.usb_interface; --- 27,31 ---- import net.sf.libusb.usb_config_descriptor; import net.sf.libusb.usb_device; ! //import net.sf.libusb.usb_device_descriptor; import net.sf.libusb.usb_endpoint_descriptor; import net.sf.libusb.usb_interface; *************** *** 43,46 **** --- 43,47 ---- import javax.usb.UsbException; + import java.io.UnsupportedEncodingException; /** *************** *** 61,65 **** private static Mutex mutex = new Mutex(); ! private static void printEndpoint(usb_endpoint_descriptor endpoint) { --- 62,66 ---- private static Mutex mutex = new Mutex(); ! /* private static void printEndpoint(usb_endpoint_descriptor endpoint) { *************** *** 148,152 **** // " bSynchAddress: " + endpoint.getBSynchAddress()); } ! private static void printIfaceDesc(usb_interface_descriptor iface) { --- 149,154 ---- // " bSynchAddress: " + endpoint.getBSynchAddress()); } ! */ ! /* private static void printIfaceDesc(usb_interface_descriptor iface) { *************** *** 186,191 **** } } ! ! private static void printInterface(usb_interface iface) { int i; --- 188,193 ---- } } ! */ ! /* private static void printInterface(usb_interface iface) { int i; *************** *** 198,203 **** } } ! ! private static void printConfiguration(usb_config_descriptor config) { int i; --- 200,205 ---- } } ! */ ! /* private static void printConfiguration(usb_config_descriptor config) { int i; *************** *** 230,234 **** } } ! /** * Log messages to System.err, if the level <= current traceLevel. --- 232,236 ---- } } ! */ /** * Log messages to System.err, if the level <= current traceLevel. *************** *** 392,395 **** --- 394,398 ---- } + /** * Convert the error code to a UsbException. *************** *** 402,405 **** --- 405,409 ---- } + /** * Convert the error code to a UsbException using the specified text. *************** *** 418,421 **** --- 422,426 ---- } + //************************************************************************* // Native methods *************** *** 456,460 **** /** ! * Return if the specified devices appear to be equal. * <p> * If either of the device's descriptors are null, this returns false. --- 461,468 ---- /** ! * Return if the compared devices are the same device. ! * FIXME relies on the device serial number string to be unique for each device (this may not always be true ! * if we could examine the port number / path, we could make a more definative argument ! * Testing starts with easy things to compare and becomes progressivly more complicated * <p> * If either of the device's descriptors are null, this returns false. *************** *** 463,469 **** * @return If the devices appear to be equal. */ ! protected static boolean isUsbDevicesEqual( ! UsbDeviceImp dev1, ! UsbDeviceImp dev2) { try --- 471,475 ---- * @return If the devices appear to be equal. */ ! protected static boolean isUsbDevicesEqual(UsbDeviceImp dev1,UsbDeviceImp dev2) { try *************** *** 472,484 **** UsbDeviceDescriptor desc2 = dev2.getUsbDeviceDescriptor(); ! return (dev1.isUsbHub() == dev1.isUsbHub()) ! && (dev1.getSpeed() == dev2.getSpeed()) && desc1.equals(desc2); } catch (NullPointerException npE) { ! return false; } } /** * Check for the existence of a device in the given <code>connected</code> and --- 478,502 ---- UsbDeviceDescriptor desc2 = dev2.getUsbDeviceDescriptor(); ! if( dev1.isUsbHub() != dev1.isUsbHub() ) return(false); ! if( dev1.getSpeed() != dev2.getSpeed() ) return(false); ! if( !desc1.equals(desc2) ) return(false); ! if( !dev1.getSerialNumberString().equals(dev2.getSerialNumberString() )) return(false); ! return(true); ! } ! catch (UnsupportedEncodingException uee) ! { ! return(false); } catch (NullPointerException npE) { ! return(false); ! } ! catch (UsbException ue) ! { ! return(false); } } + /** * Check for the existence of a device in the given <code>connected</code> and *************** *** 495,560 **** * @param p The parent port number. * @param device The UsbDeviceImp to add. ! * @param disconnected The List of disconnected devices. ! * @param connected The List of connected devices. * @return The new UsbDeviceImp or existing UsbDeviceImp. */ ! private static UsbDeviceImp checkUsbDeviceImp( ! UsbHubImp hub, ! int p, ! UsbDeviceImp device, ! List connected, ! List disconnected) { String meth = "checkUsbDeviceImp"; ! log(LOG_HOTPLUG, FUNC, CLASS, meth, "Entered with device " + device); ! ! byte port = (byte) p; ! UsbPortImp usbPortImp = hub.getUsbPortImp(port); ! if (null == usbPortImp) { ! hub.resize(port); ! usbPortImp = hub.getUsbPortImp(port); } ! log( ! LOG_HOTPLUG, DEBUG, CLASS, meth, ! meth+ " Hub now has " + hub.getNumberOfPorts() + " ports"); ! ! UsbDeviceImp existingDevice = usbPortImp.getUsbDeviceImp(); ! // If they are equal, it means, that the ! // device was disconnected and then reconnected, ! // because the main loop in topologyUpdater only ! // calls buildDevice in case of changes in the ! // topology (Libusb.usb_find_devices != 0). ! if (isUsbDevicesEqual(existingDevice, device)) { ! disconnected.remove(existingDevice); ! log( ! LOG_HOTPLUG, FUNC, CLASS, meth, ! "Removed disconnected device " + existingDevice); } ! if (usbPortImp.isUsbDeviceAttached()) ! { ! usbPortImp.detachUsbDeviceImp(usbPortImp.getUsbDeviceImp()); } ! connected.add(device); ! device.setParentUsbPortImp(usbPortImp); ! log(LOG_HOTPLUG, FUNC, CLASS, meth, "Leaving with device " + device); ! ! return device; } private static void buildConfig(UsbDeviceImp usbDev,usb_config_descriptor config) { - // (*env)->CallStaticObjectMethod( - // env, JavaxUsb, createUsbConfigurationImp, usbDeviceImp, - // config_desc->bLength, config_desc->bDescriptorType, - // config_desc->wTotalLength, config_desc->bNumInterfaces, - // config_desc->bConfigurationValue, config_desc->iConfiguration, - // config_desc->bmAttributes, config_desc->MaxPower, is_active); UsbConfigurationDescriptorImp desc = new UsbConfigurationDescriptorImp((byte) config.getBLength(), (byte) config.getBDescriptorType(),(short) config.getWTotalLength(), --- 513,564 ---- * @param p The parent port number. * @param device The UsbDeviceImp to add. ! * @param currentDevices - the list of devices that were connected before this call to topology update ! * @param disconnected The List of all devices that can possibly disconnected ! * @param connected The List of connected devices that are newely connected * @return The new UsbDeviceImp or existing UsbDeviceImp. */ ! private static void checkUsbDeviceImp(UsbHubImp hub, int p, 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(); ! found = true; ! } } ! // 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); } ! if(log.isDebugEnabled()) log.debug(meth+ " Leaving with device " + device); } + private static void buildConfig(UsbDeviceImp usbDev,usb_config_descriptor config) { UsbConfigurationDescriptorImp desc = new UsbConfigurationDescriptorImp((byte) config.getBLength(), (byte) config.getBDescriptorType(),(short) config.getWTotalLength(), *************** *** 571,575 **** 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()); --- 575,579 ---- 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()); *************** *** 579,583 **** { usb_interface iface; - // iface = Libusb.usb_interface_index(config.getInterface(),i); iface = Libusb.usb_interface_index(config.get_interface(),i); --- 583,586 ---- *************** *** 600,604 **** 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(), --- 603,607 ---- 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(), *************** *** 608,612 **** (byte) ifaceDesc.getBInterfaceProtocol(),(byte) ifaceDesc.getIInterface()); ! log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","new interface descriptor " + desc); UsbInterfaceImp iface = new UsbInterfaceImp(usbConfig, desc); --- 611,615 ---- (byte) ifaceDesc.getBInterfaceProtocol(),(byte) ifaceDesc.getIInterface()); ! // log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","new interface descriptor " + desc); UsbInterfaceImp iface = new UsbInterfaceImp(usbConfig, desc); *************** *** 617,621 **** if(usbConfig.isActive() && active) { ! log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","inteface is active"); iface.setActiveSettingNumber(iface.getUsbInterfaceDescriptor().bAlternateSetting()); } --- 620,624 ---- if(usbConfig.isActive() && active) { ! // log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","inteface is active"); iface.setActiveSettingNumber(iface.getUsbInterfaceDescriptor().bAlternateSetting()); } *************** *** 635,639 **** } ! log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","Leaving with interface " + iface); } --- 638,642 ---- } ! // log(LOG_HOTPLUG, FUNC, CLASS, "buildInterface","Leaving with interface " + iface); } *************** *** 642,648 **** * @param usb_endpoint_descriptor */ ! private static void buildEndpoint( ! UsbInterfaceImp iface, ! usb_endpoint_descriptor endPointDesc) { UsbEndpointDescriptorImp desc = --- 645,649 ---- * @param usb_endpoint_descriptor */ ! private static void buildEndpoint(UsbInterfaceImp iface,usb_endpoint_descriptor endPointDesc) { UsbEndpointDescriptorImp desc = *************** *** 718,725 **** * @param parentHub * @param parentport - * @param connectedDevices - * @param disconnectedDevices */ ! static void buildDevice( usb_device dev, usb_bus bus, UsbHubImp parentHub, int parentport, List connectedDevices, List disconnectedDevices) { String meth = "buildDevice"; --- 719,724 ---- * @param parentHub * @param parentport */ ! static UsbDeviceImp buildDevice( usb_device dev, usb_bus bus, UsbHubImp parentHub, int parentport) { String meth = "buildDevice"; *************** *** 729,733 **** UsbDeviceImp usbDev; ! if (dev.getDescriptor().getBDeviceClass() == Libusb.USB_CLASS_HUB) { log(LOG_HOTPLUG, DEBUG, CLASS, meth, "Device is a hub."); --- 728,732 ---- UsbDeviceImp usbDev; ! if( dev.getDescriptor().getBDeviceClass() == Libusb.USB_CLASS_HUB) { log(LOG_HOTPLUG, DEBUG, CLASS, meth, "Device is a hub."); *************** *** 739,771 **** else { ! log(LOG_HOTPLUG, DEBUG, CLASS, meth, "Device is normal (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)); } ! ! checkUsbDeviceImp(parentHub, parentport, usbDev, connectedDevices, disconnectedDevices); ! ! log.debug("Leaving buildDevice with device " + dev.getFilename()); ! log.debug(""); } /** * @param services ! * @param connectedDevices ! * @param disconnectedDevices ! * @return */ ! static int nativeTopologyUpdater( ! WindowsUsbServices services, ! List connectedDevices, ! List disconnectedDevices) { String method = "topologyUpdater"; --- 738,773 ---- 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("Leaving buildDevice with device " + dev.getFilename()); ! log.debug(""); } ! 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"; *************** *** 773,786 **** 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(); --- 775,785 ---- 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(); *************** *** 788,796 **** { int busCount = Libusb.usb_find_busses(); - String msg = "Found " + busCount + " new busses."; - log(LOG_HOTPLUG, DEBUG, CLASS, method, msg); - int deviceCount = Libusb.usb_find_devices(); ! msg = "Found " + deviceCount + " new devices."; log(LOG_HOTPLUG, DEBUG, CLASS, method, msg); --- 787,793 ---- { 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); *************** *** 809,861 **** } ! usb_bus bus = Libusb.usb_get_busses(); int portNum = 1; ! ! while (bus != null) { ! msg = "Scanning bus " + bus.getDirname(); log(LOG_HOTPLUG, DEBUG, CLASS, method, msg); ! usb_device dev = bus.getDevices(); ! ! // int index = 1; ! while (dev != null) { ! usb_device_descriptor devDesc = dev.getDescriptor(); ! log(LOG_HOTPLUG, DEBUG, CLASS, method, "nativeTopologyListener devDesc: "+devDesc); ! msg = "Device: " + dev.getFilename(); ! log(LOG_HOTPLUG, DEBUG, CLASS, method, msg); ! ! if (dev.getConfig() == null) { ! log( ! LOG_HOTPLUG, ERROR, CLASS, method, ! "Couldn't retrieve descriptors for device '" ! + bus.getDirname() + "/" + dev.getFilename() + "'"); ! ! dev = dev.getNext(); continue; } ! for ( ! int i = 0; ! i < dev.getDescriptor().getBNumConfigurations(); ! i++) { ! usb_config_descriptor config; ! config = Libusb.usb_config_descriptor_index(dev.getConfig(),i); ! if(log.isDebugEnabled()) printConfiguration(config); } ! ! buildDevice(dev, bus, rootHub, portNum++, connectedDevices,disconnectedDevices); ! ! dev = dev.getNext(); } ! ! bus = bus.getNext(); } } finally --- 806,862 ---- } ! usb_bus libusb_bus = Libusb.usb_get_busses(); ! log.debug(" disconnectedDevices size: "+disconnectedDevices.size()); int portNum = 1; ! ! while (libusb_bus != null) { ! msg = "Scanning bus " + libusb_bus.getDirname(); 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()) { ! 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, rootHub, portNum++ ); ! // portNum++; ! // log.debug(method+" device is Hub, skipping"); ! // } ! // else { ! UsbDeviceImp usbDev = buildDevice(libusb_dev, libusb_bus, rootHub, portNum ); ! // 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, portNum++, usbDev, disconnectedDevices, connectedDevices); } ! libusb_dev = libusb_dev.getNext(); } ! libusb_bus = libusb_bus.getNext(); } + log.debug(" disconnectedDevices size: "+disconnectedDevices.size()); } finally |