Skip to content

Commit

Permalink
Change usb enumeration to only specific Vid/Pid (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
sctanf authored Apr 28, 2024
1 parent 6d01491 commit 91a31b3
Showing 1 changed file with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import org.hid4java.HidServices
import org.hid4java.HidServicesListener
import org.hid4java.HidServicesSpecification
import org.hid4java.event.HidServicesEvent
import org.hid4java.jna.HidApi
import org.hid4java.jna.HidDeviceInfoStructure
import java.util.function.Consumer
import kotlin.experimental.and

private const val HID_TRACKER_RECEIVER_VID = 0x2FE3
private const val HID_TRACKER_RECEIVER_PID = 0x5652

/**
* Receives trackers data by UDP using extended owoTrack protocol.
*/
Expand All @@ -39,10 +44,16 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
dataReadThread.isDaemon = true
dataReadThread.name = "hid4java data reader"
dataReadThread.start()
// We use hid4java but actually do not start the service ever, because it will just enumerate everything and cause problems
// Do enumeration ourself
val deviceEnumerateThread = Thread(deviceEnumerateRunnable)
deviceEnumerateThread.isDaemon = true
deviceEnumerateThread.name = "hid4java device enumerator"
deviceEnumerateThread.start()
}

private fun checkConfigureDevice(hidDevice: HidDevice) {
if (hidDevice.vendorId == 0x2FE3 && hidDevice.productId == 0x5652) { // TODO: Use correct ids
if (hidDevice.vendorId == HID_TRACKER_RECEIVER_VID && hidDevice.productId == HID_TRACKER_RECEIVER_PID) { // TODO: Use correct ids
if (hidDevice.isClosed) {
check(hidDevice.open()) { "Unable to open device" }
}
Expand Down Expand Up @@ -140,22 +151,35 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)

@get:Synchronized
private val dataReadRunnable: Runnable
get() = Runnable {
while (true) {
try {
sleep(0) // Possible performance impact
} catch (e: InterruptedException) {
currentThread().interrupt()
break
}
dataRead() // not in try catch?
}
}

@get:Synchronized
private val deviceEnumerateRunnable: Runnable
get() = Runnable {
try {
sleep(100) // Delayed start
} catch (e: InterruptedException) {
currentThread().interrupt()
return@Runnable
}
hidServices.start()
while (true) {
try {
sleep(0) // Possible performance impact
sleep(1000)
} catch (e: InterruptedException) {
currentThread().interrupt()
break
}
dataRead() // not in try catch?
deviceEnumerate() // not in try catch?
}
}

Expand Down Expand Up @@ -227,25 +251,52 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
}
}

private fun deviceEnumerate() {
var root: HidDeviceInfoStructure? = null
try {
root = HidApi.enumerateDevices(HID_TRACKER_RECEIVER_VID, HID_TRACKER_RECEIVER_PID) // TODO: change to proper vendorId and productId, need to enum all appropriate productId
} catch (e: Throwable) {
LogManager.severe("[TrackerServer] Couldn't enumerate HID devices", e)
}
val hidDeviceList: MutableList<HidDevice> = mutableListOf()
if (root != null) {
var hidDeviceInfoStructure: HidDeviceInfoStructure? = root
do {
hidDeviceList.add(HidDevice(hidDeviceInfoStructure, null, hidServicesSpecification))
hidDeviceInfoStructure = hidDeviceInfoStructure?.next()
} while (hidDeviceInfoStructure != null)
HidApi.freeEnumeration(root)
}
synchronized(devicesByHID) {
// Work on devicesByHid and add/remove as necessary
val removeList: MutableList<HidDevice> = devicesByHID.keys.toMutableList()
removeList.removeAll(hidDeviceList)
hidDeviceList.removeAll(devicesByHID.keys) // addList
for (device in removeList) {
removeDevice(device)
}
for (device in hidDeviceList) {
checkConfigureDevice(device)
}
}
}

override fun run() { // Doesn't seem to run
}

fun getDevices(): List<Device> = devices

// We don't use these
override fun hidDeviceAttached(event: HidServicesEvent) {
checkConfigureDevice(event.hidDevice)
}

override fun hidDeviceDetached(event: HidServicesEvent) {
removeDevice(event.hidDevice)
}

override fun hidFailure(event: HidServicesEvent) {
// TODO:
}

override fun hidDataReceived(p0: HidServicesEvent?) {
// Seems to have issues with the size of data returned
}

companion object {
Expand Down

0 comments on commit 91a31b3

Please sign in to comment.