feat: Desktop USB serial transport (#4836)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich 2026-03-18 07:42:24 -05:00 committed by GitHub
parent 06c990026f
commit 59408ef46e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 457 additions and 37 deletions

View file

@ -34,13 +34,15 @@ class CommonGetDiscoveredDevicesUseCase(
private val recentAddressesDataSource: RecentAddressesDataSource,
private val nodeRepository: NodeRepository,
private val databaseManager: DatabaseManager,
private val usbScanner: UsbScanner? = null,
) : GetDiscoveredDevicesUseCase {
private val suffixLength = 4
override fun invoke(showMock: Boolean): Flow<DiscoveredDevices> {
val nodeDb = nodeRepository.nodeDBbyNum
val usbFlow = usbScanner?.scanUsbDevices() ?: kotlinx.coroutines.flow.flowOf(emptyList())
return combine(nodeDb, recentAddressesDataSource.recentAddresses) { db, recentList ->
return combine(nodeDb, recentAddressesDataSource.recentAddresses, usbFlow) { db, recentList, usbList ->
val recentTcpForUi =
recentList
.map { DeviceListEntry.Tcp(it.name, it.address) }
@ -63,12 +65,14 @@ class CommonGetDiscoveredDevicesUseCase(
DiscoveredDevices(
recentTcpDevices = recentTcpForUi,
usbDevices =
if (showMock) {
val demoModeLabel = runCatching { getString(Res.string.demo_mode) }.getOrDefault("Demo Mode")
listOf(DeviceListEntry.Mock(demoModeLabel))
} else {
emptyList()
},
usbList +
if (showMock) {
val demoModeLabel =
runCatching { getString(Res.string.demo_mode) }.getOrDefault("Demo Mode")
listOf(DeviceListEntry.Mock(demoModeLabel))
} else {
emptyList()
},
)
}
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.feature.connections.domain.usecase
import kotlinx.coroutines.flow.Flow
import org.meshtastic.feature.connections.model.DeviceListEntry
/** Platform-specific scanner for USB/Serial devices. */
interface UsbScanner {
fun scanUsbDevices(): Flow<List<DeviceListEntry.Usb>>
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.feature.connections.domain.usecase
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.isActive
import org.koin.core.annotation.Single
import org.meshtastic.core.network.SerialTransport
import org.meshtastic.feature.connections.model.DeviceListEntry
import org.meshtastic.feature.connections.model.JvmUsbDeviceData
import kotlin.coroutines.coroutineContext
@Single
class JvmUsbScanner : UsbScanner {
override fun scanUsbDevices(): Flow<List<DeviceListEntry.Usb>> = flow {
while (coroutineContext.isActive) {
val ports =
SerialTransport.getAvailablePorts().map { portName ->
DeviceListEntry.Usb(
usbData = JvmUsbDeviceData(portName),
name = portName,
fullAddress = "s$portName",
bonded = true, // Desktop serial ports don't need Android USB permission bonding
node = null,
)
}
emit(ports)
delay(POLL_INTERVAL_MS)
}
}
.distinctUntilChanged()
companion object {
private const val POLL_INTERVAL_MS = 2000L
}
}

View file

@ -0,0 +1,20 @@
/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.feature.connections.model
/** JVM-specific implementation of [UsbDeviceData] wrapping the serial port name. */
data class JvmUsbDeviceData(val portName: String) : UsbDeviceData