From 02ce2ef901c96cf4a66d0027e0cb9c8f39f67c1d Mon Sep 17 00:00:00 2001 From: loucass003 Date: Thu, 15 Oct 2026 07:12:24 +0200 Subject: [PATCH] Separate devices.json config tracker data now lives outside of profiles. Use hardwareId instead of mac address for all devices actions --- gui/app/public/i18n/en/translation.ftl | 1 + gui/app/src/components/home/Home.tsx | 2 +- .../components/tracker/PendingTrackers.tsx | 8 +- .../components/tracker/TrackerSettings.tsx | 37 ++++--- .../src/components/tracker/TrackersTable.tsx | 4 +- gui/app/src/hooks/unknown-devices.ts | 12 +-- .../dev/slimevr/config/devices-reducer.kt | 19 ++++ .../main/java/dev/slimevr/config/devices.kt | 99 +++++++++++++++++++ .../main/java/dev/slimevr/config/module.kt | 6 +- .../dev/slimevr/config/settings-reducer.kt | 14 --- .../main/java/dev/slimevr/config/settings.kt | 27 ----- .../main/java/dev/slimevr/device/module.kt | 3 + .../main/java/dev/slimevr/firmware/module.kt | 8 +- .../main/java/dev/slimevr/firmware/serial.kt | 16 +-- .../java/dev/slimevr/hid/behaviours/config.kt | 21 +--- .../src/main/java/dev/slimevr/hid/module.kt | 6 +- .../java/dev/slimevr/provisioning/module.kt | 8 +- .../java/dev/slimevr/provisioning/phases.kt | 12 +-- .../dev/slimevr/solarxr/datafeed/datafeed.kt | 5 +- .../dev/slimevr/solarxr/rpc/known-trackers.kt | 26 ++--- .../dev/slimevr/tracker/behaviours/config.kt | 43 +------- .../main/java/dev/slimevr/tracker/module.kt | 5 +- .../main/java/dev/slimevr/udp/behaviours.kt | 63 ++++++------ .../src/main/java/dev/slimevr/udp/forget.kt | 22 ++--- .../src/main/java/dev/slimevr/udp/reducer.kt | 12 +-- .../src/main/java/dev/slimevr/udp/server.kt | 6 +- .../java/dev/slimevr/udp/unknown-devices.kt | 2 +- .../src/main/java/dev/slimevr/util/address.kt | 11 +++ .../src/test/java/dev/slimevr/TestServer.kt | 16 +++ .../dev/slimevr/config/ProfileSwitchTest.kt | 42 ++------ .../dev/slimevr/firmware/DoSerialFlashTest.kt | 20 ++-- .../provisioning/ProvisioningManagerTest.kt | 4 +- .../solarxr/ProvisioningBehaviourTest.kt | 4 +- .../dev/slimevr/solarxr/SettingsResetTest.kt | 11 ++- .../tracker/TrackerProfileConfigTest.kt | 35 ------- .../java/dev/slimevr/udp/ForgetDeviceTest.kt | 13 +-- .../dev/slimevr/udp/UnknownDevicesTest.kt | 10 +- solarxr-protocol | 2 +- 38 files changed, 334 insertions(+), 321 deletions(-) create mode 100644 server/core/src/main/java/dev/slimevr/config/devices-reducer.kt create mode 100644 server/core/src/main/java/dev/slimevr/config/devices.kt delete mode 100644 server/core/src/test/java/dev/slimevr/tracker/TrackerProfileConfigTest.kt diff --git a/gui/app/public/i18n/en/translation.ftl b/gui/app/public/i18n/en/translation.ftl index 7d46c9157..b026d716b 100644 --- a/gui/app/public/i18n/en/translation.ftl +++ b/gui/app/public/i18n/en/translation.ftl @@ -322,6 +322,7 @@ tracker-infos-display_name = Display Name tracker-infos-custom_name = Custom Name tracker-infos-url = Tracker URL tracker-infos-hardware_identifier = Hardware ID +tracker-infos-mac_address = MAC address tracker-infos-data_type = Data type tracker-infos-imu = IMU Sensor tracker-infos-board_type = Main board diff --git a/gui/app/src/components/home/Home.tsx b/gui/app/src/components/home/Home.tsx index 35b3b26e8..37b1da75c 100644 --- a/gui/app/src/components/home/Home.tsx +++ b/gui/app/src/components/home/Home.tsx @@ -165,7 +165,7 @@ export function Home() {
{group.pending.map((device) => ( ))} diff --git a/gui/app/src/components/tracker/PendingTrackers.tsx b/gui/app/src/components/tracker/PendingTrackers.tsx index 63fa86ad4..ca0d4fc71 100644 --- a/gui/app/src/components/tracker/PendingTrackers.tsx +++ b/gui/app/src/components/tracker/PendingTrackers.tsx @@ -31,7 +31,7 @@ export function pendingBoardLabel( export function PendingTrackerActions({ device }: { device: UnknownDeviceT }) { const { add, ignore } = usePendingTrackerActions(); const adding = device.status === UnknownDeviceStatus.ADDING; - const macAddress = device.macAddress as string; + const hardwareId = device.hardwareId as string; return (
@@ -45,7 +45,7 @@ export function PendingTrackerActions({ device }: { device: UnknownDeviceT }) { labelId="home-new_trackers-ignore" tooltipDirection="top" className="rounded-full w-[35px] h-[35px] flex items-center justify-center text-background-30 hover:text-background-10 hover:bg-background-60" - onClick={() => ignore(macAddress)} + onClick={() => ignore(hardwareId)} > @@ -54,7 +54,7 @@ export function PendingTrackerActions({ device }: { device: UnknownDeviceT }) { rounded className="px-4" id="home-new_trackers-add" - onClick={() => add(macAddress)} + onClick={() => add(hardwareId)} />
{ - if ( - /(?:[a-zA-Z\d]{2}:){5}[a-zA-Z\d]{2}/.test( - (tracker?.device?.hardwareInfo?.hardwareIdentifier as string | null) ?? - '' - ) - ) { - return tracker?.device?.hardwareInfo?.hardwareIdentifier as string; - } - return null; - }, [tracker?.device?.hardwareInfo?.hardwareIdentifier]); + // MAC address, or IP for firmware that sends no MAC + const hardwareId = + (tracker?.device?.hardwareInfo?.hardwareIdentifier as string | null) ?? + null; + const macAddressNumber = tracker?.device?.hardwareInfo?.hardwareAddress?.addr; + const macAddress = + macAddressNumber != null + ? macAddressNumber + .toString(16) + .padStart(12, '0') + .toUpperCase() + .match(/.{2}/g) + ?.join(':') + : null; const { currentFirmwareRelease } = useAppContext(); @@ -356,12 +359,8 @@ export function TrackerSettingsPage() {
- - {l10n.getString('tracker-infos-hardware_identifier')} - - - {tracker?.device?.hardwareInfo?.hardwareIdentifier || '--'} - + + {macAddress || '--'}
@@ -581,7 +580,7 @@ export function TrackerSettingsPage() { label={l10n.getString('tracker-settings-name_section-label')} />
- {macAddress && tracker?.device?.origin === DeviceOrigin.UDP && ( + {hardwareId && tracker?.device?.origin === DeviceOrigin.UDP && (
{l10n.getString('tracker-settings-forget')} @@ -599,7 +598,7 @@ export function TrackerSettingsPage() { onConfirm={() => { sendRPCPacket( RpcMessage.ForgetDeviceRequest, - new ForgetDeviceRequestT(macAddress) + new ForgetDeviceRequestT(hardwareId) ); navigate('/'); }} diff --git a/gui/app/src/components/tracker/TrackersTable.tsx b/gui/app/src/components/tracker/TrackersTable.tsx index f7167a50e..9e1570c4c 100644 --- a/gui/app/src/components/tracker/TrackersTable.tsx +++ b/gui/app/src/components/tracker/TrackersTable.tsx @@ -475,7 +475,7 @@ function PendingRow({
- {device.macAddress as string} + {device.hardwareId as string} {device.firmwareVersion} @@ -689,7 +689,7 @@ export function TrackersTable({ /> {group.pending.map((device) => ( { + const add = (hardwareId: string) => { sendRPCPacket( RpcMessage.AddUnknownDeviceRequest, - new AddUnknownDeviceRequestT(macAddress) + new AddUnknownDeviceRequestT(hardwareId) ); }; - const ignore = (macAddress: string) => { + const ignore = (hardwareId: string) => { sendRPCPacket( RpcMessage.IgnoreUnknownDeviceRequest, - new IgnoreUnknownDeviceRequestT(macAddress) + new IgnoreUnknownDeviceRequestT(hardwareId) ); }; const addAll = (devices: UnknownDeviceT[]) => { devices .filter( - (device) => device.status !== UnknownDeviceStatus.ADDING && device.macAddress + (device) => device.status !== UnknownDeviceStatus.ADDING && device.hardwareId ) - .forEach((device) => add(device.macAddress as string)); + .forEach((device) => add(device.hardwareId as string)); }; return { add, ignore, addAll }; diff --git a/server/core/src/main/java/dev/slimevr/config/devices-reducer.kt b/server/core/src/main/java/dev/slimevr/config/devices-reducer.kt new file mode 100644 index 000000000..de9a19005 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/config/devices-reducer.kt @@ -0,0 +1,19 @@ +package dev.slimevr.config + +fun reduce(state: DevicesConfigState, action: DevicesConfigActions): DevicesConfigState = when (action) { + is DevicesConfigActions.Update -> action.transform(state) + + is DevicesConfigActions.UpdateTracker -> { + val existing = state.trackers[action.hardwareId] ?: TrackerConfig() + state.copy(trackers = state.trackers + (action.hardwareId to action.transform(existing))) + } + + is DevicesConfigActions.UpdateDongle -> { + val existing = state.dongles[action.serialNumber] ?: DongleConfig() + state.copy(dongles = state.dongles + (action.serialNumber to action.transform(existing))) + } + + is DevicesConfigActions.AddAllowedUdpDevice -> state.copy(allowedUdpDevices = state.allowedUdpDevices + action.mac) + + is DevicesConfigActions.RemoveAllowedUdpDevice -> state.copy(allowedUdpDevices = state.allowedUdpDevices - action.mac) +} diff --git a/server/core/src/main/java/dev/slimevr/config/devices.kt b/server/core/src/main/java/dev/slimevr/config/devices.kt new file mode 100644 index 000000000..b447e28f5 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/config/devices.kt @@ -0,0 +1,99 @@ +package dev.slimevr.config + +import dev.slimevr.context.Behaviour +import dev.slimevr.context.Context +import io.github.axisangles.ktmath.Quaternion +import io.github.axisangles.ktmath.Vector3 +import kotlinx.coroutines.CoroutineScope +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.decodeFromJsonElement +import kotlinx.serialization.json.intOrNull +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive +import solarxr_protocol.datatypes.BodyPart + +private const val DEVICES_CONFIG_VERSION = 1 +const val DEVICES_CONFIG_PATH = "devices.json" + +@Serializable +data class TrackerConfig( + @Serializable(with = BodyPartSerializer::class) + val bodyPart: BodyPart? = null, + val customName: String? = null, + @Serializable(with = QuaternionSerializer::class) + val mountingOrientation: Quaternion = Quaternion.IDENTITY, + @Serializable(with = QuaternionSerializer::class) + val mountingResetOrientation: Quaternion? = null, + val magEnabled: Boolean? = null, + val boneOffsets: Map = emptyMap(), +) + +@Serializable +data class DongleConfig( + val customName: String? = null, +) + +@Serializable +data class DevicesConfigState( + val version: Int = DEVICES_CONFIG_VERSION, + val allowedUdpDevices: Set = emptySet(), + val trackers: Map = emptyMap(), + val dongles: Map = emptyMap(), +) + +sealed interface DevicesConfigActions { + data class Update(val transform: DevicesConfigState.() -> DevicesConfigState) : DevicesConfigActions + data class UpdateTracker(val hardwareId: String, val transform: TrackerConfig.() -> TrackerConfig) : DevicesConfigActions + data class UpdateDongle(val serialNumber: String, val transform: DongleConfig.() -> DongleConfig) : DevicesConfigActions + data class AddAllowedUdpDevice(val mac: String) : DevicesConfigActions + data class RemoveAllowedUdpDevice(val mac: String) : DevicesConfigActions +} + +typealias DevicesConfigContext = Context +typealias DevicesConfigBehaviour = Behaviour + +class DevicesConfig( + val context: DevicesConfigContext, + private val autosave: Autosave, +) { + fun startObserving() = context.observeAll(this) + + suspend fun flush() = autosave.flush() + + companion object { + suspend fun create(scope: CoroutineScope, storage: ConfigStorage): DevicesConfig { + val initialState = loadFileWithBackup(storage, DEVICES_CONFIG_PATH, DevicesConfigState()) { + parseAndMigrateDevicesConfig(it) + } + val context = Context.create( + initialState = initialState, + scope = scope, + reducer = ::reduce, + name = "DevicesConfig", + ) + val autosave = launchAutosave( + scope = scope, + state = context.state, + storage = storage, + toPath = { DEVICES_CONFIG_PATH }, + serialize = { jsonConfig.encodeToString(it) }, + ) + val devices = DevicesConfig(context, autosave) + devices.startObserving() + return devices + } + } +} + +private fun migrateDevicesConfig(json: JsonObject): JsonObject { + val version = json["version"]?.jsonPrimitive?.intOrNull ?: 0 + return when { + else -> json + } +} + +private fun parseAndMigrateDevicesConfig(raw: String): DevicesConfigState { + val json = jsonConfig.parseToJsonElement(raw).jsonObject + return jsonConfig.decodeFromJsonElement(migrateDevicesConfig(json)) +} diff --git a/server/core/src/main/java/dev/slimevr/config/module.kt b/server/core/src/main/java/dev/slimevr/config/module.kt index b5373610d..d2c813930 100644 --- a/server/core/src/main/java/dev/slimevr/config/module.kt +++ b/server/core/src/main/java/dev/slimevr/config/module.kt @@ -117,6 +117,7 @@ class AppConfig( val globalConfig: GlobalConfig, val userConfig: UserConfig, val settings: Settings, + val devices: DevicesConfig, private val storage: ConfigStorage, ) { private val profilesLock = Mutex() @@ -250,7 +251,7 @@ class AppConfig( /** * Switches both kinds back to the default profile and restores its default values. Other profiles are kept. - * A deleted default profile is created again. + * A deleted default profile is created again. Known devices are cleared. */ suspend fun reset() = profilesLock.withLock { val defaults = GlobalConfigState() @@ -266,6 +267,7 @@ class AppConfig( } settings.context.dispatch(SettingsActions.Update { SettingsConfigState() }) userConfig.context.dispatch(UserConfigActions.Update { UserConfigData() }) + devices.context.dispatch(DevicesConfigActions.Update { DevicesConfigState() }) val errorReporting = globalConfig.context.state.value.errorReporting globalConfig.context.dispatch( GlobalConfigActions.SetErrorReporting(changeConsent(errorReporting, ErrorReportingConsent.UNDECIDED)), @@ -282,11 +284,13 @@ class AppConfig( val userConfig = UserConfig.create(scope, storage, globalConfig.context.state.value.selectedUserProfile) val settings = Settings.create(scope, storage, globalConfig.context.state.value.selectedSettingsProfile) + val devices = DevicesConfig.create(scope, storage) return AppConfig( globalConfig = globalConfig, userConfig = userConfig, settings = settings, + devices = devices, storage = storage, ).also { it.refreshProfiles() } } diff --git a/server/core/src/main/java/dev/slimevr/config/settings-reducer.kt b/server/core/src/main/java/dev/slimevr/config/settings-reducer.kt index ce2eb3729..9cf1694cb 100644 --- a/server/core/src/main/java/dev/slimevr/config/settings-reducer.kt +++ b/server/core/src/main/java/dev/slimevr/config/settings-reducer.kt @@ -6,18 +6,4 @@ fun reduce(state: SettingsState, action: SettingsActions): SettingsState = when is SettingsActions.LoadProfile -> action.newState is SettingsActions.Rename -> state.copy(name = action.name) - - is SettingsActions.UpdateTracker -> { - val existing = state.data.trackers[action.hardwareId] ?: TrackerConfig() - state.copy(data = state.data.copy(trackers = state.data.trackers + (action.hardwareId to action.transform(existing)))) - } - - is SettingsActions.UpdateDongle -> { - val existing = state.data.dongles[action.serialNumber] ?: DongleConfig() - state.copy(data = state.data.copy(dongles = state.data.dongles + (action.serialNumber to action.transform(existing)))) - } - - is SettingsActions.AddAllowedUdpDevice -> state.copy(data = state.data.copy(allowedUdpDevices = state.data.allowedUdpDevices + action.mac)) - - is SettingsActions.RemoveAllowedUdpDevice -> state.copy(data = state.data.copy(allowedUdpDevices = state.data.allowedUdpDevices - action.mac)) } diff --git a/server/core/src/main/java/dev/slimevr/config/settings.kt b/server/core/src/main/java/dev/slimevr/config/settings.kt index 8a6a6c071..f010202e3 100644 --- a/server/core/src/main/java/dev/slimevr/config/settings.kt +++ b/server/core/src/main/java/dev/slimevr/config/settings.kt @@ -3,8 +3,6 @@ package dev.slimevr.config import dev.slimevr.context.Behaviour import dev.slimevr.context.Context import dev.slimevr.skeleton.inputprocessors.DEFAULT_SPINE_UPPER_LOWER -import io.github.axisangles.ktmath.Quaternion -import io.github.axisangles.ktmath.Vector3 import kotlinx.coroutines.CoroutineScope import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject @@ -21,24 +19,6 @@ import solarxr_protocol.rpc.RoutingOutput private const val SETTINGS_CONFIG_VERSION = 2 -@Serializable -data class TrackerConfig( - @Serializable(with = BodyPartSerializer::class) - val bodyPart: BodyPart? = null, - val customName: String? = null, - @Serializable(with = QuaternionSerializer::class) - val mountingOrientation: Quaternion = Quaternion.IDENTITY, - @Serializable(with = QuaternionSerializer::class) - val mountingResetOrientation: Quaternion? = null, - val magEnabled: Boolean? = null, - val boneOffsets: Map = emptyMap(), -) - -@Serializable -data class DongleConfig( - val customName: String? = null, -) - @Serializable data class TrackersConfig( val trackerPort: Int = 6969, // Not in SolarXR @@ -214,9 +194,6 @@ data class SettingsConfigState( val version: Int = SETTINGS_CONFIG_VERSION, val mutedVRCWarnings: List = listOf(), val mutedChecklistSteps: Set = emptySet(), - val allowedUdpDevices: Set = emptySet(), - val trackers: Map = emptyMap(), - val dongles: Map = emptyMap(), val trackersConfig: TrackersConfig = TrackersConfig(), val hidConfig: HidConfig = HidConfig(), val boneRoutingConfig: BoneRoutingConfig = BoneRoutingConfig(), @@ -251,10 +228,6 @@ sealed interface SettingsActions { data class Update(val transform: SettingsConfigState.() -> SettingsConfigState) : SettingsActions data class LoadProfile(val newState: SettingsState) : SettingsActions data class Rename(val name: String) : SettingsActions - data class UpdateTracker(val hardwareId: String, val transform: TrackerConfig.() -> TrackerConfig) : SettingsActions - data class UpdateDongle(val serialNumber: String, val transform: DongleConfig.() -> DongleConfig) : SettingsActions - data class AddAllowedUdpDevice(val mac: String) : SettingsActions - data class RemoveAllowedUdpDevice(val mac: String) : SettingsActions } typealias SettingsContext = Context diff --git a/server/core/src/main/java/dev/slimevr/device/module.kt b/server/core/src/main/java/dev/slimevr/device/module.kt index aa97cb5ce..d62c18ef2 100644 --- a/server/core/src/main/java/dev/slimevr/device/module.kt +++ b/server/core/src/main/java/dev/slimevr/device/module.kt @@ -17,6 +17,7 @@ data class DeviceState( val manufacturer: String, val address: String, val macAddress: String?, + val hardwareId: String? = null, val batteryLevel: Float?, val batteryVoltage: Float?, val batteryRemainingRuntime: Long?, @@ -66,6 +67,7 @@ class Device( manufacturer: String = "SlimeVR", address: String, macAddress: String? = null, + hardwareId: String? = null, origin: DeviceOrigin, driverName: String? = null, protocolVersion: Int, @@ -81,6 +83,7 @@ class Device( driverName = driverName, address = address, macAddress = macAddress, + hardwareId = hardwareId, protocolVersion = protocolVersion, ping = null, signalStrength = null, diff --git a/server/core/src/main/java/dev/slimevr/firmware/module.kt b/server/core/src/main/java/dev/slimevr/firmware/module.kt index b90173729..334c3faa9 100644 --- a/server/core/src/main/java/dev/slimevr/firmware/module.kt +++ b/server/core/src/main/java/dev/slimevr/firmware/module.kt @@ -2,7 +2,7 @@ package dev.slimevr.firmware import dev.slimevr.Phase1ContextProvider import dev.slimevr.VRServer -import dev.slimevr.config.Settings +import dev.slimevr.config.DevicesConfig import dev.slimevr.context.Behaviour import dev.slimevr.context.Context import dev.slimevr.context.debug.DiffStyle @@ -46,7 +46,7 @@ typealias FirmwareManagerBehaviour = Behaviour class FirmwareManager( val context: FirmwareManagerContext, private val serialServer: SerialServer, - private val settings: Settings, + private val devices: DevicesConfig, private val flasher: FirmwareFlasher, private val scope: CoroutineScope, ) { @@ -72,7 +72,7 @@ class FirmwareManager( ssid = ssid, password = password, serialServer = serialServer, - settings = settings, + devices = devices, server = server, flasher = flasher, onStatus = { status, progress -> @@ -148,7 +148,7 @@ class FirmwareManager( return FirmwareManager( context = context, serialServer = ctx.serialServer, - settings = ctx.config.settings, + devices = ctx.config.devices, flasher = flasher, scope = scope, ) diff --git a/server/core/src/main/java/dev/slimevr/firmware/serial.kt b/server/core/src/main/java/dev/slimevr/firmware/serial.kt index d29ac72b8..d91a01ee1 100644 --- a/server/core/src/main/java/dev/slimevr/firmware/serial.kt +++ b/server/core/src/main/java/dev/slimevr/firmware/serial.kt @@ -1,8 +1,8 @@ package dev.slimevr.firmware import dev.slimevr.VRServer -import dev.slimevr.config.Settings -import dev.slimevr.config.SettingsActions +import dev.slimevr.config.DevicesConfig +import dev.slimevr.config.DevicesConfigActions import dev.slimevr.serial.FlashingHandler import dev.slimevr.serial.MAC_REGEX import dev.slimevr.serial.SerialConsole @@ -36,7 +36,7 @@ suspend fun doSerialFlash( ssid: String?, password: String?, serialServer: SerialServer, - settings: Settings, + devices: DevicesConfig, server: VRServer, flasher: FirmwareFlasher, onStatus: suspend (FirmwareUpdateStatus, Int) -> Unit, @@ -91,7 +91,7 @@ suspend fun doSerialFlash( ssid = ssid, password = password, serialServer = serialServer, - settings = settings, + devices = devices, server = server, onStatus = onStatus, ) @@ -111,7 +111,7 @@ internal suspend fun doSerialFlashPostFlash( ssid: String?, password: String?, serialServer: SerialServer, - settings: Settings, + devices: DevicesConfig, server: VRServer, onStatus: suspend (FirmwareUpdateStatus, Int) -> Unit, ) { @@ -132,7 +132,7 @@ internal suspend fun doSerialFlashPostFlash( } try { - provisionFlashedPort(lease.console, needManualReboot, ssid, password, settings, server, onStatus) + provisionFlashedPort(lease.console, needManualReboot, ssid, password, devices, server, onStatus) } finally { lease.release() } @@ -144,7 +144,7 @@ private suspend fun provisionFlashedPort( needManualReboot: Boolean, ssid: String?, password: String?, - settings: Settings, + devices: DevicesConfig, server: VRServer, onStatus: suspend (FirmwareUpdateStatus, Int) -> Unit, ) { @@ -174,7 +174,7 @@ private suspend fun provisionFlashedPort( return } - settings.context.dispatch(SettingsActions.AddAllowedUdpDevice(macAddress)) + devices.context.dispatch(DevicesConfigActions.AddAllowedUdpDevice(macAddress)) // Remove old UDP connection for this MAC before rebooting to prevent false inactivity timeouts val existingDevice = server.context.state.value.devices.values.find { d -> diff --git a/server/core/src/main/java/dev/slimevr/hid/behaviours/config.kt b/server/core/src/main/java/dev/slimevr/hid/behaviours/config.kt index ed3518e08..44b99d738 100644 --- a/server/core/src/main/java/dev/slimevr/hid/behaviours/config.kt +++ b/server/core/src/main/java/dev/slimevr/hid/behaviours/config.kt @@ -1,19 +1,16 @@ package dev.slimevr.hid.behaviours -import dev.slimevr.config.Settings -import dev.slimevr.config.SettingsActions +import dev.slimevr.config.DevicesConfig +import dev.slimevr.config.DevicesConfigActions import dev.slimevr.hid.HIDReceiver -import dev.slimevr.hid.HIDReceiverActions import dev.slimevr.hid.HIDReceiverBehaviour -import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChangedBy import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach class HIDReceiverConfigBehaviour( - private val settings: Settings, + private val devices: DevicesConfig, private val serialNumber: String, ) : HIDReceiverBehaviour { override fun observe(receiver: HIDReceiver) { @@ -21,17 +18,7 @@ class HIDReceiverConfigBehaviour( .distinctUntilChangedBy { it.customName } .drop(1) .onEach { state -> - settings.context.dispatch(SettingsActions.UpdateDongle(serialNumber) { copy(customName = state.customName) }) - } - .launchIn(receiver.context.scope) - - settings.context.state - .map { it.name } - .distinctUntilChanged() - .drop(1) - .onEach { - val customName = settings.context.state.value.data.dongles[serialNumber]?.customName - receiver.context.dispatch(HIDReceiverActions.SetCustomName(customName)) + devices.context.dispatch(DevicesConfigActions.UpdateDongle(serialNumber) { copy(customName = state.customName) }) } .launchIn(receiver.context.scope) } diff --git a/server/core/src/main/java/dev/slimevr/hid/module.kt b/server/core/src/main/java/dev/slimevr/hid/module.kt index dd0ffff57..0b7118ec6 100644 --- a/server/core/src/main/java/dev/slimevr/hid/module.kt +++ b/server/core/src/main/java/dev/slimevr/hid/module.kt @@ -161,11 +161,11 @@ class HIDReceiver( appContext: AppContextProvider, scope: CoroutineScope, ): HIDReceiver { - val settings = appContext.config.settings - val savedConfig = settings.context.state.value.data.dongles[serialNumber] + val devices = appContext.config.devices + val savedConfig = devices.context.state.value.dongles[serialNumber] val behaviours = listOf( - HIDReceiverConfigBehaviour(settings, serialNumber), + HIDReceiverConfigBehaviour(devices, serialNumber), ) val context = Context.create( diff --git a/server/core/src/main/java/dev/slimevr/provisioning/module.kt b/server/core/src/main/java/dev/slimevr/provisioning/module.kt index 5874a58eb..eb1a67411 100644 --- a/server/core/src/main/java/dev/slimevr/provisioning/module.kt +++ b/server/core/src/main/java/dev/slimevr/provisioning/module.kt @@ -2,7 +2,7 @@ package dev.slimevr.provisioning import dev.slimevr.Phase1ContextProvider import dev.slimevr.VRServer -import dev.slimevr.config.Settings +import dev.slimevr.config.DevicesConfig import dev.slimevr.context.Behaviour import dev.slimevr.context.Context import dev.slimevr.serial.SerialServer @@ -60,7 +60,7 @@ typealias ProvisioningManagerBehaviour = Behaviour data class ProvisioningManager( val context: ProvisioningManagerContext, private val serialServer: SerialServer, - private val settings: Settings, + private val devices: DevicesConfig, private val scope: CoroutineScope, ) { fun startObserving() = context.observeAll(this) @@ -100,7 +100,7 @@ data class ProvisioningManager( startedPorts += portLocation trackerJobs[portLocation] = launch { try { - runProvisioningForPort(context, server, settings, serialServer, portLocation, ssid, password) + runProvisioningForPort(context, server, devices, serialServer, portLocation, ssid, password) } catch (e: CancellationException) { throw e } catch (e: Exception) { @@ -188,7 +188,7 @@ data class ProvisioningManager( return ProvisioningManager( context = context, serialServer = ctx.serialServer, - settings = ctx.config.settings, + devices = ctx.config.devices, scope = scope, ) } diff --git a/server/core/src/main/java/dev/slimevr/provisioning/phases.kt b/server/core/src/main/java/dev/slimevr/provisioning/phases.kt index 3f3ec5050..fee8190e4 100644 --- a/server/core/src/main/java/dev/slimevr/provisioning/phases.kt +++ b/server/core/src/main/java/dev/slimevr/provisioning/phases.kt @@ -1,8 +1,8 @@ package dev.slimevr.provisioning import dev.slimevr.VRServer -import dev.slimevr.config.Settings -import dev.slimevr.config.SettingsActions +import dev.slimevr.config.DevicesConfig +import dev.slimevr.config.DevicesConfigActions import dev.slimevr.firmware.waitForConnected import dev.slimevr.serial.MAC_REGEX import dev.slimevr.serial.SerialConsole @@ -187,7 +187,7 @@ internal suspend fun selectScanPort( internal suspend fun runProvisioningForPort( context: ProvisioningManagerContext, server: VRServer, - settings: Settings, + devices: DevicesConfig, serialServer: SerialServer, portLocation: String, ssid: String, @@ -203,7 +203,7 @@ internal suspend fun runProvisioningForPort( } try { - provisionPort(context, server, settings, lease.console, ssid, password) + provisionPort(context, server, devices, lease.console, ssid, password) } finally { lease.release() } @@ -346,7 +346,7 @@ internal suspend fun waitForWifiConnect( internal suspend fun provisionPort( context: ProvisioningManagerContext, server: VRServer, - settings: Settings, + devices: DevicesConfig, serialConn: SerialConsole, ssid: String, password: String?, @@ -356,7 +356,7 @@ internal suspend fun provisionPort( if (!obtainMacAddress(context, serialConn)) return val macAddress = context.state.value.trackers[portLocation]?.macAddress ?: return - settings.context.dispatch(SettingsActions.AddAllowedUdpDevice(macAddress)) + devices.context.dispatch(DevicesConfigActions.AddAllowedUdpDevice(macAddress)) // Remove old UDP connection for this MAC before rebooting to prevent false inactivity timeouts val existingDevice = server.context.state.value.devices.values.find { d -> diff --git a/server/core/src/main/java/dev/slimevr/solarxr/datafeed/datafeed.kt b/server/core/src/main/java/dev/slimevr/solarxr/datafeed/datafeed.kt index 1efd07775..2480979f9 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/datafeed/datafeed.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/datafeed/datafeed.kt @@ -17,6 +17,7 @@ import dev.slimevr.solarxr.toVec3f import dev.slimevr.tracker.Motion import dev.slimevr.tracker.TrackerState import dev.slimevr.util.ipv4AddressFromString +import dev.slimevr.util.macAddressToULong import io.github.axisangles.ktmath.Vector3 import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.delay @@ -39,6 +40,7 @@ import solarxr_protocol.data_feed.tracker_data.TrackerInfo import solarxr_protocol.datatypes.BodyPart import solarxr_protocol.datatypes.MagnetometerStatus import solarxr_protocol.datatypes.TrackerStatus +import solarxr_protocol.datatypes.hardware_info.HardwareAddress import solarxr_protocol.datatypes.hardware_info.HardwareInfo import solarxr_protocol.datatypes.hardware_info.HardwareStatus import solarxr_protocol.datatypes.hardware_info.ImuType @@ -117,7 +119,8 @@ private fun createDevice( firmwareVersion = deviceState.firmwareVersion, firmwareDate = deviceState.firmwareDate, ipAddress = ipv4AddressFromString(deviceState.address), - hardwareIdentifier = deviceState.macAddress, + hardwareIdentifier = deviceState.hardwareId ?: deviceState.macAddress, + hardwareAddress = deviceState.macAddress?.let(::macAddressToULong)?.let(::HardwareAddress), networkProtocolVersion = deviceState.protocolVersion.toUShort(), // TODO missing fields ), diff --git a/server/core/src/main/java/dev/slimevr/solarxr/rpc/known-trackers.kt b/server/core/src/main/java/dev/slimevr/solarxr/rpc/known-trackers.kt index 3d27e474f..f08c8bd79 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/rpc/known-trackers.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/rpc/known-trackers.kt @@ -1,7 +1,7 @@ package dev.slimevr.solarxr.rpc import dev.slimevr.AppContextProvider -import dev.slimevr.config.SettingsActions +import dev.slimevr.config.DevicesConfigActions import dev.slimevr.solarxr.SolarXRBridge import dev.slimevr.solarxr.SolarXRBridgeBehaviour import dev.slimevr.udp.UdpServerActions @@ -20,13 +20,13 @@ import solarxr_protocol.rpc.UnknownDevice import solarxr_protocol.rpc.UnknownDeviceStatus import solarxr_protocol.rpc.UnknownDevicesResponse -internal fun createUnknownDevice(device: UnknownDeviceState, allowedMacs: Set) = UnknownDevice( - macAddress = device.macAddress, +internal fun createUnknownDevice(device: UnknownDeviceState, allowed: Set) = UnknownDevice( + hardwareId = device.hardwareId, ipAddress = ipv4AddressFromString(device.address), boardType = device.boardType, mcuType = device.mcuType, firmwareVersion = device.firmwareVersion, - status = if (device.macAddress in allowedMacs) UnknownDeviceStatus.ADDING else UnknownDeviceStatus.WAITING, + status = if (device.hardwareId in allowed) UnknownDeviceStatus.ADDING else UnknownDeviceStatus.WAITING, ) class KnownTrackersBehaviour( @@ -36,25 +36,25 @@ class KnownTrackersBehaviour( val udpServer = appContext.udpServer receiver.rpcDispatcher.on { req -> - val mac = req.macAddress ?: return@on - appContext.config.settings.context.dispatch(SettingsActions.AddAllowedUdpDevice(mac)) + val hardwareId = req.hardwareId ?: return@on + appContext.config.devices.context.dispatch(DevicesConfigActions.AddAllowedUdpDevice(hardwareId)) }.launchIn(receiver.context.scope) receiver.rpcDispatcher.on { req -> - val mac = req.macAddress ?: return@on - udpServer.context.dispatch(UdpServerActions.IgnoreUnknownDevice(mac)) + val hardwareId = req.hardwareId ?: return@on + udpServer.context.dispatch(UdpServerActions.IgnoreUnknownDevice(hardwareId)) }.launchIn(receiver.context.scope) receiver.rpcDispatcher.on { req -> - val mac = req.macAddress ?: return@on - forgetUdpDevice(appContext, mac) + val hardwareId = req.hardwareId ?: return@on + forgetUdpDevice(appContext, hardwareId) }.launchIn(receiver.context.scope) combine( udpServer.context.state.map { it.unknownDevices }, - appContext.config.settings.context.state.map { it.data.allowedUdpDevices }, - ) { unknownDevices, allowedMacs -> - unknownDevices.values.map { createUnknownDevice(it, allowedMacs) } + appContext.config.devices.context.state.map { it.allowedUdpDevices }, + ) { unknownDevices, allowed -> + unknownDevices.values.map { createUnknownDevice(it, allowed) } } .distinctUntilChanged() .onEach { devices -> receiver.sendRpc(UnknownDevicesResponse(devices = devices)) } diff --git a/server/core/src/main/java/dev/slimevr/tracker/behaviours/config.kt b/server/core/src/main/java/dev/slimevr/tracker/behaviours/config.kt index 53cf283e2..ce6995598 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/behaviours/config.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/behaviours/config.kt @@ -1,26 +1,21 @@ package dev.slimevr.tracker.behaviours -import dev.slimevr.config.Settings -import dev.slimevr.config.SettingsActions +import dev.slimevr.config.DevicesConfig +import dev.slimevr.config.DevicesConfigActions import dev.slimevr.config.TrackerConfig import dev.slimevr.tracker.Tracker -import dev.slimevr.tracker.TrackerActions import dev.slimevr.tracker.TrackerBehaviour import dev.slimevr.tracker.TrackerState -import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChangedBy import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import solarxr_protocol.datatypes.MagnetometerStatus import solarxr_protocol.datatypes.MountingMethod class TrackerConfigBehaviour( - private val settings: Settings, + private val devices: DevicesConfig, private val hardwareId: String, - /** State the tracker was created with, before any saved config was applied */ - private val baseState: TrackerState, ) : TrackerBehaviour { override fun observe(receiver: Tracker) { receiver.context.state @@ -31,42 +26,12 @@ class TrackerConfigBehaviour( } .drop(1) .onEach { state -> - settings.context.dispatch(SettingsActions.UpdateTracker(hardwareId) { applyStateToConfig(this, state, receiver.settings.context.state.value.data.resetsConfig.saveMountingReset) }) - } - .launchIn(receiver.context.scope) - - settings.context.state - .map { it.name } - .distinctUntilChanged() - .drop(1) - .onEach { - val data = settings.context.state.value.data - val config = data.trackers[hardwareId] - receiver.context.dispatch( - TrackerActions.Update { applyProfileConfig(this, baseState, config, data.resetsConfig.saveMountingReset) }, - ) + devices.context.dispatch(DevicesConfigActions.UpdateTracker(hardwareId) { applyStateToConfig(this, state, receiver.settings.context.state.value.data.resetsConfig.saveMountingReset) }) } .launchIn(receiver.context.scope) } companion object { - /** - * Applies the config of a newly selected profile to a live tracker. A tracker the profile - * doesn't know goes back to the state it was created with. - */ - fun applyProfileConfig(state: TrackerState, baseState: TrackerState, config: TrackerConfig?, saveMountingReset: Boolean): TrackerState { - val restored = if (config != null) restoreFromConfig(baseState, config, saveMountingReset) else baseState - return state.copy( - bodyPart = restored.bodyPart, - customName = restored.customName, - lastMountingMethod = restored.lastMountingMethod, - mountingOrientation = restored.mountingOrientation, - sessionCalibration = state.sessionCalibration.copy(headingAlignment = restored.sessionCalibration.headingAlignment), - magStatus = if (config?.magEnabled != null) restored.magStatus else state.magStatus, - boneOffsets = restored.boneOffsets, - ) - } - fun restoreFromConfig(state: TrackerState, config: TrackerConfig, saveMountingReset: Boolean): TrackerState = state.copy( bodyPart = config.bodyPart, customName = config.customName ?: state.customName, diff --git a/server/core/src/main/java/dev/slimevr/tracker/module.kt b/server/core/src/main/java/dev/slimevr/tracker/module.kt index 82a0892b2..3b7ba8b2c 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/module.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/module.kt @@ -152,7 +152,8 @@ class Tracker( appContext: AppContextProvider, ): Tracker { val settings = appContext.config.settings - val trackerConfigs = settings.context.state.value.data.trackers + val devices = appContext.config.devices + val trackerConfigs = devices.context.state.value.trackers val savedConfig = trackerConfigs[hardwareId] val baseState = DEFAULT_STATE.copy( id = id, @@ -181,7 +182,7 @@ class Tracker( TrackerYawResetSmoothingBehaviour(), TrackerDefaultMountingOrientationBehaviour(), TrackerStaleCalibrationBehaviour(), - TrackerConfigBehaviour(settings, hardwareId, baseState), + TrackerConfigBehaviour(devices, hardwareId), TrackerMotionDetectionBehaviour(), TrackerToSkeletonBehaviour(), TrackerStayAlignedBehaviour(settings), diff --git a/server/core/src/main/java/dev/slimevr/udp/behaviours.kt b/server/core/src/main/java/dev/slimevr/udp/behaviours.kt index 55dd4d98d..ebca9ba48 100644 --- a/server/core/src/main/java/dev/slimevr/udp/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/udp/behaviours.kt @@ -7,6 +7,7 @@ import dev.slimevr.device.DeviceActions import dev.slimevr.logging.AppLogger import dev.slimevr.tracker.Tracker import dev.slimevr.tracker.TrackerActions +import dev.slimevr.util.udpHardwareId import io.github.axisangles.ktmath.Quaternion import kotlinx.coroutines.delay import kotlinx.coroutines.flow.distinctUntilChangedBy @@ -30,11 +31,8 @@ internal const val CONNECTION_TIMEOUT_MS = 5000L internal suspend fun assignTracker(receiver: UDPConnection, device: Device, info: SensorInfo): Pair { val deviceState = device.context.state.value - val mac = deviceState.macAddress ?: run { - AppLogger.udp.warn("No MAC address available, falling back to IP for hardware ID") - deviceState.address - } - val hardwareId = "$mac:${info.sensorId}" + val deviceHardwareId = deviceState.hardwareId ?: udpHardwareId(deviceState.macAddress, deviceState.address) + val hardwareId = "$deviceHardwareId:${info.sensorId}" val existingTracker = receiver.appContext.server.context.state.value.trackers.values .find { t -> t.context.state.value.deviceId == deviceState.id && t.context.state.value.hardwareId == hardwareId } @@ -147,13 +145,11 @@ class PingBehaviour : UDPConnectionBehaviour { } class HandshakeBehaviour : UDPConnectionBehaviour { - private fun findOrCreateDevice(receiver: UDPConnection, state: UDPConnectionState, data: Handshake): Device { + private fun findOrCreateDevice(receiver: UDPConnection, state: UDPConnectionState, data: Handshake, hardwareId: String): Device { val devices = receiver.appContext.server.context.state.value.devices.values - val existing = data.macString?.let { mac -> - devices.find { device -> - val ds = device.context.state.value - ds.macAddress == mac && ds.origin == DeviceOrigin.UDP - } + val existing = devices.find { device -> + val ds = device.context.state.value + ds.hardwareId == hardwareId && ds.origin == DeviceOrigin.UDP } if (existing != null) { val existingId = existing.context.state.value.id @@ -172,6 +168,7 @@ class HandshakeBehaviour : UDPConnectionBehaviour { id = deviceId, address = state.address, macAddress = data.macString, + hardwareId = hardwareId, origin = DeviceOrigin.UDP, protocolVersion = data.protocolVersion, ) @@ -183,33 +180,31 @@ class HandshakeBehaviour : UDPConnectionBehaviour { override fun observe(receiver: UDPConnection) { receiver.packetEvents.onPacket { packet -> val state = receiver.context.state.value - val mac = packet.data.macString + val hardwareId = udpHardwareId(packet.data.macString, state.address) - if (mac != null) { - val settings = receiver.appContext.config.settings.context.state.value.data - val udpServer = receiver.appContext.udpServer - if (mac !in settings.allowedUdpDevices) { - udpServer.context.dispatch( - UdpServerActions.UnknownDeviceSeen( - UnknownDeviceState( - macAddress = mac, - address = state.address, - boardType = packet.data.boardType, - mcuType = packet.data.mcuType, - firmwareVersion = packet.data.firmware, - lastSeen = udpServer.clock(), - ), + val allowed = receiver.appContext.config.devices.context.state.value.allowedUdpDevices + val udpServer = receiver.appContext.udpServer + if (hardwareId !in allowed) { + udpServer.context.dispatch( + UdpServerActions.UnknownDeviceSeen( + UnknownDeviceState( + hardwareId = hardwareId, + address = state.address, + boardType = packet.data.boardType, + mcuType = packet.data.mcuType, + firmwareVersion = packet.data.firmware, + lastSeen = udpServer.clock(), ), - ) - return@onPacket - } - if (mac in udpServer.context.state.value.unknownDevices) { - udpServer.context.dispatch(UdpServerActions.UnknownDeviceRemoved(mac)) - } + ), + ) + return@onPacket + } + if (hardwareId in udpServer.context.state.value.unknownDevices) { + udpServer.context.dispatch(UdpServerActions.UnknownDeviceRemoved(hardwareId)) } val device = if (state.deviceId == null) { - findOrCreateDevice(receiver, state, packet.data) + findOrCreateDevice(receiver, state, packet.data, hardwareId) } else { receiver.context.dispatch(UDPConnectionActions.Handshake(state.deviceId)) receiver.getDevice() ?: run { @@ -221,7 +216,7 @@ class HandshakeBehaviour : UDPConnectionBehaviour { val previousStatus = device.context.state.value.status if (previousStatus != TrackerStatus.OK) { - AppLogger.udp.info("Handshake from ${device.context.state.value.macAddress}, was $previousStatus") + AppLogger.udp.info("Handshake from $hardwareId, was $previousStatus") } // Apply handshake fields to device, always, for both first connect and reconnect diff --git a/server/core/src/main/java/dev/slimevr/udp/forget.kt b/server/core/src/main/java/dev/slimevr/udp/forget.kt index e8285e1fa..4e0165aab 100644 --- a/server/core/src/main/java/dev/slimevr/udp/forget.kt +++ b/server/core/src/main/java/dev/slimevr/udp/forget.kt @@ -2,7 +2,7 @@ package dev.slimevr.udp import dev.slimevr.AppContextProvider import dev.slimevr.VRServerActions -import dev.slimevr.config.SettingsActions +import dev.slimevr.config.DevicesConfigActions import dev.slimevr.device.Device import dev.slimevr.logging.AppLogger import dev.slimevr.skeleton.SkeletonActions @@ -13,8 +13,8 @@ import solarxr_protocol.datatypes.TrackerStatus private fun findUdpDevices(appContext: AppContextProvider, predicate: (String) -> Boolean): List = appContext.server.context.state.value.devices.values.filter { device -> val state = device.context.state.value - val mac = state.macAddress - state.origin == DeviceOrigin.UDP && mac != null && predicate(mac) + val hardwareId = state.hardwareId + state.origin == DeviceOrigin.UDP && hardwareId != null && predicate(hardwareId) } /** @@ -47,19 +47,19 @@ fun removeUdpDevice(appContext: AppContextProvider, device: Device) { // After the scopes are cancelled, so the tracker config behaviour can't write it back val hardwareIds = trackerStates.map { it.hardwareId }.toSet() - appContext.config.settings.context.dispatch(SettingsActions.Update { copy(trackers = trackers - hardwareIds) }) + appContext.config.devices.context.dispatch(DevicesConfigActions.Update { copy(trackers = trackers - hardwareIds) }) } -/** Drops a MAC from the allowed list and removes its UDP device from the server. */ -suspend fun forgetUdpDevice(appContext: AppContextProvider, mac: String) { +/** Drops a hardware ID from the allowed list and removes its UDP device from the server. */ +suspend fun forgetUdpDevice(appContext: AppContextProvider, hardwareId: String) { // Removed first so a handshake arriving mid-forget gets rejected instead of recreating the device - appContext.config.settings.context.dispatch(SettingsActions.RemoveAllowedUdpDevice(mac)) - findUdpDevices(appContext) { it == mac }.forEach { removeUdpDevice(appContext, it) } - AppLogger.udp.info("Forgot device $mac") + appContext.config.devices.context.dispatch(DevicesConfigActions.RemoveAllowedUdpDevice(hardwareId)) + findUdpDevices(appContext) { it == hardwareId }.forEach { removeUdpDevice(appContext, it) } + AppLogger.udp.info("Forgot device $hardwareId") } suspend fun forgetAllUdpDevices(appContext: AppContextProvider) { findUdpDevices(appContext) { true } - .mapNotNull { it.context.state.value.macAddress } - .forEach { mac -> forgetUdpDevice(appContext, mac) } + .mapNotNull { it.context.state.value.hardwareId } + .forEach { hardwareId -> forgetUdpDevice(appContext, hardwareId) } } diff --git a/server/core/src/main/java/dev/slimevr/udp/reducer.kt b/server/core/src/main/java/dev/slimevr/udp/reducer.kt index 83767de4f..89cbb9b41 100644 --- a/server/core/src/main/java/dev/slimevr/udp/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/udp/reducer.kt @@ -6,22 +6,22 @@ fun reduce(state: UdpServerState, action: UdpServerActions): UdpServerState = wh is UdpServerActions.ConnectionRemoved -> state.copy(connections = state.connections - action.address) is UdpServerActions.UnknownDeviceSeen -> { - val mac = action.device.macAddress - if (mac in state.ignoredMacs) { + val id = action.device.hardwareId + if (id in state.ignoredHardwareIds) { state } else { - state.copy(unknownDevices = state.unknownDevices + (mac to action.device)) + state.copy(unknownDevices = state.unknownDevices + (id to action.device)) } } - is UdpServerActions.UnknownDeviceRemoved -> state.copy(unknownDevices = state.unknownDevices - action.macAddress) + is UdpServerActions.UnknownDeviceRemoved -> state.copy(unknownDevices = state.unknownDevices - action.hardwareId) is UdpServerActions.UnknownDevicesExpired -> state.copy( unknownDevices = state.unknownDevices.filterValues { it.lastSeen > action.cutoff }, ) is UdpServerActions.IgnoreUnknownDevice -> state.copy( - unknownDevices = state.unknownDevices - action.macAddress, - ignoredMacs = state.ignoredMacs + action.macAddress, + unknownDevices = state.unknownDevices - action.hardwareId, + ignoredHardwareIds = state.ignoredHardwareIds + action.hardwareId, ) } diff --git a/server/core/src/main/java/dev/slimevr/udp/server.kt b/server/core/src/main/java/dev/slimevr/udp/server.kt index fb22b5603..33bf589a3 100644 --- a/server/core/src/main/java/dev/slimevr/udp/server.kt +++ b/server/core/src/main/java/dev/slimevr/udp/server.kt @@ -26,16 +26,16 @@ import kotlin.time.measureTime data class UdpServerState( val connections: Map, val unknownDevices: Map = emptyMap(), - val ignoredMacs: Set = emptySet(), + val ignoredHardwareIds: Set = emptySet(), ) sealed interface UdpServerActions { data class ConnectionAdded(val address: String, val conn: UDPConnection) : UdpServerActions data class ConnectionRemoved(val address: String) : UdpServerActions data class UnknownDeviceSeen(val device: UnknownDeviceState) : UdpServerActions - data class UnknownDeviceRemoved(val macAddress: String) : UdpServerActions + data class UnknownDeviceRemoved(val hardwareId: String) : UdpServerActions data class UnknownDevicesExpired(val cutoff: Long) : UdpServerActions - data class IgnoreUnknownDevice(val macAddress: String) : UdpServerActions + data class IgnoreUnknownDevice(val hardwareId: String) : UdpServerActions } typealias UdpServerContext = Context diff --git a/server/core/src/main/java/dev/slimevr/udp/unknown-devices.kt b/server/core/src/main/java/dev/slimevr/udp/unknown-devices.kt index 3af035aa7..7e8689fdf 100644 --- a/server/core/src/main/java/dev/slimevr/udp/unknown-devices.kt +++ b/server/core/src/main/java/dev/slimevr/udp/unknown-devices.kt @@ -15,7 +15,7 @@ internal const val UNKNOWN_DEVICE_TIMEOUT_MS = 5000L /** A device handshaking without being on the allowed list. */ data class UnknownDeviceState( - val macAddress: String, + val hardwareId: String, // Same "ip/port" form as UDPConnectionState.address val address: String, val boardType: BoardType, diff --git a/server/core/src/main/java/dev/slimevr/util/address.kt b/server/core/src/main/java/dev/slimevr/util/address.kt index f9970975b..45ec760bf 100644 --- a/server/core/src/main/java/dev/slimevr/util/address.kt +++ b/server/core/src/main/java/dev/slimevr/util/address.kt @@ -4,6 +4,8 @@ fun stripIpAddressPort(string: String) = string.lastIndexOf('/').takeIf { it != string.substring(0, it) } ?: string +fun udpHardwareId(macAddress: String?, address: String): String = macAddress ?: stripIpAddressPort(address) + fun ipv4AddressFromString(address: String): UInt { val parts = stripIpAddressPort(address).split('.') if (parts.size != 4) return 0u @@ -12,3 +14,12 @@ fun ipv4AddressFromString(address: String): UInt { (acc shl 8) or value } } + +fun macAddressToULong(macAddress: String): ULong? { + val bytes = macAddress.split(':') + if (bytes.size != 6) return null + return bytes.fold(0uL) { acc, byte -> + val value = byte.toULongOrNull(16)?.takeIf { byte.length == 2 } ?: return null + (acc shl 8) or value + } +} diff --git a/server/core/src/test/java/dev/slimevr/TestServer.kt b/server/core/src/test/java/dev/slimevr/TestServer.kt index ac2fc1bf5..660296e24 100644 --- a/server/core/src/test/java/dev/slimevr/TestServer.kt +++ b/server/core/src/test/java/dev/slimevr/TestServer.kt @@ -3,6 +3,9 @@ package dev.slimevr import dev.slimevr.bvh.BVHManager import dev.slimevr.config.AppConfig import dev.slimevr.config.ConfigStorage +import dev.slimevr.config.DEVICES_CONFIG_PATH +import dev.slimevr.config.DevicesConfig +import dev.slimevr.config.DevicesConfigState import dev.slimevr.config.GlobalConfig import dev.slimevr.config.GlobalConfigState import dev.slimevr.config.Settings @@ -12,6 +15,7 @@ import dev.slimevr.config.TextFileHandle import dev.slimevr.config.UserConfig import dev.slimevr.config.UserConfigData import dev.slimevr.config.UserConfigState +import dev.slimevr.config.launchAutosave import dev.slimevr.context.Context import dev.slimevr.firmware.FirmwareManager import dev.slimevr.heightcalibration.HeightCalibrationManager @@ -205,6 +209,17 @@ fun buildTestSettings(scope: CoroutineScope): Settings { return Settings(context, scope, NoopConfigStorage, "settings") } +fun buildTestDevicesConfig(scope: CoroutineScope): DevicesConfig { + val context = Context.create( + initialState = DevicesConfigState(), + scope = scope, + reducer = ::reduceConfig, + name = "DevicesConfig[test]", + ) + val autosave = launchAutosave(scope, context.state, NoopConfigStorage, toPath = { DEVICES_CONFIG_PATH }, serialize = { "" }) + return DevicesConfig(context, autosave) +} + fun buildTestHeightCalibration(server: VRServer, userConfig: UserConfig, scope: CoroutineScope): HeightCalibrationManager { val initialState = HeightCalibrationState(status = UserHeightCalibrationStatus.NONE, currentHeight = 1.6f, canDoUserHeightCalibration = true) val context = Context.create( @@ -244,6 +259,7 @@ fun buildTestAppConfig(scope: CoroutineScope): AppConfig { globalConfig = GlobalConfig(globalContext).also { it.startObserving() }, userConfig = buildTestUserConfig(scope), settings = buildTestSettings(scope), + devices = buildTestDevicesConfig(scope), storage = NoopConfigStorage, ) } diff --git a/server/core/src/test/java/dev/slimevr/config/ProfileSwitchTest.kt b/server/core/src/test/java/dev/slimevr/config/ProfileSwitchTest.kt index c604cbc3a..1a100cc98 100644 --- a/server/core/src/test/java/dev/slimevr/config/ProfileSwitchTest.kt +++ b/server/core/src/test/java/dev/slimevr/config/ProfileSwitchTest.kt @@ -9,6 +9,7 @@ import dev.slimevr.buildTestVrServerStub import dev.slimevr.skeleton.Skeleton import dev.slimevr.skeleton.computeAllDefaultProportionsByBone import dev.slimevr.tracker.Tracker +import dev.slimevr.tracker.TrackerActions import dev.slimevr.tracker.behaviours.TrackerConfigBehaviour import dev.slimevr.vmc.VMCInputBehaviour import dev.slimevr.vmc.VMCManager @@ -27,7 +28,6 @@ import java.net.DatagramSocket import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotEquals -import kotlin.test.assertNull import kotlin.test.assertTrue private fun settingsFile(name: String) = "settings/$name.json" @@ -169,47 +169,25 @@ class ProfileSwitchTest { private fun buildTracker(scope: TestScope, config: AppConfig): Tracker { val server = buildTestVrServerStub(scope.backgroundScope) val tracker = buildTestTracker(scope.backgroundScope, buildTestAppContext(server), config.settings, id = 1) - TrackerConfigBehaviour(config.settings, "test-1", Tracker.DEFAULT_STATE).observe(tracker) + TrackerConfigBehaviour(config.devices, "test-1").observe(tracker) return tracker } @Test - fun `a live tracker takes the config of the selected profile and never writes its old one into it`() = runTest { + fun `switching the settings profile keeps the tracker config`() = runTest { val storage = MemoryStorage() - putSettings(storage, "default", SettingsConfigState(trackers = mapOf("test-1" to TrackerConfig(bodyPart = BodyPart.HIP, customName = "hip")))) - putSettings(storage, "feet", SettingsConfigState(trackers = mapOf("test-1" to TrackerConfig(bodyPart = BodyPart.LEFT_FOOT)))) + putSettings(storage, "other", SettingsConfigState()) val config = buildConfig(this, storage) val tracker = buildTracker(this, config) - - config.switchProfile(ProfileKind.SETTINGS, "feet") runCurrent() - assertEquals(BodyPart.LEFT_FOOT, tracker.context.state.value.bodyPart) - assertNull(tracker.context.state.value.customName) - - config.switchProfile(ProfileKind.SETTINGS, "default") + tracker.context.dispatch(TrackerActions.Update { copy(bodyPart = BodyPart.HIP) }) runCurrent() + + config.switchProfile(ProfileKind.SETTINGS, "other") + runCurrent() + assertEquals(BodyPart.HIP, tracker.context.state.value.bodyPart) - assertEquals("hip", tracker.context.state.value.customName) - - config.switchProfile(ProfileKind.SETTINGS, "feet") - runCurrent() - assertEquals(BodyPart.LEFT_FOOT, config.settings.context.state.value.data.trackers.getValue("test-1").bodyPart) - } - - @Test - fun `a tracker missing from the selected profile returns to its creation state`() = runTest { - val storage = MemoryStorage() - putSettings(storage, "default", SettingsConfigState(trackers = mapOf("test-1" to TrackerConfig(bodyPart = BodyPart.HIP, customName = "hip")))) - val config = buildConfig(this, storage) - val tracker = buildTracker(this, config) - config.switchProfile(ProfileKind.SETTINGS, "default") - config.createProfile(ProfileKind.SETTINGS, "empty", copyFrom = null) - - config.switchProfile(ProfileKind.SETTINGS, "empty") - runCurrent() - - assertNull(tracker.context.state.value.bodyPart) - assertNull(tracker.context.state.value.customName) + assertEquals(BodyPart.HIP, config.devices.context.state.value.trackers.getValue("test-1").bodyPart) } private fun vmcHarness(scope: TestScope, config: AppConfig): VMCManager { diff --git a/server/core/src/test/java/dev/slimevr/firmware/DoSerialFlashTest.kt b/server/core/src/test/java/dev/slimevr/firmware/DoSerialFlashTest.kt index fceaf6ac1..b7eb112b7 100644 --- a/server/core/src/test/java/dev/slimevr/firmware/DoSerialFlashTest.kt +++ b/server/core/src/test/java/dev/slimevr/firmware/DoSerialFlashTest.kt @@ -5,7 +5,7 @@ import dev.slimevr.VRServer import dev.slimevr.VRServerActions import dev.slimevr.buildTestAppContext import dev.slimevr.buildTestSerial -import dev.slimevr.buildTestSettings +import dev.slimevr.buildTestDevicesConfig import dev.slimevr.buildTestVrServerStub import dev.slimevr.device.Device import dev.slimevr.device.DeviceActions @@ -72,7 +72,7 @@ class DoSerialFlashTest { ssid = null, password = null, serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = vrServer, flasher = failingFirmwareFlasher, onStatus = { s, _ -> statuses += s }, @@ -96,7 +96,7 @@ class DoSerialFlashTest { ssid = null, password = null, serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = buildTestVrServerStub(backgroundScope), flasher = failingFirmwareFlasher, onStatus = { s, _ -> statuses += s }, @@ -120,7 +120,7 @@ class DoSerialFlashTest { ssid = null, password = null, serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = buildTestVrServerStub(backgroundScope), flasher = failingFirmwareFlasher, onStatus = { s, _ -> statuses += s }, @@ -144,7 +144,7 @@ class DoSerialFlashTest { ssid = "wifi", password = "pass", serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = buildTestVrServerStub(backgroundScope), onStatus = { s, _ -> statuses += s }, ) @@ -167,7 +167,7 @@ class DoSerialFlashTest { ssid = "wifi", password = "pass", serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = buildTestVrServerStub(backgroundScope), onStatus = { s, _ -> statuses += s }, ) @@ -194,7 +194,7 @@ class DoSerialFlashTest { ssid = null, password = null, serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = buildTestVrServerStub(backgroundScope), onStatus = { s, _ -> statuses += s }, ) @@ -225,7 +225,7 @@ class DoSerialFlashTest { ssid = "wifi", password = "pass", serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = buildTestVrServerStub(backgroundScope), onStatus = { s, _ -> statuses += s }, ) @@ -258,7 +258,7 @@ class DoSerialFlashTest { ssid = "wifi", password = "pass", serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = buildTestVrServerStub(backgroundScope), onStatus = { s, _ -> statuses += s }, ) @@ -294,7 +294,7 @@ class DoSerialFlashTest { ssid = "wifi", password = "pass", serialServer = serial.server, - settings = buildTestSettings(backgroundScope), + devices = buildTestDevicesConfig(backgroundScope), server = vrServer, onStatus = { s, _ -> statuses += s }, ) diff --git a/server/core/src/test/java/dev/slimevr/provisioning/ProvisioningManagerTest.kt b/server/core/src/test/java/dev/slimevr/provisioning/ProvisioningManagerTest.kt index 513f71fe1..b6e5f8d57 100644 --- a/server/core/src/test/java/dev/slimevr/provisioning/ProvisioningManagerTest.kt +++ b/server/core/src/test/java/dev/slimevr/provisioning/ProvisioningManagerTest.kt @@ -4,7 +4,7 @@ import dev.slimevr.VRServer import dev.slimevr.VRServerActions import dev.slimevr.buildTestAppContext import dev.slimevr.buildTestSerial -import dev.slimevr.buildTestSettings +import dev.slimevr.buildTestDevicesConfig import dev.slimevr.buildTestVrServer import dev.slimevr.context.Context import dev.slimevr.device.Device @@ -39,7 +39,7 @@ private fun buildManager(serialServer: dev.slimevr.serial.SerialServer, scope: C reducer = ::reduceProvisioning, name = "ProvisioningManagerTest", ) - return ProvisioningManager(context = context, serialServer = serialServer, settings = buildTestSettings(scope), scope = scope).also { it.startObserving() } + return ProvisioningManager(context = context, serialServer = serialServer, devices = buildTestDevicesConfig(scope), scope = scope).also { it.startObserving() } } private fun ProvisioningManager.trackerStatus(port: String = "COM1") = context.state.value.trackers[port]?.status diff --git a/server/core/src/test/java/dev/slimevr/solarxr/ProvisioningBehaviourTest.kt b/server/core/src/test/java/dev/slimevr/solarxr/ProvisioningBehaviourTest.kt index 744d90905..66b95f36b 100644 --- a/server/core/src/test/java/dev/slimevr/solarxr/ProvisioningBehaviourTest.kt +++ b/server/core/src/test/java/dev/slimevr/solarxr/ProvisioningBehaviourTest.kt @@ -4,7 +4,7 @@ import dev.slimevr.TestAppContext import dev.slimevr.TestSerial import dev.slimevr.VRServer import dev.slimevr.buildTestSerial -import dev.slimevr.buildTestSettings +import dev.slimevr.buildTestDevicesConfig import dev.slimevr.buildTestVrServerStub import dev.slimevr.context.Context import dev.slimevr.provisioning.ProvisioningManager @@ -33,7 +33,7 @@ private fun buildManager(serial: TestSerial, scope: CoroutineScope): Provisionin return ProvisioningManager( context = context, serialServer = serial.server, - settings = buildTestSettings(scope), + devices = buildTestDevicesConfig(scope), scope = scope, ).also { it.startObserving() } } diff --git a/server/core/src/test/java/dev/slimevr/solarxr/SettingsResetTest.kt b/server/core/src/test/java/dev/slimevr/solarxr/SettingsResetTest.kt index 6161a87ef..166af01b7 100644 --- a/server/core/src/test/java/dev/slimevr/solarxr/SettingsResetTest.kt +++ b/server/core/src/test/java/dev/slimevr/solarxr/SettingsResetTest.kt @@ -7,8 +7,8 @@ import dev.slimevr.buildTestAppConfig import dev.slimevr.buildTestTracker import dev.slimevr.buildTestVrServerStub import dev.slimevr.config.AppConfig +import dev.slimevr.config.DevicesConfigActions import dev.slimevr.config.GlobalConfigActions -import dev.slimevr.config.SettingsActions import dev.slimevr.config.UserConfigActions import dev.slimevr.config.changeConsent import dev.slimevr.context.Context @@ -83,13 +83,14 @@ class SettingsResetTest { val server = buildTestVrServerStub(backgroundScope) val bridge = buildBridge(backgroundScope, config, server) val mac = "AA:BB:CC:DD:EE:FF" - config.settings.context.dispatch(SettingsActions.AddAllowedUdpDevice(mac)) + config.devices.context.dispatch(DevicesConfigActions.AddAllowedUdpDevice(mac)) val device = Device.create( scope = server.context.scope, appContext = bridge.appContext, id = server.nextHandle(), address = "127.0.0.1", macAddress = mac, + hardwareId = mac, origin = DeviceOrigin.UDP, protocolVersion = 21, ) @@ -97,7 +98,7 @@ class SettingsResetTest { val tracker = buildTestTracker(server.context.scope, bridge.appContext, config.settings, server.nextHandle()) tracker.context.dispatch(TrackerActions.Update { copy(deviceId = device.context.state.value.id, hardwareId = "$mac:0") }) server.context.dispatch(VRServerActions.NewTracker(tracker.context.state.value.id, tracker)) - config.settings.context.dispatch(SettingsActions.UpdateTracker("$mac:0") { copy(customName = "Left foot") }) + config.devices.context.dispatch(DevicesConfigActions.UpdateTracker("$mac:0") { copy(customName = "Left foot") }) config.globalConfig.context.dispatch( GlobalConfigActions.SetErrorReporting( changeConsent(config.globalConfig.context.state.value.errorReporting, ErrorReportingConsent.ALLOWED), @@ -111,7 +112,7 @@ class SettingsResetTest { assertTrue(server.context.state.value.devices.isEmpty()) assertTrue(server.context.state.value.trackers.isEmpty()) - assertTrue(config.settings.context.state.value.data.trackers.isEmpty()) - assertTrue(config.settings.context.state.value.data.allowedUdpDevices.isEmpty()) + assertTrue(config.devices.context.state.value.trackers.isEmpty()) + assertTrue(config.devices.context.state.value.allowedUdpDevices.isEmpty()) } } diff --git a/server/core/src/test/java/dev/slimevr/tracker/TrackerProfileConfigTest.kt b/server/core/src/test/java/dev/slimevr/tracker/TrackerProfileConfigTest.kt deleted file mode 100644 index e49975cc4..000000000 --- a/server/core/src/test/java/dev/slimevr/tracker/TrackerProfileConfigTest.kt +++ /dev/null @@ -1,35 +0,0 @@ -package dev.slimevr.tracker - -import dev.slimevr.config.TrackerConfig -import dev.slimevr.tracker.behaviours.TrackerConfigBehaviour -import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNull - -class TrackerProfileConfigTest { - private val base = Tracker.DEFAULT_STATE.copy(bodyPart = BodyPart.UPPER_CHEST) - - @Test - fun `a profile config replaces what the previous profile set`() { - val live = base.copy(bodyPart = BodyPart.HIP, customName = "old") - val config = TrackerConfig(bodyPart = BodyPart.LEFT_FOOT, customName = "new") - - val result = TrackerConfigBehaviour.applyProfileConfig(live, base, config, saveMountingReset = false) - - assertEquals(BodyPart.LEFT_FOOT, result.bodyPart) - assertEquals("new", result.customName) - } - - @Test - fun `a tracker unknown to the profile goes back to its creation state`() { - val live = base.copy(bodyPart = BodyPart.HIP, customName = "old", mountingOrientation = Quaternion(0f, 0f, 1f, 0f)) - - val result = TrackerConfigBehaviour.applyProfileConfig(live, base, null, saveMountingReset = false) - - assertEquals(BodyPart.UPPER_CHEST, result.bodyPart) - assertNull(result.customName) - assertEquals(Quaternion.IDENTITY, result.mountingOrientation) - } -} diff --git a/server/core/src/test/java/dev/slimevr/udp/ForgetDeviceTest.kt b/server/core/src/test/java/dev/slimevr/udp/ForgetDeviceTest.kt index 81e435148..5c51447bc 100644 --- a/server/core/src/test/java/dev/slimevr/udp/ForgetDeviceTest.kt +++ b/server/core/src/test/java/dev/slimevr/udp/ForgetDeviceTest.kt @@ -9,7 +9,7 @@ import dev.slimevr.buildTestSkeleton import dev.slimevr.buildTestTracker import dev.slimevr.buildTestVrServerStub import dev.slimevr.config.AppConfig -import dev.slimevr.config.SettingsActions +import dev.slimevr.config.DevicesConfigActions import dev.slimevr.device.Device import dev.slimevr.skeleton.Skeleton import dev.slimevr.skeleton.SkeletonActions @@ -50,6 +50,7 @@ private fun addDevice(appContext: AppContextProvider, origin: DeviceOrigin): Dev id = id, address = "127.0.0.$id", macAddress = MAC, + hardwareId = MAC, origin = origin, protocolVersion = 21, ) @@ -77,7 +78,7 @@ private fun addTracker( val hardwareId = "$MAC:$sensorId" tracker.context.dispatch(TrackerActions.Update { copy(deviceId = device.context.state.value.id, hardwareId = hardwareId) }) appContext.server.context.dispatch(VRServerActions.NewTracker(id, tracker)) - appContext.config.settings.context.dispatch(SettingsActions.UpdateTracker(hardwareId) { copy(bodyPart = bodyPart) }) + appContext.config.devices.context.dispatch(DevicesConfigActions.UpdateTracker(hardwareId) { copy(bodyPart = bodyPart) }) return tracker } @@ -86,8 +87,8 @@ class ForgetDeviceTest { @Test fun `forget removes the udp device, its trackers, the allowed mac and saved config, and nothing else`() = runTest { val appContext = buildContext(backgroundScope) - val settings = appContext.config.settings - settings.context.dispatch(SettingsActions.AddAllowedUdpDevice(MAC)) + val devices = appContext.config.devices + devices.context.dispatch(DevicesConfigActions.AddAllowedUdpDevice(MAC)) val device = addDevice(appContext, DeviceOrigin.UDP) val trackers = listOf( addTracker(appContext, device, 0, BodyPart.UPPER_CHEST, TrackerStatus.OK), @@ -108,8 +109,8 @@ class ForgetDeviceTest { assertFalse(device.context.scope.isActive) trackers.forEach { assertFalse(it.context.scope.isActive) } assertTrue(hidDevice.context.scope.isActive) - assertFalse(MAC in settings.context.state.value.data.allowedUdpDevices) - assertEquals(setOf("$MAC:5"), settings.context.state.value.data.trackers.keys) + assertFalse(MAC in devices.context.state.value.allowedUdpDevices) + assertEquals(setOf("$MAC:5"), devices.context.state.value.trackers.keys) assertEquals(false, appContext.skeleton.context.state.value.boneInputs[BodyPart.UPPER_CHEST]?.isRotationActive) } } diff --git a/server/core/src/test/java/dev/slimevr/udp/UnknownDevicesTest.kt b/server/core/src/test/java/dev/slimevr/udp/UnknownDevicesTest.kt index ca2d07e87..0529a5c5c 100644 --- a/server/core/src/test/java/dev/slimevr/udp/UnknownDevicesTest.kt +++ b/server/core/src/test/java/dev/slimevr/udp/UnknownDevicesTest.kt @@ -1,6 +1,8 @@ package dev.slimevr.udp import dev.slimevr.solarxr.rpc.createUnknownDevice +import dev.slimevr.util.macAddressToULong +import dev.slimevr.util.udpHardwareId import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.runCurrent @@ -17,7 +19,7 @@ private const val MAC = "AA:BB:CC:DD:EE:FF" private const val OTHER_MAC = "11:22:33:44:55:66" private fun device(mac: String = MAC, lastSeen: Long = 1000L, firmware: String? = "0.6.3") = UnknownDeviceState( - macAddress = mac, + hardwareId = mac, address = "192.168.1.70/54321", boardType = BoardType.SLIMEVR, mcuType = McuType.ESP32_C3, @@ -29,13 +31,17 @@ private val EMPTY = UdpServer.INITIAL_STATE class UnknownDevicesTest { @Test - fun `status follows the allowed list and the address drops its port`() { + fun `status follows the allowed list, and identity is the mac or the ip without port`() { val allowed = createUnknownDevice(device(), setOf(MAC)) val waiting = createUnknownDevice(device(), setOf(OTHER_MAC)) assertEquals(UnknownDeviceStatus.ADDING, allowed.status) assertEquals(UnknownDeviceStatus.WAITING, waiting.status) assertEquals(0xC0A80146u, allowed.ipAddress) + assertEquals(MAC, udpHardwareId(MAC, "192.168.1.70/54321")) + assertEquals("192.168.1.70", udpHardwareId(null, "192.168.1.70/54321")) + assertEquals(0xAABBCCDDEEFFuL, macAddressToULong(MAC)) + assertEquals(null, macAddressToULong("not a mac")) } @Test diff --git a/solarxr-protocol b/solarxr-protocol index 03c80290c..348a0a7c1 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit 03c80290cb6aa18d39eccd3714fc5a7e88c20aa8 +Subproject commit 348a0a7c129f66e1820e130cfd8aa6d1ed300633