public boolean write(AdbDevice device) { synchronized (device) { UsbRequest request = device.getOutRequest(); request.setClientData(this); if (request.queue(mMessageBuffer, 24)) { int length = getDataLength(); if (length > 0) { request = device.getOutRequest(); request.setClientData(this); if (request.queue(mDataBuffer, length)) { return true; } else { device.releaseOutRequest(request); return false; } } return true; } else { device.releaseOutRequest(request); return false; } } }
@Override public boolean open() { boolean ret = openPL2303(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public boolean open() { boolean ret = openCDC(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public boolean open() { boolean ret = openFTDI(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public boolean open() { boolean ret = openCH34X(); if(ret) { setBaudRate(DEFAULT_BAUDRATE); // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public synchronized int bulkOutTransfer(ByteBuffer src) throws IOException { int length = src.remaining(); int oldPosition = src.position(); // workaround: UsbRequest.queue always reads at position 0 :/ workaroundBuffer.clear(); workaroundBuffer.put(src); if (!outRequest.queue(workaroundBuffer, length)) { throw new IOException("Error queueing request."); } UsbRequest request = deviceConnection.requestWait(); if (request == outRequest) { src.position(oldPosition + workaroundBuffer.position()); return workaroundBuffer.position(); } throw new IOException("requestWait failed! Request: " + request); }
@Override public synchronized int bulkInTransfer(ByteBuffer dest) throws IOException { int length = dest.remaining(); // workaround: UsbRequest.queue always writes at position 0 :/ workaroundBuffer.clear(); workaroundBuffer.limit(length); if (!inRequest.queue(workaroundBuffer, length)) { throw new IOException("Error queueing request."); } UsbRequest request = deviceConnection.requestWait(); if (request == inRequest) { workaroundBuffer.flip(); dest.put(workaroundBuffer); return workaroundBuffer.limit(); } throw new IOException("requestWait failed! Request: " + request); }
/** * Constructs a UsbMidiDeviceAndroid. * @param manager * @param device The USB device which this object is assocated with. */ UsbMidiDeviceAndroid(UsbManager manager, UsbDevice device) { mConnection = manager.openDevice(device); mEndpointMap = new HashMap<Integer, UsbEndpoint>(); mRequestMap = new HashMap<UsbEndpoint, UsbRequest>(); for (int i = 0; i < device.getInterfaceCount(); ++i) { UsbInterface iface = device.getInterface(i); if (iface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO || iface.getInterfaceSubclass() != MIDI_SUBCLASS) { continue; } mConnection.claimInterface(iface, true); for (int j = 0; j < iface.getEndpointCount(); ++j) { UsbEndpoint endpoint = iface.getEndpoint(j); if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) { mEndpointMap.put(endpoint.getEndpointNumber(), endpoint); } } } }
public UsbRequest getOutRequest() { synchronized(mOutRequestPool) { if (mOutRequestPool.isEmpty()) { UsbRequest request = new UsbRequest(); request.initialize(mDeviceConnection, mEndpointOut); return request; } else { return mOutRequestPool.removeFirst(); } } }
public UsbRequest getInRequest() { synchronized(mInRequestPool) { if (mInRequestPool.isEmpty()) { UsbRequest request = new UsbRequest(); request.initialize(mDeviceConnection, mEndpointIn); return request; } else { return mInRequestPool.removeFirst(); } } }
@Override public boolean open() { initSerialBuffer(); boolean ret = openCH34X(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Create Flow control thread but it will only be started if necessary createFlowControlThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public void run() { while(working.get()) { UsbRequest request = connection.requestWait(); if(request != null && request.getEndpoint().getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && request.getEndpoint().getDirection() == UsbConstants.USB_DIR_IN) { byte[] data = serialBuffer.getDataReceived(); // FTDI devices reserves two first bytes of an IN endpoint with info about // modem and Line. if(isFTDIDevice()) { ((FTDISerialDevice) usbSerialDevice).ftdiUtilities.checkModemStatus(data); //Check the Modem status serialBuffer.clearReadBuffer(); if(data.length > 2) { data = ((FTDISerialDevice) usbSerialDevice).ftdiUtilities.adaptArray(data); onReceivedData(data); } }else { // Clear buffer, execute the callback serialBuffer.clearReadBuffer(); onReceivedData(data); } // Queue a new request requestIN.queue(serialBuffer.getReadBuffer(), serialBuffer.getReadBufferSize()); } } }
protected void setThreadsParams(UsbRequest request, UsbEndpoint endpoint) { if(mr1Version) { workerThread.setUsbRequest(request); writeThread.setUsbEndpoint(endpoint); }else { readThread.setUsbEndpoint(request.getEndpoint()); writeThread.setUsbEndpoint(endpoint); } }
@Override public boolean open() { initSerialBuffer(); boolean ret = openCP2102(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Create Flow control thread but it will only be started if necessary createFlowControlThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public boolean open() { initSerialBuffer(); boolean ret = openPL2303(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public boolean open() { initSerialBuffer(); boolean ret = openCDC(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public boolean open() { initSerialBuffer(); boolean ret = openFTDI(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public boolean open() { boolean ret = openCH34X(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Create Flow control thread but it will only be started if necessary createFlowControlThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
@Override public void run() { while(working.get()) { UsbRequest request = connection.requestWait(); if(request != null && request.getEndpoint().getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && request.getEndpoint().getDirection() == UsbConstants.USB_DIR_IN) { byte[] data = serialBuffer.getDataReceived(); // FTDI devices reserves two first bytes of an IN endpoint with info about // modem and Line. if(isFTDIDevice()) { ((FTDISerialDevice) usbSerialDevice).ftdiUtilities.checkModemStatus(data); //Check the Modem status serialBuffer.clearReadBuffer(); if(data.length > 2) { data = ((FTDISerialDevice) usbSerialDevice).ftdiUtilities.adaptArray(data); onReceivedData(data); } }else { // Clear buffer, execute the callback serialBuffer.clearReadBuffer(); onReceivedData(data); } // Queue a new request requestIN.queue(serialBuffer.getReadBuffer(), SerialBuffer.DEFAULT_READ_BUFFER_SIZE); } } }
@Override public boolean open() { boolean ret = openCP2102(); if(ret) { // Initialize UsbRequest requestIN = new UsbRequest(); requestIN.initialize(connection, inEndpoint); // Restart the working thread if it has been killed before and get and claim interface restartWorkingThread(); restartWriteThread(); // Create Flow control thread but it will only be started if necessary createFlowControlThread(); // Pass references to the threads setThreadsParams(requestIN, outEndpoint); asyncMode = true; return true; }else { return false; } }
public static SubjectFactory<UsbRequestSubject, UsbRequest> type() { return new SubjectFactory<UsbRequestSubject, UsbRequest>() { @Override public UsbRequestSubject getSubject(FailureStrategy fs, UsbRequest that) { return new UsbRequestSubject(fs, that); } }; }