Skip to content

Commit

Permalink
Merge pull request #297 from mik3y/v3
Browse files Browse the repository at this point in the history
ftdi rewrite, MIT license, ...
  • Loading branch information
kai-morich authored Aug 1, 2020
2 parents e0ed25b + a664082 commit 57d10a0
Show file tree
Hide file tree
Showing 19 changed files with 349 additions and 1,104 deletions.
478 changes: 22 additions & 456 deletions LICENSE.txt

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,6 @@ and devices implementing the CDC/ACM protocol like
* BBC micro:bit using ARM mbed DAPLink firmware
* ...

## Author, License, and Copyright

usb-serial-for-android is written and maintained by *mike wakerly* and *kai morich*

This library is licensed under *LGPL Version 2.1*. Please see LICENSE.txt for the
complete license.

Copyright 2011-2012, Google Inc. All Rights Reserved.

Portions of this library are based on [libftdi](http://www.intra2net.com/en/developer/libftdi).
Please see FtdiSerialDriver.java for more information.

## Help & Discussion

For common problems, see the
Expand Down
4 changes: 4 additions & 0 deletions usbSerialForAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ android {
'rfc2217_server_nonstandard_baudrates': 'true', // true false false
]
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion usbSerialForAndroid/publishToMavenLocal.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ publishing {
maven(MavenPublication) {
groupId 'com.github.mik3y'
artifactId 'usb-serial-for-android'
version '2.2.2a'
version '3.0.0beta'
afterEvaluate {
artifact androidSourcesJar
artifact bundleReleaseAar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

@RunWith(AndroidJUnit4.class)
public class CrossoverTest {
Expand All @@ -51,6 +52,9 @@ protected void starting(Description description) {

@Before
public void setUp() throws Exception {
assumeTrue("ignore test for device specific coverage report",
InstrumentationRegistry.getArguments().getString("test_device_driver") == null);

context = InstrumentationRegistry.getContext();
usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,44 @@ else if (usb.serialDriver instanceof CdcAcmSerialDriver)
assertThat("42000/8N1", data2, equalTo(buf2));
}
}
if (usb.serialDriver instanceof FtdiSerialDriver) {
try {
usb.setParameters(183, 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate to low expected");
} catch (IOException ignored) {
}
usb.setParameters(184, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters( 960000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1043478, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1090909, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1142857, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1200000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1263157, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1333333, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1411764, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(1500000, 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters((int)(2000000/1.04), 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate error expected");
} catch (IOException ignored) {
}
usb.setParameters((int)(2000000/1.03), 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(2000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters((int)(2000000*1.03), 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters((int)(2000000*1.04), 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate error expected");
} catch (IOException ignored) {
}
usb.setParameters(2000000, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(3000000, 8, 1, UsbSerialPort.PARITY_NONE);
try {
usb.setParameters(4000000, 8, 1, UsbSerialPort.PARITY_NONE);
fail("baud rate to high expected");
} catch (IOException ignored) {
}
}
{ // non matching baud rate
telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
usb.setParameters(2400, 8, 1, UsbSerialPort.PARITY_NONE);
Expand Down Expand Up @@ -922,7 +960,13 @@ public void purgeHwBuffers() throws Exception {

usb.write(buf);
Thread.sleep(50); // ~ 12 bytes
boolean purged = usb.serialPort.purgeHwBuffers(true, false);
boolean purged;
try {
usb.serialPort.purgeHwBuffers(true, false);
purged = true;
} catch (UnsupportedOperationException ex) {
purged = false;
}
usb.write("bcd".getBytes());
Thread.sleep(50);
while(data.length()==0 || data.charAt(data.length()-1)!='d')
Expand All @@ -946,7 +990,8 @@ public void purgeHwBuffers() throws Exception {
telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
telnet.write("x".getBytes());
Thread.sleep(10); // ~ 20 bytes
purged = usb.serialPort.purgeHwBuffers(false, true);
if(purged)
usb.serialPort.purgeHwBuffers(false, true);
Log.d(TAG, "purged = " + purged);
telnet.write("y".getBytes());
Thread.sleep(10); // ~ 20 bytes
Expand Down Expand Up @@ -1261,6 +1306,9 @@ public void controlLines() throws Exception {
inputLinesSupported = true;
inputLinesConnected = true;
}
Boolean inputLineFalse = inputLinesSupported ? Boolean.FALSE : null;
Boolean inputLineTrue = inputLinesConnected ? Boolean.TRUE : inputLineFalse;

EnumSet<UsbSerialPort.ControlLine> supportedControlLines = EnumSet.of(UsbSerialPort.ControlLine.RTS, UsbSerialPort.ControlLine.DTR);
if(inputLinesSupported) {
supportedControlLines.add(UsbSerialPort.ControlLine.CTS);
Expand Down Expand Up @@ -1293,12 +1341,12 @@ public void controlLines() throws Exception {
? EnumSet.of(UsbSerialPort.ControlLine.RI)
: EnumSet.noneOf(UsbSerialPort.ControlLine.class),
usb.serialPort.getControlLines());
assertFalse(usb.serialPort.getRTS());
assertFalse(usb.serialPort.getCTS());
assertFalse(usb.serialPort.getDTR());
assertFalse(usb.serialPort.getDSR());
assertFalse(usb.serialPort.getCD());
assertEquals(usb.serialPort.getRI(), inputLinesConnected);
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineTrue));
telnet.write(data);
if(usb.serialDriver instanceof CdcAcmSerialDriver)
// arduino: control line feedback as serial_state notification is not implemented.
Expand All @@ -1316,12 +1364,12 @@ public void controlLines() throws Exception {
? EnumSet.of(UsbSerialPort.ControlLine.RTS, UsbSerialPort.ControlLine.CTS)
: EnumSet.of(UsbSerialPort.ControlLine.RTS),
usb.serialPort.getControlLines());
assertTrue(usb.serialPort.getRTS());
assertEquals(usb.serialPort.getCTS(), inputLinesConnected);
assertFalse(usb.serialPort.getDTR());
assertFalse(usb.serialPort.getDSR());
assertFalse(usb.serialPort.getCD());
assertFalse(usb.serialPort.getRI());
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.TRUE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineTrue));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineFalse));
telnet.write(data);
assertThat(Arrays.toString(data), usb.read(4), equalTo(data));
usb.write(data);
Expand All @@ -1334,12 +1382,12 @@ public void controlLines() throws Exception {
? EnumSet.of(UsbSerialPort.ControlLine.RTS, UsbSerialPort.ControlLine.DTR, UsbSerialPort.ControlLine.CD)
: EnumSet.of(UsbSerialPort.ControlLine.RTS, UsbSerialPort.ControlLine.DTR),
usb.serialPort.getControlLines());
assertTrue(usb.serialPort.getRTS());
assertFalse(usb.serialPort.getCTS());
assertTrue(usb.serialPort.getDTR());
assertFalse(usb.serialPort.getDSR());
assertEquals(usb.serialPort.getCD(), inputLinesConnected);
assertFalse(usb.serialPort.getRI());
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.TRUE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(Boolean.TRUE));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineTrue));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineFalse));
telnet.write(data);
assertThat(Arrays.toString(data), usb.read(4), equalTo(data));
usb.write(data);
Expand All @@ -1352,12 +1400,12 @@ public void controlLines() throws Exception {
? EnumSet.of(UsbSerialPort.ControlLine.DTR, UsbSerialPort.ControlLine.DSR)
: EnumSet.of(UsbSerialPort.ControlLine.DTR),
usb.serialPort.getControlLines());
assertFalse(usb.serialPort.getRTS());
assertFalse(usb.serialPort.getCTS());
assertTrue(usb.serialPort.getDTR());
assertEquals(usb.serialPort.getDSR(), inputLinesConnected);
assertFalse(usb.serialPort.getCD());
assertFalse(usb.serialPort.getRI());
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(Boolean.TRUE));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputLineTrue));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineFalse));
telnet.write(data);
assertThat(Arrays.toString(data), usb.read(4), equalTo(data));
usb.write(data);
Expand All @@ -1366,8 +1414,6 @@ public void controlLines() throws Exception {
// control lines retained over close+open
boolean inputRetained = inputLinesConnected;
boolean outputRetained = true;
if(usb.serialDriver instanceof FtdiSerialDriver)
outputRetained = false; // todo
usb.close(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_CONTROL_LINE_INIT));
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_CONTROL_LINE_INIT, UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
Expand All @@ -1376,12 +1422,12 @@ public void controlLines() throws Exception {
if(outputRetained) retainedControlLines.add(UsbSerialPort.ControlLine.DTR);
if(inputRetained) retainedControlLines.add(UsbSerialPort.ControlLine.DSR);
assertEquals(retainedControlLines, usb.serialPort.getControlLines());
assertFalse(usb.serialPort.getRTS());
assertFalse(usb.serialPort.getCTS());
assertEquals(usb.serialPort.getDTR(), outputRetained);
assertEquals(usb.serialPort.getDSR(), inputRetained);
assertFalse(usb.serialPort.getCD());
assertFalse(usb.serialPort.getRI());
assertThat(usb.getControlLine(usb.serialPort::getRTS), equalTo(Boolean.FALSE));
assertThat(usb.getControlLine(usb.serialPort::getCTS), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getDTR), equalTo(outputRetained));
assertThat(usb.getControlLine(usb.serialPort::getDSR), equalTo(inputRetained ? inputLineTrue : inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getCD), equalTo(inputLineFalse));
assertThat(usb.getControlLine(usb.serialPort::getRI), equalTo(inputLineFalse));

usb.close(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_CONTROL_LINE_INIT));
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_CONTROL_LINE_INIT, UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
Expand All @@ -1400,28 +1446,35 @@ public void controlLines() throws Exception {
// get... error
try {
usb.serialPort.getRI();
if (!inputLinesSupported)
;
else if (usb.serialDriver instanceof ProlificSerialDriver)
if (usb.serialDriver instanceof ProlificSerialDriver)
; // todo: currently not possible to detect, as bulkTransfer in background thread does not distinguish timeout and error
else
fail("error expected");
} catch (IOException ignored) {
} catch (UnsupportedOperationException ignored) {
}
}

