mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-31 10:27:27 +00:00 
			
		
		
		
	FTDI driver: Filter status bytes
Filter status bytes at the beginning of every USB package received from FTDI serial adapters
This commit is contained in:
		
							parent
							
								
									b709823906
								
							
						
					
					
						commit
						eca40d6b11
					
				| @ -166,6 +166,32 @@ public class FtdiSerialDriver extends CommonUsbSerialDriver { | |||||||
|      */ |      */ | ||||||
|     private static final boolean ENABLE_ASYNC_READS = false; |     private static final boolean ENABLE_ASYNC_READS = false; | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Filter FTDI status bytes from buffer | ||||||
|  |      * @param src The source buffer (which contains status bytes) | ||||||
|  |      * @param dest The destination buffer to write the status bytes into (can be src) | ||||||
|  |      * @param totalBytesRead Number of bytes read to src | ||||||
|  |      * @param maxPacketSize The USB endpoint max packet size | ||||||
|  |      * @return The number of payload bytes | ||||||
|  |      */ | ||||||
|  |     private final int filterStatusBytes(byte[] src, byte[] dest, int totalBytesRead, int maxPacketSize) { | ||||||
|  |         final int packetsCount = totalBytesRead / maxPacketSize + 1; | ||||||
|  |         for (int packetIdx = 0; packetIdx < packetsCount; ++packetIdx) { | ||||||
|  |             final int count = (packetIdx == (packetsCount - 1)) | ||||||
|  |                     ? (totalBytesRead % maxPacketSize) - MODEM_STATUS_HEADER_LENGTH | ||||||
|  |                     : maxPacketSize - MODEM_STATUS_HEADER_LENGTH; | ||||||
|  |             if (count > 0) { | ||||||
|  |                 System.arraycopy(src, | ||||||
|  |                         packetIdx * maxPacketSize + MODEM_STATUS_HEADER_LENGTH, | ||||||
|  |                         dest, | ||||||
|  |                         packetIdx * (maxPacketSize - MODEM_STATUS_HEADER_LENGTH), | ||||||
|  |                         count); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |       return totalBytesRead - (packetsCount * 2); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Constructor. |      * Constructor. | ||||||
|      * |      * | ||||||
| @ -253,17 +279,13 @@ public class FtdiSerialDriver extends CommonUsbSerialDriver { | |||||||
|                 final int readAmt = Math.min(dest.length, mReadBuffer.length); |                 final int readAmt = Math.min(dest.length, mReadBuffer.length); | ||||||
|                 totalBytesRead = mConnection.bulkTransfer(endpoint, mReadBuffer, |                 totalBytesRead = mConnection.bulkTransfer(endpoint, mReadBuffer, | ||||||
|                         readAmt, timeoutMillis); |                         readAmt, timeoutMillis); | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) { |                 if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) { | ||||||
|                 throw new IOException("Expected at least " + MODEM_STATUS_HEADER_LENGTH + " bytes"); |                     throw new IOException("Expected at least " + MODEM_STATUS_HEADER_LENGTH + " bytes"); | ||||||
|             } |                 } | ||||||
|    |    | ||||||
|             final int payloadBytesRead = totalBytesRead - MODEM_STATUS_HEADER_LENGTH; |                 return filterStatusBytes(mReadBuffer, dest, totalBytesRead, endpoint.getMaxPacketSize()); | ||||||
|             if (payloadBytesRead > 0) { |  | ||||||
|                 System.arraycopy(mReadBuffer, MODEM_STATUS_HEADER_LENGTH, dest, 0, payloadBytesRead); |  | ||||||
|             } |             } | ||||||
|             return payloadBytesRead; |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user