Remove BodyPart fully from solarxr, implement new connection sequence

This commit is contained in:
loucass003 committed 2026-09-15 00:12:06 +02:00
1 parent ee38e9f6ba
commit 65cdc83e3c
120 files changed
+605 -377

No files matched your search

@@ -55,7 +55,7 @@ import io.github.axisangles.ktmath.Vector3
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import solarxr_protocol.datatypes.DeviceOrigin
import solarxr_protocol.datatypes.TrackerStatus
import solarxr_protocol.datatypes.hardware_info.ImuType
@@ -2,7 +2,6 @@ package dev.slimevr.bones
import kotlinx.coroutines.test.runTest
import solarxr_protocol.connection.BoneDefinition
import solarxr_protocol.datatypes.BodyPart
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@@ -1,7 +1,6 @@
package dev.slimevr.bones
import solarxr_protocol.connection.BoneDefinition
import solarxr_protocol.datatypes.BodyPart
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@@ -33,9 +32,9 @@ class BoneRegistryTest {
val registry = BoneRegistry.from(
WireBoneRegistry(
bones = listOf(
BoneDefinition(1u, "a", null, 0u, null),
BoneDefinition(2u, "b", null, 1u, null),
BoneDefinition(3u, "c", null, 2u, null),
BoneDefinition(1u, "a", null, 0u),
BoneDefinition(2u, "b", null, 1u),
BoneDefinition(3u, "c", null, 2u),
),
),
)
@@ -48,8 +47,8 @@ class BoneRegistryTest {
BoneRegistry.from(
WireBoneRegistry(
bones = listOf(
BoneDefinition(1u, "a", null, 0u, null),
BoneDefinition(1u, "b", null, 0u, null),
BoneDefinition(1u, "a", null, 0u),
BoneDefinition(1u, "b", null, 0u),
),
),
)
@@ -61,7 +60,7 @@ class BoneRegistryTest {
assertFailsWith<IllegalArgumentException> {
BoneRegistry.from(
WireBoneRegistry(
bones = listOf(BoneDefinition(0u, "a", null, 0u, null)),
bones = listOf(BoneDefinition(0u, "a", null, 0u)),
),
)
}
@@ -72,7 +71,7 @@ class BoneRegistryTest {
assertFailsWith<IllegalArgumentException> {
BoneRegistry.from(
WireBoneRegistry(
bones = listOf(BoneDefinition(1u, "a", null, 2u, null)),
bones = listOf(BoneDefinition(1u, "a", null, 2u)),
),
)
}
@@ -84,8 +83,8 @@ class BoneRegistryTest {
BoneRegistry.from(
WireBoneRegistry(
bones = listOf(
BoneDefinition(1u, "a", null, 2u, null),
BoneDefinition(2u, "b", null, 1u, null),
BoneDefinition(1u, "a", null, 2u),
BoneDefinition(2u, "b", null, 1u),
),
),
)
@@ -97,21 +96,7 @@ class BoneRegistryTest {
assertFailsWith<IllegalArgumentException> {
BoneRegistry.from(
WireBoneRegistry(
bones = listOf(BoneDefinition(1u, "a", null, 1u, null)),
),
)
}
}
@Test
fun `rejects duplicate standard body parts`() {
assertFailsWith<IllegalArgumentException> {
BoneRegistry.from(
WireBoneRegistry(
bones = listOf(
BoneDefinition(1u, "a", null, 0u, BodyPart.HIP),
BoneDefinition(2u, "b", null, 0u, BodyPart.HIP),
),
bones = listOf(BoneDefinition(1u, "a", null, 1u)),
),
)
}
@@ -123,8 +108,8 @@ class BoneRegistryTest {
BoneRegistry.from(
WireBoneRegistry(
bones = listOf(
BoneDefinition(1u, "a", null, 0u, null),
BoneDefinition(3u, "b", null, 0u, null),
BoneDefinition(1u, "a", null, 0u),
BoneDefinition(3u, "b", null, 0u),
),
),
)
@@ -8,7 +8,7 @@ import dev.slimevr.skeleton.buildBones
import dev.slimevr.skeleton.defaultSkeletonState
import io.github.axisangles.ktmath.Vector3
import kotlinx.coroutines.test.runTest
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@@ -4,7 +4,7 @@ import dev.slimevr.bones.BoneRegistry
import dev.slimevr.solarxr.driver.DRIVER_SUPPORTED_BONES
import dev.slimevr.vmc.VMC_SUPPORTED_BONES
import dev.slimevr.vrcosc.VRC_OSC_SUPPORTED_BONES
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import solarxr_protocol.rpc.RoutingOutput
import solarxr_protocol.rpc.RoutingOutputState
import kotlin.test.Test
@@ -1,6 +1,6 @@
package dev.slimevr.routing
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -3,7 +3,7 @@ package dev.slimevr.routing
import dev.slimevr.bones.BoneRegistry
import dev.slimevr.bones.boneId
import dev.slimevr.config.BoneRoutingConfig
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import solarxr_protocol.rpc.RoutingOutput
import solarxr_protocol.rpc.RoutingOutputState
import kotlin.test.Test
@@ -6,7 +6,7 @@ import dev.slimevr.bones.boneId
import dev.slimevr.bones.resolveToBoneIds
import io.github.axisangles.ktmath.EulerOrder
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import kotlin.test.Test
class IKTest {
@@ -6,21 +6,23 @@ import dev.slimevr.TestAppContext
import dev.slimevr.bones.BoneRegistryManager
import dev.slimevr.buildTestVrServer
import dev.slimevr.context.Context
import dev.slimevr.fbscodegen.runtime.JvmFlatBufferWriter
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import solarxr_protocol.ClientHello
import solarxr_protocol.HelloStatus
import solarxr_protocol.MessageBundle
import solarxr_protocol.ServerHello
import solarxr_protocol.connection.BoneDefinition
import solarxr_protocol.connection.BoneRegistry
import solarxr_protocol.connection.ConfigurationAcknowledged
import solarxr_protocol.connection.BoneRegistryRequest
import solarxr_protocol.connection.ClientHello
import solarxr_protocol.connection.ConfigurationDone
import solarxr_protocol.connection.ConnectionError
import solarxr_protocol.connection.ConnectionErrorCode
import solarxr_protocol.connection.ConnectionMessage
import solarxr_protocol.connection.ConnectionMessageHeader
import solarxr_protocol.connection.HelloStatus
import solarxr_protocol.connection.InitializationRequiredError
import solarxr_protocol.connection.ServerHello
import solarxr_protocol.connection.UnsupportedRequestError
import solarxr_protocol.data_feed.DataFeedMessageHeader
import solarxr_protocol.data_feed.StartDataFeed
import java.nio.ByteBuffer
@@ -28,10 +30,13 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
@OptIn(ExperimentalCoroutinesApi::class)
private fun TestScope.buildTestBridge(sent: MutableList<MessageBundle>): SolarXRBridge {
private fun TestScope.buildTestBridge(sent: MutableList<OutboundFrame>): SolarXRBridge {
val appContext = object : TestAppContext() {
override val server = buildTestVrServer(backgroundScope)
override val bones = BoneRegistryManager.create(backgroundScope)
@@ -50,64 +55,114 @@ private fun TestScope.buildTestBridge(sent: MutableList<MessageBundle>): SolarXR
rpcDispatcher = EventDispatcher("test.rpc", backgroundScope),
driverDispatcher = EventDispatcher("test.driver", backgroundScope),
)
bridge.outbound.on<MessageBundle> { sent += it }.launchIn(backgroundScope)
bridge.outbound.on<OutboundFrame> { sent += it }.launchIn(backgroundScope)
runCurrent()
return bridge
}
private fun MessageBundle.singleConnectionError(): ConnectionError = connectionMsgs?.single()?.message as? ConnectionError ?: error("Expected a single ConnectionError")
private fun clientHello(version: UInt = SOLARXR_PROTOCOL_VERSION) = MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(ClientHello(protocolVersion = version))))
private fun OutboundFrame.connectionMessage(): ConnectionMessage? = bundle.connectionMsgs?.single()?.message
@OptIn(ExperimentalCoroutinesApi::class)
class ConnectionInitTest {
@Test
fun `checkedSolarXRFrame rejects a wrong identifier`() {
val fbb = FlatBufferBuilder(64)
ClientHello(protocolVersion = SOLARXR_PROTOCOL_VERSION).finish(JvmFlatBufferWriter(fbb))
assertFailsWith<IllegalArgumentException> {
checkedSolarXRFrame(fbb.dataBuffer(), ServerHello.FILE_IDENTIFIER)
}
}
@Test
fun `checkedSolarXRFrame rejects a truncated frame`() {
assertFailsWith<IllegalArgumentException> {
checkedSolarXRFrame(ByteBuffer.allocate(4), ClientHello.FILE_IDENTIFIER)
checkedSolarXRFrame(ByteBuffer.allocate(4))
}
}
@Test
fun `checkedSolarXRFrame rejects an oversized frame`() {
assertFailsWith<IllegalArgumentException> {
checkedSolarXRFrame(ByteBuffer.allocate(SOLARXR_MAX_FRAME_SIZE + 1), ClientHello.FILE_IDENTIFIER)
checkedSolarXRFrame(ByteBuffer.allocate(SOLARXR_MAX_FRAME_SIZE + 1))
}
}
@Test
fun `ClientHello, ServerHello and MessageBundle round-trip with their identifiers`() {
val helloFbb = FlatBufferBuilder(64)
ClientHello(protocolVersion = SOLARXR_PROTOCOL_VERSION).finish(JvmFlatBufferWriter(helloFbb))
val helloReader = checkedSolarXRFrame(helloFbb.dataBuffer(), ClientHello.FILE_IDENTIFIER)
assertTrue(ClientHello.hasIdentifier(helloReader))
assertEquals(SOLARXR_PROTOCOL_VERSION, ClientHello.fromByteBuffer(helloReader).protocolVersion)
fun `a MessageBundle round-trips through writeSolarXRBundle and checkedSolarXRFrame`() {
val fbb = FlatBufferBuilder(64)
writeSolarXRBundle(fbb, clientHello())
val reader = checkedSolarXRFrame(fbb.dataBuffer())
val decoded = MessageBundle.fromByteBuffer(reader).connectionMsgs?.single()?.message
assertEquals(SOLARXR_PROTOCOL_VERSION, (decoded as? ClientHello)?.protocolVersion)
}
val replyFbb = FlatBufferBuilder(64)
ServerHello(HelloStatus.ACCEPTED, SOLARXR_PROTOCOL_VERSION).finish(JvmFlatBufferWriter(replyFbb))
val replyReader = checkedSolarXRFrame(replyFbb.dataBuffer(), ServerHello.FILE_IDENTIFIER)
assertTrue(ServerHello.hasIdentifier(replyReader))
assertEquals(HelloStatus.ACCEPTED, ServerHello.fromByteBuffer(replyReader).status)
@Test
fun `ClientHello with a matching version is accepted and moves to configuring`() = runTest {
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
val bundleFbb = FlatBufferBuilder(64)
MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(ConfigurationAcknowledged())))
.finish(JvmFlatBufferWriter(bundleFbb))
val bundleReader = checkedSolarXRFrame(bundleFbb.dataBuffer(), MessageBundle.FILE_IDENTIFIER)
assertTrue(MessageBundle.hasIdentifier(bundleReader))
assertTrue(MessageBundle.fromByteBuffer(bundleReader).connectionMsgs?.single()?.message is ConfigurationAcknowledged)
onSolarXRMessage(clientHello(), bridge)
runCurrent()
val hello = sent.single()
assertNull(hello.closeReason)
assertEquals(ServerHello(HelloStatus.ACCEPTED, SOLARXR_PROTOCOL_VERSION), hello.connectionMessage())
assertEquals(ConnectionPhase.CONFIGURING, bridge.context.state.value.phase)
}
@Test
fun `ClientHello with a mismatched version is rejected and closes`() = runTest {
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
onSolarXRMessage(clientHello(version = SOLARXR_PROTOCOL_VERSION + 1u), bridge)
runCurrent()
val hello = sent.single()
assertNotNull(hello.closeReason)
assertEquals(ServerHello(HelloStatus.REJECTED_UNSUPPORTED_VERSION, SOLARXR_PROTOCOL_VERSION), hello.connectionMessage())
assertEquals(ConnectionPhase.HELLO, bridge.context.state.value.phase)
}
@Test
fun `anything but ClientHello during HELLO is rejected and closes`() = runTest {
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(BoneRegistryRequest()))), bridge)
runCurrent()
val frame = sent.single()
assertNotNull(frame.closeReason)
assertIs<InitializationRequiredError>((frame.connectionMessage() as ConnectionError).data)
}
@Test
fun `BoneRegistryRequest during configuration replies with the registry`() = runTest {
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
onSolarXRMessage(clientHello(), bridge)
runCurrent()
sent.clear()
onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(BoneRegistryRequest()))), bridge)
runCurrent()
assertEquals(bridge.registry.value, sent.single().connectionMessage())
}
@Test
fun `an undecodable connection message during configuration gets UnsupportedRequestError`() = runTest {
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
onSolarXRMessage(clientHello(), bridge)
runCurrent()
sent.clear()
onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(message = null))), bridge)
runCurrent()
assertIs<UnsupportedRequestError>((sent.single().connectionMessage() as ConnectionError).data)
}
@Test
fun `application bundles are rejected before configuration completes`() = runTest {
val sent = mutableListOf<MessageBundle>()
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
var dispatched = false
bridge.dataFeedDispatcher.on<StartDataFeed> { dispatched = true }.launchIn(backgroundScope)
@@ -120,18 +175,20 @@ class ConnectionInitTest {
runCurrent()
assertFalse(dispatched)
assertEquals(ConnectionErrorCode.INITIALIZATION_REQUIRED, sent.single().singleConnectionError().code)
assertIs<InitializationRequiredError>((sent.single().connectionMessage() as ConnectionError).data)
}
@Test
fun `application bundles dispatch once configuration completes`() = runTest {
val sent = mutableListOf<MessageBundle>()
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
var dispatched = false
bridge.dataFeedDispatcher.on<StartDataFeed> { dispatched = true }.launchIn(backgroundScope)
runCurrent()
onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(ConfigurationAcknowledged()))), bridge)
onSolarXRMessage(clientHello(), bridge)
runCurrent()
onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(ConfigurationDone()))), bridge)
runCurrent()
assertTrue(bridge.isReady)
@@ -145,13 +202,39 @@ class ConnectionInitTest {
}
@Test
fun `a client-sent bone registry is silently ignored`() = runTest {
val sent = mutableListOf<MessageBundle>()
fun `a single bundle carrying ClientHello, BoneRegistryRequest and ConfigurationDone completes the handshake`() = runTest {
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
val clientRegistry = BoneRegistry(
bones = listOf(BoneDefinition(1u, "x", null, 0u, null)),
onSolarXRMessage(
MessageBundle(
connectionMsgs = listOf(
ConnectionMessageHeader(ClientHello(protocolVersion = SOLARXR_PROTOCOL_VERSION)),
ConnectionMessageHeader(BoneRegistryRequest()),
ConnectionMessageHeader(ConfigurationDone()),
),
),
bridge,
)
runCurrent()
assertEquals(3, sent.size)
assertEquals(ServerHello(HelloStatus.ACCEPTED, SOLARXR_PROTOCOL_VERSION), sent[0].connectionMessage())
assertEquals(bridge.registry.value, sent[1].connectionMessage())
assertIs<ConfigurationDone>(sent[2].connectionMessage())
assertTrue(bridge.isReady)
}
@Test
fun `a client-sent bone registry is silently ignored during configuration`() = runTest {
val sent = mutableListOf<OutboundFrame>()
val bridge = buildTestBridge(sent)
onSolarXRMessage(clientHello(), bridge)
runCurrent()
sent.clear()
val clientRegistry = BoneRegistry(bones = listOf(BoneDefinition(1u, "x", null, 0u)))
onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(clientRegistry))), bridge)
runCurrent()
@@ -22,7 +22,7 @@ import solarxr_protocol.data_feed.DataFeedConfig
import solarxr_protocol.data_feed.DataFeedUpdate
import solarxr_protocol.data_feed.PollDataFeed
import solarxr_protocol.data_feed.StartDataFeed
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import solarxr_protocol.datatypes.BoneMask
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -58,7 +58,7 @@ private fun TestScope.testConn(onSend: suspend (MessageBundle) -> Unit): SolarXR
rpcDispatcher = EventDispatcher("test.rpc", backgroundScope),
)
bridge.startObserving()
bridge.outbound.on<MessageBundle> { onSend(it) }.launchIn(backgroundScope)
bridge.outbound.on<OutboundFrame> { onSend(it.bundle) }.launchIn(backgroundScope)
// launchIn registers the handler synchronously, but the dispatcher's own drain loop still has to
// start before anything emitted here can reach it.
@@ -11,7 +11,7 @@ import dev.slimevr.tracker.stayaligned.TrackerYawCorrection
import io.github.axisangles.ktmath.EulerAngles
import io.github.axisangles.ktmath.EulerOrder
import kotlinx.coroutines.test.runTest
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import solarxr_protocol.datatypes.TrackerStatus
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -13,7 +13,7 @@ import dev.slimevr.tracker.behaviours.TrackerYawResetSmoothingBehaviour
import io.github.axisangles.ktmath.Quaternion
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.test.runTest
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import solarxr_protocol.datatypes.TrackerStatus
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -25,7 +25,7 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import solarxr_protocol.datatypes.DeviceOrigin
import solarxr_protocol.datatypes.TrackerStatus
import solarxr_protocol.datatypes.hardware_info.ImuType
@@ -17,7 +17,7 @@ import dev.slimevr.skeleton.Velocity
import dev.slimevr.vectorAssertEquals
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
@@ -13,7 +13,7 @@ import dev.slimevr.skeleton.BoneState
import dev.slimevr.skeleton.Velocity
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
@@ -21,7 +21,7 @@ import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.test.runTest
import solarxr_protocol.datatypes.BodyPart
import dev.slimevr.bones.BodyPart
import solarxr_protocol.datatypes.DeviceOrigin
import solarxr_protocol.rpc.VMCOSCVrmState
import kotlin.test.Test