mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 00:32:35 -08:00
Serial + Serial Flasher
This commit is contained in:
1 parent
853155600a
commit
8ffd00eb47
30 files changed
+1344
-329
No files matched your search
@@ -5,16 +5,20 @@ package dev.slimevr.desktop
|
||||
import dev.slimevr.VRServer
|
||||
import dev.slimevr.config.createAppConfig
|
||||
import dev.slimevr.desktop.ipc.createIpcServers
|
||||
import dev.slimevr.desktop.serial.createDesktopSerialServer
|
||||
import dev.slimevr.firmware.createFirmwareManager
|
||||
import dev.slimevr.resolveConfigDirectory
|
||||
import dev.slimevr.solarxr.createSolarXRWebsocketServer
|
||||
import dev.slimevr.tracker.udp.createUDPTrackerServer
|
||||
import dev.slimevr.resolveConfigDirectory
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
fun main(args: Array<String>) = runBlocking {
|
||||
val configFolder = resolveConfigDirectory() ?: error("Unable to resolve config folder")
|
||||
val config = createAppConfig(this, configFolder = configFolder.toFile())
|
||||
val server = VRServer.create(this)
|
||||
val serialServer = createDesktopSerialServer(this)
|
||||
val firmwareManager = createFirmwareManager(serialServer = serialServer, scope = this)
|
||||
val server = VRServer.create(this, serialServer, firmwareManager)
|
||||
|
||||
launch {
|
||||
createUDPTrackerServer(server, config)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package dev.slimevr.desktop.ipc
|
||||
|
||||
import dev.slimevr.VRServer
|
||||
import dev.slimevr.CURRENT_PLATFORM
|
||||
import dev.slimevr.Platform
|
||||
import dev.slimevr.VRServer
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@@ -21,11 +21,13 @@ suspend fun createIpcServers(server: VRServer) = coroutineScope {
|
||||
launch { createUnixFeederSocket(server) }
|
||||
launch { createUnixSolarXRSocket(server) }
|
||||
}
|
||||
|
||||
Platform.WINDOWS -> {
|
||||
launch { createWindowsDriverPipe(server) }
|
||||
launch { createWindowsFeederPipe(server) }
|
||||
launch { createWindowsSolarXRPipe(server) }
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
@@ -16,39 +16,38 @@ import java.nio.channels.ServerSocketChannel
|
||||
import java.nio.channels.SocketChannel
|
||||
import kotlin.io.path.Path
|
||||
|
||||
suspend fun createUnixDriverSocket(server: VRServer) =
|
||||
acceptUnixClients(DRIVER_SOCKET_NAME) { channel ->
|
||||
handleDriverConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
)
|
||||
}
|
||||
suspend fun createUnixDriverSocket(server: VRServer) = acceptUnixClients(DRIVER_SOCKET_NAME) { channel ->
|
||||
handleDriverConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun createUnixFeederSocket(server: VRServer) =
|
||||
acceptUnixClients(FEEDER_SOCKET_NAME) { channel ->
|
||||
handleFeederConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
)
|
||||
}
|
||||
suspend fun createUnixFeederSocket(server: VRServer) = acceptUnixClients(FEEDER_SOCKET_NAME) { channel ->
|
||||
handleFeederConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun createUnixSolarXRSocket(server: VRServer) =
|
||||
acceptUnixClients(SOLARXR_SOCKET_NAME) { channel ->
|
||||
handleSolarXRConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
)
|
||||
}
|
||||
suspend fun createUnixSolarXRSocket(server: VRServer) = acceptUnixClients(SOLARXR_SOCKET_NAME) { channel ->
|
||||
handleSolarXRConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
)
|
||||
}
|
||||
|
||||
private fun isSocketInUse(socketPath: String): Boolean = try {
|
||||
SocketChannel.open(StandardProtocolFamily.UNIX).use {
|
||||
it.connect(UnixDomainSocketAddress.of(socketPath))
|
||||
true
|
||||
}
|
||||
} catch (_: Exception) { false }
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
|
||||
// Length field is LE u32 and includes the 4-byte header itself
|
||||
private fun readFramedMessages(channel: SocketChannel) = flow {
|
||||
@@ -92,4 +91,4 @@ private suspend fun acceptUnixClients(
|
||||
launch { handle(client) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,32 +19,29 @@ import java.nio.ByteOrder
|
||||
private val k32 = Kernel32.INSTANCE
|
||||
private val adv32 = Advapi32.INSTANCE
|
||||
|
||||
suspend fun createWindowsDriverPipe(server: VRServer) =
|
||||
acceptWindowsClients(DRIVER_PIPE) { handle ->
|
||||
handleDriverConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(handle),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramedPipe(handle, bytes) } },
|
||||
)
|
||||
}
|
||||
suspend fun createWindowsDriverPipe(server: VRServer) = acceptWindowsClients(DRIVER_PIPE) { handle ->
|
||||
handleDriverConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(handle),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramedPipe(handle, bytes) } },
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun createWindowsFeederPipe(server: VRServer) =
|
||||
acceptWindowsClients(FEEDER_PIPE) { handle ->
|
||||
handleFeederConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(handle),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramedPipe(handle, bytes) } },
|
||||
)
|
||||
}
|
||||
suspend fun createWindowsFeederPipe(server: VRServer) = acceptWindowsClients(FEEDER_PIPE) { handle ->
|
||||
handleFeederConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(handle),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramedPipe(handle, bytes) } },
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun createWindowsSolarXRPipe(server: VRServer) =
|
||||
acceptWindowsClients(SOLARXR_PIPE) { handle ->
|
||||
handleSolarXRConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(handle),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramedPipe(handle, bytes) } },
|
||||
)
|
||||
}
|
||||
suspend fun createWindowsSolarXRPipe(server: VRServer) = acceptWindowsClients(SOLARXR_PIPE) { handle ->
|
||||
handleSolarXRConnection(
|
||||
server = server,
|
||||
messages = readFramedMessages(handle),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramedPipe(handle, bytes) } },
|
||||
)
|
||||
}
|
||||
|
||||
// Length field is LE u32 and includes the 4-byte header itself
|
||||
private fun readFramedMessages(handle: WinNT.HANDLE) = flow {
|
||||
@@ -92,7 +89,10 @@ private fun createSecurePipe(pipeName: String): WinNT.HANDLE {
|
||||
WinBase.PIPE_ACCESS_DUPLEX,
|
||||
WinBase.PIPE_TYPE_BYTE or WinBase.PIPE_READMODE_BYTE or WinBase.PIPE_WAIT,
|
||||
WinBase.PIPE_UNLIMITED_INSTANCES,
|
||||
65536, 65536, 0, attributes,
|
||||
65536,
|
||||
65536,
|
||||
0,
|
||||
attributes,
|
||||
)
|
||||
check(pipe != WinNT.INVALID_HANDLE_VALUE) {
|
||||
"CreateNamedPipe failed for $pipeName: ${k32.GetLastError()}"
|
||||
@@ -123,4 +123,4 @@ private suspend fun acceptWindowsClients(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package dev.slimevr.desktop.serial
|
||||
|
||||
import dev.slimevr.serial.FlashingHandler
|
||||
import com.fazecast.jSerialComm.SerialPort as JSerialPort
|
||||
|
||||
class DesktopFlashingHandler : FlashingHandler {
|
||||
private var port: JSerialPort? = null
|
||||
|
||||
override fun openSerial(portObj: Any) {
|
||||
val portLocation = portObj as? String ?: error("expected port location string")
|
||||
val comPort = JSerialPort.getCommPorts().find { it.portLocation == portLocation }
|
||||
?: error("Unable to find port $portLocation")
|
||||
if (comPort.isOpen) {
|
||||
comPort.closePort()
|
||||
}
|
||||
if (!comPort.openPort(1000)) {
|
||||
error("Unable to open port $portLocation")
|
||||
}
|
||||
this.port = comPort
|
||||
}
|
||||
|
||||
override fun closeSerial() {
|
||||
port?.closePort()
|
||||
port = null
|
||||
}
|
||||
|
||||
override fun setDTR(value: Boolean) {
|
||||
val p = port ?: error("no port to set DTR")
|
||||
if (value) p.setDTR() else p.clearDTR()
|
||||
}
|
||||
|
||||
override fun setRTS(value: Boolean) {
|
||||
val p = port ?: error("no port to set RTS")
|
||||
if (value) p.setRTS() else p.clearRTS()
|
||||
}
|
||||
|
||||
override fun write(data: ByteArray) {
|
||||
val p = port ?: error("no port to write")
|
||||
p.writeBytes(data, data.size)
|
||||
}
|
||||
|
||||
override fun read(length: Int): ByteArray {
|
||||
val p = port ?: error("no port to read")
|
||||
val data = ByteArray(length)
|
||||
p.readBytes(data, length)
|
||||
return data
|
||||
}
|
||||
|
||||
override fun changeBaud(baud: Int) {
|
||||
val p = port ?: error("no port to set baud")
|
||||
if (!p.setBaudRate(baud)) error("Unable to change baudrate to $baud")
|
||||
}
|
||||
|
||||
override fun setReadTimeout(timeout: Long) {
|
||||
val p = port ?: error("no port to set timeout")
|
||||
p.setComPortTimeouts(JSerialPort.TIMEOUT_READ_BLOCKING, timeout.toInt(), 0)
|
||||
}
|
||||
|
||||
override fun availableBytes(): Int {
|
||||
val p = port ?: error("no port to check available bytes")
|
||||
return p.bytesAvailable()
|
||||
}
|
||||
|
||||
override fun flushIOBuffers() {
|
||||
val p = port ?: error("no port to flush")
|
||||
p.flushIOBuffers()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package dev.slimevr.desktop.serial
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPortEvent
|
||||
import com.fazecast.jSerialComm.SerialPortMessageListener
|
||||
import dev.slimevr.AppLogger
|
||||
import dev.slimevr.serial.FlashingHandler
|
||||
import dev.slimevr.serial.SerialPortHandle
|
||||
import dev.slimevr.serial.SerialPortInfo
|
||||
import dev.slimevr.serial.SerialServer
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.OutputStreamWriter
|
||||
import com.fazecast.jSerialComm.SerialPort as JSerialPort
|
||||
|
||||
private val SUPPORTED_BOARDS: Set<Pair<Int, Int>> = setOf(
|
||||
Pair(0x1A86, 0x7522), // CH340
|
||||
Pair(0x1A86, 0x7523), // CH340
|
||||
Pair(0x1A86, 0x5523), // CH341
|
||||
Pair(0x1A86, 0x55D3), // CH343
|
||||
Pair(0x1A86, 0x55D4), // CH9102x
|
||||
Pair(0x10C4, 0xEA60), // CP210x
|
||||
Pair(0x303A, 0x1001), // ESP32-S3
|
||||
Pair(0x303A, 0x0002), // ESP32
|
||||
Pair(0x0403, 0x6001), // FTDI FT232
|
||||
)
|
||||
|
||||
private fun isKnownBoard(vid: Int, pid: Int) = SUPPORTED_BOARDS.contains(vid to pid)
|
||||
|
||||
private fun openPort(
|
||||
portLocation: String,
|
||||
scope: CoroutineScope,
|
||||
onDataReceived: suspend (String, String) -> Unit,
|
||||
onPortDisconnected: suspend (String) -> Unit,
|
||||
): SerialPortHandle? {
|
||||
val port = JSerialPort.getCommPorts().find { it.portLocation == portLocation } ?: return null
|
||||
port.baudRate = 115200
|
||||
port.clearRTS()
|
||||
port.clearDTR()
|
||||
if (!port.openPort(1000)) return null
|
||||
|
||||
// Anonymous object is required by the jSerialComm API
|
||||
port.addDataListener(object : SerialPortMessageListener {
|
||||
override fun getListeningEvents() = JSerialPort.LISTENING_EVENT_DATA_RECEIVED or JSerialPort.LISTENING_EVENT_PORT_DISCONNECTED
|
||||
|
||||
override fun getMessageDelimiter() = byteArrayOf(0x0A)
|
||||
override fun delimiterIndicatesEndOfMessage() = true
|
||||
|
||||
override fun serialEvent(event: SerialPortEvent) {
|
||||
when (event.eventType) {
|
||||
JSerialPort.LISTENING_EVENT_DATA_RECEIVED -> {
|
||||
val line = event.receivedData.toString(Charsets.UTF_8).trimEnd()
|
||||
scope.launch { onDataReceived(portLocation, line) }
|
||||
}
|
||||
|
||||
JSerialPort.LISTENING_EVENT_PORT_DISCONNECTED ->
|
||||
scope.launch { onPortDisconnected(portLocation) }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return SerialPortHandle(
|
||||
portLocation = portLocation,
|
||||
descriptivePortName = port.descriptivePortName,
|
||||
writeCommand = { text ->
|
||||
OutputStreamWriter(port.outputStream).append(text).append("\n").flush()
|
||||
},
|
||||
close = {
|
||||
port.removeDataListener()
|
||||
port.closePort()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun runSerialPoller(server: SerialServer) {
|
||||
var lastKnown: Set<String> = emptySet()
|
||||
|
||||
while (true) {
|
||||
val current = withContext(Dispatchers.IO) {
|
||||
JSerialPort.getCommPorts()
|
||||
.filter { isKnownBoard(it.vendorID, it.productID) }
|
||||
.associate { port ->
|
||||
port.portLocation to SerialPortInfo(
|
||||
portLocation = port.portLocation,
|
||||
descriptivePortName = port.descriptivePortName,
|
||||
vendorId = port.vendorID,
|
||||
productId = port.productID,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val added = current.keys - lastKnown
|
||||
val removed = lastKnown - current.keys
|
||||
|
||||
added.forEach { loc -> server.onPortDetected(current.getValue(loc)) }
|
||||
removed.forEach { loc -> server.onPortLost(loc) }
|
||||
|
||||
lastKnown = current.keys
|
||||
delay(3000)
|
||||
}
|
||||
}
|
||||
|
||||
fun createDesktopSerialServer(scope: CoroutineScope): SerialServer {
|
||||
val server = SerialServer.create(openPort = ::openPort, openFlashingPort = { DesktopFlashingHandler() }, scope = scope)
|
||||
scope.launch { runSerialPoller(server) }
|
||||
return server
|
||||
}
|
||||
Reference in new issue
Block a user