Skip to content

Instantly share code, notes, and snippets.

@Sardorbekcyber
Created September 24, 2021 12:52
Show Gist options
  • Save Sardorbekcyber/a6538a8a38029263acc244b5ec09054a to your computer and use it in GitHub Desktop.
Save Sardorbekcyber/a6538a8a38029263acc244b5ec09054a to your computer and use it in GitHub Desktop.
get endpoint by direction
class UsbDeviceHelper {
/**
* Returns first interface on USB Smart Card Device or throws an UsbInterfaceNotFound
* exception if device is not Smart Card or Interface does not exist
*
* @param usbDevice
* @return UsbEndpoint
* @throws UsbEndpointNotFound
*/
@Throws(UsbInterfaceNotFound::class)
fun getInterface(usbDevice: UsbDevice): UsbInterface {
val interfaceCount = usbDevice.interfaceCount
for (i in 0 until interfaceCount) {
val usbInterface = usbDevice.getInterface(i)
if (usbInterface.interfaceClass == UsbConstants.USB_CLASS_CSCID)
return usbInterface
}
throw UsbInterfaceNotFound()
}
@Throws(UsbEndpointNotFound::class)
fun getSendEndpoint(usbInterface: UsbInterface): UsbEndpoint {
val endpointsCount = usbInterface.endpointCount
for (i in 0 until endpointsCount) {
val endpoint = usbInterface.getEndpoint(i)
if (endpoint.type == UsbConstants.USB_ENDPOINT_XFER_BULK && endpoint.direction == UsbConstants.USB_DIR_OUT) {
return endpoint
}
}
throw UsbEndpointNotFound()
}
@Throws(UsbEndpointNotFound::class)
fun getReceiveEndpoint(usbInterface: UsbInterface): UsbEndpoint {
val endpointsCount = usbInterface.endpointCount
for (i in 0 until endpointsCount) {
val endpoint = usbInterface.getEndpoint(i)
if (endpoint.type == UsbConstants.USB_ENDPOINT_XFER_BULK && endpoint.direction == UsbConstants.USB_DIR_IN) {
return endpoint
}
}
throw UsbEndpointNotFound()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment