Created
March 31, 2022 23:30
-
-
Save gavingt/d4e23727aa9c078211c88d881447ecea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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