Compare commits

...
Author SHA1 Message Date
sctanf f68f80eb21 Update solarxr-protocol 2026-04-12 07:54:02 -05:00
sctanf c587af33f4 Testing send HID commands to receiver 2026-04-12 07:54:01 -05:00
sctanf dcd2d131d3 HIDManager extend HIDCommon 2026-04-12 07:54:01 -05:00
10 changed files with 114 additions and 7 deletions

No files matched your search

@@ -47,12 +47,13 @@ fun startVRServer(activity: AppCompatActivity) {
vrServer.start()
// Start service for USB HID trackers
val androidHidManager = AndroidHIDManager(
LogManager.info("Starting the HID tracker service")
vrServer.hidTrackersServer = AndroidHIDManager(
"Sensors HID service",
{ tracker: Tracker -> vrServer.registerTracker(tracker) },
activity,
)
androidHidManager.start()
vrServer.hidTrackersServer.start()
Keybinding(vrServer)
vrServer.join()
@@ -32,7 +32,7 @@ class AndroidHIDManager(
name: String,
private val trackersConsumer: Consumer<Tracker>,
private val context: Context,
) : Thread(name) {
) : HIDCommon(name) {
private val devices: MutableList<HIDDevice> = mutableListOf()
private val devicesBySerial: MutableMap<String, MutableList<Int>> = HashMap()
private val devicesByHID: MutableMap<UsbDevice, MutableList<Int>> = HashMap()
@@ -30,6 +30,7 @@ import dev.slimevr.status.StatusSystem
import dev.slimevr.tracking.processor.HumanPoseManager
import dev.slimevr.tracking.processor.skeleton.HumanSkeleton
import dev.slimevr.tracking.trackers.*
import dev.slimevr.tracking.trackers.hid.HIDCommon
import dev.slimevr.tracking.trackers.udp.TrackersUDPServer
import dev.slimevr.trackingchecklist.TrackingChecklistManager
import dev.slimevr.util.ann.VRServerThread
@@ -67,6 +68,7 @@ class VRServer @JvmOverloads constructor(
@JvmField
val humanPoseManager: HumanPoseManager
private val trackers: MutableList<Tracker> = FastList()
var hidTrackersServer: HIDCommon? = null
val trackersServer: TrackersUDPServer
private val bridges: MutableList<Bridge> = FastList()
private val tasks: Queue<Runnable> = LinkedBlockingQueue()
@@ -0,0 +1,4 @@
package dev.slimevr.hid
interface HIDListener {
}
@@ -21,6 +21,7 @@ import dev.slimevr.protocol.rpc.setup.RPCUtil.getLocalIp
import dev.slimevr.protocol.rpc.status.RPCStatusHandler
import dev.slimevr.protocol.rpc.trackingchecklist.RPCTrackingChecklistHandler
import dev.slimevr.protocol.rpc.trackingpause.RPCTrackingPause
import dev.slimevr.protocol.rpc.hid.RPCHIDHandler
import dev.slimevr.tracking.processor.config.SkeletonConfigOffsets
import dev.slimevr.tracking.processor.stayaligned.poses.RelaxedPose
import dev.slimevr.tracking.trackers.TrackerPosition
@@ -52,6 +53,7 @@ class RPCHandler(private val api: ProtocolAPI) : ProtocolHandler<RpcMessageHeade
RPCVRChatHandler(this, api)
RPCTrackingChecklistHandler(this, api)
RPCUserHeightCalibration(this, api)
RPCHIDHandler(this, api)
registerPacketListener(
RpcMessage.AssignTrackerRequest,
@@ -0,0 +1,29 @@
package dev.slimevr.protocol.rpc.hid
import com.google.flatbuffers.FlatBufferBuilder
import dev.slimevr.protocol.GenericConnection
import dev.slimevr.protocol.ProtocolAPI
import dev.slimevr.protocol.ProtocolAPIServer
import dev.slimevr.protocol.rpc.RPCHandler
import dev.slimevr.hid.HIDListener
import dev.slimevr.tracking.trackers.hid.HIDCommon
import solarxr_protocol.rpc.UnknownHIDDeviceHandshakeNotification
import solarxr_protocol.rpc.HIDAllReceiversCommandRequest
import solarxr_protocol.rpc.HIDTrackerCommandRequest
import solarxr_protocol.rpc.RpcMessage
import solarxr_protocol.rpc.RpcMessageHeader
import java.util.function.Consumer
class RPCHIDHandler(var rpcHandler: RPCHandler, var api: ProtocolAPI) : HIDListener {
init {
rpcHandler.registerPacketListener(RpcMessage.HIDTrackerCommandRequest, ::onTrackerRequest)
}
fun onTrackerRequest(conn: GenericConnection, messageHeader: RpcMessageHeader) {
val req = messageHeader.message(HIDTrackerCommandRequest()) as? HIDTrackerCommandRequest ?: return
val hwid = req.deviceAddress()
val command = req.command()
api.server.hidTrackersServer!!.parseCommand(hwid, command)
}
}
@@ -23,7 +23,21 @@ import kotlin.math.sqrt
/**
* A collection of shared HID functions between OS specific HID implementations.
*/
class HIDCommon {
open class HIDCommon(name: String) :
Thread(name) {
fun parseCommand(
hwid: String?,
command: String
) {
val tokens = command.split(' ')
val parsed = tokens[0].toUByte()
sendTrackerCommand(hwid, parsed)
}
open fun sendTrackerCommand(
hwid: String?,
command: UByte
)
{}
companion object {
const val HID_TRACKER_RECEIVER_VID = 0x1209
const val HID_TRACKER_RECEIVER_PID = 0x7690
@@ -379,5 +393,16 @@ class HIDCommon {
tracker.dataTick() // only data tick if there is rotation data
}
}
fun constructCommandPacket(
hwid: String,
command: UByte
): ByteArray {
var array = ByteArray(16)
array.set(0, 254.toByte())
array.set(1, command.toByte())
// hwid.hexToByteArray().copyInto(array, 2)
return array
}
}
}
@@ -135,7 +135,8 @@ fun main(args: Array<String>) {
vrServer.start()
// Start service for USB HID trackers
DesktopHIDManager(
LogManager.info("Starting the HID tracker service")
vrServer.hidTrackersServer = DesktopHIDManager(
"Sensors HID service",
) { tracker: Tracker -> vrServer.registerTracker(tracker) }
@@ -28,7 +28,7 @@ import java.util.function.Consumer
* Handles desktop USB HID dongles and receives tracker data from them.
*/
class DesktopHIDManager(name: String, private val trackersConsumer: Consumer<Tracker>) :
Thread(name),
HIDCommon(name),
HidServicesListener {
private val devices: MutableList<HIDDevice> = mutableListOf()
private val devicesBySerial: MutableMap<String, MutableList<Int>> = HashMap()
@@ -265,6 +265,49 @@ class DesktopHIDManager(name: String, private val trackersConsumer: Consumer<Tra
}
}
override fun sendTrackerCommand(
hwid: String?,
command: UByte
) {
var address: String
var id: Int? = null
if (hwid != null) {
synchronized(devices) {
for ((index, device) in devices.withIndex()) {
if (device.hardwareIdentifier == hwid) {
id = index
break
}
}
}
if (id == null) {
return
}
address = hwid
}
else {
address = "000000000000" // send to all
}
val buffer = constructCommandPacket(address, command)
synchronized(devicesByHID) {
for ((hidDevice, deviceList) in devicesByHID) {
if (id != null)
{
var device = deviceList.getOrNull(id)
if (device == null) {
continue
}
}
try {
//public int write(byte[] message, int packetLength, byte reportId, boolean applyPadding=false)
hidDevice.write(buffer, 16, 0)
} catch (e: IllegalStateException) {
continue // Skip devices with open error (Maybe disconnected)
}
}
}
}
override fun run() { // Doesn't seem to run
}