mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-30 18:07:21 +00:00 
			
		
		
		
	FTDI driver: Experimental async support.
Disabled due to Android bug.
This commit is contained in:
		
							parent
							
								
									fbf2607124
								
							
						
					
					
						commit
						b96d43639e
					
				| @ -20,15 +20,19 @@ | |||||||
| 
 | 
 | ||||||
| package com.hoho.android.usbserial.driver; | package com.hoho.android.usbserial.driver; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; |  | ||||||
| import java.util.Arrays; |  | ||||||
| 
 |  | ||||||
| import android.hardware.usb.UsbConstants; | import android.hardware.usb.UsbConstants; | ||||||
| import android.hardware.usb.UsbDevice; | import android.hardware.usb.UsbDevice; | ||||||
| import android.hardware.usb.UsbDeviceConnection; | import android.hardware.usb.UsbDeviceConnection; | ||||||
| import android.hardware.usb.UsbEndpoint; | import android.hardware.usb.UsbEndpoint; | ||||||
|  | import android.hardware.usb.UsbRequest; | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
| 
 | 
 | ||||||
|  | import com.hoho.android.usbserial.util.HexDump; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.nio.ByteBuffer; | ||||||
|  | import java.util.Arrays; | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * A {@link UsbSerialDriver} implementation for a variety of FTDI devices |  * A {@link UsbSerialDriver} implementation for a variety of FTDI devices | ||||||
|  * <p> |  * <p> | ||||||
| @ -165,6 +169,13 @@ public class FtdiSerialDriver implements UsbSerialDriver { | |||||||
| 
 | 
 | ||||||
|     private int mMaxPacketSize = 64; // TODO(mikey): detect |     private int mMaxPacketSize = 64; // TODO(mikey): detect | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Due to http://b.android.com/28023 , we cannot use UsbRequest async reads | ||||||
|  |      * since it gives no indication of number of bytes read. Set this to | ||||||
|  |      * {@code true} on platforms where it is fixed. | ||||||
|  |      */ | ||||||
|  |     private static final boolean ENABLE_ASYNC_READS = false; | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Constructor. |      * Constructor. | ||||||
|      * |      * | ||||||
| @ -224,6 +235,28 @@ public class FtdiSerialDriver implements UsbSerialDriver { | |||||||
|         final int readAmt = Math.min(dest.length, mReadBuffer.length); |         final int readAmt = Math.min(dest.length, mReadBuffer.length); | ||||||
|         final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(0); |         final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(0); | ||||||
| 
 | 
 | ||||||
|  |         if (ENABLE_ASYNC_READS) { | ||||||
|  |             final UsbRequest request = new UsbRequest(); | ||||||
|  |             request.initialize(mConnection, endpoint); | ||||||
|  | 
 | ||||||
|  |             final ByteBuffer buf = ByteBuffer.wrap(dest); | ||||||
|  |             if (!request.queue(buf, readAmt)) { | ||||||
|  |                 throw new IOException("Error queueing request."); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             final UsbRequest response = mConnection.requestWait(); | ||||||
|  |             if (response == null) { | ||||||
|  |                 throw new IOException("Null response"); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             final int nread = buf.position() - MODEM_STATUS_HEADER_LENGTH; | ||||||
|  |             if (nread > 0) { | ||||||
|  |                 Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length))); | ||||||
|  |                 return nread; | ||||||
|  |             } else { | ||||||
|  |                 return 0; | ||||||
|  |             } | ||||||
|  |         } else { | ||||||
|             final int transferred = mConnection.bulkTransfer(endpoint, mReadBuffer, readAmt, |             final int transferred = mConnection.bulkTransfer(endpoint, mReadBuffer, readAmt, | ||||||
|                     timeoutMillis); |                     timeoutMillis); | ||||||
|             if (transferred < MODEM_STATUS_HEADER_LENGTH) { |             if (transferred < MODEM_STATUS_HEADER_LENGTH) { | ||||||
| @ -237,6 +270,8 @@ public class FtdiSerialDriver implements UsbSerialDriver { | |||||||
|             return nread; |             return nread; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Override |     @Override | ||||||
|     public int write(byte[] src, int timeoutMillis) throws IOException { |     public int write(byte[] src, int timeoutMillis) throws IOException { | ||||||
|         final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1); |         final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user