public void open() throws IOException { if (!device_found) { throw new IOException("Device not Connected"); } mConnection = mUsbManager.openDevice(mUsbDevice); Log.d(TAG, "Claiming interfaces, count=" + mUsbDevice.getInterfaceCount()); mConnection.controlTransfer(0x21, 0x22, 0x1, 0, null, 0, 0); mControlInterface = mUsbDevice.getInterface(0); Log.d(TAG, "Control interface=" + mControlInterface); if (!mConnection.claimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.getEndpoint(0); Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); Log.d(TAG, "Claiming data interface."); mDataInterface = mUsbDevice.getInterface(1); Log.d(TAG, "data interface=" + mDataInterface); if (!mConnection.claimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } mReadEndpoint = mDataInterface.getEndpoint(1); Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); mWriteEndpoint = mDataInterface.getEndpoint(0); Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); connected = true; setBaudRate(1000000); //Thread.sleep(1000); clear(); }
private boolean openCP2130() { if(connection.claimInterface(mInterface, true)) { Log.i(CLASS_ID, "Interface succesfully claimed"); }else { Log.i(CLASS_ID, "Interface could not be claimed"); return false; } // Assign endpoints int numberEndpoints = mInterface.getEndpointCount(); for(int i=0;i<=numberEndpoints-1;i++) { UsbEndpoint endpoint = mInterface.getEndpoint(i); if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && endpoint.getDirection() == UsbConstants.USB_DIR_IN) { inEndpoint = endpoint; }else { outEndpoint = endpoint; } } return true; }
UsbEndpoint endpoint = iface.getEndpoint(i); if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
@Override public int write(byte[] src, int timeoutMillis) throws IOException { final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1); int offset = 0; while (offset < src.length) { final int writeLength; final int amtWritten; synchronized (mWriteBufferLock) { final byte[] writeBuffer; writeLength = Math.min(src.length - offset, mWriteBuffer.length); if (offset == 0) { writeBuffer = src; } else { // bulkTransfer does not support offsets, make a copy. System.arraycopy(src, offset, mWriteBuffer, 0, writeLength); writeBuffer = mWriteBuffer; } amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength, timeoutMillis); } if (amtWritten <= 0) { throw new IOException("Error writing " + writeLength + " bytes at offset " + offset + " length=" + src.length); } Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength); offset += amtWritten; } return offset; }
private UsbMidiInterface asMidiInterface(UsbInterface iface) { // We really ought to check interface class and subclass, but we don't since a lot of MIDI // devices don't fully comply with the standard. // if (iface.getInterfaceClass() != 1 || iface.getInterfaceSubclass() != 3) return null; List<UsbMidiInput> inputs = new ArrayList<UsbMidiInput>(); List<UsbMidiOutput> outputs = new ArrayList<UsbMidiOutput>(); int epCount = iface.getEndpointCount(); for (int j = 0; j < epCount; ++j) { UsbEndpoint ep = iface.getEndpoint(j); // If the endpoint looks like a MIDI endpoint, assume that it is one. My reading of the USB // MIDI specification is that the max // packet size ought to be 0x40, but I've seen at least one interface (Focusrite Scarlett 2i4) // that reports a max packet size // of 0x200. So, we'll just check the minimum requirement for the rest of this class to work, // i.e., the max packet size must be // a positive multiple of 4. if ((ep.getType() & UsbConstants.USB_ENDPOINT_XFERTYPE_MASK) == UsbConstants.USB_ENDPOINT_XFER_BULK && (ep.getMaxPacketSize() & 0x03) == 0 && ep.getMaxPacketSize() > 0) { if ((ep.getDirection() & UsbConstants.USB_ENDPOINT_DIR_MASK) == UsbConstants.USB_DIR_IN) { inputs.add(new UsbMidiInput(iface, ep)); } else { outputs.add(new UsbMidiOutput(iface, ep)); } } } return (inputs.isEmpty() && outputs.isEmpty()) ? null : new UsbMidiInterface(iface, inputs, outputs); }
@Override public int write(byte[] src, int timeoutMillis) throws IOException { final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1); int offset = 0; while (offset < src.length) { final int writeLength; final int amtWritten; synchronized (mWriteBufferLock) { final byte[] writeBuffer; writeLength = Math.min(src.length - offset, mWriteBuffer.length); if (offset == 0) { writeBuffer = src; } else { // bulkTransfer does not support offsets, make a copy. System.arraycopy(src, offset, mWriteBuffer, 0, writeLength); writeBuffer = mWriteBuffer; } amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength, timeoutMillis); } if (amtWritten <= 0) { throw new IOException("Error writing " + writeLength + " bytes at offset " + offset + " length=" + src.length); } Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength); offset += amtWritten; } return offset; }
private boolean openCH34X() { if(connection.claimInterface(mInterface, true)) { Log.i(CLASS_ID, "Interface succesfully claimed"); }else { Log.i(CLASS_ID, "Interface could not be claimed"); return false; } // Assign endpoints int numberEndpoints = mInterface.getEndpointCount(); for(int i=0;i<=numberEndpoints-1;i++) { UsbEndpoint endpoint = mInterface.getEndpoint(i); if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && endpoint.getDirection() == UsbConstants.USB_DIR_IN) { inEndpoint = endpoint; }else if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && endpoint.getDirection() == UsbConstants.USB_DIR_OUT) { outEndpoint = endpoint; } } return init() == 0; }
private void openInterface() throws IOException { Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount()); mControlInterface = mDevice.getInterface(0); Log.d(TAG, "Control iface=" + mControlInterface); // class should be USB_CLASS_COMM if (!mConnection.claimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.getEndpoint(0); Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); Log.d(TAG, "Claiming data interface."); mDataInterface = mDevice.getInterface(1); Log.d(TAG, "data iface=" + mDataInterface); // class should be USB_CLASS_CDC_DATA if (!mConnection.claimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } mReadEndpoint = mDataInterface.getEndpoint(1); Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); mWriteEndpoint = mDataInterface.getEndpoint(0); Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); }
if (usbInterface.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (usbInterface.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN) receiveEndPoint = usbInterface.getEndpoint(i); else sendEndPoint = usbInterface.getEndpoint(i);
private void openInterface() throws IOException { Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount()); mControlInterface = mDevice.getInterface(0); Log.d(TAG, "Control iface=" + mControlInterface); // class should be USB_CLASS_COMM if (!mConnection.claimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.getEndpoint(0); Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); Log.d(TAG, "Claiming data interface."); mDataInterface = mDevice.getInterface(1); Log.d(TAG, "data iface=" + mDataInterface); // class should be USB_CLASS_CDC_DATA if (!mConnection.claimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } mReadEndpoint = mDataInterface.getEndpoint(1); Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); mWriteEndpoint = mDataInterface.getEndpoint(0); Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); }
for(int i=0;i<=numberEndpoints-1;i++) UsbEndpoint endpoint = mInterface.getEndpoint(i); if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && endpoint.getDirection() == UsbConstants.USB_DIR_IN)
@Override public int write(byte[] src, int timeoutMillis) throws IOException { final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1); int offset = 0; while (offset < src.length) { final int writeLength; final int amtWritten; synchronized (mWriteBufferLock) { final byte[] writeBuffer; writeLength = Math.min(src.length - offset, mWriteBuffer.length); if (offset == 0) { writeBuffer = src; } else { // bulkTransfer does not support offsets, make a copy. System.arraycopy(src, offset, mWriteBuffer, 0, writeLength); writeBuffer = mWriteBuffer; } amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength, timeoutMillis); } if (amtWritten <= 0) { throw new IOException("Error writing " + writeLength + " bytes at offset " + offset + " length=" + src.length); } Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength); offset += amtWritten; } return offset; }
for(int i=0;i<=numberEndpoints-1;i++) UsbEndpoint endpoint = mInterface.getEndpoint(i); if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && endpoint.getDirection() == UsbConstants.USB_DIR_IN)
@Override public int write(byte[] src, int timeoutMillis) throws IOException { final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1); int offset = 0; while (offset < src.length) { final int writeLength; final int amtWritten; synchronized (mWriteBufferLock) { final byte[] writeBuffer; writeLength = Math.min(src.length - offset, mWriteBuffer.length); if (offset == 0) { writeBuffer = src; } else { // bulkTransfer does not support offsets, make a copy. System.arraycopy(src, offset, mWriteBuffer, 0, writeLength); writeBuffer = mWriteBuffer; } amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength, timeoutMillis); } if (amtWritten <= 0) { throw new IOException("Error writing " + writeLength + " bytes at offset " + offset + " length=" + src.length); } Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength); offset += amtWritten; } return offset; }
for(int i=0;i<=numberEndpoints-1;i++) UsbEndpoint endpoint = mInterface.getEndpoint(i); if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && endpoint.getDirection() == UsbConstants.USB_DIR_IN)
@Override public void onConnect() { // Serial mode only // TODO: find the interface and endpoint indexes no matter what mode it is int ifIdx = 1; int epInIdx = 1; int epOutIdx = 0; UsbInterface iface = usbDevice.getInterface(ifIdx); if (usbConnection.claimInterface(iface, true)) { logger.log("Interface claimed successfully\n"); } else { logger.log("ERROR - can't claim interface\n"); return; } endpointIn = iface.getEndpoint(epInIdx); endpointOut = iface.getEndpoint(epOutIdx); super.onConnect(); }
usb_in[i] = usb_intf[i].getEndpoint(0); Log.info("Interface " + i + " endpoint count:" + usb_intf[i].getEndpointCount());
@Override public int read(byte[] dest, int timeoutMillis) throws IOException { final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(0);
@Override public int read(byte[] dest, int timeoutMillis) throws IOException { final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(0);
throw new IOException("Could not claim control interface."); mControlEndpoint = mControlInterface.getEndpoint(0); Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); throw new IOException("Could not claim data interface."); mReadEndpoint = mDataInterface.getEndpoint(1); Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); mWriteEndpoint = mDataInterface.getEndpoint(0); Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); if (mEnableAsyncReads) {