mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-31 02:17:23 +00:00 
			
		
		
		
	more coverage tests
purgeHwBuffers: adjust parameter names to match read/write methods and actual behavior
This commit is contained in:
		
							parent
							
								
									37059b1a27
								
							
						
					
					
						commit
						f7399c2aad
					
				| @ -73,8 +73,9 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|     private static int     test_device_port; |     private static int     test_device_port; | ||||||
| 
 | 
 | ||||||
|     private final static int     TELNET_READ_WAIT = 500; |     private final static int     TELNET_READ_WAIT = 500; | ||||||
|     private final static int     USB_READ_WAIT    = 500; |     private final static int     TELNET_COMMAND_WAIT = 2000; | ||||||
|     private final static int     USB_WRITE_WAIT   = 500; |     private final static int     USB_READ_WAIT = 500; | ||||||
|  |     private final static int     USB_WRITE_WAIT = 500; | ||||||
|     private final static Integer SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY = Process.THREAD_PRIORITY_URGENT_AUDIO; |     private final static Integer SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY = Process.THREAD_PRIORITY_URGENT_AUDIO; | ||||||
| 
 | 
 | ||||||
|     private final static String  TAG = "DeviceTest"; |     private final static String  TAG = "DeviceTest"; | ||||||
| @ -83,13 +84,16 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|     private final static byte    RFC2217_SET_DATASIZE = 2; |     private final static byte    RFC2217_SET_DATASIZE = 2; | ||||||
|     private final static byte    RFC2217_SET_PARITY   = 3; |     private final static byte    RFC2217_SET_PARITY   = 3; | ||||||
|     private final static byte    RFC2217_SET_STOPSIZE = 4; |     private final static byte    RFC2217_SET_STOPSIZE = 4; | ||||||
|  |     private final static byte    RFC2217_PURGE_DATA = 12; | ||||||
| 
 | 
 | ||||||
|     private Context context; |     private Context context; | ||||||
|  |     private UsbManager usbManager; | ||||||
|     private UsbSerialDriver usbSerialDriver; |     private UsbSerialDriver usbSerialDriver; | ||||||
|     private UsbDeviceConnection usbDeviceConnection; |     private UsbDeviceConnection usbDeviceConnection; | ||||||
|     private UsbSerialPort usbSerialPort; |     private UsbSerialPort usbSerialPort; | ||||||
|     private SerialInputOutputManager usbIoManager; |     private SerialInputOutputManager usbIoManager; | ||||||
|     private final Deque<byte[]> usbReadBuffer = new LinkedList<>(); |     private final Deque<byte[]> usbReadBuffer = new LinkedList<>(); | ||||||
|  |     private Exception usbReadError; | ||||||
|     private boolean usbReadBlock = false; |     private boolean usbReadBlock = false; | ||||||
|     private long usbReadTime = 0; |     private long usbReadTime = 0; | ||||||
| 
 | 
 | ||||||
| @ -137,10 +141,18 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|         setUpFixtureInt(); |         setUpFixtureInt(); | ||||||
|         telnetClient.sendAYT(1000); // not correctly handled by rfc2217_server.py, but WARNING output "ignoring Telnet command: '\xf6'" is a nice separator between tests |         telnetClient.sendAYT(1000); // not correctly handled by rfc2217_server.py, but WARNING output "ignoring Telnet command: '\xf6'" is a nice separator between tests | ||||||
|         telnetComPortOptionCounter[0] = 0; |         telnetComPortOptionCounter[0] = 0; | ||||||
|  |         telnetClient.sendCommand((byte)TelnetCommand.SB); | ||||||
|  |         telnetWriteStream.write(new byte[] {RFC2217_COM_PORT_OPTION, RFC2217_PURGE_DATA, 3}); | ||||||
|  |         telnetClient.sendCommand((byte)TelnetCommand.SE); | ||||||
|  |         for(int i=0; i<TELNET_COMMAND_WAIT; i++) { | ||||||
|  |             if(telnetComPortOptionCounter[0] == 1) break; | ||||||
|  |             Thread.sleep(1); | ||||||
|  |         } | ||||||
|  |         assertEquals("telnet connection lost", 1, telnetComPortOptionCounter[0].intValue()); | ||||||
|         telnetWriteDelay = 0; |         telnetWriteDelay = 0; | ||||||
| 
 | 
 | ||||||
