public AdbDevice(AdbTestActivity activity, UsbDeviceConnection connection, UsbInterface intf) { mActivity = activity; mDeviceConnection = connection; mSerial = connection.getSerial(); UsbEndpoint epOut = null; UsbEndpoint epIn = null; // look for our bulk endpoints for (int i = 0; i < intf.getEndpointCount(); i++) { UsbEndpoint ep = intf.getEndpoint(i); if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { epOut = ep; } else { epIn = ep; } } } if (epOut == null || epIn == null) { throw new IllegalArgumentException("not all endpoints found"); } mEndpointOut = epOut; mEndpointIn = epIn; }
private UsbHidDevice(UsbDevice usbDevice, UsbInterface usbInterface, UsbManager usbManager) { mUsbDevice = usbDevice; mUsbInterface = usbInterface; mUsbManager= usbManager; for (int i = 0; i < mUsbInterface.getEndpointCount(); i++) { UsbEndpoint endpoint = mUsbInterface.getEndpoint(i); int dir = endpoint.getDirection(); int type = endpoint.getType(); if (mInUsbEndpoint == null && dir == UsbConstants.USB_DIR_IN && type == UsbConstants.USB_ENDPOINT_XFER_INT) { mInUsbEndpoint = endpoint; } if (mOutUsbEndpoint == null && dir == UsbConstants.USB_DIR_OUT && type == UsbConstants.USB_ENDPOINT_XFER_INT) { mOutUsbEndpoint = endpoint; } } }
/** * constructor * * @param usbDevice * @param usbDeviceConnection * @param usbInterface * @param midiEventListener * @throws IllegalArgumentException endpoint not found. */ public MidiInputDevice(UsbDevice usbDevice, UsbDeviceConnection usbDeviceConnection, UsbInterface usbInterface, UsbEndpoint usbEndpoint, OnMidiInputEventListener midiEventListener) throws IllegalArgumentException { //this.usbDevice = usbDevice; this.usbDeviceConnection = usbDeviceConnection; this.usbInterface = usbInterface; this.midiEventListener = midiEventListener; waiterThread = new WaiterThread(); inputEndpoint = usbEndpoint; usbDeviceConnection.claimInterface(usbInterface, true); waiterThread.setPriority(8); waiterThread.setName("MidiInputDevice[" + usbDevice.getDeviceName() + "].WaiterThread"); waiterThread.start(); FragMentManager.getInstance().updateUSBConnection(true); }
private void startConnecting() { handleConnecting(); try { UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); deviceConnection = usbManager.openDevice(device); driver = UsbSerialDriverFactory.createDriver(device, deviceConnection); UsbEndpoint[] endpoints = driver.open(); readEndpoint = endpoints[0]; writeEndpoint = endpoints[1]; driver.setParameters(115200, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE); } catch (Exception e) { Log.e(LOG_TAG, "Failed to connect to USB device(" + device.getDeviceName() + "). cause=" + e.getMessage()); cleanUp(); handleFailedToConnect(e); return; } // Start a thread for reading readThread = new UsbReadThread(); readThread.start(); Log.i(LOG_TAG, "USB device is connected! connection=" + getDescription()); handleConnected(); }
@Override public boolean open() { iface = device.getInterface(0); Log.d(TAG, "Endpoint Count: " + iface.getEndpointCount()); UsbEndpoint ep0 = iface.getEndpoint(0); UsbEndpoint ep1 = iface.getEndpoint(1); if (ep0.getDirection() == UsbConstants.USB_DIR_IN) { in = ep0; out = ep1; } else { in = ep1; out = ep0; } con = manager.openDevice(device); return con != null; }
/** * Test method for {@link com.digi.xbee.api.connection.android.AndroidUSBInputStream#startReadThread()} */ @Test public void testStartReadThread() throws Exception { // Prepare the resources for the test. Whitebox.setInternalState(is, VARIABLE_READ_BUFFER, circularBuffer); // Call the method under test. is.startReadThread(); Thread.sleep(10); boolean working = (Boolean) Whitebox.getInternalState(is, VARIABLE_WORKING); // Perform the verifications. assertThat(working, is(equalTo(true))); Mockito.verify(usbConnection, Mockito.atLeast(1)).bulkTransfer(Mockito.any(UsbEndpoint.class), Mockito.any(byte[].class), Mockito.anyInt(), Mockito.anyInt()); is.stopReadThread(); }
public static UsbCommunication createUsbCommunication(UsbDeviceConnection deviceConnection, UsbEndpoint outEndpoint, UsbEndpoint inEndpoint) { UsbCommunication communication; if (underlyingUsbCommunication == UnderlyingUsbCommunication.DEVICE_CONNECTION_SYNC) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { communication = new JellyBeanMr2Communication(deviceConnection, outEndpoint, inEndpoint); } else { Log.i(TAG, "using workaround usb communication"); communication = new HoneyCombMr1Communication(deviceConnection, outEndpoint, inEndpoint); } } else { communication = new UsbRequestCommunication(deviceConnection, outEndpoint, inEndpoint); } return communication; }
public int write(final byte[] data, final int timeout) throws IOException { //TODO: score the real interface and endpoint final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1); int offset = 0; while (offset < data.length) { final int write_length; final int result; synchronized (mWriteBufferLock) { final byte[] writeBuffer; write_length = Math.min(data.length-offset, mWriteBuffer.length); if (offset == 0) { writeBuffer = data; } else { System.arraycopy(data, offset, mWriteBuffer, 0, write_length); writeBuffer = mWriteBuffer; } result = mConnection.bulkTransfer(endpoint, writeBuffer, write_length, timeout); } if (result <=0) { throw new IOException("Error writing " + write_length + " bytes at offset " + offset + " length="+data.length); } Log.d(TAG, "Wrote " + result + " bytes. Attempted=" + write_length); offset += result; } return offset; }
public ConnectedUsbDevice(UsbDeviceConnection connection, UsbInterface usbInterface) { this.connection = connection; this.usbInterface = usbInterface; initConnection(connection); int endPoints = usbInterface.getEndpointCount(); int interfaceProtocol = usbInterface.getInterfaceProtocol(); System.out.println("EndPoints: " + endPoints + " | interfaces: " + interfaceProtocol); out = usbInterface.getEndpoint(1); in = usbInterface.getEndpoint(2); for (int x = 0; x < endPoints; x++) { UsbEndpoint endpoint = usbInterface.getEndpoint(x); boolean bulk = endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK; boolean crtl = endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_CONTROL; boolean inDir = endpoint.getDirection() == UsbConstants.USB_DIR_IN; boolean outDir = endpoint.getDirection() == UsbConstants.USB_DIR_OUT; System.out.println("ID: " + x + " Bulk: " + bulk + " Ctrl: " + crtl + " Out: " + outDir + " In: " + inDir); } }
private boolean getCdcEndpoint() { UsbEndpoint ep; if (mInterface[0] == null) { return false; } for (int i = 0; i < 2; ++i) { ep = mInterface[0].getEndpoint(i); if (ep.getDirection() == UsbConstants.USB_DIR_IN) { mFTDIEndpointIN[0] = ep; } else { mFTDIEndpointOUT[0] = ep; } } if (mFTDIEndpointIN == null || mFTDIEndpointOUT == null) { return false; } return true; }
private boolean setFTDIEndpoints(UsbInterface[] intf, int portNum) { UsbEndpoint epIn; UsbEndpoint epOut; if (intf[0] == null) { return false; } for (int i = 0; i < portNum; ++i) { epIn = intf[i].getEndpoint(0); epOut = intf[i].getEndpoint(1); if (epIn != null && epOut != null) { mFTDIEndpointIN[i] = epIn; mFTDIEndpointOUT[i] = epOut; } else { return false; } } return true; }
/** * 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); } } } }
@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; }
public UsbHiSpeedBulk(UsbDeviceConnection usbDeviceConnection, UsbEndpoint usbEndpoint, int nrequests, int packetsPerRequests) { this.usbDeviceConnection = usbDeviceConnection; this.fileDescriptor = usbDeviceConnection.getFileDescriptor(); this.nrequests = nrequests; this.requests = new ArrayList<>(nrequests); this.packetSize = usbEndpoint.getMaxPacketSize(); this.usbEndpoint = usbEndpoint; this.packetsPerRequests = packetsPerRequests; this.buffer = new Buffer(packetsPerRequests * packetSize); }
public UsbBulkSource(UsbDeviceConnection usbDeviceConnection, UsbEndpoint usbEndpoint, AlternateUsbInterface usbInterface, int numRequests, int numPacketsPerReq) { this.usbDeviceConnection = usbDeviceConnection; this.usbEndpoint = usbEndpoint; this.usbInterface = usbInterface; this.numRequests = numRequests; this.numPacketsPerReq = numPacketsPerReq; }
/** * Constructs a class driver object, if the device supports * operations according to Annex D of the PTP specification. * * @param dev the first PTP interface will be used * @param connection * @throws IllegalArgumentException if the device has no * Digital Still Imaging Class or PTP interfaces */ public SonyInitiator(UsbDevice dev, UsbDeviceConnection connection) throws PTPException { super(); this.mConnection = connection; if (dev == null) { throw new PTPException ("dev = null");//IllegalArgumentException(); } session = new Session(); this.device = dev; intf = findUsbInterface (dev); if (intf == null) { //if (usbInterface == null) { throw new PTPException("No PTP interfaces associated to the device"); } for (int i = 0; i < intf.getEndpointCount(); i++) { UsbEndpoint ep = intf.getEndpoint(i); if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { epOut = ep; } else { epIn = ep; } } if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT){ epEv = ep; } } endpointSanityCheck(); inMaxPS = epOut.getMaxPacketSize(); intrMaxPS = epIn.getMaxPacketSize(); // clear epOut any previous state reset(); }
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; }
protected void setThreadsParams(UsbRequest request, UsbEndpoint endpoint) { if(mr1Version) { workerThread.setUsbRequest(request); writeThread.setUsbEndpoint(endpoint); }else { readThread.setUsbEndpoint(request.getEndpoint()); writeThread.setUsbEndpoint(endpoint); } }
private boolean openCP2102() { 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; } } // Default Setup if(setControlCommand(CP210x_IFC_ENABLE, CP210x_UART_ENABLE, null) < 0) return false; setBaudRate(DEFAULT_BAUDRATE); if(setControlCommand(CP210x_SET_LINE_CTL, CP210x_LINE_CTL_DEFAULT,null) < 0) return false; setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); if(setControlCommand(CP210x_SET_MHS, CP210x_MHS_DEFAULT, null) < 0) return false; return true; }
private boolean openCDC() { 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; } } if(outEndpoint == null || inEndpoint == null) { Log.i(CLASS_ID, "Interface does not have an IN or OUT interface"); return false; } // Default Setup setControlCommand(CDC_SET_LINE_CODING, 0, getInitialLineCoding()); setControlCommand(CDC_SET_CONTROL_LINE_STATE, CDC_CONTROL_LINE_ON, null); return true; }
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; }
protected void setThreadsParams(UsbEndpoint inEndpoint, UsbEndpoint outEndpoint) { if(writeThread != null) writeThread.setUsbEndpoint(outEndpoint); if(readThread != null) readThread.setUsbEndpoint(inEndpoint); }
/** Constructor. Inicia los EndPoints del Interfaz del dispositivo * @param usbDevCon * @param usbInterface */ protected SmartCardChannel(final UsbDeviceConnection usbDevCon, final UsbInterface usbInterface) { this.usbDeviceConnection = usbDevCon; for (int i = 0; i < usbInterface.getEndpointCount(); i++) { final UsbEndpoint usbEndPoint = usbInterface.getEndpoint(i); if (usbEndPoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (usbEndPoint.getDirection() == UsbConstants.USB_DIR_IN) { this.endPointIn = usbEndPoint; } else if (usbEndPoint.getDirection() == UsbConstants.USB_DIR_OUT) { this.endPointOut = usbEndPoint; } } } }
@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); } L.INSTANCE.d("Wrote amtWritten=" + amtWritten + " attempted=" + writeLength); offset += amtWritten; } return offset; }
private boolean openCDC() { 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; } } if(outEndpoint == null || inEndpoint == null) { Log.i(CLASS_ID, "Interface does not have an IN or OUT interface"); return false; } // Default Setup setControlCommand(CDC_SET_LINE_CODING, 0, CDC_DEFAULT_LINE_CODING); setControlCommand(CDC_SET_CONTROL_LINE_STATE, CDC_CONTROL_LINE_ON, null); return true; }
public static SubjectFactory<UsbEndpointSubject, UsbEndpoint> type() { return new SubjectFactory<UsbEndpointSubject, UsbEndpoint>() { @Override public UsbEndpointSubject getSubject(FailureStrategy fs, UsbEndpoint that) { return new UsbEndpointSubject(fs, that); } }; }
public U2FTransportAndroidHID(UsbDeviceConnection connection, UsbInterface dongleInterface, UsbEndpoint in, UsbEndpoint out, int timeout) { this.connection = connection; this.dongleInterface = dongleInterface; this.in = in; this.out = out; this.timeout = timeout; transferBuffer = new byte[HID_BUFFER_SIZE]; helper = new U2FHelper(); random = new Random(); }
public FcUsbWaiterThread(FcDevice device, UsbDeviceConnection usbDeviceConnection, UsbEndpoint endpointIn) { super(device); this.mUsbDeviceConnection = usbDeviceConnection; this.mEndpointIn = endpointIn; this.setName("LP2GoDeviceUsbWaiterThread"); }
public FcUsbDevice(MainActivity activity, UsbDeviceConnection connection, UsbInterface intf, Map<String, UAVTalkXMLObject> xmlObjects) { super(activity); //mActivity = activity; mDeviceConnection = connection; mObjectTree = new UAVTalkObjectTree(); mObjectTree.setXmlObjects(xmlObjects); mActivity.setPollThreadObjectTree(mObjectTree); UsbEndpoint epOut = null; UsbEndpoint epIn = null; // look for our bulk endpoints for (int i = 0; i < intf.getEndpointCount(); i++) { UsbEndpoint ep = intf.getEndpoint(i); if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) { if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { epOut = ep; } else { epIn = ep; } } } if (epOut == null || epIn == null) { throw new IllegalArgumentException("not all endpoints found"); } mEndpointOut = epOut; mEndpointIn = epIn; mWaiterThread = new FcUsbWaiterThread(this, mDeviceConnection, mEndpointIn); }