@Test
public void deviceConnection() throws Exception {
byte buf[] = new byte[256];
byte[] buf = new byte[256];
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);

usb.write("x".getBytes());
usb.serialPort.read(buf, 1000);
usb.serialPort.setRTS(true);
usb.serialPort.getRI();
boolean purged = usb.serialPort.purgeHwBuffers(true, true);

try {
usb.serialPort.getRI();
} catch (UnsupportedOperationException ignored) {
}
boolean purged;
try {
usb.serialPort.purgeHwBuffers(true, true);
purged = true;
} catch (UnsupportedOperationException ex) {
purged = false;
}
usb.deviceConnection.close();
try {
usb.setParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);
Expand All @@ -1445,13 +1498,12 @@ public void deviceConnection() throws Exception {
fail("setRts error expected");
} catch (IOException ignored) {
}
if(usb.serialPort.getSupportedControlLines().contains(UsbSerialPort.ControlLine.RI) ) {
try {
usb.serialPort.getRI();
if(!(usb.serialDriver instanceof ProlificSerialDriver))
fail("getRI error expected");
} catch (IOException ignored) {
}
try {
usb.serialPort.getRI();
if(!(usb.serialDriver instanceof ProlificSerialDriver))
fail("getRI error expected");
} catch (IOException ignored) {
} catch (UnsupportedOperationException ignored) {
}
if(purged) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Deque;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -223,6 +224,15 @@ public void setParameters(int baudRate, int dataBits, int stopBits, int parity)
Thread.sleep(1);
}

