Skip to content

Commit

Permalink
values supported by default setFlowControl() consistent with getSuppo…
Browse files Browse the repository at this point in the history
…rtedFlowControl()
  • Loading branch information
kai-morich committed Jul 12, 2024
1 parent 0a32c3f commit f538097
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,22 @@ public void controlLines() throws Exception {
}
}

@Test
public void flowControlBase() throws Exception {
usb.open();
usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);
assertEquals(FlowControl.NONE, usb.serialPort.getFlowControl());
assertTrue(usb.serialPort.getSupportedFlowControl().contains(FlowControl.NONE));
for(FlowControl flowControl : FlowControl.values()) {
if(usb.serialPort.getSupportedFlowControl().contains(flowControl)) {
usb.serialPort.setFlowControl(flowControl);
assertEquals(flowControl, usb.serialPort.getFlowControl());
} else {
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.setFlowControl(flowControl));
}
}
}

@Test
public void flowControlXonXoff() throws Exception {
final byte[] off_on = new byte[]{'x',CommonUsbSerialPort.CHAR_XOFF,'y',CommonUsbSerialPort.CHAR_XON,'z'};
Expand All @@ -2006,12 +2022,11 @@ public void flowControlXonXoff() throws Exception {
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD, UsbWrapper.OpenCloseFlags.NO_CONTROL_LINE_INIT));
telnet.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);
assertEquals(FlowControl.NONE, usb.serialPort.getFlowControl());
if (!usb.serialPort.getSupportedFlowControl().contains(FlowControl.XON_XOFF_INLINE) &&
!usb.serialPort.getSupportedFlowControl().contains(FlowControl.XON_XOFF)) {
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.setFlowControl(FlowControl.XON_XOFF_INLINE));
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.setFlowControl(FlowControl.XON_XOFF));
if(!usb.serialPort.getSupportedFlowControl().contains(FlowControl.XON_XOFF)) {
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getXON());
}
if (!usb.serialPort.getSupportedFlowControl().contains(FlowControl.XON_XOFF_INLINE) &&
!usb.serialPort.getSupportedFlowControl().contains(FlowControl.XON_XOFF)) {
Assume.assumeTrue("flow control not supported", false);
}
if (usb.serialPort.getSupportedFlowControl().contains(FlowControl.XON_XOFF_INLINE) &&
Expand All @@ -2020,11 +2035,8 @@ public void flowControlXonXoff() throws Exception {
}
if (usb.serialPort.getSupportedFlowControl().contains(FlowControl.XON_XOFF_INLINE)) {
filter = new XonXoffFilter();
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.setFlowControl(FlowControl.XON_XOFF));
usb.serialPort.setFlowControl(FlowControl.XON_XOFF_INLINE);
assertEquals(FlowControl.XON_XOFF_INLINE, usb.serialPort.getFlowControl());
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.getXON());

assertTrue(filter.getXON());
assertThat(filter.filter(off_on), equalTo(off_on_filtered));
assertTrue(filter.getXON());
Expand All @@ -2034,7 +2046,6 @@ public void flowControlXonXoff() throws Exception {
assertTrue(filter.getXON());
} else {
filter = null;
assertThrows(UnsupportedOperationException.class, () -> usb.serialPort.setFlowControl(FlowControl.XON_XOFF_INLINE));
usb.serialPort.setFlowControl(FlowControl.XON_XOFF);
assertEquals(FlowControl.XON_XOFF, usb.serialPort.getFlowControl());
assertTrue(usb.serialPort.getXON());
Expand Down Expand Up @@ -2203,17 +2214,10 @@ private void flowControlHw(FlowControl flowControl) throws Exception {
usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);

// early exit, if flow control not supported
if (!usb.serialPort.getSupportedFlowControl().contains(flowControl))
Assume.assumeTrue("flow control not supported", false);
assertEquals(usb.inputLinesConnected ? EnumSet.of(ControlLine.RI) : EnumSet.noneOf(ControlLine.class), usb.serialPort.getControlLines()); // [1]
assertEquals(FlowControl.NONE, usb.serialPort.getFlowControl());
try {
usb.serialPort.setFlowControl(flowControl);
} catch (UnsupportedOperationException ignored) {
if (usb.serialPort.getSupportedFlowControl().contains(flowControl)) {
assertTrue("flow control support expected", false);
} else {
Assume.assumeTrue("flow control not supported", false);
}
}
usb.serialPort.setFlowControl(flowControl);
assertEquals(flowControl, usb.serialPort.getFlowControl());
if (!usb.inputLinesConnected)
Assume.assumeTrue("flow control lines not connected", false);
Expand Down Expand Up @@ -2528,7 +2532,8 @@ public void commonMethods() throws Exception {
assertThrows(UnsupportedOperationException.class, wrongSerialPort::getControlLines);
assertEquals(EnumSet.of(FlowControl.NONE), wrongSerialPort.getSupportedFlowControl());
assertEquals(FlowControl.NONE, wrongSerialPort.getFlowControl());
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setFlowControl(FlowControl.NONE));
wrongSerialPort.setFlowControl(FlowControl.NONE);
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setFlowControl(FlowControl.RTS_CTS));
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.purgeHwBuffers(true, true));
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setBreak(true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.InvalidParameterException;
import java.util.EnumSet;

/**
Expand Down Expand Up @@ -334,7 +333,10 @@ public boolean isOpen() {
public EnumSet<ControlLine> getSupportedControlLines() throws IOException { return EnumSet.noneOf(ControlLine.class); }

@Override
public void setFlowControl(FlowControl flowcontrol) throws IOException { throw new UnsupportedOperationException(); }
public void setFlowControl(FlowControl flowcontrol) throws IOException {
if (flowcontrol != FlowControl.NONE)
throw new UnsupportedOperationException();
}

@Override
public FlowControl getFlowControl() { return mFlowControl; }
Expand Down

0 comments on commit f538097

Please sign in to comment.