hid4java icon indicating copy to clipboard operation
hid4java copied to clipboard

setAutoDataRead(true) - nothing happens, program waits forever

Open Sciss opened this issue 3 years ago • 1 comments

I'm trying to open an HID using the latest (snapshot) version of hid4java. This works, as long as I run via sudo; but after .open I am expecting some packages to come in. Instead the program halts forever, it seems the main thread is just waiting.

I'm using Java 11 on Debian 11:

$ uname -a
Linux aleph 5.10.0-10-amd64 #1 SMP Debian 5.10.84-1 (2021-12-08) x86_64 GNU/Linux

This is the output of dmesg when the device is connected:

[768929.931938] usb 3-1: new low-speed USB device number 26 using xhci_hcd
[768930.084589] usb 3-1: New USB device found, idVendor=0079, idProduct=0006, bcdDevice= 1.07
[768930.084593] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[768930.084595] usb 3-1: Product: Generic   USB  Joystick  
[768930.084596] usb 3-1: Manufacturer: DragonRise Inc.  
[768930.093749] input: DragonRise Inc.   Generic   USB  Joystick   as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:0079:0006.001E/input/input58
[768930.093805] dragonrise 0003:0079:0006.001E: input,hidraw3: USB HID v1.10 Joystick [DragonRise Inc.   Generic   USB  Joystick  ] on usb-0000:00:14.0-1/input0
[768930.093809] dragonrise 0003:0079:0006.001E: Force Feedback for DragonRise Inc. game controllers by Richard Walmsley <[email protected]>

Listing the devices works, this is printed by my Scala code:

HidDevice [path=/dev/hidraw3, vendorId=0x79, productId=0x6, serialNumber=, releaseNumber=0x107, manufacturer=DragonRise Inc.  , product=Generic   USB  Joystick  , usagePage=0x0, usage=0x0, interfaceNumber=0]

It's a DYI game controller circuit board. I also tried with an external mouse, same problem. Here is code, which is essentially the example from the hid4java repository:

import org.hid4java.{HidManager, HidServices, HidServicesListener, HidServicesSpecification}
import org.hid4java.event.HidServicesEvent
import org.hid4java.jna.HidApi

// more or less verbatim translation from hid4java by Gary Rowe (MIT License)
object HIDTest:
  def main(args: Array[String]): Unit =
    val hidServicesSpecification = new HidServicesSpecification
    hidServicesSpecification.setAutoDataRead(true)
    hidServicesSpecification.setDataReadInterval(500)

    val hidServices = HidManager.getHidServices(hidServicesSpecification)
    hidServices.addHidServicesListener(Listener)
    
    println("Manually starting HID services.")
    hidServices.start()
    
    println("Enumerating attached devices...")
    
    // Provide a list of attached devices    
    import scala.jdk.CollectionConverters._
    val hidDevices = hidServices.getAttachedHidDevices.asScala
    for hidDevice <- hidDevices do
      println(hidDevice)

    if hidDevices.nonEmpty then
      val hidDevice = hidDevices.find(_.getProduct.trim == "Generic   USB  Joystick").get
      println(s"Opening for four seconds... isClosed? ${hidDevice.isClosed}")
      hidDevice.open()
      Thread.sleep(4000)
      hidDevice.close()

    Thread.sleep(1000)

    println("Triggering shutdown...")
    hidServices.shutdown()

  object Listener extends HidServicesListener:
    override def hidDeviceAttached(event: HidServicesEvent): Unit =
      println("Device attached: " + event)

    override def hidDeviceDetached(event: HidServicesEvent): Unit =
      println("Device detached: " + event)
  
    override def hidFailure(event: HidServicesEvent): Unit =
      println("HID failure: " + event)
  
    override def hidDataReceived(event: HidServicesEvent): Unit =
      println("Data received.")

  end Listener

When I add HidApi.useLibUsbVariant = true, the program does not wait indefinitely, but the main thread completes after four seconds, as intended; however I never see a "Data received" message, even when pressing buttons on the controller. When I sudo hexdump -C /dev/hidraw3, data is definitely coming in:

00000000  7f 7f 7c 7f 7f 0f 00 c0  7f 7f 7c 7f 7f 0f 00 c0  |..|.......|.....|
*
000000f0  7f 7f 7c 7f 7f 0f 00 c0  7f 7f 7d 7f 7f 0f 00 c0  |..|.......}.....|
00000100  7f 7f 7c 7f 7f 0f 00 c0  7f 7f 7c 7f 7f 0f 00 c0  |..|.......|.....|
*
000002b0  7f 7f 7e 7f 7f 0f 00 c0  7f 7f 7d 7f 7f 0f 00 c0  |..~.......}.....|
000002c0  7f 7f 7d 7f 7f 0f 00 c0  7f 7f 7e 7f 7f 0f 00 c0  |..}.......~.....|
000002d0  7f 7f 7d 7f 7f 0f 00 c0  7f 7f 7f 7f 7f 0f 00 c0  |..}.............|
000002e0  7f 7f 7d 7f 7f 0f 00 c0  7f 7f 7d 7f 7f 0f 00 c0  |..}.......}.....|
...

Sciss avatar Jan 07 '22 19:01 Sciss

I checked with 'purejavahidapi', this works in the same situation, so the controller is definitely correctly readable. Perhaps I'm just missing a way to trigger the reading?

Sciss avatar Jan 07 '22 20:01 Sciss

It's been some time since this occurred. Is it still an issue?

gary-rowe avatar Dec 05 '22 10:12 gary-rowe