mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-31 02:17:23 +00:00 
			
		
		
		
	reuse UsbRequest
less LogCat output
This commit is contained in:
		
							parent
							
								
									7b578918b0
								
							
						
					
					
						commit
						ce97a3408b
					
				| @ -6,7 +6,7 @@ buildscript { | |||||||
|         google() |         google() | ||||||
|     } |     } | ||||||
|     dependencies { |     dependencies { | ||||||
|         classpath 'com.android.tools.build:gradle:3.5.1' |         classpath 'com.android.tools.build:gradle:3.5.3' | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -5,7 +5,7 @@ publishing { | |||||||
|         maven(MavenPublication) { |         maven(MavenPublication) { | ||||||
|             groupId 'com.github.mik3y' |             groupId 'com.github.mik3y' | ||||||
|             artifactId 'usb-serial-for-android' |             artifactId 'usb-serial-for-android' | ||||||
|             version '2.1.0a' |             version '2.2.0a' | ||||||
|             afterEvaluate { |             afterEvaluate { | ||||||
|                 artifact androidSourcesJar |                 artifact androidSourcesJar | ||||||
|                 artifact bundleReleaseAar |                 artifact bundleReleaseAar | ||||||
|  | |||||||
| @ -1097,7 +1097,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
| 
 | 
 | ||||||
|     private int readSpeedInt(int writeSeconds, int readTimeout) throws Exception { |     private int readSpeedInt(int writeSeconds, int readTimeout) throws Exception { | ||||||
|         int baudrate = 115200; |         int baudrate = 115200; | ||||||
|         if(usbSerialDriver instanceof Ch34xSerialDriver && readTimeout != 0) |         if(usbSerialDriver instanceof Ch34xSerialDriver) | ||||||
|             baudrate = 38400; |             baudrate = 38400; | ||||||
|         int writeAhead = 5*baudrate/10; // write ahead for another 5 second read |         int writeAhead = 5*baudrate/10; // write ahead for another 5 second read | ||||||
|         if(usbSerialDriver instanceof CdcAcmSerialDriver) |         if(usbSerialDriver instanceof CdcAcmSerialDriver) | ||||||
| @ -1355,8 +1355,6 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
| 
 | 
 | ||||||
|         int longTimeout = 1000; |         int longTimeout = 1000; | ||||||
|         int shortTimeout = 10; |         int shortTimeout = 10; | ||||||
|         if(usbSerialDriver instanceof Ch34xSerialDriver) |  | ||||||
|             shortTimeout = 20; // too short timeout causes mysterious effects like lost telnet data |  | ||||||
|         time = System.currentTimeMillis(); |         time = System.currentTimeMillis(); | ||||||
|         len = usbSerialPort.read(buf, shortTimeout); |         len = usbSerialPort.read(buf, shortTimeout); | ||||||
|         assertEquals(0, len); |         assertEquals(0, len); | ||||||
|  | |||||||
| @ -94,14 +94,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public void open(UsbDeviceConnection connection) throws IOException { |         public void openInt(UsbDeviceConnection connection) throws IOException { | ||||||
|             if (mConnection != null) { |  | ||||||
|                 throw new IOException("Already open"); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             mConnection = connection; |  | ||||||
|             boolean opened = false; |  | ||||||
|             try { |  | ||||||
|             if (1 == mDevice.getInterfaceCount()) { |             if (1 == mDevice.getInterfaceCount()) { | ||||||
|                 Log.d(TAG,"device might be castrated ACM device, trying single interface logic"); |                 Log.d(TAG,"device might be castrated ACM device, trying single interface logic"); | ||||||
|                 openSingleInterface(); |                 openSingleInterface(); | ||||||
| @ -109,12 +102,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | |||||||
|                 Log.d(TAG,"trying default interface logic"); |                 Log.d(TAG,"trying default interface logic"); | ||||||
|                 openInterface(); |                 openInterface(); | ||||||
|             } |             } | ||||||
|                 opened = true; |  | ||||||
|             } finally { |  | ||||||
|                 if (!opened) { |  | ||||||
|                     close(); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private void openSingleInterface() throws IOException { |         private void openSingleInterface() throws IOException { | ||||||
| @ -224,9 +211,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | |||||||
|                 if (ep.getDirection() == UsbConstants.USB_DIR_OUT && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) |                 if (ep.getDirection() == UsbConstants.USB_DIR_OUT && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) | ||||||
|                     mWriteEndpoint = ep; |                     mWriteEndpoint = ep; | ||||||
|             } |             } | ||||||
|             if (mReadEndpoint == null || mWriteEndpoint == null) { |  | ||||||
|                 throw new IOException("Could not get read&write endpoints"); |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private int sendAcmControlMessage(int request, int value, byte[] buf) throws IOException { |         private int sendAcmControlMessage(int request, int value, byte[] buf) throws IOException { | ||||||
|  | |||||||
| @ -90,14 +90,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | |||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		@Override | 		@Override | ||||||
| 		public void open(UsbDeviceConnection connection) throws IOException { | 		public void openInt(UsbDeviceConnection connection) throws IOException { | ||||||
| 			if (mConnection != null) { |  | ||||||
| 				throw new IOException("Already open"); |  | ||||||
| 			} |  | ||||||
| 
 |  | ||||||
| 			mConnection = connection; |  | ||||||
| 			boolean opened = false; |  | ||||||
| 			try { |  | ||||||
| 			for (int i = 0; i < mDevice.getInterfaceCount(); i++) { | 			for (int i = 0; i < mDevice.getInterfaceCount(); i++) { | ||||||
| 				UsbInterface usbIface = mDevice.getInterface(i); | 				UsbInterface usbIface = mDevice.getInterface(i); | ||||||
| 				if (!mConnection.claimInterface(usbIface, true)) { | 				if (!mConnection.claimInterface(usbIface, true)) { | ||||||
| @ -119,13 +112,6 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | |||||||
| 
 | 
 | ||||||
| 			initialize(); | 			initialize(); | ||||||
| 			setBaudRate(DEFAULT_BAUD_RATE); | 			setBaudRate(DEFAULT_BAUD_RATE); | ||||||
| 
 |  | ||||||
| 				opened = true; |  | ||||||
| 			} finally { |  | ||||||
| 				if (!opened) { |  | ||||||
| 					close(); |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		@Override | 		@Override | ||||||
|  | |||||||
| @ -107,28 +107,44 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public abstract void open(UsbDeviceConnection connection) throws IOException; |     public void open(UsbDeviceConnection connection) throws IOException { | ||||||
|  |         if (mConnection != null) { | ||||||
|  |             throw new IOException("Already open"); | ||||||
|  |         } | ||||||
|  |         mConnection = connection; | ||||||
|  |         try { | ||||||
|  |             openInt(connection); | ||||||
|  |             if (mReadEndpoint == null || mWriteEndpoint == null) { | ||||||
|  |                 throw new IOException("Could not get read & write endpoints"); | ||||||
|  |             } | ||||||
|  |             mUsbRequest = new UsbRequest(); | ||||||
|  |             mUsbRequest.initialize(mConnection, mReadEndpoint); | ||||||
|  |         } catch(Exception e) { | ||||||
|  |             close(); | ||||||
|  |             throw e; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected abstract void openInt(UsbDeviceConnection connection) throws IOException; | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void close() throws IOException { |     public void close() throws IOException { | ||||||
|         if (mConnection == null) { |         if (mConnection == null) { | ||||||
|             throw new IOException("Already closed"); |             throw new IOException("Already closed"); | ||||||
|         } |         } | ||||||
|         synchronized (this) { |         try { | ||||||
|             if (mUsbRequest != null) |  | ||||||
|             mUsbRequest.cancel(); |             mUsbRequest.cancel(); | ||||||
|         } |         } catch(Exception ignored) {} | ||||||
|  |         mUsbRequest = null; | ||||||
|         try { |         try { | ||||||
|             closeInt(); |             closeInt(); | ||||||
|         } catch(Exception ignored) {} |         } catch(Exception ignored) {} | ||||||
|         try { |         try { | ||||||
|             mConnection.close(); |             mConnection.close(); | ||||||
|         } finally { |         } catch(Exception ignored) {} | ||||||
|         mConnection = null; |         mConnection = null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     protected abstract void closeInt(); |     protected abstract void closeInt(); | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @ -150,26 +166,15 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
|             nread = mConnection.bulkTransfer(mReadEndpoint, dest, readMax, timeout); |             nread = mConnection.bulkTransfer(mReadEndpoint, dest, readMax, timeout); | ||||||
| 
 | 
 | ||||||
|         } else { |         } else { | ||||||
|             final UsbRequest request = new UsbRequest(); |  | ||||||
|             try { |  | ||||||
|                 request.initialize(mConnection, mReadEndpoint); |  | ||||||
|             final ByteBuffer buf = ByteBuffer.wrap(dest); |             final ByteBuffer buf = ByteBuffer.wrap(dest); | ||||||
|                 if (!request.queue(buf, dest.length)) { |             if (!mUsbRequest.queue(buf, dest.length)) { | ||||||
|                     throw new IOException("Error queueing request"); |                 throw new IOException("Queueing USB request failed"); | ||||||
|             } |             } | ||||||
|                 mUsbRequest = request; |  | ||||||
|             final UsbRequest response = mConnection.requestWait(); |             final UsbRequest response = mConnection.requestWait(); | ||||||
|                 synchronized (this) { |  | ||||||
|                     mUsbRequest = null; |  | ||||||
|                 } |  | ||||||
|             if (response == null) { |             if (response == null) { | ||||||
|                     throw new IOException("Null response"); |                 throw new IOException("Waiting for USB request failed"); | ||||||
|             } |             } | ||||||
|             nread = buf.position(); |             nread = buf.position(); | ||||||
|             } finally { |  | ||||||
|                 mUsbRequest = null; |  | ||||||
|                 request.close(); |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|         if (nread > 0) { |         if (nread > 0) { | ||||||
|             return readFilter(dest, nread); |             return readFilter(dest, nread); | ||||||
|  | |||||||
| @ -138,15 +138,8 @@ public class Cp21xxSerialDriver implements UsbSerialDriver { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public void open(UsbDeviceConnection connection) throws IOException { |         public void openInt(UsbDeviceConnection connection) throws IOException { | ||||||
|             if (mConnection != null) { |  | ||||||
|                 throw new IOException("Already open"); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             mConnection = connection; |  | ||||||
|             boolean opened = false; |  | ||||||
|             mIsRestrictedPort = mDevice.getInterfaceCount() == 2 && mPortNumber == 1; |             mIsRestrictedPort = mDevice.getInterfaceCount() == 2 && mPortNumber == 1; | ||||||
|             try { |  | ||||||
|             if(mPortNumber >= mDevice.getInterfaceCount()) { |             if(mPortNumber >= mDevice.getInterfaceCount()) { | ||||||
|                 throw new IOException("Unknown port number"); |                 throw new IOException("Unknown port number"); | ||||||
|             } |             } | ||||||
| @ -169,12 +162,6 @@ public class Cp21xxSerialDriver implements UsbSerialDriver { | |||||||
|             setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS); |             setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS); | ||||||
|             setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE); |             setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE); | ||||||
| //            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY); | //            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY); | ||||||
|                 opened = true; |  | ||||||
|             } finally { |  | ||||||
|                 if (!opened) { |  | ||||||
|                     close(); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|  | |||||||
| @ -295,14 +295,7 @@ public class FtdiSerialDriver implements UsbSerialDriver { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public void open(UsbDeviceConnection connection) throws IOException { |         public void openInt(UsbDeviceConnection connection) throws IOException { | ||||||
|             if (mConnection != null) { |  | ||||||
|                 throw new IOException("Already open"); |  | ||||||
|             } |  | ||||||
|             mConnection = connection; |  | ||||||
| 
 |  | ||||||
|             boolean opened = false; |  | ||||||
|             try { |  | ||||||
|             if (connection.claimInterface(mDevice.getInterface(mPortNumber), true)) { |             if (connection.claimInterface(mDevice.getInterface(mPortNumber), true)) { | ||||||
|                 Log.d(TAG, "claimInterface " + mPortNumber + " SUCCESS"); |                 Log.d(TAG, "claimInterface " + mPortNumber + " SUCCESS"); | ||||||
|             } else { |             } else { | ||||||
| @ -315,12 +308,6 @@ public class FtdiSerialDriver implements UsbSerialDriver { | |||||||
|             mReadEndpoint = mDevice.getInterface(mPortNumber).getEndpoint(0); |             mReadEndpoint = mDevice.getInterface(mPortNumber).getEndpoint(0); | ||||||
|             mWriteEndpoint = mDevice.getInterface(mPortNumber).getEndpoint(1); |             mWriteEndpoint = mDevice.getInterface(mPortNumber).getEndpoint(1); | ||||||
|             reset(); |             reset(); | ||||||
|                 opened = true; |  | ||||||
|             } finally { |  | ||||||
|                 if (!opened) { |  | ||||||
|                     close(); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|  | |||||||
| @ -263,20 +263,13 @@ public class ProlificSerialDriver implements UsbSerialDriver { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public void open(UsbDeviceConnection connection) throws IOException { |         public void openInt(UsbDeviceConnection connection) throws IOException { | ||||||
|             if (mConnection != null) { |  | ||||||
|                 throw new IOException("Already open"); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             UsbInterface usbInterface = mDevice.getInterface(0); |             UsbInterface usbInterface = mDevice.getInterface(0); | ||||||
| 
 | 
 | ||||||
|             if (!connection.claimInterface(usbInterface, true)) { |             if (!connection.claimInterface(usbInterface, true)) { | ||||||
|                 throw new IOException("Error claiming Prolific interface 0"); |                 throw new IOException("Error claiming Prolific interface 0"); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             mConnection = connection; |  | ||||||
|             boolean opened = false; |  | ||||||
|             try { |  | ||||||
|             for (int i = 0; i < usbInterface.getEndpointCount(); ++i) { |             for (int i = 0; i < usbInterface.getEndpointCount(); ++i) { | ||||||
|                 UsbEndpoint currentEndpoint = usbInterface.getEndpoint(i); |                 UsbEndpoint currentEndpoint = usbInterface.getEndpoint(i); | ||||||
| 
 | 
 | ||||||
| @ -324,17 +317,9 @@ public class ProlificSerialDriver implements UsbSerialDriver { | |||||||
|                             + "to detect PL2303 subtype", e); |                             + "to detect PL2303 subtype", e); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| 
 |  | ||||||
|             setControlLines(mControlLinesValue); |             setControlLines(mControlLinesValue); | ||||||
|             resetDevice(); |             resetDevice(); | ||||||
| 
 |  | ||||||
|             doBlackMagic(); |             doBlackMagic(); | ||||||
|                 opened = true; |  | ||||||
|             } finally { |  | ||||||
|                 if (!opened) { |  | ||||||
|                     close(); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user