package expo.modules.usbserial import android.hardware.usb.UsbDevice // --------------------------------------------------------------------------- // Internal constants // --------------------------------------------------------------------------- internal const val LIBRARY_VERSION = "0.0.1" /** Read buffer size in bytes. 4 KB is large enough for most serial protocols. */ internal const val READ_BUFFER_SIZE = 4096 /** * Timeout for each blocking read() call in milliseconds. * Shorter values increase CPU usage; longer values increase latency. * 50 ms is a good balance for interactive serial communication. */ internal const val READ_TIMEOUT_MS = 50 // --------------------------------------------------------------------------- // Shared utility functions — internal so unit tests can access without reflection // --------------------------------------------------------------------------- /** * Builds the typed result map for a discovered USB device. * Optional string descriptor fields (manufacturer, product, serial) are only * included when the USB device reports them (some devices leave them null). */ internal fun buildDeviceMap( device: UsbDevice, driverName: String, portCount: Int, ): Map { val normalizedDriverName = when (driverName) { "FtdiSerialDriver" -> "FtdiSerial" "CdcAcmSerialDriver" -> "CdcAcmSerial" "Ch34xSerialDriver" -> "Ch34xSerial" "Cp21xxSerialDriver" -> "Cp21xxSerial" "ProlificSerialDriver" -> "ProlificSerial" else -> driverName.removeSuffix("Driver") } val map = mutableMapOf( "deviceId" to device.deviceId, "vendorId" to device.vendorId, "productId" to device.productId, "driverName" to normalizedDriverName, "portCount" to portCount, ) device.manufacturerName?.let { map["manufacturerName"] = it } device.productName?.let { map["productName"] = it } device.serialNumber?.let { map["serialNumber"] = it } return map }