/* return TRUE/FALSE/null instead of true/false/<throw UnsupportedOperationException> */
public Boolean getControlLine(Callable<?> callable) throws Exception {
try {
return (Boolean)callable.call();
} catch (UnsupportedOperationException t) {
return null;
}
}

@Override
public void onNewData(byte[] data) {
long now = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
/* Copyright 2011-2013 Google Inc.
* Copyright 2013 mike wakerly <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* Project home page: https://github.com/mik3y/usb-serial-for-android
*/

Expand Down Expand Up @@ -254,21 +239,6 @@ public void setParameters(int baudRate, int dataBits, int stopBits, int parity)
sendAcmControlMessage(SET_LINE_CODING, 0, msg);
}

@Override
public boolean getCD() throws IOException {
return false; // TODO
}

@Override
public boolean getCTS() throws IOException {
return false; // TODO
}

@Override
public boolean getDSR() throws IOException {
return false; // TODO
}

@Override
public boolean getDTR() throws IOException {
return mDtr;
Expand All @@ -280,11 +250,6 @@ public void setDTR(boolean value) throws IOException {
setDtrRts();
}

@Override
public boolean getRI() throws IOException {
return false; // TODO
}

@Override
public boolean getRTS() throws IOException {
return mRts;
Expand Down
Loading

0 comments on commit 57d10a0

Please sign in to comment.