Skip to content

Instantly share code, notes, and snippets.

@gavingt
Created March 31, 2022 23:30
Show Gist options
  • Save gavingt/d4e23727aa9c078211c88d881447ecea to your computer and use it in GitHub Desktop.
Save gavingt/d4e23727aa9c078211c88d881447ecea to your computer and use it in GitHub Desktop.
// Returns true if this UsbDevice has the necessary interface and endpoints to qualify as a UsbMassStorageDevice.
fun UsbDevice.isUsbMassStorageDevice(): Boolean {
(0 until this.interfaceCount).map { getInterface(it) }
.filter { it.interfaceClass == UsbConstants.USB_CLASS_MASS_STORAGE }
.map { usbInterface ->
logMessage("Tools", "Found usb interface: $usbInterface")
// Every mass storage device has exactly two endpoints
// One IN and one OUT endpoint
val endpointCount = usbInterface.endpointCount
if (endpointCount != 2) {
logMessage("Tools", "Interface endpoint count != 2")
}
var outEndpoint: UsbEndpoint? = null
var inEndpoint: UsbEndpoint? = null
for (j in 0 until endpointCount) {
val endpoint = usbInterface.getEndpoint(j)
logMessage("Tools", "Found usb endpoint: $endpoint")
if (endpoint.type == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (endpoint.direction == UsbConstants.USB_DIR_OUT) {
outEndpoint = endpoint
} else {
inEndpoint = endpoint
}
}
}
if (outEndpoint == null || inEndpoint == null) {
logMessage("Tools", "Not all needed endpoints found. In: ${outEndpoint != null}, Out: ${outEndpoint != null}")
return@map null
} else {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment