mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-21 17:41:28 -08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12354f0a95 |
No files matched your search
@@ -32,6 +32,7 @@ import dev.slimevr.tracking.processor.skeleton.HumanSkeleton
|
||||
import dev.slimevr.tracking.trackers.*
|
||||
import dev.slimevr.tracking.trackers.udp.TrackersUDPServer
|
||||
import dev.slimevr.trackingchecklist.TrackingChecklistManager
|
||||
import dev.slimevr.util.TrackerLogger
|
||||
import dev.slimevr.util.ann.VRServerThread
|
||||
import dev.slimevr.websocketapi.WebSocketVRBridge
|
||||
import io.eiren.util.ann.ThreadSafe
|
||||
@@ -125,6 +126,8 @@ class VRServer @JvmOverloads constructor(
|
||||
|
||||
val serverGuards = ServerGuards()
|
||||
|
||||
val trackerLogger = TrackerLogger()
|
||||
|
||||
init {
|
||||
// UwU
|
||||
deviceManager = DeviceManager(this)
|
||||
@@ -179,6 +182,8 @@ class VRServer @JvmOverloads constructor(
|
||||
registerTracker(tracker)
|
||||
}
|
||||
|
||||
addNewTrackerConsumer(trackerLogger)
|
||||
|
||||
instance = this
|
||||
}
|
||||
|
||||
@@ -259,6 +264,7 @@ class VRServer @JvmOverloads constructor(
|
||||
}
|
||||
vrcOSCHandler.update()
|
||||
vMCHandler.update()
|
||||
trackerLogger.update()
|
||||
// final long time = System.currentTimeMillis() - start;
|
||||
try {
|
||||
sleep(1) // 1000Hz
|
||||
|
||||
@@ -11,7 +11,6 @@ import dev.slimevr.serial.SerialPort
|
||||
import dev.slimevr.tracking.trackers.Tracker
|
||||
import dev.slimevr.tracking.trackers.TrackerStatus
|
||||
import dev.slimevr.tracking.trackers.TrackerStatusListener
|
||||
import dev.slimevr.tracking.trackers.udp.MCUType
|
||||
import dev.slimevr.tracking.trackers.udp.UDPDevice
|
||||
import io.eiren.util.logging.LogManager
|
||||
import kotlinx.coroutines.*
|
||||
@@ -102,27 +101,11 @@ class FirmwareUpdateHandler(private val server: VRServer) :
|
||||
)
|
||||
return@suspendCancellableCoroutine
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// - Use the Firmware Builder to get the expected MCU
|
||||
// It would be wrong to assume that the Target MCU is the correct one,
|
||||
// just because the device is listening on the correct port.
|
||||
// The Upload protocol does not verify the compatibility of the firmware with the MCU.
|
||||
|
||||
val port = when (udpDevice.mcuType) {
|
||||
MCUType.ESP8266 -> 8266
|
||||
MCUType.ESP32, MCUType.ESP32_C3 -> 3232
|
||||
else -> error("MCU-Typ: ${udpDevice.mcuType} not supported for OTA updates")
|
||||
}
|
||||
|
||||
LogManager.info("[FirmwareUpdateHandler] Starting OTA update for device ${deviceId.id} at ${udpDevice.ipAddress.hostAddress}:$port and MCU ${udpDevice.mcuType}")
|
||||
|
||||
val task = OTAUpdateTask(
|
||||
part.firmware,
|
||||
deviceId,
|
||||
udpDevice.ipAddress,
|
||||
::onStatusChange,
|
||||
port,
|
||||
)
|
||||
c.invokeOnCancellation {
|
||||
task.cancel()
|
||||
@@ -173,15 +156,6 @@ class FirmwareUpdateHandler(private val server: VRServer) :
|
||||
flasher.addBin(part.firmware, part.offset.toInt())
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// - Check if FW is able to use flashmode
|
||||
// - Add check if the flashmode was successfully set to surpress the request
|
||||
// for manual flashmode setting prompt in gui
|
||||
|
||||
server.serialHandler.openSerial(deviceId.id, false)
|
||||
server.serialHandler.write("SET FLASHMODE\r\n".toByteArray())
|
||||
server.serialHandler.closeSerial()
|
||||
|
||||
flasher.addProgressListener(object : FlashingProgressListener {
|
||||
override fun progress(progress: Float) {
|
||||
onStatusChange(
|
||||
|
||||
@@ -14,8 +14,6 @@ import java.security.MessageDigest
|
||||
import java.security.NoSuchAlgorithmException
|
||||
import java.util.*
|
||||
import java.util.function.Consumer
|
||||
import javax.crypto.SecretKeyFactory
|
||||
import javax.crypto.spec.PBEKeySpec
|
||||
import kotlin.math.min
|
||||
|
||||
class OTAUpdateTask(
|
||||
@@ -23,9 +21,8 @@ class OTAUpdateTask(
|
||||
private val deviceId: UpdateDeviceId<Int>,
|
||||
private val deviceIp: InetAddress,
|
||||
private val statusCallback: Consumer<UpdateStatusEvent<Int>>,
|
||||
private val port: Int = 8266,
|
||||
) {
|
||||
private val receiveBuffer: ByteArray = ByteArray(69)
|
||||
private val receiveBuffer: ByteArray = ByteArray(38)
|
||||
var socketServer: ServerSocket? = null
|
||||
var uploadSocket: Socket? = null
|
||||
var authSocket: DatagramSocket? = null
|
||||
@@ -43,35 +40,17 @@ class OTAUpdateTask(
|
||||
return md5str.toString()
|
||||
}
|
||||
|
||||
@Throws(NoSuchAlgorithmException::class)
|
||||
private fun bytesToSha256(bytes: ByteArray): String {
|
||||
val sha256 = MessageDigest.getInstance("SHA-256")
|
||||
sha256.update(bytes)
|
||||
val digest = sha256.digest()
|
||||
val sha256str = StringBuilder()
|
||||
for (b in digest) {
|
||||
sha256str.append(String.format("%02x", b))
|
||||
}
|
||||
return sha256str.toString()
|
||||
}
|
||||
|
||||
fun pbkdf2Hmac(password: String, salt: ByteArray, iterations: Int, keyLength: Int): ByteArray {
|
||||
val spec = PBEKeySpec(password.toCharArray(), salt, iterations, keyLength * 8)
|
||||
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
|
||||
return factory.generateSecret(spec).encoded
|
||||
}
|
||||
|
||||
private fun authenticate(localPort: Int): Boolean {
|
||||
try {
|
||||
DatagramSocket().use { socket ->
|
||||
authSocket = socket
|
||||
statusCallback.accept(UpdateStatusEvent(deviceId, FirmwareUpdateStatus.AUTHENTICATING))
|
||||
LogManager.info("[OTAUpdate] Sending OTA invitation to: $deviceIp:$port")
|
||||
LogManager.info("[OTAUpdate] Sending OTA invitation to: $deviceIp")
|
||||
|
||||
val fileMd5 = bytesToMd5(firmware)
|
||||
val message = "$FLASH $localPort ${firmware.size} $fileMd5\n"
|
||||
|
||||
socket.send(DatagramPacket(message.toByteArray(), message.length, deviceIp, port))
|
||||
socket.send(DatagramPacket(message.toByteArray(), message.length, deviceIp, PORT))
|
||||
socket.soTimeout = 10000
|
||||
|
||||
val authPacket = DatagramPacket(receiveBuffer, receiveBuffer.size)
|
||||
@@ -89,26 +68,13 @@ class OTAUpdateTask(
|
||||
if (args.size != 2 || args[0] != "AUTH") return false
|
||||
|
||||
LogManager.info("[OTAUpdate] Authenticating...")
|
||||
var payload = ""
|
||||
var signature = ""
|
||||
|
||||
val authToken = args[1]
|
||||
if (authToken.length == 32) {
|
||||
signature =
|
||||
bytesToMd5(UUID.randomUUID().toString().toByteArray())
|
||||
val hashedPassword = bytesToMd5(PASSWORD.toByteArray())
|
||||
val resultText = "$hashedPassword:$authToken:$signature"
|
||||
payload = bytesToMd5(resultText.toByteArray())
|
||||
} else if (authToken.length == 64) {
|
||||
signature =
|
||||
bytesToSha256(UUID.randomUUID().toString().toByteArray())
|
||||
val salt = "$authToken:$signature"
|
||||
val hashedPassword = bytesToSha256(PASSWORD.toByteArray())
|
||||
val derivedkey = pbkdf2Hmac(hashedPassword, salt.toByteArray(), 10000, 32)
|
||||
val derivedkeyHex = derivedkey.joinToString("") { "%02x".format(it) }
|
||||
val challenge = "$derivedkeyHex:$authToken:$signature"
|
||||
payload = bytesToSha256(challenge.toByteArray())
|
||||
}
|
||||
val signature = bytesToMd5(UUID.randomUUID().toString().toByteArray())
|
||||
val hashedPassword = bytesToMd5(PASSWORD.toByteArray())
|
||||
val resultText = "$hashedPassword:$authToken:$signature"
|
||||
val payload = bytesToMd5(resultText.toByteArray())
|
||||
|
||||
val authMessage = "$AUTH $signature $payload\n"
|
||||
|
||||
socket.soTimeout = 10000
|
||||
@@ -117,7 +83,7 @@ class OTAUpdateTask(
|
||||
authMessage.toByteArray(),
|
||||
authMessage.length,
|
||||
deviceIp,
|
||||
port,
|
||||
PORT,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -232,6 +198,7 @@ class OTAUpdateTask(
|
||||
|
||||
companion object {
|
||||
private const val FLASH = 0
|
||||
private const val PORT = 8266
|
||||
private const val PASSWORD = "SlimeVR-OTA"
|
||||
private const val AUTH = 200
|
||||
}
|
||||
|
||||
@@ -39,39 +39,24 @@ class RPCResetHandler(var rpcHandler: RPCHandler, var api: ProtocolAPI) : ResetL
|
||||
}
|
||||
|
||||
if (req.resetType() == ResetType.Yaw) {
|
||||
val delay = if (req.hasDelay()) {
|
||||
req.delay()
|
||||
} else {
|
||||
resetsConfig.yawResetDelay
|
||||
}
|
||||
if (bodyParts.isEmpty()) {
|
||||
api.server.scheduleResetTrackersYaw(RESET_SOURCE_NAME, (delay * 1000).toLong())
|
||||
api.server.scheduleResetTrackersYaw(RESET_SOURCE_NAME, ((req.delay() ?: resetsConfig.yawResetDelay) * 1000).toLong())
|
||||
} else {
|
||||
api.server.scheduleResetTrackersYaw(RESET_SOURCE_NAME, (delay * 1000).toLong(), bodyParts.toList())
|
||||
api.server.scheduleResetTrackersYaw(RESET_SOURCE_NAME, ((req.delay() ?: resetsConfig.yawResetDelay) * 1000).toLong(), bodyParts.toList())
|
||||
}
|
||||
}
|
||||
if (req.resetType() == ResetType.Full) {
|
||||
val delay = if (req.hasDelay()) {
|
||||
req.delay()
|
||||
} else {
|
||||
resetsConfig.fullResetDelay
|
||||
}
|
||||
if (bodyParts.isEmpty()) {
|
||||
api.server.scheduleResetTrackersFull(RESET_SOURCE_NAME, (delay * 1000).toLong())
|
||||
api.server.scheduleResetTrackersFull(RESET_SOURCE_NAME, ((req.delay() ?: resetsConfig.fullResetDelay) * 1000).toLong())
|
||||
} else {
|
||||
api.server.scheduleResetTrackersFull(RESET_SOURCE_NAME, (delay * 1000).toLong(), bodyParts.toList())
|
||||
api.server.scheduleResetTrackersFull(RESET_SOURCE_NAME, ((req.delay() ?: resetsConfig.fullResetDelay) * 1000).toLong(), bodyParts.toList())
|
||||
}
|
||||
}
|
||||
if (req.resetType() == ResetType.Mounting) {
|
||||
val delay = if (req.hasDelay()) {
|
||||
req.delay()
|
||||
} else {
|
||||
resetsConfig.mountingResetDelay
|
||||
}
|
||||
if (bodyParts.isEmpty()) {
|
||||
api.server.scheduleResetTrackersMounting(RESET_SOURCE_NAME, (delay * 1000).toLong())
|
||||
api.server.scheduleResetTrackersMounting(RESET_SOURCE_NAME, ((req.delay() ?: resetsConfig.mountingResetDelay) * 1000).toLong())
|
||||
} else {
|
||||
api.server.scheduleResetTrackersMounting(RESET_SOURCE_NAME, (delay * 1000).toLong(), bodyParts.toList())
|
||||
api.server.scheduleResetTrackersMounting(RESET_SOURCE_NAME, ((req.delay() ?: resetsConfig.mountingResetDelay) * 1000).toLong(), bodyParts.toList())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,8 @@ class Tracker @JvmOverloads constructor(
|
||||
var packetsReceived: Int? = null
|
||||
var packetsLost: Int? = null
|
||||
var packetLoss: Float? = null
|
||||
var windowsHit: Int? = null
|
||||
var windowsMiss: Int? = null
|
||||
var customName: String? = null
|
||||
var magStatus: MagnetometerStatus = magStatus
|
||||
private set
|
||||
|
||||
@@ -321,6 +321,8 @@ class HIDCommon {
|
||||
tracker.packetsReceived = packets_received
|
||||
tracker.packetsLost = packets_lost
|
||||
tracker.packetLoss = if (packets_lost == 0) 0.0f else packets_lost.toFloat() / (packets_received + packets_lost).toFloat()
|
||||
tracker.windowsHit = windows_hit
|
||||
tracker.windowsMiss = windows_missed
|
||||
}
|
||||
|
||||
// Assign rotation and acceleration
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package dev.slimevr.util
|
||||
|
||||
import dev.slimevr.SLIMEVR_IDENTIFIER
|
||||
import dev.slimevr.VRServer
|
||||
import dev.slimevr.tracking.trackers.Tracker
|
||||
import io.eiren.util.OperatingSystem
|
||||
import io.eiren.util.Util
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.function.Consumer
|
||||
|
||||
class TrackerLogger : Consumer<Tracker> {
|
||||
|
||||
var saveLogs = true
|
||||
private var file: FileWriter? = null
|
||||
private var lastSaved = 0L
|
||||
val saveInterval = 100
|
||||
|
||||
fun update() {
|
||||
if (saveLogs) {
|
||||
if (file == null) {
|
||||
val dir = OperatingSystem.resolveLogDirectory(SLIMEVR_IDENTIFIER)
|
||||
?.toFile()?.absoluteFile
|
||||
?: File("").absoluteFile
|
||||
file = FileWriter(
|
||||
File(
|
||||
dir,
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss").format(
|
||||
LocalDateTime.now()
|
||||
) + ".csv"
|
||||
)
|
||||
)
|
||||
writeCSVHeader()
|
||||
}
|
||||
if(lastSaved + saveInterval < System.currentTimeMillis() && VRServer.instance.allTrackers.isNotEmpty()) {
|
||||
lastSaved = System.currentTimeMillis()
|
||||
var isFirst = true
|
||||
VRServer.instance.allTrackers.forEach {
|
||||
if(it.isInternal || it.isComputed)
|
||||
return@forEach
|
||||
if (!isFirst) {
|
||||
file!!.write(';'.code)
|
||||
}
|
||||
isFirst = false
|
||||
file!!.write(
|
||||
"\"" + it.position.x + "\";"
|
||||
+ "\"" + it.position.y + "\";"
|
||||
+ "\"" + it.position.z + "\";"
|
||||
+ "\"" + it.getRotation().w + "\";"
|
||||
+ "\"" + it.getRotation().x + "\";"
|
||||
+ "\"" + it.getRotation().y + "\";"
|
||||
+ "\"" + it.getRotation().z + "\";"
|
||||
+ "\"" + it.getAcceleration().x + "\";"
|
||||
+ "\"" + it.getAcceleration().y + "\";"
|
||||
+ "\"" + it.getAcceleration().z + "\";"
|
||||
+ "\"" + it.getMagVector().x + "\";"
|
||||
+ "\"" + it.getMagVector().y + "\";"
|
||||
+ "\"" + it.getMagVector().z + "\";"
|
||||
+ "\"" + it.batteryVoltage + "\";"
|
||||
+ "\"" + it.batteryLevel + "\";"
|
||||
+ "\"" + it.signalStrength + "\";"
|
||||
+ "\"" + it.ping + "\";"
|
||||
+ "\"" + it.packetsReceived + "\";"
|
||||
+ "\"" + it.packetsLost + "\";"
|
||||
+ "\"" + it.packetLoss + "\";"
|
||||
+ "\"" + it.windowsHit + "\";"
|
||||
+ "\"" + it.windowsMiss + "\""
|
||||
)
|
||||
}
|
||||
file!!.write('\n'.code)
|
||||
file!!.flush()
|
||||
}
|
||||
} else {
|
||||
if(file != null) {
|
||||
saveCSVAndClose()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveCSVAndClose() {
|
||||
if (file != null) {
|
||||
Util.close(file)
|
||||
file = null
|
||||
}
|
||||
}
|
||||
|
||||
private fun writeCSVHeader() {
|
||||
var isFirst = true
|
||||
VRServer.instance.allTrackers.forEach {
|
||||
if(it.isInternal || it.isComputed)
|
||||
return@forEach
|
||||
if (!isFirst) {
|
||||
file!!.write(';'.code)
|
||||
}
|
||||
isFirst = false
|
||||
val tid = it.id.toString()
|
||||
file!!.write("\"" + tid + "_x\";"
|
||||
+ "\"" + tid + "_y\";"
|
||||
+ "\"" + tid + "_z\";"
|
||||
+ "\"" + tid + "_qw\";"
|
||||
+ "\"" + tid + "_qx\";"
|
||||
+ "\"" + tid + "_qy\";"
|
||||
+ "\"" + tid + "_qz\";"
|
||||
+ "\"" + tid + "_ax\";"
|
||||
+ "\"" + tid + "_ay\";"
|
||||
+ "\"" + tid + "_az\";"
|
||||
+ "\"" + tid + "_mx\";"
|
||||
+ "\"" + tid + "_my\";"
|
||||
+ "\"" + tid + "_mz\";"
|
||||
+ "\"" + tid + "_batv\";"
|
||||
+ "\"" + tid + "_batp\";"
|
||||
+ "\"" + tid + "_signal\";"
|
||||
+ "\"" + tid + "_ping\";"
|
||||
+ "\"" + tid + "_p_rcv\";"
|
||||
+ "\"" + tid + "_p_lost\";"
|
||||
+ "\"" + tid + "_p_loss\";"
|
||||
+ "\"" + tid + "_w_hit\";"
|
||||
+ "\"" + tid + "_w_miss\"")
|
||||
}
|
||||
file!!.write('\n'.code)
|
||||
}
|
||||
|
||||
override fun accept(t: Tracker) {
|
||||
// New tracker appeared, re-create the log file
|
||||
saveCSVAndClose()
|
||||
}
|
||||
}
|
||||
@@ -190,7 +190,7 @@ class DesktopSerialHandler :
|
||||
}
|
||||
|
||||
override fun write(buff: ByteArray) {
|
||||
LogManager.info("[SerialHandler] WRITING ${buff.toString(Charsets.UTF_8)}")
|
||||
LogManager.info("[SerialHandler] WRITING $buff")
|
||||
currentPort?.outputStream?.write(buff)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user