|         context = InstrumentationRegistry.getContext(); |         context = InstrumentationRegistry.getContext(); | ||||||
|         final UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); |         usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); | ||||||
|         List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager); |         List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager); | ||||||
|         assertEquals("no USB device found", 1, availableDrivers.size()); |         assertEquals("no USB device found", 1, availableDrivers.size()); | ||||||
|         usbSerialDriver = availableDrivers.get(0); |         usbSerialDriver = availableDrivers.get(0); | ||||||
| @ -173,24 +185,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|             } |             } | ||||||
|             assertTrue("USB permission dialog not confirmed", granted[0]); |             assertTrue("USB permission dialog not confirmed", granted[0]); | ||||||
|         } |         } | ||||||
|         usbDeviceConnection = usbManager.openDevice(usbSerialDriver.getDevice()); |         usbOpen(true); | ||||||
|         usbSerialPort.open(usbDeviceConnection); |  | ||||||
|         usbSerialPort.setDTR(true); |  | ||||||
|         usbSerialPort.setRTS(true); |  | ||||||
|         usbIoManager = new SerialInputOutputManager(usbSerialPort, this) { |  | ||||||
|             @Override |  | ||||||
|             public void run() { |  | ||||||
|                 if(SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY != null) |  | ||||||
|                     Process.setThreadPriority(SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY); |  | ||||||
|                 super.run(); |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
| 
 |  | ||||||
|         Executors.newSingleThreadExecutor().submit(usbIoManager); |  | ||||||
| 
 |  | ||||||
|         synchronized (usbReadBuffer) { |  | ||||||
|             usbReadBuffer.clear(); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @After |     @After | ||||||
| @ -266,6 +261,46 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private void usbClose() { | ||||||
|  |         if (usbIoManager != null) { | ||||||
|  |             usbIoManager.stop(); | ||||||
|  |             usbIoManager = null; | ||||||
|  |         } | ||||||
|  |         if (usbSerialPort != null) { | ||||||
|  |             try { | ||||||
|  |                 usbSerialPort.close(); | ||||||
|  |             } catch (IOException ignored) { | ||||||
|  |             } | ||||||
|  |             usbSerialPort = null; | ||||||
|  |         } | ||||||
|  |         if(usbDeviceConnection != null) | ||||||
|  |             usbDeviceConnection.close(); | ||||||
|  |         usbDeviceConnection = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private void usbOpen(boolean withIoManager) throws Exception { | ||||||
|  |         usbDeviceConnection = usbManager.openDevice(usbSerialDriver.getDevice()); | ||||||
|  |         usbSerialPort = usbSerialDriver.getPorts().get(test_device_port); | ||||||
|  |         usbSerialPort.open(usbDeviceConnection); | ||||||
|  |         usbSerialPort.setDTR(true); | ||||||
|  |         usbSerialPort.setRTS(true); | ||||||
|  |         if(withIoManager) { | ||||||
|  |             usbIoManager = new SerialInputOutputManager(usbSerialPort, this) { | ||||||
|  |                 @Override | ||||||
|  |                 public void run() { | ||||||
|  |                     if (SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY != null) | ||||||
|  |                         Process.setThreadPriority(SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY); | ||||||
|  |                     super.run(); | ||||||
|  |                 } | ||||||
|  |             }; | ||||||
|  |             Executors.newSingleThreadExecutor().submit(usbIoManager); | ||||||
|  |         } | ||||||
|  |         synchronized (usbReadBuffer) { | ||||||
|  |             usbReadBuffer.clear(); | ||||||
|  |         } | ||||||
|  |         usbReadError = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     // wait full time |     // wait full time | ||||||
|     private byte[] usbRead() throws Exception { |     private byte[] usbRead() throws Exception { | ||||||
|         return usbRead(-1); |         return usbRead(-1); | ||||||
| @ -276,6 +311,8 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|         ByteBuffer buf = ByteBuffer.allocate(8192); |         ByteBuffer buf = ByteBuffer.allocate(8192); | ||||||
|         if(usbIoManager != null) { |         if(usbIoManager != null) { | ||||||
|             while (System.currentTimeMillis() < end) { |             while (System.currentTimeMillis() < end) { | ||||||
|  |                 if(usbReadError != null) | ||||||
|  |                     throw usbReadError; | ||||||
|                 synchronized (usbReadBuffer) { |                 synchronized (usbReadBuffer) { | ||||||
|                     while(usbReadBuffer.peek() != null) |                     while(usbReadBuffer.peek() != null) | ||||||
|                         buf.put(usbReadBuffer.remove()); |                         buf.put(usbReadBuffer.remove()); | ||||||
| @ -336,7 +373,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|         telnetClient.sendCommand((byte)TelnetCommand.SE); |         telnetClient.sendCommand((byte)TelnetCommand.SE); | ||||||
| 
 | 
 | ||||||
|         // windows does not like nonstandard baudrates. rfc2217_server.py terminates w/o response |         // windows does not like nonstandard baudrates. rfc2217_server.py terminates w/o response | ||||||
|         for(int i=0; i<2000; i++) { |         for(int i=0; i<TELNET_COMMAND_WAIT; i++) { | ||||||
|             if(telnetComPortOptionCounter[0] == 4) break; |             if(telnetComPortOptionCounter[0] == 4) break; | ||||||
|             Thread.sleep(1); |             Thread.sleep(1); | ||||||
|         } |         } | ||||||
| @ -368,7 +405,8 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void onRunError(Exception e) { |     public void onRunError(Exception e) { | ||||||
|         fail("usb connection lost"); |         usbReadError = e; | ||||||
|  |         //fail("usb connection lost"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // clone of org.apache.commons.lang3.StringUtils.indexOfDifference + optional startpos |     // clone of org.apache.commons.lang3.StringUtils.indexOfDifference + optional startpos | ||||||
| @ -419,6 +457,79 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private void doReadWrite(String reason) throws Exception { | ||||||
|  |         byte[] buf1 = new byte[]{ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; | ||||||
|  |         byte[] buf2 = new byte[]{ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26}; | ||||||
|  |         byte[] data; | ||||||
|  | 
 | ||||||
|  |         telnetWrite(buf1); | ||||||
|  |         data = usbRead(buf1.length); | ||||||
|  |         assertThat(reason, data, equalTo(buf1)); // includes array content in output | ||||||
|  |         //assertArrayEquals("net2usb".getBytes(), data); // only includes array length in output | ||||||
|  |         usbWrite(buf2); | ||||||
|  |         data = telnetRead(buf2.length); | ||||||
|  |         assertThat(reason, data, equalTo(buf2)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void openClose() throws Exception { | ||||||
|  |         byte[] data; | ||||||
|  |         telnetParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |         usbParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |         doReadWrite(""); | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             usbSerialPort.open(usbDeviceConnection); | ||||||
|  |             fail("already open expected"); | ||||||
|  |         } catch (IOException ignored) { | ||||||
|  |         } | ||||||
|  |         doReadWrite(""); | ||||||
|  | 
 | ||||||
|  |         usbSerialPort.close(); | ||||||
|  |         try { | ||||||
|  |             usbSerialPort.close(); | ||||||
|  |             fail("already closed expected"); | ||||||
|  |         } catch (IOException ignored) { | ||||||
|  |         } | ||||||
|  |         try { | ||||||
|  |             usbWrite(new byte[]{0x00}); | ||||||
|  |             fail("write error expected"); | ||||||
|  |         } catch (IOException ignored) { | ||||||
|  |         } catch (NullPointerException ignored) { | ||||||
|  |         } | ||||||
|  |         try { | ||||||
|  |             usbRead(1); | ||||||
|  |             //fail("read error expected"); | ||||||
|  |         } catch (IOException ignored) { | ||||||
|  |         } catch (NullPointerException ignored) { | ||||||
|  |         } | ||||||
|  |         try { | ||||||
|  |             usbParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |             fail("error expected"); | ||||||
|  |         } catch (IOException ignored) { | ||||||
|  |         } catch (NullPointerException ignored) { | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // partial re-open not supported | ||||||
|  |         try { | ||||||
|  |             usbSerialPort.open(usbDeviceConnection); | ||||||
|  |             //usbParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |             doReadWrite(""); | ||||||
|  |             fail("re-open not supported"); | ||||||
|  |         } catch (IOException ignored) { | ||||||
|  |         } | ||||||
|  |         try { | ||||||
|  |             usbSerialPort.close(); | ||||||
|  |         } catch (IOException ignored) { | ||||||
|  |         } | ||||||
|  |         // full re-open supported | ||||||
|  |         usbClose(); | ||||||
|  |         usbOpen(true); | ||||||
|  |         telnetParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |         usbParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |         doReadWrite(""); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void baudRate() throws Exception { |     public void baudRate() throws Exception { | ||||||
|         byte[] data; |         byte[] data; | ||||||
| @ -428,13 +539,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|             // PL2303, FTDI: it's not 9600 |             // PL2303, FTDI: it's not 9600 | ||||||
|             telnetParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE); |             telnetParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
| 
 | 
 | ||||||
|             telnetWrite("net2usb".getBytes()); |             doReadWrite(""); | ||||||
|             data = usbRead(7); |  | ||||||
|             assertThat(data, equalTo("net2usb".getBytes())); // includes array content in output |  | ||||||
|             //assertArrayEquals("net2usb".getBytes(), data); // only includes array length in output |  | ||||||
|             usbWrite("usb2net".getBytes()); |  | ||||||
|             data = telnetRead(7); |  | ||||||
|             assertThat(data, equalTo("usb2net".getBytes())); |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         // invalid values |         // invalid values | ||||||
| @ -452,8 +557,8 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|                 ; // todo: add range check in driver |                 ; // todo: add range check in driver | ||||||
|             else |             else | ||||||
|                 fail("invalid baudrate 0"); |                 fail("invalid baudrate 0"); | ||||||
|         } catch (java.io.IOException ignored) { // cp2105 second port |         } catch (IOException ignored) { // cp2105 second port | ||||||
|         } catch (java.lang.IllegalArgumentException ignored) { |         } catch (IllegalArgumentException ignored) { | ||||||
|         } |         } | ||||||
|         try { |         try { | ||||||
|             usbParameters(0, 8, 1, UsbSerialPort.PARITY_NONE); |             usbParameters(0, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
| @ -465,9 +570,9 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|                 ; // todo: add range check in driver |                 ; // todo: add range check in driver | ||||||
|             else |             else | ||||||
|                 fail("invalid baudrate 0"); |                 fail("invalid baudrate 0"); | ||||||
|         } catch (java.lang.ArithmeticException ignored) { // ch340 |         } catch (ArithmeticException ignored) { // ch340 | ||||||
|         } catch (java.io.IOException ignored) { // cp2105 second port |         } catch (IOException ignored) { // cp2105 second port | ||||||
|         } catch (java.lang.IllegalArgumentException ignored) { |         } catch (IllegalArgumentException ignored) { | ||||||
|         } |         } | ||||||
|         try { |         try { | ||||||
|             usbParameters(1, 8, 1, UsbSerialPort.PARITY_NONE); |             usbParameters(1, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
| @ -481,8 +586,8 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|                 ; |                 ; | ||||||
|             else |             else | ||||||
|                 fail("invalid baudrate 0"); |                 fail("invalid baudrate 0"); | ||||||
|         } catch (java.io.IOException ignored) { // ch340 |         } catch (IOException ignored) { // ch340 | ||||||
|         } catch (java.lang.IllegalArgumentException ignored) { |         } catch (IllegalArgumentException ignored) { | ||||||
|         } |         } | ||||||
|         try { |         try { | ||||||
|             usbParameters(2<<31, 8, 1, UsbSerialPort.PARITY_NONE); |             usbParameters(2<<31, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
| @ -494,9 +599,9 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|                 ; |                 ; | ||||||
|             else |             else | ||||||
|                 fail("invalid baudrate 2^31"); |                 fail("invalid baudrate 2^31"); | ||||||
|         } catch (java.lang.ArithmeticException ignored) { // ch340 |         } catch (ArithmeticException ignored) { // ch340 | ||||||
|         } catch (java.io.IOException ignored) { // cp2105 second port |         } catch (IOException ignored) { // cp2105 second port | ||||||
|         } catch (java.lang.IllegalArgumentException ignored) { |         } catch (IllegalArgumentException ignored) { | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         for(int baudRate : new int[] {300, 2400, 19200, 42000, 115200} ) { |         for(int baudRate : new int[] {300, 2400, 19200, 42000, 115200} ) { | ||||||
| @ -506,19 +611,14 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|                 try { |                 try { | ||||||
|                     usbParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE); |                     usbParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|                     fail("baudrate 300 on cp21xx restricted port"); |                     fail("baudrate 300 on cp21xx restricted port"); | ||||||
|                 } catch (java.io.IOException ignored) { |                 } catch (IOException ignored) { | ||||||
|                 } |                 } | ||||||
|                 continue; |                 continue; | ||||||
|             } |             } | ||||||
|             telnetParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE); |             telnetParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|             usbParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE); |             usbParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
| 
 | 
 | ||||||
|             telnetWrite("net2usb".getBytes()); |             doReadWrite(baudRate+"/8N1"); | ||||||
|             data = usbRead(7); |  | ||||||
|             assertThat(baudRate+"/8N1", data, equalTo("net2usb".getBytes())); |  | ||||||
|             usbWrite("usb2net".getBytes()); |  | ||||||
|             data = telnetRead(7); |  | ||||||
|             assertThat(baudRate+"/8N1", data, equalTo("usb2net".getBytes())); |  | ||||||
|         } |         } | ||||||
|         { // non matching baud rate |         { // non matching baud rate | ||||||
|             telnetParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); |             telnetParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
| @ -546,7 +646,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|                     ; // todo: add range check in driver |                     ; // todo: add range check in driver | ||||||
|                 else |                 else | ||||||
|                     fail("invalid databits "+i); |                     fail("invalid databits "+i); | ||||||
|             } catch (java.lang.IllegalArgumentException ignored) { |             } catch (IllegalArgumentException ignored) { | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -582,7 +682,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|             usbWrite(new byte[]{(byte) 0xff}); |             usbWrite(new byte[]{(byte) 0xff}); | ||||||
|             data = telnetRead(2); |             data = telnetRead(2); | ||||||
|             assertThat("19000/7N1", data, equalTo(new byte[]{(byte) 0x80, (byte) 0xff})); |             assertThat("19000/7N1", data, equalTo(new byte[]{(byte) 0x80, (byte) 0xff})); | ||||||
|         } catch (java.lang.IllegalArgumentException e) { |         } catch (IllegalArgumentException e) { | ||||||
|                 if(!isCp21xxRestrictedPort) |                 if(!isCp21xxRestrictedPort) | ||||||
|                     throw e; |                     throw e; | ||||||
|         } |         } | ||||||
| @ -593,7 +693,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|             usbWrite(new byte[]{(byte) 0xff}); |             usbWrite(new byte[]{(byte) 0xff}); | ||||||
|             data = telnetRead(2); |             data = telnetRead(2); | ||||||
|             assertThat("19000/6N1", data, equalTo(new byte[]{(byte) 0xc0, (byte) 0xff})); |             assertThat("19000/6N1", data, equalTo(new byte[]{(byte) 0xc0, (byte) 0xff})); | ||||||
|         } catch (java.lang.IllegalArgumentException e) { |         } catch (IllegalArgumentException e) { | ||||||
|             if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver)) |             if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver)) | ||||||
|                 throw e; |                 throw e; | ||||||
|         } |         } | ||||||
| @ -604,7 +704,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|             usbWrite(new byte[] {(byte)0xff}); |             usbWrite(new byte[] {(byte)0xff}); | ||||||
|             data = telnetRead(2); |             data = telnetRead(2); | ||||||
|             assertThat("19000/5N1", data, equalTo(new byte[] {(byte)0xe0, (byte)0xff})); |             assertThat("19000/5N1", data, equalTo(new byte[] {(byte)0xe0, (byte)0xff})); | ||||||
|         } catch (java.lang.IllegalArgumentException e) { |         } catch (IllegalArgumentException e) { | ||||||
|             if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver)) |             if (!(isCp21xxRestrictedPort || usbSerialDriver instanceof FtdiSerialDriver)) | ||||||
|                 throw e; |                 throw e; | ||||||
|         } |         } | ||||||
| @ -624,7 +724,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|             try { |             try { | ||||||
|                 usbParameters(19200, 8, 1, i); |                 usbParameters(19200, 8, 1, i); | ||||||
|                 fail("invalid parity "+i); |                 fail("invalid parity "+i); | ||||||
|             } catch (java.lang.IllegalArgumentException ignored) { |             } catch (IllegalArgumentException ignored) { | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         if(isCp21xxRestrictedPort) { |         if(isCp21xxRestrictedPort) { | ||||||
| @ -634,11 +734,11 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|             try { |             try { | ||||||
|                 usbParameters(19200, 8, 1, UsbSerialPort.PARITY_MARK); |                 usbParameters(19200, 8, 1, UsbSerialPort.PARITY_MARK); | ||||||
|                 fail("parity mark"); |                 fail("parity mark"); | ||||||
|             } catch (java.lang.IllegalArgumentException ignored) {} |             } catch (IllegalArgumentException ignored) {} | ||||||
|             try { |             try { | ||||||
|                 usbParameters(19200, 8, 1, UsbSerialPort.PARITY_SPACE); |                 usbParameters(19200, 8, 1, UsbSerialPort.PARITY_SPACE); | ||||||
|                 fail("parity space"); |                 fail("parity space"); | ||||||
|             } catch (java.lang.IllegalArgumentException ignored) {} |             } catch (IllegalArgumentException ignored) {} | ||||||
|             return; |             return; | ||||||
|             // test below not possible as it requires unsupported 7 dataBits |             // test below not possible as it requires unsupported 7 dataBits | ||||||
|         } |         } | ||||||
| @ -721,7 +821,7 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|             try { |             try { | ||||||
|                 usbParameters(19200, 8, i, UsbSerialPort.PARITY_NONE); |                 usbParameters(19200, 8, i, UsbSerialPort.PARITY_NONE); | ||||||
|                 fail("invalid stopbits " + i); |                 fail("invalid stopbits " + i); | ||||||
|             } catch (java.lang.IllegalArgumentException ignored) { |             } catch (IllegalArgumentException ignored) { | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -754,12 +854,16 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|                 usbWrite(new byte[]{(byte) 0x41, (byte) 0xf9}); |                 usbWrite(new byte[]{(byte) 0x41, (byte) 0xf9}); | ||||||
|                 data = telnetRead(2); |                 data = telnetRead(2); | ||||||
|                 assertThat("19200/8N1", data, equalTo(new byte[]{1, 11})); |                 assertThat("19200/8N1", data, equalTo(new byte[]{1, 11})); | ||||||
|             } catch(java.lang.IllegalArgumentException e) { |             } catch(IllegalArgumentException e) { | ||||||
|                 if(!isCp21xxRestrictedPort) |                 if(!isCp21xxRestrictedPort) | ||||||
|                     throw e; |                     throw e; | ||||||
|             } |             } | ||||||
|             // todo: could create similar test for 1.5 stopbits, by reading at double speed |             try { | ||||||
|             //       but only some devices support 1.5 stopbits and it is basically not used any more |                 usbParameters(19200, 8, UsbSerialPort.STOPBITS_1_5, UsbSerialPort.PARITY_NONE); | ||||||
|  |                 // todo: could create similar test for 1.5 stopbits, by reading at double speed | ||||||
|  |                 //       but only some devices support 1.5 stopbits and it is basically not used any more | ||||||
|  |             } catch(IllegalArgumentException ignored) { | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -953,4 +1057,31 @@ public class DeviceTest implements SerialInputOutputManager.Listener { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Test | ||||||
|  |     public void purgeHwBuffers() throws Exception { | ||||||
|  |         // 2400 is slowest baud rate for isCp21xxRestrictedPort | ||||||
|  |         usbParameters(2400, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |         telnetParameters(2400, 8, 1, UsbSerialPort.PARITY_NONE); | ||||||
|  |         byte[] buf = new byte[64]; | ||||||
|  |         for(int i=0; i<buf.length; i++) buf[i]='a'; | ||||||
|  |         StringBuilder data = new StringBuilder(); | ||||||
|  | 
 | ||||||
|  |         // purge send buffer | ||||||
|  |         usbWrite(buf); | ||||||
|  |         Thread.sleep(50); // ~ 12 characters | ||||||
|  |         boolean purged = usbSerialPort.purgeHwBuffers(true, false); | ||||||
|  |         usbWrite("bcd".getBytes()); | ||||||
|  |         Thread.sleep(50); | ||||||
|  |         while(data.length()==0 || data.charAt(data.length()-1)!='d') | ||||||
|  |             data.append(new String(telnetRead())); | ||||||
|  |         Log.i(TAG, "purgeHwBuffers " + purged + ": " + buf.length+1 + " -> " + data.length()); | ||||||
|  | 
 | ||||||
|  |         assertTrue(data.length() > 5); | ||||||
|  |         if(purged) | ||||||
|  |             assertTrue(data.length() < buf.length+1); | ||||||
|  |         else | ||||||
|  |             assertEquals(data.length(), buf.length + 3); | ||||||
|  | 
 | ||||||
|  |         // todo: purge receive buffer | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -401,7 +401,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | |||||||
| 
 | 
 | ||||||
|     public static Map<Integer, int[]> getSupportedDevices() { |     public static Map<Integer, int[]> getSupportedDevices() { | ||||||
|         final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); |         final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); | ||||||
|         supportedDevices.put(Integer.valueOf(UsbId.VENDOR_ARDUINO), |         supportedDevices.put(UsbId.VENDOR_ARDUINO, | ||||||
|                 new int[] { |                 new int[] { | ||||||
|                         UsbId.ARDUINO_UNO, |                         UsbId.ARDUINO_UNO, | ||||||
|                         UsbId.ARDUINO_UNO_R3, |                         UsbId.ARDUINO_UNO_R3, | ||||||
| @ -414,19 +414,19 @@ public class CdcAcmSerialDriver implements UsbSerialDriver { | |||||||
|                         UsbId.ARDUINO_LEONARDO, |                         UsbId.ARDUINO_LEONARDO, | ||||||
|                         UsbId.ARDUINO_MICRO, |                         UsbId.ARDUINO_MICRO, | ||||||
|                 }); |                 }); | ||||||
|         supportedDevices.put(Integer.valueOf(UsbId.VENDOR_VAN_OOIJEN_TECH), |         supportedDevices.put(UsbId.VENDOR_VAN_OOIJEN_TECH, | ||||||
|                 new int[] { |                 new int[] { | ||||||
|                     UsbId.VAN_OOIJEN_TECH_TEENSYDUINO_SERIAL, |                     UsbId.VAN_OOIJEN_TECH_TEENSYDUINO_SERIAL, | ||||||
|                 }); |                 }); | ||||||
|         supportedDevices.put(Integer.valueOf(UsbId.VENDOR_ATMEL), |         supportedDevices.put(UsbId.VENDOR_ATMEL, | ||||||
|                 new int[] { |                 new int[] { | ||||||
|                     UsbId.ATMEL_LUFA_CDC_DEMO_APP, |                     UsbId.ATMEL_LUFA_CDC_DEMO_APP, | ||||||
|                 }); |                 }); | ||||||
|         supportedDevices.put(Integer.valueOf(UsbId.VENDOR_LEAFLABS), |         supportedDevices.put(UsbId.VENDOR_LEAFLABS, | ||||||
|                 new int[] { |                 new int[] { | ||||||
|                     UsbId.LEAFLABS_MAPLE, |                     UsbId.LEAFLABS_MAPLE, | ||||||
|                 }); |                 }); | ||||||
|         supportedDevices.put(Integer.valueOf(UsbId.VENDOR_ARM), |         supportedDevices.put(UsbId.VENDOR_ARM, | ||||||
|                 new int[] { |                 new int[] { | ||||||
|                     UsbId.ARM_MBED, |                     UsbId.ARM_MBED, | ||||||
|                 }); |                 }); | ||||||
|  | |||||||
| @ -426,11 +426,6 @@ public class Ch34xSerialDriver implements UsbSerialDriver { | |||||||
| 			writeHandshakeByte(); | 			writeHandshakeByte(); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		@Override |  | ||||||
| 		public boolean purgeHwBuffers(boolean purgeReadBuffers, boolean purgeWriteBuffers) throws IOException { |  | ||||||
| 			return true; |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	public static Map<Integer, int[]> getSupportedDevices() { | 	public static Map<Integer, int[]> getSupportedDevices() { | ||||||
|  | |||||||
| @ -160,8 +160,8 @@ abstract class CommonUsbSerialPort implements UsbSerialPort { | |||||||
|     public abstract void setRTS(boolean value) throws IOException; |     public abstract void setRTS(boolean value) throws IOException; | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public boolean purgeHwBuffers(boolean flushReadBuffers, boolean flushWriteBuffers) throws IOException { |     public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException { | ||||||
|         return !flushReadBuffers && !flushWriteBuffers; |         return false; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -379,8 +379,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public boolean purgeHwBuffers(boolean purgeReadBuffers, |         public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException { | ||||||
|                 boolean purgeWriteBuffers) throws IOException { |  | ||||||
|             int value = (purgeReadBuffers ? FLUSH_READ_CODE : 0) |             int value = (purgeReadBuffers ? FLUSH_READ_CODE : 0) | ||||||
|                     | (purgeWriteBuffers ? FLUSH_WRITE_CODE : 0); |                     | (purgeWriteBuffers ? FLUSH_WRITE_CODE : 0); | ||||||
| 
 | 
 | ||||||
| @ -395,7 +394,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver { | |||||||
| 
 | 
 | ||||||
|     public static Map<Integer, int[]> getSupportedDevices() { |     public static Map<Integer, int[]> getSupportedDevices() { | ||||||
|         final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); |         final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); | ||||||
|         supportedDevices.put(Integer.valueOf(UsbId.VENDOR_SILABS), |         supportedDevices.put(UsbId.VENDOR_SILABS, | ||||||
|                 new int[] { |                 new int[] { | ||||||
|             UsbId.SILABS_CP2102, |             UsbId.SILABS_CP2102, | ||||||
|             UsbId.SILABS_CP2105, |             UsbId.SILABS_CP2105, | ||||||
|  | |||||||
| @ -165,7 +165,7 @@ public class FtdiSerialDriver implements UsbSerialDriver { | |||||||
|         private static final int SIO_SET_DATA_REQUEST = 4; |         private static final int SIO_SET_DATA_REQUEST = 4; | ||||||
| 
 | 
 | ||||||
|         private static final int SIO_RESET_SIO = 0; |         private static final int SIO_RESET_SIO = 0; | ||||||
|         private static final int SIO_RESET_PURGE_RX = 1; |         private static final int SIO_RESET_PURGE_RX = 1; // RX @ FTDI device = write @ usb-serial-for-android library | ||||||
|         private static final int SIO_RESET_PURGE_TX = 2; |         private static final int SIO_RESET_PURGE_TX = 2; | ||||||
| 
 | 
 | ||||||
|         public static final int FTDI_DEVICE_OUT_REQTYPE = |         public static final int FTDI_DEVICE_OUT_REQTYPE = | ||||||
| @ -534,20 +534,20 @@ public class FtdiSerialDriver implements UsbSerialDriver { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public boolean purgeHwBuffers(boolean purgeReadBuffers, boolean purgeWriteBuffers) throws IOException { |         public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException { | ||||||
|             if (purgeReadBuffers) { |             if (purgeWriteBuffers) { | ||||||
|                 int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, SIO_RESET_REQUEST, |                 int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, SIO_RESET_REQUEST, | ||||||
|                         SIO_RESET_PURGE_RX, mIndex, null, 0, USB_WRITE_TIMEOUT_MILLIS); |                         SIO_RESET_PURGE_RX, mIndex, null, 0, USB_WRITE_TIMEOUT_MILLIS); | ||||||
|                 if (result != 0) { |                 if (result != 0) { | ||||||
|                     throw new IOException("Flushing RX failed: result=" + result); |                     throw new IOException("purge write buffer failed: result=" + result); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if (purgeWriteBuffers) { |             if (purgeReadBuffers) { | ||||||
|                 int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, SIO_RESET_REQUEST, |                 int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, SIO_RESET_REQUEST, | ||||||
|                         SIO_RESET_PURGE_TX, mIndex, null, 0, USB_WRITE_TIMEOUT_MILLIS); |                         SIO_RESET_PURGE_TX, mIndex, null, 0, USB_WRITE_TIMEOUT_MILLIS); | ||||||
|                 if (result != 0) { |                 if (result != 0) { | ||||||
|                     throw new IOException("Flushing RX failed: result=" + result); |                     throw new IOException("purge read buffer failed: result=" + result); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             return true; |             return true; | ||||||
| @ -556,7 +556,7 @@ public class FtdiSerialDriver implements UsbSerialDriver { | |||||||
| 
 | 
 | ||||||
|     public static Map<Integer, int[]> getSupportedDevices() { |     public static Map<Integer, int[]> getSupportedDevices() { | ||||||
|         final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); |         final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); | ||||||
|         supportedDevices.put(Integer.valueOf(UsbId.VENDOR_FTDI), |         supportedDevices.put(UsbId.VENDOR_FTDI, | ||||||
|                 new int[] { |                 new int[] { | ||||||
|                     UsbId.FTDI_FT232R, |                     UsbId.FTDI_FT232R, | ||||||
|                     UsbId.FTDI_FT232H, |                     UsbId.FTDI_FT232H, | ||||||
|  | |||||||
| @ -88,7 +88,7 @@ public class ProlificSerialDriver implements UsbSerialDriver { | |||||||
|         private static final int READ_ENDPOINT = 0x83; |         private static final int READ_ENDPOINT = 0x83; | ||||||
|         private static final int INTERRUPT_ENDPOINT = 0x81; |         private static final int INTERRUPT_ENDPOINT = 0x81; | ||||||
| 
 | 
 | ||||||
|         private static final int FLUSH_RX_REQUEST = 0x08; |         private static final int FLUSH_RX_REQUEST = 0x08; // RX @ Prolific device = write @ usb-serial-for-android library | ||||||
|         private static final int FLUSH_TX_REQUEST = 0x09; |         private static final int FLUSH_TX_REQUEST = 0x09; | ||||||
| 
 | 
 | ||||||
|         private static final int SET_LINE_REQUEST = 0x20; |         private static final int SET_LINE_REQUEST = 0x20; | ||||||
| @ -553,22 +553,22 @@ public class ProlificSerialDriver implements UsbSerialDriver { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public boolean purgeHwBuffers(boolean purgeReadBuffers, boolean purgeWriteBuffers) throws IOException { |         public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException { | ||||||
|             if (purgeReadBuffers) { |             if (purgeWriteBuffers) { | ||||||
|                 vendorOut(FLUSH_RX_REQUEST, 0, null); |                 vendorOut(FLUSH_RX_REQUEST, 0, null); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if (purgeWriteBuffers) { |             if (purgeReadBuffers) { | ||||||
|                 vendorOut(FLUSH_TX_REQUEST, 0, null); |                 vendorOut(FLUSH_TX_REQUEST, 0, null); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             return purgeReadBuffers || purgeWriteBuffers; |             return true; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static Map<Integer, int[]> getSupportedDevices() { |     public static Map<Integer, int[]> getSupportedDevices() { | ||||||
|         final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); |         final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); | ||||||
|         supportedDevices.put(Integer.valueOf(UsbId.VENDOR_PROLIFIC), |         supportedDevices.put(UsbId.VENDOR_PROLIFIC, | ||||||
|                 new int[] { UsbId.PROLIFIC_PL2303, }); |                 new int[] { UsbId.PROLIFIC_PL2303, }); | ||||||
|         return supportedDevices; |         return supportedDevices; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -216,13 +216,13 @@ public interface UsbSerialPort { | |||||||
|     public void setRTS(boolean value) throws IOException; |     public void setRTS(boolean value) throws IOException; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Flush non-transmitted output data and / or non-read input data |      * purge non-transmitted output data and / or non-read input data | ||||||
|      * @param flushRX {@code true} to flush non-transmitted output data |      * @param purgeWriteBuffers {@code true} to discard non-transmitted output data | ||||||
|      * @param flushTX {@code true} to flush non-read input data |      * @param purgeReadBuffers {@code true} to discard non-read input data | ||||||
|      * @return {@code true} if the operation was successful, or |      * @return {@code true} if the operation was successful, or | ||||||
|      * {@code false} if the operation is not supported by the driver or device |      * {@code false} if the operation is not supported by the driver or device | ||||||
|      * @throws IOException if an error occurred during flush |      * @throws IOException if an error occurred during flush | ||||||
|      */ |      */ | ||||||
|     public boolean purgeHwBuffers(boolean flushRX, boolean flushTX) throws IOException; |     public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user