mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-30 22:02:45 -08:00
Move serial communication to desktop subproject (#756)
Co-authored-by: Erimel <[email protected]>
This commit is contained in:
1 parent
414482c139
commit
f84efc413d
12 files changed
+355
-346
No files matched your search
@@ -9,6 +9,7 @@ import dev.slimevr.bridge.ISteamVRBridge
|
||||
import dev.slimevr.desktop.platform.SteamVRBridge
|
||||
import dev.slimevr.desktop.platform.linux.UnixSocketBridge
|
||||
import dev.slimevr.desktop.platform.windows.WindowsNamedPipeBridge
|
||||
import dev.slimevr.desktop.serial.DesktopSerialHandler
|
||||
import dev.slimevr.tracking.trackers.Tracker
|
||||
import io.eiren.util.OperatingSystem
|
||||
import io.eiren.util.collections.FastList
|
||||
@@ -116,7 +117,12 @@ fun main(args: Array<String>) {
|
||||
try {
|
||||
val configDir = resolveConfig()
|
||||
LogManager.info("Using config dir: $configDir")
|
||||
val vrServer = VRServer(::provideSteamVRBridge, ::provideFeederBridge, configDir)
|
||||
val vrServer = VRServer(
|
||||
::provideSteamVRBridge,
|
||||
::provideFeederBridge,
|
||||
{ _ -> DesktopSerialHandler() },
|
||||
configDir
|
||||
)
|
||||
vrServer.start()
|
||||
Keybinding(vrServer)
|
||||
val scanner = thread {
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
package dev.slimevr.desktop.serial
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort
|
||||
import com.fazecast.jSerialComm.SerialPortEvent
|
||||
import com.fazecast.jSerialComm.SerialPortMessageListener
|
||||
import dev.slimevr.serial.SerialHandler
|
||||
import dev.slimevr.serial.SerialListener
|
||||
import io.eiren.util.logging.LogManager
|
||||
import java.io.IOException
|
||||
import java.io.OutputStreamWriter
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.util.*
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import java.util.stream.Stream
|
||||
import kotlin.concurrent.timerTask
|
||||
import kotlin.streams.asSequence
|
||||
import kotlin.streams.asStream
|
||||
import dev.slimevr.serial.SerialPort as SlimeSerialPort
|
||||
|
||||
class SerialPortWrapper(val port: SerialPort) : SlimeSerialPort() {
|
||||
override val portLocation: String
|
||||
get() = port.portLocation
|
||||
override val descriptivePortName: String
|
||||
get() = port.descriptivePortName
|
||||
|
||||
override val vendorId: Int
|
||||
get() = port.vendorID
|
||||
|
||||
override val productId: Int
|
||||
get() = port.productID
|
||||
}
|
||||
|
||||
class DesktopSerialHandler : SerialHandler(), SerialPortMessageListener {
|
||||
private val listeners: MutableList<SerialListener> = CopyOnWriteArrayList()
|
||||
private val getDevicesTimer = Timer("GetDevicesTimer")
|
||||
private var currentPort: SerialPort? = null
|
||||
private var watchingNewDevices = false
|
||||
private var lastKnownPorts = setOf<SerialPortWrapper>()
|
||||
|
||||
init {
|
||||
startWatchingNewDevices()
|
||||
}
|
||||
|
||||
fun startWatchingNewDevices() {
|
||||
if (watchingNewDevices) return
|
||||
watchingNewDevices = true
|
||||
getDevicesTimer.scheduleAtFixedRate(
|
||||
timerTask {
|
||||
try {
|
||||
detectNewPorts()
|
||||
} catch (t: Throwable) {
|
||||
LogManager.severe(
|
||||
"[SerialHandler] Error while watching for new devices, cancelling the \"getDevicesTimer\".",
|
||||
t
|
||||
)
|
||||
getDevicesTimer.cancel()
|
||||
}
|
||||
},
|
||||
0,
|
||||
3000
|
||||
)
|
||||
}
|
||||
|
||||
fun stopWatchingNewDevices() {
|
||||
if (!watchingNewDevices) return
|
||||
watchingNewDevices = false
|
||||
getDevicesTimer.cancel()
|
||||
getDevicesTimer.purge()
|
||||
}
|
||||
|
||||
fun onNewDevice(port: SerialPort) {
|
||||
listeners.forEach { it.onNewSerialDevice(SerialPortWrapper(port)) }
|
||||
}
|
||||
|
||||
override fun addListener(channel: SerialListener) {
|
||||
listeners.add(channel)
|
||||
}
|
||||
|
||||
override fun removeListener(channel: SerialListener) {
|
||||
listeners.removeIf { channel === it }
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun openSerial(portLocation: String?, auto: Boolean): Boolean {
|
||||
LogManager.info("[SerialHandler] Trying to open: $portLocation, auto: $auto")
|
||||
val ports = SerialPort.getCommPorts()
|
||||
lastKnownPorts = ports.map { SerialPortWrapper(it) }.toSet()
|
||||
val newPort: SerialPort? = ports.find {
|
||||
(!auto && it.portLocation == portLocation) ||
|
||||
(auto && isKnownBoard(SerialPortWrapper(it)))
|
||||
}
|
||||
if (newPort == null) {
|
||||
LogManager.info(
|
||||
"[SerialHandler] No serial ports found to connect to (${ports.size}) total ports"
|
||||
)
|
||||
return false
|
||||
}
|
||||
if (isConnected) {
|
||||
if (SerialPortWrapper(newPort) != currentPort?.let { SerialPortWrapper(it) }) {
|
||||
LogManager.info(
|
||||
"[SerialHandler] Closing current serial port " +
|
||||
currentPort!!.descriptivePortName
|
||||
)
|
||||
currentPort!!.removeDataListener()
|
||||
currentPort!!.closePort()
|
||||
} else {
|
||||
LogManager.info("[SerialHandler] Reusing already open port")
|
||||
listeners.forEach { it.onSerialConnected(SerialPortWrapper(currentPort!!)) }
|
||||
return true
|
||||
}
|
||||
}
|
||||
currentPort = newPort
|
||||
LogManager.info(
|
||||
"[SerialHandler] Trying to connect to new serial port " +
|
||||
currentPort!!.descriptivePortName
|
||||
)
|
||||
currentPort?.setBaudRate(115200)
|
||||
currentPort?.clearRTS()
|
||||
currentPort?.clearDTR()
|
||||
if (currentPort?.openPort(1000) == false) {
|
||||
LogManager.warning(
|
||||
"[SerialHandler] Can't open serial port ${currentPort?.descriptivePortName}, last error: ${currentPort?.lastErrorCode}"
|
||||
|
||||
)
|
||||
currentPort = null
|
||||
return false
|
||||
}
|
||||
currentPort?.addDataListener(this)
|
||||
listeners.forEach { it.onSerialConnected(SerialPortWrapper(currentPort!!)) }
|
||||
LogManager.info("[SerialHandler] Serial port ${newPort.descriptivePortName} is open")
|
||||
return true
|
||||
}
|
||||
|
||||
override fun rebootRequest() {
|
||||
writeSerial("REBOOT")
|
||||
}
|
||||
|
||||
override fun factoryResetRequest() {
|
||||
writeSerial("FRST")
|
||||
}
|
||||
|
||||
override fun infoRequest() {
|
||||
writeSerial("GET INFO")
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun closeSerial() {
|
||||
try {
|
||||
currentPort?.closePort()
|
||||
listeners.forEach { it.onSerialDisconnected() }
|
||||
LogManager.info(
|
||||
"[SerialHandler] Port ${currentPort?.descriptivePortName} closed okay"
|
||||
)
|
||||
currentPort = null
|
||||
} catch (e: Exception) {
|
||||
LogManager.warning(
|
||||
"[SerialHandler] Error closing port ${currentPort?.descriptivePortName}",
|
||||
e
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun writeSerial(serialText: String) {
|
||||
val os = currentPort?.outputStream ?: return
|
||||
val writer = OutputStreamWriter(os)
|
||||
try {
|
||||
writer.append(serialText).append("\n")
|
||||
writer.flush()
|
||||
addLog("-> $serialText\n")
|
||||
} catch (e: IOException) {
|
||||
addLog("[!] Serial error: ${e.message}\n")
|
||||
LogManager.warning("[SerialHandler] Serial port write error", e)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun setWifi(ssid: String, passwd: String) {
|
||||
val os = currentPort?.outputStream ?: return
|
||||
val writer = OutputStreamWriter(os)
|
||||
try {
|
||||
writer.append("SET WIFI \"").append(ssid).append("\" \"").append(passwd).append("\"\n")
|
||||
writer.flush()
|
||||
addLog("-> SET WIFI \"$ssid\" \"${passwd.replace(".".toRegex(), "*")}\"\n")
|
||||
} catch (e: IOException) {
|
||||
addLog("$e\n")
|
||||
LogManager.warning("[SerialHandler] Serial port write error", e)
|
||||
}
|
||||
}
|
||||
|
||||
fun addLog(str: String) {
|
||||
LogManager.info("[Serial] $str")
|
||||
listeners.forEach { it.onSerialLog(str) }
|
||||
}
|
||||
|
||||
override fun getListeningEvents(): Int {
|
||||
return (
|
||||
SerialPort.LISTENING_EVENT_PORT_DISCONNECTED
|
||||
or SerialPort.LISTENING_EVENT_DATA_RECEIVED
|
||||
)
|
||||
}
|
||||
|
||||
override fun serialEvent(event: SerialPortEvent) {
|
||||
when (event.eventType) {
|
||||
SerialPort.LISTENING_EVENT_DATA_RECEIVED -> {
|
||||
val newData = event.receivedData
|
||||
val s = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(newData)).toString()
|
||||
addLog(s)
|
||||
}
|
||||
SerialPort.LISTENING_EVENT_PORT_DISCONNECTED -> {
|
||||
closeSerial()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@get:Synchronized
|
||||
override val isConnected: Boolean
|
||||
get() = currentPort?.isOpen ?: false
|
||||
|
||||
override fun getMessageDelimiter(): ByteArray {
|
||||
return byteArrayOf(0x0A.toByte())
|
||||
}
|
||||
|
||||
override fun delimiterIndicatesEndOfMessage(): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
override val knownPorts: Stream<SerialPortWrapper>
|
||||
get() = SerialPort.getCommPorts()
|
||||
.asSequence()
|
||||
.map { SerialPortWrapper(it) }
|
||||
.filter { isKnownBoard(it) }
|
||||
.asStream()
|
||||
|
||||
private fun detectNewPorts() {
|
||||
try {
|
||||
val differences = knownPorts.asSequence() - lastKnownPorts
|
||||
lastKnownPorts = SerialPort.getCommPorts().map { SerialPortWrapper(it) }.toSet()
|
||||
differences.forEach { onNewDevice(it.port) }
|
||||
} catch (e: Throwable) {
|
||||
LogManager
|
||||
.severe("[SerialHandler] Using serial ports is not supported on this platform", e)
|
||||
throw RuntimeException("Serial unsupported")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user