mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-08 00:16:13 +00:00
Compare commits
No commits in common. "master" and "v3.5.1" have entirely different histories.
7
.github/workflows/build.yml
vendored
7
.github/workflows/build.yml
vendored
@ -8,10 +8,11 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-java@v3
|
- name: set up JDK 1.11
|
||||||
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
distribution: 'temurin'
|
distribution: 'temurin'
|
||||||
java-version: '17'
|
java-version: '11'
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew assembleDebug
|
run: ./gradlew assembleDebug
|
||||||
|
3
.idea/.gitignore
generated
vendored
3
.idea/.gitignore
generated
vendored
@ -3,7 +3,4 @@ codeStyles
|
|||||||
libraries
|
libraries
|
||||||
workspace.xml
|
workspace.xml
|
||||||
androidTestResultsUserPreferences.xml
|
androidTestResultsUserPreferences.xml
|
||||||
appInsightsSettings.xml
|
|
||||||
deploymentTargetDropDown.xml
|
deploymentTargetDropDown.xml
|
||||||
deploymentTargetSelector.xml
|
|
||||||
other.xml
|
|
||||||
|
6
.idea/AndroidProjectSystem.xml
generated
6
.idea/AndroidProjectSystem.xml
generated
@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="AndroidProjectSystem">
|
|
||||||
<option name="providerId" value="com.android.tools.idea.GradleProjectSystem" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
2
.idea/compiler.xml
generated
2
.idea/compiler.xml
generated
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="CompilerConfiguration">
|
<component name="CompilerConfiguration">
|
||||||
<bytecodeTargetLevel target="21" />
|
<bytecodeTargetLevel target="11" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
12
.idea/misc.xml
generated
12
.idea/misc.xml
generated
@ -6,7 +6,7 @@
|
|||||||
<option name="myDefaultNotNull" value="androidx.annotation.NonNull" />
|
<option name="myDefaultNotNull" value="androidx.annotation.NonNull" />
|
||||||
<option name="myNullables">
|
<option name="myNullables">
|
||||||
<value>
|
<value>
|
||||||
<list size="18">
|
<list size="15">
|
||||||
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
|
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
|
||||||
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
|
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
|
||||||
<item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
|
<item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
|
||||||
@ -22,15 +22,12 @@
|
|||||||
<item index="12" class="java.lang.String" itemvalue="org.eclipse.jdt.annotation.Nullable" />
|
<item index="12" class="java.lang.String" itemvalue="org.eclipse.jdt.annotation.Nullable" />
|
||||||
<item index="13" class="java.lang.String" itemvalue="io.reactivex.annotations.Nullable" />
|
<item index="13" class="java.lang.String" itemvalue="io.reactivex.annotations.Nullable" />
|
||||||
<item index="14" class="java.lang.String" itemvalue="io.reactivex.rxjava3.annotations.Nullable" />
|
<item index="14" class="java.lang.String" itemvalue="io.reactivex.rxjava3.annotations.Nullable" />
|
||||||
<item index="15" class="java.lang.String" itemvalue="org.jspecify.nullness.Nullable" />
|
|
||||||
<item index="16" class="java.lang.String" itemvalue="jakarta.annotation.Nullable" />
|
|
||||||
<item index="17" class="java.lang.String" itemvalue="org.jspecify.annotations.Nullable" />
|
|
||||||
</list>
|
</list>
|
||||||
</value>
|
</value>
|
||||||
</option>
|
</option>
|
||||||
<option name="myNotNulls">
|
<option name="myNotNulls">
|
||||||
<value>
|
<value>
|
||||||
<list size="17">
|
<list size="14">
|
||||||
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
|
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
|
||||||
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
|
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
|
||||||
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
|
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
|
||||||
@ -45,14 +42,11 @@
|
|||||||
<item index="11" class="java.lang.String" itemvalue="org.eclipse.jdt.annotation.NonNull" />
|
<item index="11" class="java.lang.String" itemvalue="org.eclipse.jdt.annotation.NonNull" />
|
||||||
<item index="12" class="java.lang.String" itemvalue="io.reactivex.annotations.NonNull" />
|
<item index="12" class="java.lang.String" itemvalue="io.reactivex.annotations.NonNull" />
|
||||||
<item index="13" class="java.lang.String" itemvalue="io.reactivex.rxjava3.annotations.NonNull" />
|
<item index="13" class="java.lang.String" itemvalue="io.reactivex.rxjava3.annotations.NonNull" />
|
||||||
<item index="14" class="java.lang.String" itemvalue="jakarta.annotation.Nonnull" />
|
|
||||||
<item index="15" class="java.lang.String" itemvalue="org.jspecify.nullness.NonNull" />
|
|
||||||
<item index="16" class="java.lang.String" itemvalue="org.jspecify.annotations.NonNull" />
|
|
||||||
</list>
|
</list>
|
||||||
</value>
|
</value>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
17
.idea/runConfigurations.xml
generated
17
.idea/runConfigurations.xml
generated
@ -1,17 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="RunConfigurationProducerService">
|
|
||||||
<option name="ignoredProducers">
|
|
||||||
<set>
|
|
||||||
<option value="com.intellij.execution.junit.AbstractAllInDirectoryConfigurationProducer" />
|
|
||||||
<option value="com.intellij.execution.junit.AllInPackageConfigurationProducer" />
|
|
||||||
<option value="com.intellij.execution.junit.PatternConfigurationProducer" />
|
|
||||||
<option value="com.intellij.execution.junit.TestInClassConfigurationProducer" />
|
|
||||||
<option value="com.intellij.execution.junit.UniqueIdConfigurationProducer" />
|
|
||||||
<option value="com.intellij.execution.junit.testDiscovery.JUnitTestDiscoveryConfigurationProducer" />
|
|
||||||
<option value="org.jetbrains.kotlin.idea.junit.KotlinJUnitRunConfigurationProducer" />
|
|
||||||
<option value="org.jetbrains.kotlin.idea.junit.KotlinPatternConfigurationProducer" />
|
|
||||||
</set>
|
|
||||||
</option>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
35
README.md
35
README.md
@ -1,6 +1,6 @@
|
|||||||
[](https://github.com/mik3y/usb-serial-for-android/actions)
|
[](https://github.com/mik3y/usb-serial-for-android/actions)
|
||||||
[](https://jitpack.io/#mik3y/usb-serial-for-android)
|
[](https://jitpack.io/#mik3y/usb-serial-for-android)
|
||||||
[](https://app.codacy.com/gh/kai-morich/usb-serial-for-android-mik3y/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
[](https://www.codacy.com/gh/kai-morich/usb-serial-for-android-mik3y/dashboard?utm_source=github.com&utm_medium=referral&utm_content=kai-morich/usb-serial-for-android-mik3y&utm_campaign=Badge_Grade)
|
||||||
[](https://codecov.io/gh/mik3y/usb-serial-for-android)
|
[](https://codecov.io/gh/mik3y/usb-serial-for-android)
|
||||||
|
|
||||||
# usb-serial-for-android
|
# usb-serial-for-android
|
||||||
@ -37,15 +37,10 @@ dependencyResolutionManagement {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
If using gradle kotlin use line
|
|
||||||
```gradle.kts
|
|
||||||
maven(url = "https://jitpack.io")
|
|
||||||
```
|
|
||||||
|
|
||||||
Add library to dependencies
|
Add library to dependencies
|
||||||
```gradle
|
```gradle
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.github.mik3y:usb-serial-for-android:3.9.0'
|
implementation 'com.github.mik3y:usb-serial-for-android:3.5.1'
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -115,10 +110,9 @@ For a simple example, see
|
|||||||
[UsbSerialExamples](https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialExamples)
|
[UsbSerialExamples](https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialExamples)
|
||||||
folder in this project.
|
folder in this project.
|
||||||
|
|
||||||
See separate github project [SimpleUsbTerminal](https://github.com/kai-morich/SimpleUsbTerminal)
|
For a more complete example with background service to stay connected while
|
||||||
for a more complete example with:
|
the app is not visible or rotating, see separate github project
|
||||||
* Background service to stay connected while the app is not visible or rotating
|
[SimpleUsbTerminal](https://github.com/kai-morich/SimpleUsbTerminal).
|
||||||
* Flow control
|
|
||||||
|
|
||||||
## Probing for Unrecognized Devices
|
## Probing for Unrecognized Devices
|
||||||
|
|
||||||
@ -145,8 +139,8 @@ UsbSerialProber prober = new UsbSerialProber(customTable);
|
|||||||
List<UsbSerialDriver> drivers = prober.findAllDrivers(usbManager);
|
List<UsbSerialDriver> drivers = prober.findAllDrivers(usbManager);
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
*Note*: as of v3.5.0 this library detects CDC/ACM devices by USB interface types instead of fixed VID+PID,
|
*Note*: as of v3.5.0 this library detects CDC devices by USB interface types instead of fixed VID+PID,
|
||||||
so custom probers are typically not required any more for CDC/ACM devices.
|
so custom probers are typically not required any more for CDC devices.
|
||||||
|
|
||||||
Of course, nothing requires you to use UsbSerialProber at all: you can
|
Of course, nothing requires you to use UsbSerialProber at all: you can
|
||||||
instantiate driver classes directly if you know what you're doing; just supply
|
instantiate driver classes directly if you know what you're doing; just supply
|
||||||
@ -154,21 +148,16 @@ a compatible UsbDevice.
|
|||||||
|
|
||||||
## Compatible Devices
|
## Compatible Devices
|
||||||
|
|
||||||
This library supports USB to serial converter chips with specific drivers
|
This library supports USB to serial converter chips:
|
||||||
* FTDI FT232R, FT232H, FT2232H, FT4232H, FT230X, FT231X, FT234XD
|
* FTDI FT232R, FT232H, FT2232H, FT4232H, FT230X, FT231X, FT234XD
|
||||||
* Prolific PL2303
|
* Prolific PL2303
|
||||||
* Silabs CP2102, CP210*
|
* Silabs CP2102 and all other CP210x
|
||||||
* Qinheng CH340, CH341A
|
* Qinheng CH340, CH341A, CH9102
|
||||||
|
|
||||||
some other device specific drivers
|
and devices implementing the CDC/ACM protocol like
|
||||||
* GsmModem devices, e.g. for Unisoc based Fibocom GSM modems
|
|
||||||
* Chrome OS CCD (Closed Case Debugging)
|
|
||||||
|
|
||||||
and devices implementing the generic CDC/ACM protocol like
|
|
||||||
* Qinheng CH9102
|
|
||||||
* Microchip MCP2221
|
|
||||||
* Arduino using ATmega32U4
|
* Arduino using ATmega32U4
|
||||||
* Digispark using V-USB software USB
|
* Digispark using V-USB software USB
|
||||||
|
* BBC micro:bit using ARM mbed DAPLink firmware
|
||||||
* ...
|
* ...
|
||||||
|
|
||||||
## Help & Discussion
|
## Help & Discussion
|
||||||
|
@ -6,7 +6,7 @@ buildscript {
|
|||||||
google()
|
google()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:8.9.1'
|
classpath 'com.android.tools.build:gradle:7.4.2'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
codecov:
|
|
||||||
max_report_age: off
|
|
||||||
require_ci_to_pass: no
|
|
||||||
notify:
|
|
||||||
wait_for_ci: no
|
|
@ -1,3 +1,2 @@
|
|||||||
android.enableJetifier=true
|
android.enableJetifier=true
|
||||||
android.useAndroidX=true
|
android.useAndroidX=true
|
||||||
android.defaults.buildfeatures.buildconfig=true
|
|
||||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
|||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
jdk:
|
jdk:
|
||||||
- openjdk17
|
- openjdk11
|
||||||
install:
|
install:
|
||||||
- ./gradlew :usbSerialForAndroid:publishToMavenLocal
|
- ./gradlew :usbSerialForAndroid:publishToMavenLocal
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
# `tinyusb_dev_cdc_dual_ports.uf2`
|
|
||||||
|
|
||||||
compiled from `C:/Program Files/Raspberry Pi/Pico SDK v1.5.1/pico-sdk/lib/tinyusb/examples/device/cdc_dual_ports`
|
|
||||||
to `C:/Users/` _user_`/Documents/Pico-v1.5.1/pico-examples/build/usb/device/tinyusb_device_examples/cdc_dual_ports/tinyusb_dev_cdc_dual_ports.uf2`
|
|
||||||
|
|
Binary file not shown.
@ -3,7 +3,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 35
|
compileSdkVersion 33
|
||||||
|
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility JavaVersion.VERSION_1_8
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
@ -12,7 +12,7 @@ android {
|
|||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 17
|
minSdkVersion 17
|
||||||
targetSdkVersion 35
|
targetSdkVersion 33
|
||||||
vectorDrawables.useSupportLibrary = true
|
vectorDrawables.useSupportLibrary = true
|
||||||
|
|
||||||
missingDimensionStrategy 'device', 'anyDevice'
|
missingDimensionStrategy 'device', 'anyDevice'
|
||||||
@ -28,6 +28,6 @@ android {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation project(':usbSerialForAndroid')
|
implementation project(':usbSerialForAndroid')
|
||||||
implementation 'androidx.appcompat:appcompat:1.6.1' // later versions have minsdk 21
|
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||||
implementation 'com.google.android.material:material:1.11.0' // later versions have minsdk 19
|
implementation 'com.google.android.material:material:1.8.0'
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ import android.widget.ToggleButton;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
||||||
@ -90,22 +89,12 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
withIoManager = getArguments().getBoolean("withIoManager");
|
withIoManager = getArguments().getBoolean("withIoManager");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
ContextCompat.registerReceiver(getActivity(), broadcastReceiver, new IntentFilter(INTENT_ACTION_GRANT_USB), ContextCompat.RECEIVER_NOT_EXPORTED);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStop() {
|
|
||||||
getActivity().unregisterReceiver(broadcastReceiver);
|
|
||||||
super.onStop();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
if(!connected && (usbPermission == UsbPermission.Unknown || usbPermission == UsbPermission.Granted))
|
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(INTENT_ACTION_GRANT_USB));
|
||||||
|
|
||||||
|
if(usbPermission == UsbPermission.Unknown || usbPermission == UsbPermission.Granted)
|
||||||
mainLooper.post(this::connect);
|
mainLooper.post(this::connect);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,6 +104,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
status("disconnected");
|
status("disconnected");
|
||||||
disconnect();
|
disconnect();
|
||||||
}
|
}
|
||||||
|
getActivity().unregisterReceiver(broadcastReceiver);
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,7 +204,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
status("connection failed: no driver for device");
|
status("connection failed: no driver for device");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(portNum >= driver.getPorts().size()) {
|
if(driver.getPorts().size() < portNum) {
|
||||||
status("connection failed: not enough ports at device");
|
status("connection failed: not enough ports at device");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -223,9 +213,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
if(usbConnection == null && usbPermission == UsbPermission.Unknown && !usbManager.hasPermission(driver.getDevice())) {
|
if(usbConnection == null && usbPermission == UsbPermission.Unknown && !usbManager.hasPermission(driver.getDevice())) {
|
||||||
usbPermission = UsbPermission.Requested;
|
usbPermission = UsbPermission.Requested;
|
||||||
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_MUTABLE : 0;
|
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_MUTABLE : 0;
|
||||||
Intent intent = new Intent(INTENT_ACTION_GRANT_USB);
|
PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(INTENT_ACTION_GRANT_USB), flags);
|
||||||
intent.setPackage(getActivity().getPackageName());
|
|
||||||
PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, flags);
|
|
||||||
usbManager.requestPermission(driver.getDevice(), usbPermissionIntent);
|
usbManager.requestPermission(driver.getDevice(), usbPermissionIntent);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -239,11 +227,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
usbSerialPort.open(usbConnection);
|
usbSerialPort.open(usbConnection);
|
||||||
try{
|
|
||||||
usbSerialPort.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
|
usbSerialPort.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||||
}catch (UnsupportedOperationException e){
|
|
||||||
status("unsupport setparameters");
|
|
||||||
}
|
|
||||||
if(withIoManager) {
|
if(withIoManager) {
|
||||||
usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
|
usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
|
||||||
usbIoManager.start();
|
usbIoManager.start();
|
||||||
@ -367,7 +351,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
cdBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.CD));
|
cdBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.CD));
|
||||||
riBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.RI));
|
riBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.RI));
|
||||||
mainLooper.postDelayed(runnable, refreshInterval);
|
mainLooper.postDelayed(runnable, refreshInterval);
|
||||||
} catch (Exception e) {
|
} catch (IOException e) {
|
||||||
status("getControlLines() failed: " + e.getMessage() + " -> stopped control line refresh");
|
status("getControlLines() failed: " + e.getMessage() + " -> stopped control line refresh");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -384,15 +368,8 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||||||
if (!controlLines.contains(UsbSerialPort.ControlLine.CD)) cdBtn.setVisibility(View.INVISIBLE);
|
if (!controlLines.contains(UsbSerialPort.ControlLine.CD)) cdBtn.setVisibility(View.INVISIBLE);
|
||||||
if (!controlLines.contains(UsbSerialPort.ControlLine.RI)) riBtn.setVisibility(View.INVISIBLE);
|
if (!controlLines.contains(UsbSerialPort.ControlLine.RI)) riBtn.setVisibility(View.INVISIBLE);
|
||||||
run();
|
run();
|
||||||
} catch (Exception e) {
|
} catch (IOException e) {
|
||||||
Toast.makeText(getActivity(), "getSupportedControlLines() failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
Toast.makeText(getActivity(), "getSupportedControlLines() failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
rtsBtn.setVisibility(View.INVISIBLE);
|
|
||||||
ctsBtn.setVisibility(View.INVISIBLE);
|
|
||||||
dtrBtn.setVisibility(View.INVISIBLE);
|
|
||||||
dsrBtn.setVisibility(View.INVISIBLE);
|
|
||||||
cdBtn.setVisibility(View.INVISIBLE);
|
|
||||||
cdBtn.setVisibility(View.INVISIBLE);
|
|
||||||
riBtn.setVisibility(View.INVISIBLE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,17 +19,14 @@ package com.hoho.android.usbserial.util;
|
|||||||
import java.security.InvalidParameterException;
|
import java.security.InvalidParameterException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clone of Android's /core/java/com/android/internal/util/HexDump class, for use in debugging.
|
* Clone of Android's HexDump class, for use in debugging. Cosmetic changes
|
||||||
* Changes: space separated hex strings
|
* only.
|
||||||
*/
|
*/
|
||||||
public class HexDump {
|
public class HexDump {
|
||||||
private final static char[] HEX_DIGITS = {
|
private final static char[] HEX_DIGITS = {
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
|
||||||
};
|
};
|
||||||
|
|
||||||
private HexDump() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String dumpHexString(byte[] array) {
|
public static String dumpHexString(byte[] array) {
|
||||||
return dumpHexString(array, 0, array.length);
|
return dumpHexString(array, 0, array.length);
|
||||||
}
|
}
|
||||||
@ -85,12 +82,10 @@ public class HexDump {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String toHexString(byte[] array, int offset, int length) {
|
public static String toHexString(byte[] array, int offset, int length) {
|
||||||
char[] buf = new char[length > 0 ? length * 3 - 1 : 0];
|
char[] buf = new char[length * 2];
|
||||||
|
|
||||||
int bufIndex = 0;
|
int bufIndex = 0;
|
||||||
for (int i = offset; i < offset + length; i++) {
|
for (int i = offset; i < offset + length; i++) {
|
||||||
if (i > offset)
|
|
||||||
buf[bufIndex++] = ' ';
|
|
||||||
byte b = array[i];
|
byte b = array[i];
|
||||||
buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
|
buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
|
||||||
buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
|
buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
|
||||||
@ -144,13 +139,13 @@ public class HexDump {
|
|||||||
throw new InvalidParameterException("Invalid hex char '" + c + "'");
|
throw new InvalidParameterException("Invalid hex char '" + c + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** accepts any separator, e.g. space or newline */
|
|
||||||
public static byte[] hexStringToByteArray(String hexString) {
|
public static byte[] hexStringToByteArray(String hexString) {
|
||||||
int length = hexString.length();
|
int length = hexString.length();
|
||||||
byte[] buffer = new byte[(length + 1) / 3];
|
byte[] buffer = new byte[length / 2];
|
||||||
|
|
||||||
for (int i = 0; i < length; i += 3) {
|
for (int i = 0; i < length; i += 2) {
|
||||||
buffer[i / 3] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i + 1)));
|
buffer[i / 2] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString
|
||||||
|
.charAt(i + 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
@ -4,16 +4,16 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 35
|
compileSdkVersion 33
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 17
|
minSdkVersion 17
|
||||||
targetSdkVersion 35
|
targetSdkVersion 33
|
||||||
consumerProguardFiles 'proguard-rules.pro'
|
consumerProguardFiles 'proguard-rules.pro'
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
testInstrumentationRunnerArguments = [ // Raspi Windows LinuxVM ...
|
testInstrumentationRunnerArguments = [ // Raspi Windows LinuxVM ...
|
||||||
'rfc2217_server_host': '192.168.0.78',
|
'rfc2217_server_host': '192.168.0.144',
|
||||||
'rfc2217_server_nonstandard_baudrates': 'true', // true false false
|
'rfc2217_server_nonstandard_baudrates': 'true', // true false false
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -22,23 +22,17 @@ android {
|
|||||||
targetCompatibility JavaVersion.VERSION_1_8
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
}
|
}
|
||||||
namespace 'com.hoho.android.usbserial'
|
namespace 'com.hoho.android.usbserial'
|
||||||
publishing {
|
|
||||||
// if coverage is enabled, change 'release' to 'anyDeviceRelease' or comment out publishing rule
|
|
||||||
singleVariant('release') {
|
|
||||||
withSourcesJar()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "androidx.annotation:annotation:1.9.1"
|
implementation "androidx.annotation:annotation:1.5.0"
|
||||||
|
//testImplementation 'androidx.test:core:1.5.0'
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
testImplementation 'org.mockito:mockito-core:5.15.2'
|
testImplementation 'org.mockito:mockito-core:1.10.19'
|
||||||
androidTestImplementation 'androidx.appcompat:appcompat:1.6.1' // later versions have minsdk 21
|
androidTestImplementation 'androidx.test:core:1.5.0'
|
||||||
androidTestImplementation 'androidx.test:core:1.5.0' // later versions have minsdk 19
|
androidTestImplementation 'androidx.test:runner:1.5.2'
|
||||||
androidTestImplementation 'androidx.test:runner:1.5.2' // later versions have minsdk 19
|
androidTestImplementation 'commons-net:commons-net:3.8.0'
|
||||||
androidTestImplementation 'commons-net:commons-net:3.9.0' // later versions fail on old Android devices with missing java.time.Duration class
|
androidTestImplementation 'org.apache.commons:commons-lang3:3.11'
|
||||||
androidTestImplementation 'org.apache.commons:commons-lang3:3.14.0'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// gradle task: publishToMavenLocal
|
// gradle task: publishToMavenLocal
|
||||||
@ -46,15 +40,21 @@ project.afterEvaluate {
|
|||||||
publishing {
|
publishing {
|
||||||
publications {
|
publications {
|
||||||
release(MavenPublication) {
|
release(MavenPublication) {
|
||||||
from components.release
|
from components.release // change 'release' to 'anyDeviceRelease' if coverage is enabled
|
||||||
|
artifact androidSourcesJar
|
||||||
|
|
||||||
// values used for local maven repo, jitpack uses github release:
|
// values used for local maven repo, jitpack uses github release:
|
||||||
groupId 'com.github.mik3y'
|
groupId 'com.github.mik3y'
|
||||||
artifactId 'usb-serial-for-android'
|
artifactId 'usb-serial-for-android'
|
||||||
version '3.8.0beta'
|
version '3.5.0beta'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task androidSourcesJar(type: Jar) {
|
||||||
|
classifier 'sources'
|
||||||
|
from android.sourceSets.main.java.srcDirs
|
||||||
|
}
|
||||||
|
|
||||||
//apply from: 'coverage.gradle'
|
//apply from: 'coverage.gradle'
|
||||||
|
@ -2,13 +2,13 @@
|
|||||||
apply plugin: 'jacoco'
|
apply plugin: 'jacoco'
|
||||||
|
|
||||||
android {
|
android {
|
||||||
flavorDimensions += 'device'
|
flavorDimensions 'device'
|
||||||
productFlavors {
|
productFlavors {
|
||||||
anyDevice {
|
anyDevice {
|
||||||
// Used as fallback in usbSerialExample/build.gradle -> missingDimensionStrategy, but not for coverage report
|
// Used as fallback in usbSerialExample/build.gradle -> missingDimensionStrategy, but not for coverage report
|
||||||
dimension 'device'
|
dimension 'device'
|
||||||
}
|
}
|
||||||
mcp2221 {
|
arduino {
|
||||||
dimension 'device'
|
dimension 'device'
|
||||||
testInstrumentationRunnerArguments = ['test_device_driver': 'CdcAcm']
|
testInstrumentationRunnerArguments = ['test_device_driver': 'CdcAcm']
|
||||||
}
|
}
|
||||||
@ -18,7 +18,7 @@ android {
|
|||||||
}
|
}
|
||||||
cp2102 { // and cp2105 first port
|
cp2102 { // and cp2105 first port
|
||||||
dimension 'device'
|
dimension 'device'
|
||||||
testInstrumentationRunnerArguments = ['test_device_driver': 'Cp21xx', 'test_device_port': '0']
|
testInstrumentationRunnerArguments = ['test_device_driver': 'Cp21xx']
|
||||||
}
|
}
|
||||||
cp2105 { // second port
|
cp2105 { // second port
|
||||||
dimension 'device'
|
dimension 'device'
|
||||||
@ -26,7 +26,7 @@ android {
|
|||||||
}
|
}
|
||||||
ft232 { // and ft2232 first port
|
ft232 { // and ft2232 first port
|
||||||
dimension 'device'
|
dimension 'device'
|
||||||
testInstrumentationRunnerArguments = ['test_device_driver': 'Ftdi', 'test_device_port': '0']
|
testInstrumentationRunnerArguments = ['test_device_driver': 'Ftdi']
|
||||||
}
|
}
|
||||||
ft2232 { // second port
|
ft2232 { // second port
|
||||||
dimension 'device'
|
dimension 'device'
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -18,7 +18,6 @@ import com.hoho.android.usbserial.driver.CommonUsbSerialPort;
|
|||||||
import com.hoho.android.usbserial.driver.Cp21xxSerialDriver;
|
import com.hoho.android.usbserial.driver.Cp21xxSerialDriver;
|
||||||
import com.hoho.android.usbserial.driver.FtdiSerialDriver;
|
import com.hoho.android.usbserial.driver.FtdiSerialDriver;
|
||||||
import com.hoho.android.usbserial.driver.ProlificSerialDriver;
|
import com.hoho.android.usbserial.driver.ProlificSerialDriver;
|
||||||
import com.hoho.android.usbserial.driver.ProlificSerialPortWrapper;
|
|
||||||
import com.hoho.android.usbserial.driver.UsbId;
|
import com.hoho.android.usbserial.driver.UsbId;
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
||||||
@ -33,8 +32,6 @@ import java.util.concurrent.Callable;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
|
|
||||||
public class UsbWrapper implements SerialInputOutputManager.Listener {
|
public class UsbWrapper implements SerialInputOutputManager.Listener {
|
||||||
|
|
||||||
public final static int USB_READ_WAIT = 500;
|
public final static int USB_READ_WAIT = 500;
|
||||||
@ -59,13 +56,11 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||||||
|
|
||||||
// device properties
|
// device properties
|
||||||
public boolean isCp21xxRestrictedPort; // second port of Cp2105 has limited dataBits, stopBits, parity
|
public boolean isCp21xxRestrictedPort; // second port of Cp2105 has limited dataBits, stopBits, parity
|
||||||
public boolean outputLinesSupported;
|
|
||||||
public boolean inputLinesSupported;
|
public boolean inputLinesSupported;
|
||||||
public boolean inputLinesConnected;
|
public boolean inputLinesConnected;
|
||||||
public boolean inputLinesOnlyRtsCts;
|
public boolean inputLinesOnlyRtsCts;
|
||||||
public int writePacketSize = -1;
|
public int writePacketSize = -1;
|
||||||
public int writeBufferSize = -1;
|
public int writeBufferSize = -1;
|
||||||
public int readBufferSize = -1;
|
|
||||||
|
|
||||||
public UsbWrapper(Context context, UsbSerialDriver serialDriver, int devicePort) {
|
public UsbWrapper(Context context, UsbSerialDriver serialDriver, int devicePort) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
@ -90,11 +85,9 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_MUTABLE : 0;
|
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_MUTABLE : 0;
|
||||||
Intent intent = new Intent("com.android.example.USB_PERMISSION");
|
PendingIntent permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent("com.android.example.USB_PERMISSION"), flags);
|
||||||
intent.setPackage(context.getPackageName());
|
|
||||||
PendingIntent permissionIntent = PendingIntent.getBroadcast(context, 0, intent, flags);
|
|
||||||
IntentFilter filter = new IntentFilter("com.android.example.USB_PERMISSION");
|
IntentFilter filter = new IntentFilter("com.android.example.USB_PERMISSION");
|
||||||
ContextCompat.registerReceiver(context, usbReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
|
context.registerReceiver(usbReceiver, filter);
|
||||||
usbManager.requestPermission(serialDriver.getDevice(), permissionIntent);
|
usbManager.requestPermission(serialDriver.getDevice(), permissionIntent);
|
||||||
for(int i=0; i<5000; i++) {
|
for(int i=0; i<5000; i++) {
|
||||||
if(granted[0] != null) break;
|
if(granted[0] != null) break;
|
||||||
@ -106,10 +99,9 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||||||
|
|
||||||
// extract some device properties:
|
// extract some device properties:
|
||||||
isCp21xxRestrictedPort = serialDriver instanceof Cp21xxSerialDriver && serialDriver.getPorts().size()==2 && serialPort.getPortNumber() == 1;
|
isCp21xxRestrictedPort = serialDriver instanceof Cp21xxSerialDriver && serialDriver.getPorts().size()==2 && serialPort.getPortNumber() == 1;
|
||||||
// output lines are supported by all common drivers
|
// output lines are supported by all drivers
|
||||||
// input lines are supported by all common drivers except CDC
|
// input lines are supported by all drivers except CDC
|
||||||
if (serialDriver instanceof FtdiSerialDriver) {
|
if (serialDriver instanceof FtdiSerialDriver) {
|
||||||
outputLinesSupported = true;
|
|
||||||
inputLinesSupported = true;
|
inputLinesSupported = true;
|
||||||
if(serialDriver.getDevice().getProductId() == UsbId.FTDI_FT2232H)
|
if(serialDriver.getDevice().getProductId() == UsbId.FTDI_FT2232H)
|
||||||
inputLinesConnected = true; // I only have 74LS138 connected at FT2232, not at FT232
|
inputLinesConnected = true; // I only have 74LS138 connected at FT2232, not at FT232
|
||||||
@ -118,21 +110,16 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||||||
inputLinesOnlyRtsCts = true; // I only test with FT230X that has only these 2 control lines. DTR is silently ignored
|
inputLinesOnlyRtsCts = true; // I only test with FT230X that has only these 2 control lines. DTR is silently ignored
|
||||||
}
|
}
|
||||||
} else if (serialDriver instanceof Cp21xxSerialDriver) {
|
} else if (serialDriver instanceof Cp21xxSerialDriver) {
|
||||||
outputLinesSupported = true;
|
|
||||||
inputLinesSupported = true;
|
inputLinesSupported = true;
|
||||||
if(serialDriver.getPorts().size() == 1)
|
if(serialDriver.getPorts().size() == 1)
|
||||||
inputLinesConnected = true; // I only have 74LS138 connected at CP2102, not at CP2105
|
inputLinesConnected = true; // I only have 74LS138 connected at CP2102, not at CP2105
|
||||||
} else if (serialDriver instanceof ProlificSerialDriver) {
|
} else if (serialDriver instanceof ProlificSerialDriver) {
|
||||||
outputLinesSupported = true;
|
|
||||||
inputLinesSupported = true;
|
inputLinesSupported = true;
|
||||||
inputLinesConnected = true;
|
inputLinesConnected = true;
|
||||||
} else if (serialDriver instanceof Ch34xSerialDriver) {
|
} else if (serialDriver instanceof Ch34xSerialDriver) {
|
||||||
outputLinesSupported = true;
|
|
||||||
inputLinesSupported = true;
|
inputLinesSupported = true;
|
||||||
if(serialDriver.getDevice().getProductId() == UsbId.QINHENG_CH340)
|
if(serialDriver.getDevice().getProductId() == UsbId.QINHENG_CH340)
|
||||||
inputLinesConnected = true; // I only have 74LS138 connected at CH340, not connected at CH341A
|
inputLinesConnected = true; // I only have 74LS138 connected at CH340, not connected at CH341A
|
||||||
} else if (serialDriver instanceof CdcAcmSerialDriver) {
|
|
||||||
outputLinesSupported = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serialDriver instanceof Cp21xxSerialDriver) {
|
if (serialDriver instanceof Cp21xxSerialDriver) {
|
||||||
@ -149,18 +136,10 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||||||
case 2: writePacketSize = 512; writeBufferSize = 4096; break;
|
case 2: writePacketSize = 512; writeBufferSize = 4096; break;
|
||||||
case 4: writePacketSize = 512; writeBufferSize = 2048; break;
|
case 4: writePacketSize = 512; writeBufferSize = 2048; break;
|
||||||
}
|
}
|
||||||
if(serialDriver.getDevice().getProductId() == UsbId.FTDI_FT231X)
|
|
||||||
writeBufferSize = 512;
|
|
||||||
} else if (serialDriver instanceof CdcAcmSerialDriver) {
|
} else if (serialDriver instanceof CdcAcmSerialDriver) {
|
||||||
writePacketSize = 16; writeBufferSize = 32; // MCP2221 values, other devices might be different
|
writePacketSize = 64; writeBufferSize = 128;
|
||||||
}
|
}
|
||||||
|
|
||||||
readBufferSize = writeBufferSize;
|
|
||||||
if (serialDriver instanceof Cp21xxSerialDriver && serialDriver.getPorts().size() == 2) {
|
|
||||||
readBufferSize = 256;
|
|
||||||
} else if (serialDriver instanceof FtdiSerialDriver && serialDriver.getPorts().size() == 1 && serialDriver.getDevice().getProductId() != UsbId.FTDI_FT231X) {
|
|
||||||
readBufferSize = 256;
|
|
||||||
} // PL2303 HXN checked in open()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
@ -189,8 +168,6 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||||||
if(!flags.contains(OpenCloseFlags.NO_CONTROL_LINE_INIT)) {
|
if(!flags.contains(OpenCloseFlags.NO_CONTROL_LINE_INIT)) {
|
||||||
serialPort.setDTR(false);
|
serialPort.setDTR(false);
|
||||||
serialPort.setRTS(false);
|
serialPort.setRTS(false);
|
||||||
if (serialPort.getFlowControl() != UsbSerialPort.FlowControl.NONE)
|
|
||||||
serialPort.setFlowControl(UsbSerialPort.FlowControl.NONE);
|
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
@ -240,10 +217,6 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||||||
readBuffer.clear();
|
readBuffer.clear();
|
||||||
}
|
}
|
||||||
readError = null;
|
readError = null;
|
||||||
|
|
||||||
if (serialDriver instanceof ProlificSerialDriver && ProlificSerialPortWrapper.isDeviceTypeHxn(serialPort)) {
|
|
||||||
readBufferSize = 768;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void waitForIoManagerStarted() throws IOException {
|
public void waitForIoManagerStarted() throws IOException {
|
||||||
@ -258,15 +231,12 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||||||
throw new IOException("IoManager not started");
|
throw new IOException("IoManager not started");
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasIoManagerThreads() {
|
public boolean hasIoManagerThread() {
|
||||||
int c = 0;
|
|
||||||
for (Thread thread : Thread.getAllStackTraces().keySet()) {
|
for (Thread thread : Thread.getAllStackTraces().keySet()) {
|
||||||
if (thread.getName().equals(SerialInputOutputManager.class.getSimpleName() + "_read"))
|
if (thread.getName().equals(SerialInputOutputManager.class.getSimpleName()))
|
||||||
c += 1;
|
return true;
|
||||||
if (thread.getName().equals(SerialInputOutputManager.class.getSimpleName() + "_write"))
|
|
||||||
c += 1;
|
|
||||||
}
|
}
|
||||||
return c == 2;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait full time
|
// wait full time
|
||||||
|
@ -12,9 +12,6 @@ import android.hardware.usb.UsbEndpoint;
|
|||||||
import android.hardware.usb.UsbInterface;
|
import android.hardware.usb.UsbInterface;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.hoho.android.usbserial.util.HexDump;
|
|
||||||
import com.hoho.android.usbserial.util.UsbUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
@ -32,8 +29,6 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class CdcAcmSerialDriver implements UsbSerialDriver {
|
public class CdcAcmSerialDriver implements UsbSerialDriver {
|
||||||
|
|
||||||
public static final int USB_SUBCLASS_ACM = 2;
|
|
||||||
|
|
||||||
private final String TAG = CdcAcmSerialDriver.class.getSimpleName();
|
private final String TAG = CdcAcmSerialDriver.class.getSimpleName();
|
||||||
|
|
||||||
private final UsbDevice mDevice;
|
private final UsbDevice mDevice;
|
||||||
@ -60,8 +55,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
int controlInterfaceCount = 0;
|
int controlInterfaceCount = 0;
|
||||||
int dataInterfaceCount = 0;
|
int dataInterfaceCount = 0;
|
||||||
for (int i = 0; i < device.getInterfaceCount(); i++) {
|
for (int i = 0; i < device.getInterfaceCount(); i++) {
|
||||||
if (device.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_COMM &&
|
if (device.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_COMM)
|
||||||
device.getInterface(i).getInterfaceSubclass() == USB_SUBCLASS_ACM)
|
|
||||||
controlInterfaceCount++;
|
controlInterfaceCount++;
|
||||||
if (device.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA)
|
if (device.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA)
|
||||||
dataInterfaceCount++;
|
dataInterfaceCount++;
|
||||||
@ -110,10 +104,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void openInt() throws IOException {
|
protected void openInt() throws IOException {
|
||||||
Log.d(TAG, "interfaces:");
|
|
||||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
|
|
||||||
Log.d(TAG, mDevice.getInterface(i).toString());
|
|
||||||
}
|
|
||||||
if (mPortNumber == -1) {
|
if (mPortNumber == -1) {
|
||||||
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();
|
||||||
@ -149,34 +139,16 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void openInterface() throws IOException {
|
private void openInterface() throws IOException {
|
||||||
|
Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount());
|
||||||
|
|
||||||
mControlInterface = null;
|
int rndisControlInterfaceCount = 0;
|
||||||
mDataInterface = null;
|
|
||||||
int j = getInterfaceIdFromDescriptors();
|
|
||||||
Log.d(TAG, "interface count=" + mDevice.getInterfaceCount() + ", IAD=" + j);
|
|
||||||
if (j >= 0) {
|
|
||||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
|
|
||||||
UsbInterface usbInterface = mDevice.getInterface(i);
|
|
||||||
if (usbInterface.getId() == j || usbInterface.getId() == j+1) {
|
|
||||||
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_COMM &&
|
|
||||||
usbInterface.getInterfaceSubclass() == USB_SUBCLASS_ACM) {
|
|
||||||
mControlIndex = usbInterface.getId();
|
|
||||||
mControlInterface = usbInterface;
|
|
||||||
}
|
|
||||||
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
|
|
||||||
mDataInterface = usbInterface;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (mControlInterface == null || mDataInterface == null) {
|
|
||||||
Log.d(TAG, "no IAD fallback");
|
|
||||||
int controlInterfaceCount = 0;
|
int controlInterfaceCount = 0;
|
||||||
int dataInterfaceCount = 0;
|
int dataInterfaceCount = 0;
|
||||||
|
mControlInterface = null;
|
||||||
|
mDataInterface = null;
|
||||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
|
for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
|
||||||
UsbInterface usbInterface = mDevice.getInterface(i);
|
UsbInterface usbInterface = mDevice.getInterface(i);
|
||||||
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_COMM &&
|
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_COMM) {
|
||||||
usbInterface.getInterfaceSubclass() == USB_SUBCLASS_ACM) {
|
|
||||||
if(controlInterfaceCount == mPortNumber) {
|
if(controlInterfaceCount == mPortNumber) {
|
||||||
mControlIndex = usbInterface.getId();
|
mControlIndex = usbInterface.getId();
|
||||||
mControlInterface = usbInterface;
|
mControlInterface = usbInterface;
|
||||||
@ -184,22 +156,33 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
controlInterfaceCount++;
|
controlInterfaceCount++;
|
||||||
}
|
}
|
||||||
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
|
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
|
||||||
if (dataInterfaceCount == mPortNumber) {
|
if(dataInterfaceCount == mPortNumber + rndisControlInterfaceCount) {
|
||||||
mDataInterface = usbInterface;
|
mDataInterface = usbInterface;
|
||||||
}
|
}
|
||||||
dataInterfaceCount++;
|
dataInterfaceCount++;
|
||||||
}
|
}
|
||||||
|
if (mDataInterface == null &&
|
||||||
|
usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_WIRELESS_CONTROLLER &&
|
||||||
|
usbInterface.getInterfaceSubclass() == 1 &&
|
||||||
|
usbInterface.getInterfaceProtocol() == 3) {
|
||||||
|
/*
|
||||||
|
* RNDIS is a MSFT variant of CDC-ACM states the Linux kernel in rndis_host.c
|
||||||
|
* The devices provide IAD descriptors to indicate consecutive interfaces belonging
|
||||||
|
* together, but this is not exposed to Java. So simply skip related data interfaces.
|
||||||
|
*/
|
||||||
|
rndisControlInterfaceCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mControlInterface == null) {
|
if(mControlInterface == null) {
|
||||||
throw new IOException("No control interface");
|
throw new IOException("No control interface");
|
||||||
}
|
}
|
||||||
Log.d(TAG, "Control interface id " + mControlInterface.getId());
|
Log.d(TAG, "Control iface=" + mControlInterface);
|
||||||
|
|
||||||
if (!mConnection.claimInterface(mControlInterface, true)) {
|
if (!mConnection.claimInterface(mControlInterface, true)) {
|
||||||
throw new IOException("Could not claim control interface");
|
throw new IOException("Could not claim control interface");
|
||||||
}
|
}
|
||||||
|
|
||||||
mControlEndpoint = mControlInterface.getEndpoint(0);
|
mControlEndpoint = mControlInterface.getEndpoint(0);
|
||||||
if (mControlEndpoint.getDirection() != UsbConstants.USB_DIR_IN || mControlEndpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
|
if (mControlEndpoint.getDirection() != UsbConstants.USB_DIR_IN || mControlEndpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
|
||||||
throw new IOException("Invalid control endpoint");
|
throw new IOException("Invalid control endpoint");
|
||||||
@ -208,10 +191,12 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
if(mDataInterface == null) {
|
if(mDataInterface == null) {
|
||||||
throw new IOException("No data interface");
|
throw new IOException("No data interface");
|
||||||
}
|
}
|
||||||
Log.d(TAG, "data interface id " + mDataInterface.getId());
|
Log.d(TAG, "data iface=" + mDataInterface);
|
||||||
|
|
||||||
if (!mConnection.claimInterface(mDataInterface, true)) {
|
if (!mConnection.claimInterface(mDataInterface, true)) {
|
||||||
throw new IOException("Could not claim data interface");
|
throw new IOException("Could not claim data interface");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < mDataInterface.getEndpointCount(); i++) {
|
for (int i = 0; i < mDataInterface.getEndpointCount(); i++) {
|
||||||
UsbEndpoint ep = mDataInterface.getEndpoint(i);
|
UsbEndpoint ep = mDataInterface.getEndpoint(i);
|
||||||
if (ep.getDirection() == UsbConstants.USB_DIR_IN && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
|
if (ep.getDirection() == UsbConstants.USB_DIR_IN && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
|
||||||
@ -221,36 +206,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getInterfaceIdFromDescriptors() {
|
|
||||||
ArrayList<byte[]> descriptors = UsbUtils.getDescriptors(mConnection);
|
|
||||||
Log.d(TAG, "USB descriptor:");
|
|
||||||
for(byte[] descriptor : descriptors)
|
|
||||||
Log.d(TAG, HexDump.toHexString(descriptor));
|
|
||||||
|
|
||||||
if (descriptors.size() > 0 &&
|
|
||||||
descriptors.get(0).length == 18 &&
|
|
||||||
descriptors.get(0)[1] == 1 && // bDescriptorType
|
|
||||||
descriptors.get(0)[4] == (byte)(UsbConstants.USB_CLASS_MISC) && //bDeviceClass
|
|
||||||
descriptors.get(0)[5] == 2 && // bDeviceSubClass
|
|
||||||
descriptors.get(0)[6] == 1) { // bDeviceProtocol
|
|
||||||
// is IAD device, see https://www.usb.org/sites/default/files/iadclasscode_r10.pdf
|
|
||||||
int port = -1;
|
|
||||||
for (int d = 1; d < descriptors.size(); d++) {
|
|
||||||
if (descriptors.get(d).length == 8 &&
|
|
||||||
descriptors.get(d)[1] == 0x0b && // bDescriptorType == IAD
|
|
||||||
descriptors.get(d)[4] == UsbConstants.USB_CLASS_COMM && // bFunctionClass == CDC
|
|
||||||
descriptors.get(d)[5] == USB_SUBCLASS_ACM) { // bFunctionSubClass == ACM
|
|
||||||
port++;
|
|
||||||
if (port == mPortNumber &&
|
|
||||||
descriptors.get(d)[3] == 2) { // bInterfaceCount
|
|
||||||
return descriptors.get(d)[2]; // bFirstInterface
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int sendAcmControlMessage(int request, int value, byte[] buf) throws IOException {
|
private int sendAcmControlMessage(int request, int value, byte[] buf) throws IOException {
|
||||||
int len = mConnection.controlTransfer(
|
int len = mConnection.controlTransfer(
|
||||||
USB_RT_ACM, request, value, mControlIndex, buf, buf != null ? buf.length : 0, 5000);
|
USB_RT_ACM, request, value, mControlIndex, buf, buf != null ? buf.length : 0, 5000);
|
||||||
|
@ -1,91 +0,0 @@
|
|||||||
package com.hoho.android.usbserial.driver;
|
|
||||||
|
|
||||||
import android.hardware.usb.UsbConstants;
|
|
||||||
import android.hardware.usb.UsbDevice;
|
|
||||||
import android.hardware.usb.UsbEndpoint;
|
|
||||||
import android.hardware.usb.UsbInterface;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.EnumSet;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class ChromeCcdSerialDriver implements UsbSerialDriver{
|
|
||||||
|
|
||||||
private final String TAG = ChromeCcdSerialDriver.class.getSimpleName();
|
|
||||||
|
|
||||||
private final UsbDevice mDevice;
|
|
||||||
private final List<UsbSerialPort> mPorts;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UsbDevice getDevice() {
|
|
||||||
return mDevice;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<UsbSerialPort> getPorts() {
|
|
||||||
return mPorts;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ChromeCcdSerialDriver(UsbDevice mDevice) {
|
|
||||||
this.mDevice = mDevice;
|
|
||||||
mPorts = new ArrayList<UsbSerialPort>();
|
|
||||||
for (int i = 0; i < 3; i++)
|
|
||||||
mPorts.add(new ChromeCcdSerialPort(mDevice, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ChromeCcdSerialPort extends CommonUsbSerialPort {
|
|
||||||
private UsbInterface mDataInterface;
|
|
||||||
|
|
||||||
public ChromeCcdSerialPort(UsbDevice device, int portNumber) {
|
|
||||||
super(device, portNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void openInt() throws IOException {
|
|
||||||
Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount());
|
|
||||||
mDataInterface = mDevice.getInterface(mPortNumber);
|
|
||||||
if (!mConnection.claimInterface(mDataInterface, true)) {
|
|
||||||
throw new IOException("Could not claim shared control/data interface");
|
|
||||||
}
|
|
||||||
Log.d(TAG, "endpoint count=" + mDataInterface.getEndpointCount());
|
|
||||||
for (int i = 0; i < mDataInterface.getEndpointCount(); ++i) {
|
|
||||||
UsbEndpoint ep = mDataInterface.getEndpoint(i);
|
|
||||||
if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
|
|
||||||
mReadEndpoint = ep;
|
|
||||||
} else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
|
|
||||||
mWriteEndpoint = ep;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void closeInt() {
|
|
||||||
try {
|
|
||||||
mConnection.releaseInterface(mDataInterface);
|
|
||||||
} catch(Exception ignored) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UsbSerialDriver getDriver() {
|
|
||||||
return ChromeCcdSerialDriver.this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<Integer, int[]> getSupportedDevices() {
|
|
||||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<>();
|
|
||||||
supportedDevices.put(UsbId.VENDOR_GOOGLE, new int[]{
|
|
||||||
UsbId.GOOGLE_CR50,
|
|
||||||
});
|
|
||||||
return supportedDevices;
|
|
||||||
}
|
|
||||||
}
|
|
@ -10,7 +10,6 @@ import android.hardware.usb.UsbDevice;
|
|||||||
import android.hardware.usb.UsbDeviceConnection;
|
import android.hardware.usb.UsbDeviceConnection;
|
||||||
import android.hardware.usb.UsbEndpoint;
|
import android.hardware.usb.UsbEndpoint;
|
||||||
import android.hardware.usb.UsbRequest;
|
import android.hardware.usb.UsbRequest;
|
||||||
import android.os.Build;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.hoho.android.usbserial.util.MonotonicClock;
|
import com.hoho.android.usbserial.util.MonotonicClock;
|
||||||
@ -29,17 +28,16 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
public static boolean DEBUG = false;
|
public static boolean DEBUG = false;
|
||||||
|
|
||||||
private static final String TAG = CommonUsbSerialPort.class.getSimpleName();
|
private static final String TAG = CommonUsbSerialPort.class.getSimpleName();
|
||||||
private static final int MAX_READ_SIZE = 16 * 1024; // = old bulkTransfer limit prior to Android 9
|
private static final int MAX_READ_SIZE = 16 * 1024; // = old bulkTransfer limit
|
||||||
|
|
||||||
protected final UsbDevice mDevice;
|
protected final UsbDevice mDevice;
|
||||||
protected final int mPortNumber;
|
protected final int mPortNumber;
|
||||||
|
|
||||||
// non-null when open()
|
// non-null when open()
|
||||||
protected UsbDeviceConnection mConnection;
|
protected UsbDeviceConnection mConnection = null;
|
||||||
protected UsbEndpoint mReadEndpoint;
|
protected UsbEndpoint mReadEndpoint;
|
||||||
protected UsbEndpoint mWriteEndpoint;
|
protected UsbEndpoint mWriteEndpoint;
|
||||||
protected UsbRequest mUsbRequest;
|
protected UsbRequest mUsbRequest;
|
||||||
protected FlowControl mFlowControl = FlowControl.NONE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal write buffer.
|
* Internal write buffer.
|
||||||
@ -119,7 +117,6 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
throw new IllegalArgumentException("Connection is null");
|
throw new IllegalArgumentException("Connection is null");
|
||||||
}
|
}
|
||||||
mConnection = connection;
|
mConnection = connection;
|
||||||
boolean ok = false;
|
|
||||||
try {
|
try {
|
||||||
openInt();
|
openInt();
|
||||||
if (mReadEndpoint == null || mWriteEndpoint == null) {
|
if (mReadEndpoint == null || mWriteEndpoint == null) {
|
||||||
@ -127,13 +124,11 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
}
|
}
|
||||||
mUsbRequest = new UsbRequest();
|
mUsbRequest = new UsbRequest();
|
||||||
mUsbRequest.initialize(mConnection, mReadEndpoint);
|
mUsbRequest.initialize(mConnection, mReadEndpoint);
|
||||||
ok = true;
|
} catch(Exception e) {
|
||||||
} finally {
|
|
||||||
if (!ok) {
|
|
||||||
try {
|
try {
|
||||||
close();
|
close();
|
||||||
} catch(Exception ignored) {}
|
} catch(Exception ignored) {}
|
||||||
}
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,11 +139,10 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
if (mConnection == null) {
|
if (mConnection == null) {
|
||||||
throw new IOException("Already closed");
|
throw new IOException("Already closed");
|
||||||
}
|
}
|
||||||
UsbRequest usbRequest = mUsbRequest;
|
|
||||||
mUsbRequest = null;
|
|
||||||
try {
|
try {
|
||||||
usbRequest.cancel();
|
mUsbRequest.cancel();
|
||||||
} catch(Exception ignored) {}
|
} catch(Exception ignored) {}
|
||||||
|
mUsbRequest = null;
|
||||||
try {
|
try {
|
||||||
closeInt();
|
closeInt();
|
||||||
} catch(Exception ignored) {}
|
} catch(Exception ignored) {}
|
||||||
@ -163,40 +157,25 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
/**
|
/**
|
||||||
* use simple USB request supported by all devices to test if connection is still valid
|
* use simple USB request supported by all devices to test if connection is still valid
|
||||||
*/
|
*/
|
||||||
protected void testConnection(boolean full) throws IOException {
|
protected void testConnection() throws IOException {
|
||||||
testConnection(full, "USB get_status request failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void testConnection(boolean full, String msg) throws IOException {
|
|
||||||
if(mUsbRequest == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
if(!full) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
byte[] buf = new byte[2];
|
byte[] buf = new byte[2];
|
||||||
int len = mConnection.controlTransfer(0x80 /*DEVICE*/, 0 /*GET_STATUS*/, 0, 0, buf, buf.length, 200);
|
int len = mConnection.controlTransfer(0x80 /*DEVICE*/, 0 /*GET_STATUS*/, 0, 0, buf, buf.length, 200);
|
||||||
if(len < 0)
|
if(len < 0)
|
||||||
throw new IOException(msg);
|
throw new IOException("USB get_status request failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(final byte[] dest, final int timeout) throws IOException {
|
public int read(final byte[] dest, final int timeout) throws IOException {
|
||||||
if(dest.length == 0) {
|
return read(dest, timeout, true);
|
||||||
throw new IllegalArgumentException("Read buffer too small");
|
|
||||||
}
|
|
||||||
return read(dest, dest.length, timeout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
protected int read(final byte[] dest, final int timeout, boolean testConnection) throws IOException {
|
||||||
public int read(final byte[] dest, final int length, final int timeout) throws IOException {return read(dest, length, timeout, true);}
|
if(mConnection == null) {
|
||||||
|
throw new IOException("Connection closed");
|
||||||
protected int read(final byte[] dest, int length, final int timeout, boolean testConnection) throws IOException {
|
}
|
||||||
testConnection(false);
|
if(dest.length <= 0) {
|
||||||
if(length <= 0) {
|
throw new IllegalArgumentException("Read buffer to small");
|
||||||
throw new IllegalArgumentException("Read length too small");
|
|
||||||
}
|
}
|
||||||
length = Math.min(length, dest.length);
|
|
||||||
final int nread;
|
final int nread;
|
||||||
if (timeout != 0) {
|
if (timeout != 0) {
|
||||||
// bulkTransfer will cause data loss with short timeout + high baud rates + continuous transfer
|
// bulkTransfer will cause data loss with short timeout + high baud rates + continuous transfer
|
||||||
@ -208,16 +187,16 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
// /system/lib64/libandroid_runtime.so (android_hardware_UsbDeviceConnection_request_wait(_JNIEnv*, _jobject*, long)+84)
|
// /system/lib64/libandroid_runtime.so (android_hardware_UsbDeviceConnection_request_wait(_JNIEnv*, _jobject*, long)+84)
|
||||||
// data loss / crashes were observed with timeout up to 200 msec
|
// data loss / crashes were observed with timeout up to 200 msec
|
||||||
long endTime = testConnection ? MonotonicClock.millis() + timeout : 0;
|
long endTime = testConnection ? MonotonicClock.millis() + timeout : 0;
|
||||||
int readMax = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) ? length : Math.min(length, MAX_READ_SIZE);
|
int readMax = Math.min(dest.length, MAX_READ_SIZE);
|
||||||
nread = mConnection.bulkTransfer(mReadEndpoint, dest, readMax, timeout);
|
nread = mConnection.bulkTransfer(mReadEndpoint, dest, readMax, timeout);
|
||||||
// Android error propagation is improvable:
|
// Android error propagation is improvable:
|
||||||
// nread == -1 can be: timeout, connection lost, buffer to small, ???
|
// nread == -1 can be: timeout, connection lost, buffer to small, ???
|
||||||
if(nread == -1 && testConnection)
|
if(nread == -1 && testConnection && MonotonicClock.millis() < endTime)
|
||||||
testConnection(MonotonicClock.millis() < endTime);
|
testConnection();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
final ByteBuffer buf = ByteBuffer.wrap(dest, 0, length);
|
final ByteBuffer buf = ByteBuffer.wrap(dest);
|
||||||
if (!mUsbRequest.queue(buf, length)) {
|
if (!mUsbRequest.queue(buf, dest.length)) {
|
||||||
throw new IOException("Queueing USB request failed");
|
throw new IOException("Queueing USB request failed");
|
||||||
}
|
}
|
||||||
final UsbRequest response = mConnection.requestWait();
|
final UsbRequest response = mConnection.requestWait();
|
||||||
@ -228,23 +207,21 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
// Android error propagation is improvable:
|
// Android error propagation is improvable:
|
||||||
// response != null & nread == 0 can be: connection lost, buffer to small, ???
|
// response != null & nread == 0 can be: connection lost, buffer to small, ???
|
||||||
if(nread == 0) {
|
if(nread == 0) {
|
||||||
testConnection(true);
|
testConnection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Math.max(nread, 0);
|
return Math.max(nread, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(byte[] src, int timeout) throws IOException {write(src, src.length, timeout);}
|
public void write(final byte[] src, final int timeout) throws IOException {
|
||||||
|
|
||||||
@Override
|
|
||||||
public void write(final byte[] src, int length, final int timeout) throws IOException {
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
long startTime = MonotonicClock.millis();
|
final long endTime = (timeout == 0) ? 0 : (MonotonicClock.millis() + timeout);
|
||||||
length = Math.min(length, src.length);
|
|
||||||
|
|
||||||
testConnection(false);
|
if(mConnection == null) {
|
||||||
while (offset < length) {
|
throw new IOException("Connection closed");
|
||||||
|
}
|
||||||
|
while (offset < src.length) {
|
||||||
int requestTimeout;
|
int requestTimeout;
|
||||||
final int requestLength;
|
final int requestLength;
|
||||||
final int actualLength;
|
final int actualLength;
|
||||||
@ -255,7 +232,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
if (mWriteBuffer == null) {
|
if (mWriteBuffer == null) {
|
||||||
mWriteBuffer = new byte[mWriteEndpoint.getMaxPacketSize()];
|
mWriteBuffer = new byte[mWriteEndpoint.getMaxPacketSize()];
|
||||||
}
|
}
|
||||||
requestLength = Math.min(length - offset, mWriteBuffer.length);
|
requestLength = Math.min(src.length - offset, mWriteBuffer.length);
|
||||||
if (offset == 0) {
|
if (offset == 0) {
|
||||||
writeBuffer = src;
|
writeBuffer = src;
|
||||||
} else {
|
} else {
|
||||||
@ -266,7 +243,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
if (timeout == 0 || offset == 0) {
|
if (timeout == 0 || offset == 0) {
|
||||||
requestTimeout = timeout;
|
requestTimeout = timeout;
|
||||||
} else {
|
} else {
|
||||||
requestTimeout = (int)(startTime + timeout - MonotonicClock.millis());
|
requestTimeout = (int)(endTime - MonotonicClock.millis());
|
||||||
if(requestTimeout == 0)
|
if(requestTimeout == 0)
|
||||||
requestTimeout = -1;
|
requestTimeout = -1;
|
||||||
}
|
}
|
||||||
@ -276,19 +253,16 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
actualLength = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, requestLength, requestTimeout);
|
actualLength = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, requestLength, requestTimeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
long elapsed = MonotonicClock.millis() - startTime;
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "Wrote " + actualLength + "/" + requestLength + " offset " + offset + "/" + length + " time " + elapsed + "/" + requestTimeout);
|
Log.d(TAG, "Wrote " + actualLength + "/" + requestLength + " offset " + offset + "/" + src.length + " timeout " + requestTimeout);
|
||||||
}
|
}
|
||||||
if (actualLength <= 0) {
|
if (actualLength <= 0) {
|
||||||
String msg = "Error writing " + requestLength + " bytes at offset " + offset + " of total " + src.length + " after " + elapsed + "msec, rc=" + actualLength;
|
if (timeout != 0 && MonotonicClock.millis() >= endTime) {
|
||||||
if (timeout != 0) {
|
SerialTimeoutException ex = new SerialTimeoutException("Error writing " + requestLength + " bytes at offset " + offset + " of total " + src.length + ", rc=" + actualLength);
|
||||||
// could be buffer full because: writing to fast, stopped by flow control
|
ex.bytesTransferred = offset;
|
||||||
testConnection(elapsed < timeout, msg);
|
throw ex;
|
||||||
throw new SerialTimeoutException(msg, offset);
|
|
||||||
} else {
|
} else {
|
||||||
throw new IOException(msg);
|
throw new IOException("Error writing " + requestLength + " bytes at offset " + offset + " of total " + src.length);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
offset += actualLength;
|
offset += actualLength;
|
||||||
@ -297,7 +271,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isOpen() {
|
public boolean isOpen() {
|
||||||
return mUsbRequest != null;
|
return mConnection != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -328,29 +302,16 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
public void setRTS(boolean value) throws IOException { throw new UnsupportedOperationException(); }
|
public void setRTS(boolean value) throws IOException { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EnumSet<ControlLine> getControlLines() throws IOException { throw new UnsupportedOperationException(); }
|
public abstract EnumSet<ControlLine> getControlLines() throws IOException;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EnumSet<ControlLine> getSupportedControlLines() throws IOException { return EnumSet.noneOf(ControlLine.class); }
|
public abstract EnumSet<ControlLine> getSupportedControlLines() throws IOException;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFlowControl(FlowControl flowcontrol) throws IOException {
|
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
|
||||||
if (flowcontrol != FlowControl.NONE)
|
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public FlowControl getFlowControl() { return mFlowControl; }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EnumSet<FlowControl> getSupportedFlowControl() { return EnumSet.of(FlowControl.NONE); }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getXON() throws IOException { throw new UnsupportedOperationException(); }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException { throw new UnsupportedOperationException(); }
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBreak(boolean value) throws IOException { throw new UnsupportedOperationException(); }
|
public void setBreak(boolean value) throws IOException { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@ -60,14 +60,9 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
private static final int SILABSER_SET_LINE_CTL_REQUEST_CODE = 0x03;
|
private static final int SILABSER_SET_LINE_CTL_REQUEST_CODE = 0x03;
|
||||||
private static final int SILABSER_SET_BREAK_REQUEST_CODE = 0x05;
|
private static final int SILABSER_SET_BREAK_REQUEST_CODE = 0x05;
|
||||||
private static final int SILABSER_SET_MHS_REQUEST_CODE = 0x07;
|
private static final int SILABSER_SET_MHS_REQUEST_CODE = 0x07;
|
||||||
private static final int SILABSER_GET_MDMSTS_REQUEST_CODE = 0x08;
|
private static final int SILABSER_SET_BAUDRATE = 0x1E;
|
||||||
private static final int SILABSER_SET_XON_REQUEST_CODE = 0x09;
|
|
||||||
private static final int SILABSER_SET_XOFF_REQUEST_CODE = 0x0A;
|
|
||||||
private static final int SILABSER_GET_COMM_STATUS_REQUEST_CODE = 0x10;
|
|
||||||
private static final int SILABSER_FLUSH_REQUEST_CODE = 0x12;
|
private static final int SILABSER_FLUSH_REQUEST_CODE = 0x12;
|
||||||
private static final int SILABSER_SET_FLOW_REQUEST_CODE = 0x13;
|
private static final int SILABSER_GET_MDMSTS_REQUEST_CODE = 0x08;
|
||||||
private static final int SILABSER_SET_CHARS_REQUEST_CODE = 0x19;
|
|
||||||
private static final int SILABSER_SET_BAUDRATE_REQUEST_CODE = 0x1E;
|
|
||||||
|
|
||||||
private static final int FLUSH_READ_CODE = 0x0a;
|
private static final int FLUSH_READ_CODE = 0x0a;
|
||||||
private static final int FLUSH_WRITE_CODE = 0x05;
|
private static final int FLUSH_WRITE_CODE = 0x05;
|
||||||
@ -89,8 +84,6 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
/*
|
/*
|
||||||
* SILABSER_GET_MDMSTS_REQUEST_CODE
|
* SILABSER_GET_MDMSTS_REQUEST_CODE
|
||||||
*/
|
*/
|
||||||
private static final int STATUS_DTR = 0x01;
|
|
||||||
private static final int STATUS_RTS = 0x02;
|
|
||||||
private static final int STATUS_CTS = 0x10;
|
private static final int STATUS_CTS = 0x10;
|
||||||
private static final int STATUS_DSR = 0x20;
|
private static final int STATUS_DSR = 0x20;
|
||||||
private static final int STATUS_RI = 0x40;
|
private static final int STATUS_RI = 0x40;
|
||||||
@ -125,7 +118,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
byte[] buffer = new byte[1];
|
byte[] buffer = new byte[1];
|
||||||
int result = mConnection.controlTransfer(REQTYPE_DEVICE_TO_HOST, SILABSER_GET_MDMSTS_REQUEST_CODE, 0,
|
int result = mConnection.controlTransfer(REQTYPE_DEVICE_TO_HOST, SILABSER_GET_MDMSTS_REQUEST_CODE, 0,
|
||||||
mPortNumber, buffer, buffer.length, USB_WRITE_TIMEOUT_MILLIS);
|
mPortNumber, buffer, buffer.length, USB_WRITE_TIMEOUT_MILLIS);
|
||||||
if (result != buffer.length) {
|
if (result != 1) {
|
||||||
throw new IOException("Control transfer failed: " + SILABSER_GET_MDMSTS_REQUEST_CODE + " / " + 0 + " -> " + result);
|
throw new IOException("Control transfer failed: " + SILABSER_GET_MDMSTS_REQUEST_CODE + " / " + 0 + " -> " + result);
|
||||||
}
|
}
|
||||||
return buffer[0];
|
return buffer[0];
|
||||||
@ -154,7 +147,6 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
|
setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
|
||||||
setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, (dtr ? DTR_ENABLE : DTR_DISABLE) | (rts ? RTS_ENABLE : RTS_DISABLE));
|
setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, (dtr ? DTR_ENABLE : DTR_DISABLE) | (rts ? RTS_ENABLE : RTS_DISABLE));
|
||||||
setFlowControl(mFlowControl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -174,7 +166,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
(byte) ((baudRate >> 16) & 0xff),
|
(byte) ((baudRate >> 16) & 0xff),
|
||||||
(byte) ((baudRate >> 24) & 0xff)
|
(byte) ((baudRate >> 24) & 0xff)
|
||||||
};
|
};
|
||||||
int ret = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, SILABSER_SET_BAUDRATE_REQUEST_CODE,
|
int ret = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, SILABSER_SET_BAUDRATE,
|
||||||
0, mPortNumber, data, 4, USB_WRITE_TIMEOUT_MILLIS);
|
0, mPortNumber, data, 4, USB_WRITE_TIMEOUT_MILLIS);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
throw new IOException("Error setting baud rate");
|
throw new IOException("Error setting baud rate");
|
||||||
@ -297,11 +289,9 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
public EnumSet<ControlLine> getControlLines() throws IOException {
|
public EnumSet<ControlLine> getControlLines() throws IOException {
|
||||||
byte status = getStatus();
|
byte status = getStatus();
|
||||||
EnumSet<ControlLine> set = EnumSet.noneOf(ControlLine.class);
|
EnumSet<ControlLine> set = EnumSet.noneOf(ControlLine.class);
|
||||||
//if(rts) set.add(ControlLine.RTS); // configured value
|
if(rts) set.add(ControlLine.RTS);
|
||||||
if((status & STATUS_RTS) != 0) set.add(ControlLine.RTS); // actual value
|
|
||||||
if((status & STATUS_CTS) != 0) set.add(ControlLine.CTS);
|
if((status & STATUS_CTS) != 0) set.add(ControlLine.CTS);
|
||||||
//if(dtr) set.add(ControlLine.DTR); // configured value
|
if(dtr) set.add(ControlLine.DTR);
|
||||||
if((status & STATUS_DTR) != 0) set.add(ControlLine.DTR); // actual value
|
|
||||||
if((status & STATUS_DSR) != 0) set.add(ControlLine.DSR);
|
if((status & STATUS_DSR) != 0) set.add(ControlLine.DSR);
|
||||||
if((status & STATUS_CD) != 0) set.add(ControlLine.CD);
|
if((status & STATUS_CD) != 0) set.add(ControlLine.CD);
|
||||||
if((status & STATUS_RI) != 0) set.add(ControlLine.RI);
|
if((status & STATUS_RI) != 0) set.add(ControlLine.RI);
|
||||||
@ -313,73 +303,6 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
return EnumSet.allOf(ControlLine.class);
|
return EnumSet.allOf(ControlLine.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getXON() throws IOException {
|
|
||||||
byte[] buffer = new byte[0x13];
|
|
||||||
int result = mConnection.controlTransfer(REQTYPE_DEVICE_TO_HOST, SILABSER_GET_COMM_STATUS_REQUEST_CODE, 0,
|
|
||||||
mPortNumber, buffer, buffer.length, USB_WRITE_TIMEOUT_MILLIS);
|
|
||||||
if (result != buffer.length) {
|
|
||||||
throw new IOException("Control transfer failed: " + SILABSER_GET_COMM_STATUS_REQUEST_CODE + " -> " + result);
|
|
||||||
}
|
|
||||||
return (buffer[4] & 8) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* emulate external XON/OFF
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public void setXON(boolean value) throws IOException {
|
|
||||||
setConfigSingle(value ? SILABSER_SET_XON_REQUEST_CODE : SILABSER_SET_XOFF_REQUEST_CODE, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setFlowControl(FlowControl flowControl) throws IOException {
|
|
||||||
byte[] data = new byte[16];
|
|
||||||
if(flowControl == FlowControl.RTS_CTS) {
|
|
||||||
data[4] |= 0b1000_0000; // RTS
|
|
||||||
data[0] |= 0b0000_1000; // CTS
|
|
||||||
} else {
|
|
||||||
if(rts)
|
|
||||||
data[4] |= 0b0100_0000;
|
|
||||||
}
|
|
||||||
if(flowControl == FlowControl.DTR_DSR) {
|
|
||||||
data[0] |= 0b0000_0010; // DTR
|
|
||||||
data[0] |= 0b0001_0000; // DSR
|
|
||||||
} else {
|
|
||||||
if(dtr)
|
|
||||||
data[0] |= 0b0000_0001;
|
|
||||||
}
|
|
||||||
if(flowControl == FlowControl.XON_XOFF) {
|
|
||||||
byte[] chars = new byte[]{0, 0, 0, 0, CHAR_XON, CHAR_XOFF};
|
|
||||||
int ret = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, SILABSER_SET_CHARS_REQUEST_CODE,
|
|
||||||
0, mPortNumber, chars, chars.length, USB_WRITE_TIMEOUT_MILLIS);
|
|
||||||
if (ret != chars.length) {
|
|
||||||
throw new IOException("Error setting XON/XOFF chars");
|
|
||||||
}
|
|
||||||
data[4] |= 0b0000_0011;
|
|
||||||
data[7] |= 0b1000_0000;
|
|
||||||
data[8] = (byte)128;
|
|
||||||
data[12] = (byte)128;
|
|
||||||
}
|
|
||||||
if(flowControl == FlowControl.XON_XOFF_INLINE) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
int ret = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, SILABSER_SET_FLOW_REQUEST_CODE,
|
|
||||||
0, mPortNumber, data, data.length, USB_WRITE_TIMEOUT_MILLIS);
|
|
||||||
if (ret != data.length) {
|
|
||||||
throw new IOException("Error setting flow control");
|
|
||||||
}
|
|
||||||
if(flowControl == FlowControl.XON_XOFF) {
|
|
||||||
setXON(true);
|
|
||||||
}
|
|
||||||
mFlowControl = flowControl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EnumSet<FlowControl> getSupportedFlowControl() {
|
|
||||||
return EnumSet.of(FlowControl.NONE, FlowControl.RTS_CTS, FlowControl.DTR_DSR, FlowControl.XON_XOFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
// note: only working on some devices, on other devices ignored w/o error
|
// note: only working on some devices, on other devices ignored w/o error
|
||||||
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
|
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
|
||||||
|
@ -64,7 +64,6 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
private static final int RESET_REQUEST = 0;
|
private static final int RESET_REQUEST = 0;
|
||||||
private static final int MODEM_CONTROL_REQUEST = 1;
|
private static final int MODEM_CONTROL_REQUEST = 1;
|
||||||
private static final int SET_FLOW_CONTROL_REQUEST = 2;
|
|
||||||
private static final int SET_BAUD_RATE_REQUEST = 3;
|
private static final int SET_BAUD_RATE_REQUEST = 3;
|
||||||
private static final int SET_DATA_REQUEST = 4;
|
private static final int SET_DATA_REQUEST = 4;
|
||||||
private static final int GET_MODEM_STATUS_REQUEST = 5;
|
private static final int GET_MODEM_STATUS_REQUEST = 5;
|
||||||
@ -121,7 +120,6 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
throw new IOException("Init RTS,DTR failed: result=" + result);
|
throw new IOException("Init RTS,DTR failed: result=" + result);
|
||||||
}
|
}
|
||||||
setFlowControl(mFlowControl);
|
|
||||||
|
|
||||||
// mDevice.getVersion() would require API 23
|
// mDevice.getVersion() would require API 23
|
||||||
byte[] rawDescriptors = mConnection.getRawDescriptors();
|
byte[] rawDescriptors = mConnection.getRawDescriptors();
|
||||||
@ -141,37 +139,24 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(final byte[] dest, final int timeout) throws IOException
|
public int read(final byte[] dest, final int timeout) throws IOException {
|
||||||
{
|
|
||||||
if(dest.length <= READ_HEADER_LENGTH) {
|
if(dest.length <= READ_HEADER_LENGTH) {
|
||||||
throw new IllegalArgumentException("Read buffer too small");
|
throw new IllegalArgumentException("Read buffer to small");
|
||||||
// could allocate larger buffer, including space for 2 header bytes, but this would
|
// could allocate larger buffer, including space for 2 header bytes, but this would
|
||||||
// result in buffers not being 64 byte aligned any more, causing data loss at continuous
|
// result in buffers not being 64 byte aligned any more, causing data loss at continuous
|
||||||
// data transfer at high baud rates when buffers are fully filled.
|
// data transfer at high baud rates when buffers are fully filled.
|
||||||
}
|
}
|
||||||
return read(dest, dest.length, timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read(final byte[] dest, int length, final int timeout) throws IOException {
|
|
||||||
if(length <= READ_HEADER_LENGTH) {
|
|
||||||
throw new IllegalArgumentException("Read length too small");
|
|
||||||
// could allocate larger buffer, including space for 2 header bytes, but this would
|
|
||||||
// result in buffers not being 64 byte aligned any more, causing data loss at continuous
|
|
||||||
// data transfer at high baud rates when buffers are fully filled.
|
|
||||||
}
|
|
||||||
length = Math.min(length, dest.length);
|
|
||||||
int nread;
|
int nread;
|
||||||
if (timeout != 0) {
|
if (timeout != 0) {
|
||||||
long endTime = MonotonicClock.millis() + timeout;
|
long endTime = MonotonicClock.millis() + timeout;
|
||||||
do {
|
do {
|
||||||
nread = super.read(dest, length, Math.max(1, (int)(endTime - MonotonicClock.millis())), false);
|
nread = super.read(dest, Math.max(1, (int)(endTime - MonotonicClock.millis())), false);
|
||||||
} while (nread == READ_HEADER_LENGTH && MonotonicClock.millis() < endTime);
|
} while (nread == READ_HEADER_LENGTH && MonotonicClock.millis() < endTime);
|
||||||
if(nread <= 0)
|
if(nread <= 0 && MonotonicClock.millis() < endTime)
|
||||||
testConnection(MonotonicClock.millis() < endTime);
|
testConnection();
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
nread = super.read(dest, length, timeout);
|
nread = super.read(dest, timeout, false);
|
||||||
} while (nread == READ_HEADER_LENGTH);
|
} while (nread == READ_HEADER_LENGTH);
|
||||||
}
|
}
|
||||||
return readFilter(dest, nread);
|
return readFilter(dest, nread);
|
||||||
@ -305,7 +290,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
byte[] data = new byte[2];
|
byte[] data = new byte[2];
|
||||||
int result = mConnection.controlTransfer(REQTYPE_DEVICE_TO_HOST, GET_MODEM_STATUS_REQUEST,
|
int result = mConnection.controlTransfer(REQTYPE_DEVICE_TO_HOST, GET_MODEM_STATUS_REQUEST,
|
||||||
0, mPortNumber+1, data, data.length, USB_WRITE_TIMEOUT_MILLIS);
|
0, mPortNumber+1, data, data.length, USB_WRITE_TIMEOUT_MILLIS);
|
||||||
if (result != data.length) {
|
if (result != 2) {
|
||||||
throw new IOException("Get modem status failed: result=" + result);
|
throw new IOException("Get modem status failed: result=" + result);
|
||||||
}
|
}
|
||||||
return data[0];
|
return data[0];
|
||||||
@ -379,38 +364,6 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
return EnumSet.allOf(ControlLine.class);
|
return EnumSet.allOf(ControlLine.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setFlowControl(FlowControl flowControl) throws IOException {
|
|
||||||
int value = 0;
|
|
||||||
int index = mPortNumber+1;
|
|
||||||
switch (flowControl) {
|
|
||||||
case NONE:
|
|
||||||
break;
|
|
||||||
case RTS_CTS:
|
|
||||||
index |= 0x100;
|
|
||||||
break;
|
|
||||||
case DTR_DSR:
|
|
||||||
index |= 0x200;
|
|
||||||
break;
|
|
||||||
case XON_XOFF_INLINE:
|
|
||||||
value = CHAR_XON + (CHAR_XOFF << 8);
|
|
||||||
index |= 0x400;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
int result = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, SET_FLOW_CONTROL_REQUEST,
|
|
||||||
value, index, null, 0, USB_WRITE_TIMEOUT_MILLIS);
|
|
||||||
if (result != 0)
|
|
||||||
throw new IOException("Set flow control failed: result=" + result);
|
|
||||||
mFlowControl = flowControl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EnumSet<FlowControl> getSupportedFlowControl() {
|
|
||||||
return EnumSet.of(FlowControl.NONE, FlowControl.RTS_CTS, FlowControl.DTR_DSR, FlowControl.XON_XOFF_INLINE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
|
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
|
||||||
if (purgeWriteBuffers) {
|
if (purgeWriteBuffers) {
|
||||||
@ -453,7 +406,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
byte[] data = new byte[1];
|
byte[] data = new byte[1];
|
||||||
int result = mConnection.controlTransfer(REQTYPE_DEVICE_TO_HOST, GET_LATENCY_TIMER_REQUEST,
|
int result = mConnection.controlTransfer(REQTYPE_DEVICE_TO_HOST, GET_LATENCY_TIMER_REQUEST,
|
||||||
0, mPortNumber+1, data, data.length, USB_WRITE_TIMEOUT_MILLIS);
|
0, mPortNumber+1, data, data.length, USB_WRITE_TIMEOUT_MILLIS);
|
||||||
if (result != data.length) {
|
if (result != 1) {
|
||||||
throw new IOException("Get latency timer failed: result=" + result);
|
throw new IOException("Get latency timer failed: result=" + result);
|
||||||
}
|
}
|
||||||
return data[0];
|
return data[0];
|
||||||
|
@ -1,102 +0,0 @@
|
|||||||
package com.hoho.android.usbserial.driver;
|
|
||||||
|
|
||||||
import android.hardware.usb.UsbConstants;
|
|
||||||
import android.hardware.usb.UsbDevice;
|
|
||||||
import android.hardware.usb.UsbEndpoint;
|
|
||||||
import android.hardware.usb.UsbInterface;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.EnumSet;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class GsmModemSerialDriver implements UsbSerialDriver{
|
|
||||||
|
|
||||||
private final String TAG = GsmModemSerialDriver.class.getSimpleName();
|
|
||||||
|
|
||||||
private final UsbDevice mDevice;
|
|
||||||
private final UsbSerialPort mPort;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UsbDevice getDevice() {
|
|
||||||
return mDevice;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<UsbSerialPort> getPorts() {
|
|
||||||
return Collections.singletonList(mPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
public GsmModemSerialDriver(UsbDevice mDevice) {
|
|
||||||
this.mDevice = mDevice;
|
|
||||||
mPort = new GsmModemSerialPort(mDevice, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class GsmModemSerialPort extends CommonUsbSerialPort {
|
|
||||||
|
|
||||||
private UsbInterface mDataInterface;
|
|
||||||
|
|
||||||
public GsmModemSerialPort(UsbDevice device, int portNumber) {
|
|
||||||
super(device, portNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void openInt() throws IOException {
|
|
||||||
Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount());
|
|
||||||
mDataInterface = mDevice.getInterface(0);
|
|
||||||
if (!mConnection.claimInterface(mDataInterface, true)) {
|
|
||||||
throw new IOException("Could not claim shared control/data interface");
|
|
||||||
}
|
|
||||||
Log.d(TAG, "endpoint count=" + mDataInterface.getEndpointCount());
|
|
||||||
for (int i = 0; i < mDataInterface.getEndpointCount(); ++i) {
|
|
||||||
UsbEndpoint ep = mDataInterface.getEndpoint(i);
|
|
||||||
if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
|
|
||||||
mReadEndpoint = ep;
|
|
||||||
} else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
|
|
||||||
mWriteEndpoint = ep;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
initGsmModem();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void closeInt() {
|
|
||||||
try {
|
|
||||||
mConnection.releaseInterface(mDataInterface);
|
|
||||||
} catch(Exception ignored) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private int initGsmModem() throws IOException {
|
|
||||||
int len = mConnection.controlTransfer(
|
|
||||||
0x21, 0x22, 0x01, 0, null, 0, 5000);
|
|
||||||
if(len < 0) {
|
|
||||||
throw new IOException("init failed");
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UsbSerialDriver getDriver() {
|
|
||||||
return GsmModemSerialDriver.this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<Integer, int[]> getSupportedDevices() {
|
|
||||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<>();
|
|
||||||
supportedDevices.put(UsbId.VENDOR_UNISOC, new int[]{
|
|
||||||
UsbId.FIBOCOM_L610,
|
|
||||||
UsbId.FIBOCOM_L612,
|
|
||||||
});
|
|
||||||
return supportedDevices;
|
|
||||||
}
|
|
||||||
}
|
|
@ -122,7 +122,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
private volatile Thread mReadStatusThread = null;
|
private volatile Thread mReadStatusThread = null;
|
||||||
private final Object mReadStatusThreadLock = new Object();
|
private final Object mReadStatusThreadLock = new Object();
|
||||||
private boolean mStopReadStatusThread = false;
|
private boolean mStopReadStatusThread = false;
|
||||||
private Exception mReadStatusException = null;
|
private IOException mReadStatusException = null;
|
||||||
|
|
||||||
|
|
||||||
public ProlificSerialPort(UsbDevice device, int portNumber) {
|
public ProlificSerialPort(UsbDevice device, int portNumber) {
|
||||||
@ -201,12 +201,12 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
private void readStatusThreadFunction() {
|
private void readStatusThreadFunction() {
|
||||||
try {
|
try {
|
||||||
byte[] buffer = new byte[STATUS_BUFFER_SIZE];
|
|
||||||
while (!mStopReadStatusThread) {
|
while (!mStopReadStatusThread) {
|
||||||
|
byte[] buffer = new byte[STATUS_BUFFER_SIZE];
|
||||||
long endTime = MonotonicClock.millis() + 500;
|
long endTime = MonotonicClock.millis() + 500;
|
||||||
int readBytesCount = mConnection.bulkTransfer(mInterruptEndpoint, buffer, STATUS_BUFFER_SIZE, 500);
|
int readBytesCount = mConnection.bulkTransfer(mInterruptEndpoint, buffer, STATUS_BUFFER_SIZE, 500);
|
||||||
if(readBytesCount == -1)
|
if(readBytesCount == -1 && MonotonicClock.millis() < endTime)
|
||||||
testConnection(MonotonicClock.millis() < endTime);
|
testConnection();
|
||||||
if (readBytesCount > 0) {
|
if (readBytesCount > 0) {
|
||||||
if (readBytesCount != STATUS_BUFFER_SIZE) {
|
if (readBytesCount != STATUS_BUFFER_SIZE) {
|
||||||
throw new IOException("Invalid status notification, expected " + STATUS_BUFFER_SIZE + " bytes, got " + readBytesCount);
|
throw new IOException("Invalid status notification, expected " + STATUS_BUFFER_SIZE + " bytes, got " + readBytesCount);
|
||||||
@ -217,8 +217,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (IOException e) {
|
||||||
if (isOpen())
|
|
||||||
mReadStatusException = e;
|
mReadStatusException = e;
|
||||||
}
|
}
|
||||||
//Log.d(TAG, "end control line status thread " + mStopReadStatusThread + " " + (mReadStatusException == null ? "-" : mReadStatusException.getMessage()));
|
//Log.d(TAG, "end control line status thread " + mStopReadStatusThread + " " + (mReadStatusException == null ? "-" : mReadStatusException.getMessage()));
|
||||||
@ -250,8 +249,8 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* throw and clear an exception which occurred in the status read thread */
|
/* throw and clear an exception which occured in the status read thread */
|
||||||
Exception readStatusException = mReadStatusException;
|
IOException readStatusException = mReadStatusException;
|
||||||
if (mReadStatusException != null) {
|
if (mReadStatusException != null) {
|
||||||
mReadStatusException = null;
|
mReadStatusException = null;
|
||||||
throw new IOException(readStatusException);
|
throw new IOException(readStatusException);
|
||||||
@ -315,7 +314,6 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
resetDevice();
|
resetDevice();
|
||||||
doBlackMagic();
|
doBlackMagic();
|
||||||
setControlLines(mControlLinesValue);
|
setControlLines(mControlLinesValue);
|
||||||
setFlowControl(mFlowControl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -527,6 +525,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
setControlLines(newControlLinesValue);
|
setControlLines(newControlLinesValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EnumSet<ControlLine> getControlLines() throws IOException {
|
public EnumSet<ControlLine> getControlLines() throws IOException {
|
||||||
int status = getStatus();
|
int status = getStatus();
|
||||||
@ -545,39 +544,6 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
return EnumSet.allOf(ControlLine.class);
|
return EnumSet.allOf(ControlLine.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setFlowControl(FlowControl flowControl) throws IOException {
|
|
||||||
// vendorOut values from https://www.mail-archive.com/linux-usb@vger.kernel.org/msg110968.html
|
|
||||||
switch (flowControl) {
|
|
||||||
case NONE:
|
|
||||||
if (mDeviceType == DeviceType.DEVICE_TYPE_HXN)
|
|
||||||
vendorOut(0x0a, 0xff, null);
|
|
||||||
else
|
|
||||||
vendorOut(0, 0, null);
|
|
||||||
break;
|
|
||||||
case RTS_CTS:
|
|
||||||
if (mDeviceType == DeviceType.DEVICE_TYPE_HXN)
|
|
||||||
vendorOut(0x0a, 0xfa, null);
|
|
||||||
else
|
|
||||||
vendorOut(0, 0x61, null);
|
|
||||||
break;
|
|
||||||
case XON_XOFF_INLINE:
|
|
||||||
if (mDeviceType == DeviceType.DEVICE_TYPE_HXN)
|
|
||||||
vendorOut(0x0a, 0xee, null);
|
|
||||||
else
|
|
||||||
vendorOut(0, 0xc1, null);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
mFlowControl = flowControl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EnumSet<FlowControl> getSupportedFlowControl() {
|
|
||||||
return EnumSet.of(FlowControl.NONE, FlowControl.RTS_CTS, FlowControl.XON_XOFF_INLINE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
|
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException {
|
||||||
if (mDeviceType == DeviceType.DEVICE_TYPE_HXN) {
|
if (mDeviceType == DeviceType.DEVICE_TYPE_HXN) {
|
||||||
|
@ -9,8 +9,7 @@ import java.io.InterruptedIOException;
|
|||||||
* {@see InterruptedIOException#bytesTransferred} may contain bytes transferred
|
* {@see InterruptedIOException#bytesTransferred} may contain bytes transferred
|
||||||
*/
|
*/
|
||||||
public class SerialTimeoutException extends InterruptedIOException {
|
public class SerialTimeoutException extends InterruptedIOException {
|
||||||
public SerialTimeoutException(String s, int bytesTransferred) {
|
public SerialTimeoutException(String s) {
|
||||||
super(s);
|
super(s);
|
||||||
this.bytesTransferred = bytesTransferred;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,17 +37,10 @@ public final class UsbId {
|
|||||||
public static final int PROLIFIC_PL2303GE = 0x23e3; // "
|
public static final int PROLIFIC_PL2303GE = 0x23e3; // "
|
||||||
public static final int PROLIFIC_PL2303GS = 0x23f3; // "
|
public static final int PROLIFIC_PL2303GS = 0x23f3; // "
|
||||||
|
|
||||||
public static final int VENDOR_GOOGLE = 0x18d1;
|
|
||||||
public static final int GOOGLE_CR50 = 0x5014;
|
|
||||||
|
|
||||||
public static final int VENDOR_QINHENG = 0x1a86;
|
public static final int VENDOR_QINHENG = 0x1a86;
|
||||||
public static final int QINHENG_CH340 = 0x7523;
|
public static final int QINHENG_CH340 = 0x7523;
|
||||||
public static final int QINHENG_CH341A = 0x5523;
|
public static final int QINHENG_CH341A = 0x5523;
|
||||||
|
|
||||||
public static final int VENDOR_UNISOC = 0x1782;
|
|
||||||
public static final int FIBOCOM_L610 = 0x4D10;
|
|
||||||
public static final int FIBOCOM_L612 = 0x4D12;
|
|
||||||
|
|
||||||
|
|
||||||
private UsbId() {
|
private UsbId() {
|
||||||
throw new IllegalAccessError("Non-instantiable class");
|
throw new IllegalAccessError("Non-instantiable class");
|
||||||
|
@ -60,15 +60,6 @@ public interface UsbSerialPort extends Closeable {
|
|||||||
/** Values for get[Supported]ControlLines() */
|
/** Values for get[Supported]ControlLines() */
|
||||||
enum ControlLine { RTS, CTS, DTR, DSR, CD, RI }
|
enum ControlLine { RTS, CTS, DTR, DSR, CD, RI }
|
||||||
|
|
||||||
/** Values for (set|get|getSupported)FlowControl() */
|
|
||||||
enum FlowControl { NONE, RTS_CTS, DTR_DSR, XON_XOFF, XON_XOFF_INLINE }
|
|
||||||
|
|
||||||
/** XON character used with flow control XON/XOFF */
|
|
||||||
char CHAR_XON = 17;
|
|
||||||
/** XOFF character used with flow control XON/XOFF */
|
|
||||||
char CHAR_XOFF = 19;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the driver used by this port.
|
* Returns the driver used by this port.
|
||||||
*/
|
*/
|
||||||
@ -131,17 +122,6 @@ public interface UsbSerialPort extends Closeable {
|
|||||||
*/
|
*/
|
||||||
int read(final byte[] dest, final int timeout) throws IOException;
|
int read(final byte[] dest, final int timeout) throws IOException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads bytes with specified length into the destination buffer.
|
|
||||||
*
|
|
||||||
* @param dest the destination byte buffer
|
|
||||||
* @param length the maximum length of the data to read
|
|
||||||
* @param timeout the timeout for reading in milliseconds, 0 is infinite
|
|
||||||
* @return the actual number of bytes read
|
|
||||||
* @throws IOException if an error occurred during reading
|
|
||||||
*/
|
|
||||||
int read(final byte[] dest, int length, final int timeout) throws IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes as many bytes as possible from the source buffer.
|
* Writes as many bytes as possible from the source buffer.
|
||||||
*
|
*
|
||||||
@ -153,18 +133,6 @@ public interface UsbSerialPort extends Closeable {
|
|||||||
*/
|
*/
|
||||||
void write(final byte[] src, final int timeout) throws IOException;
|
void write(final byte[] src, final int timeout) throws IOException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes bytes with specified length from the source buffer.
|
|
||||||
*
|
|
||||||
* @param src the source byte buffer
|
|
||||||
* @param length the length of the data to write
|
|
||||||
* @param timeout the timeout for writing in milliseconds, 0 is infinite
|
|
||||||
* @throws SerialTimeoutException if timeout reached before sending all data.
|
|
||||||
* ex.bytesTransferred may contain bytes transferred
|
|
||||||
* @throws IOException if an error occurred during writing
|
|
||||||
*/
|
|
||||||
void write(final byte[] src, int length, final int timeout) throws IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets various serial port parameters.
|
* Sets various serial port parameters.
|
||||||
*
|
*
|
||||||
@ -175,7 +143,7 @@ public interface UsbSerialPort extends Closeable {
|
|||||||
* @param parity one of {@link #PARITY_NONE}, {@link #PARITY_ODD},
|
* @param parity one of {@link #PARITY_NONE}, {@link #PARITY_ODD},
|
||||||
* {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or {@link #PARITY_SPACE}.
|
* {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or {@link #PARITY_SPACE}.
|
||||||
* @throws IOException on error setting the port parameters
|
* @throws IOException on error setting the port parameters
|
||||||
* @throws UnsupportedOperationException if not supported or values are not supported by a specific device
|
* @throws UnsupportedOperationException if values are not supported by a specific device
|
||||||
*/
|
*/
|
||||||
void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException;
|
void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException;
|
||||||
|
|
||||||
@ -257,7 +225,6 @@ public interface UsbSerialPort extends Closeable {
|
|||||||
*
|
*
|
||||||
* @return EnumSet.contains(...) is {@code true} if set, else {@code false}
|
* @return EnumSet.contains(...) is {@code true} if set, else {@code false}
|
||||||
* @throws IOException if an error occurred during reading
|
* @throws IOException if an error occurred during reading
|
||||||
* @throws UnsupportedOperationException if not supported
|
|
||||||
*/
|
*/
|
||||||
EnumSet<ControlLine> getControlLines() throws IOException;
|
EnumSet<ControlLine> getControlLines() throws IOException;
|
||||||
|
|
||||||
@ -269,36 +236,6 @@ public interface UsbSerialPort extends Closeable {
|
|||||||
*/
|
*/
|
||||||
EnumSet<ControlLine> getSupportedControlLines() throws IOException;
|
EnumSet<ControlLine> getSupportedControlLines() throws IOException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Set flow control mode, if supported
|
|
||||||
* @param flowControl @FlowControl
|
|
||||||
* @throws IOException if an error occurred during writing
|
|
||||||
* @throws UnsupportedOperationException if not supported
|
|
||||||
*/
|
|
||||||
void setFlowControl(FlowControl flowControl) throws IOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get flow control mode.
|
|
||||||
* @return FlowControl
|
|
||||||
*/
|
|
||||||
FlowControl getFlowControl();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get supported flow control modes
|
|
||||||
* @return EnumSet.contains(...) is {@code true} if supported, else {@code false}
|
|
||||||
*/
|
|
||||||
EnumSet<FlowControl> getSupportedFlowControl();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If flow control = XON_XOFF, indicates that send is enabled by XON.
|
|
||||||
* Devices supporting flow control = XON_XOFF_INLINE return CHAR_XON/CHAR_XOFF in read() data.
|
|
||||||
*
|
|
||||||
* @return the current state
|
|
||||||
* @throws IOException if an error occurred during reading
|
|
||||||
* @throws UnsupportedOperationException if not supported
|
|
||||||
*/
|
|
||||||
boolean getXON() throws IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Purge non-transmitted output data and / or non-read input data.
|
* Purge non-transmitted output data and / or non-read input data.
|
||||||
*
|
*
|
||||||
|
@ -37,8 +37,6 @@ public class UsbSerialProber {
|
|||||||
probeTable.addDriver(FtdiSerialDriver.class);
|
probeTable.addDriver(FtdiSerialDriver.class);
|
||||||
probeTable.addDriver(ProlificSerialDriver.class);
|
probeTable.addDriver(ProlificSerialDriver.class);
|
||||||
probeTable.addDriver(Ch34xSerialDriver.class);
|
probeTable.addDriver(Ch34xSerialDriver.class);
|
||||||
probeTable.addDriver(GsmModemSerialDriver.class);
|
|
||||||
probeTable.addDriver(ChromeCcdSerialDriver.class);
|
|
||||||
return probeTable;
|
return probeTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,19 +13,16 @@ import com.hoho.android.usbserial.driver.UsbSerialPort;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class which services a {@link UsbSerialPort} in its {@link #runWrite()} ()} and {@link #runRead()} ()} ()} methods.
|
* Utility class which services a {@link UsbSerialPort} in its {@link #run()} method.
|
||||||
*
|
*
|
||||||
* @author mike wakerly (opensource@hoho.com)
|
* @author mike wakerly (opensource@hoho.com)
|
||||||
*/
|
*/
|
||||||
public class SerialInputOutputManager {
|
public class SerialInputOutputManager implements Runnable {
|
||||||
|
|
||||||
public enum State {
|
public enum State {
|
||||||
STOPPED,
|
STOPPED,
|
||||||
STARTING,
|
|
||||||
RUNNING,
|
RUNNING,
|
||||||
STOPPING
|
STOPPING
|
||||||
}
|
}
|
||||||
@ -35,6 +32,9 @@ public class SerialInputOutputManager {
|
|||||||
private static final String TAG = SerialInputOutputManager.class.getSimpleName();
|
private static final String TAG = SerialInputOutputManager.class.getSimpleName();
|
||||||
private static final int BUFSIZ = 4096;
|
private static final int BUFSIZ = 4096;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* default read timeout is infinite, to avoid data loss with bulkTransfer API
|
||||||
|
*/
|
||||||
private int mReadTimeout = 0;
|
private int mReadTimeout = 0;
|
||||||
private int mWriteTimeout = 0;
|
private int mWriteTimeout = 0;
|
||||||
|
|
||||||
@ -45,8 +45,7 @@ public class SerialInputOutputManager {
|
|||||||
private ByteBuffer mWriteBuffer = ByteBuffer.allocate(BUFSIZ);
|
private ByteBuffer mWriteBuffer = ByteBuffer.allocate(BUFSIZ);
|
||||||
|
|
||||||
private int mThreadPriority = Process.THREAD_PRIORITY_URGENT_AUDIO;
|
private int mThreadPriority = Process.THREAD_PRIORITY_URGENT_AUDIO;
|
||||||
private final AtomicReference<State> mState = new AtomicReference<>(State.STOPPED);
|
private State mState = State.STOPPED; // Synchronized by 'this'
|
||||||
private CountDownLatch mStartuplatch = new CountDownLatch(2);
|
|
||||||
private Listener mListener; // Synchronized by 'this'
|
private Listener mListener; // Synchronized by 'this'
|
||||||
private final UsbSerialPort mSerialPort;
|
private final UsbSerialPort mSerialPort;
|
||||||
|
|
||||||
@ -57,7 +56,7 @@ public class SerialInputOutputManager {
|
|||||||
void onNewData(byte[] data);
|
void onNewData(byte[] data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when {@link SerialInputOutputManager#runRead()} ()} or {@link SerialInputOutputManager#runWrite()} ()} ()} aborts due to an error.
|
* Called when {@link SerialInputOutputManager#run()} aborts due to an error.
|
||||||
*/
|
*/
|
||||||
void onRunError(Exception e);
|
void onRunError(Exception e);
|
||||||
}
|
}
|
||||||
@ -87,9 +86,8 @@ public class SerialInputOutputManager {
|
|||||||
* @param threadPriority see {@link Process#setThreadPriority(int)}
|
* @param threadPriority see {@link Process#setThreadPriority(int)}
|
||||||
* */
|
* */
|
||||||
public void setThreadPriority(int threadPriority) {
|
public void setThreadPriority(int threadPriority) {
|
||||||
if (!mState.compareAndSet(State.STOPPED, State.STOPPED)) {
|
if (mState != State.STOPPED)
|
||||||
throw new IllegalStateException("threadPriority only configurable before SerialInputOutputManager is started");
|
throw new IllegalStateException("threadPriority only configurable before SerialInputOutputManager is started");
|
||||||
}
|
|
||||||
mThreadPriority = threadPriority;
|
mThreadPriority = threadPriority;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +96,7 @@ public class SerialInputOutputManager {
|
|||||||
*/
|
*/
|
||||||
public void setReadTimeout(int timeout) {
|
public void setReadTimeout(int timeout) {
|
||||||
// when set if already running, read already blocks and the new value will not become effective now
|
// when set if already running, read already blocks and the new value will not become effective now
|
||||||
if(mReadTimeout == 0 && timeout != 0 && mState.get() != State.STOPPED)
|
if(mReadTimeout == 0 && timeout != 0 && mState != State.STOPPED)
|
||||||
throw new IllegalStateException("readTimeout only configurable before SerialInputOutputManager is started");
|
throw new IllegalStateException("readTimeout only configurable before SerialInputOutputManager is started");
|
||||||
mReadTimeout = timeout;
|
mReadTimeout = timeout;
|
||||||
}
|
}
|
||||||
@ -146,152 +144,79 @@ public class SerialInputOutputManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* write data asynchronously
|
* when using writeAsync, it is recommended to use readTimeout != 0,
|
||||||
|
* else the write will be delayed until read data is available
|
||||||
*/
|
*/
|
||||||
public void writeAsync(byte[] data) {
|
public void writeAsync(byte[] data) {
|
||||||
synchronized (mWriteBufferLock) {
|
synchronized (mWriteBufferLock) {
|
||||||
mWriteBuffer.put(data);
|
mWriteBuffer.put(data);
|
||||||
mWriteBufferLock.notifyAll(); // Notify waiting threads
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start SerialInputOutputManager in separate threads
|
* start SerialInputOutputManager in separate thread
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public void start() {
|
||||||
if(mState.compareAndSet(State.STOPPED, State.STARTING)) {
|
if(mState != State.STOPPED)
|
||||||
mStartuplatch = new CountDownLatch(2);
|
|
||||||
new Thread(this::runRead, this.getClass().getSimpleName() + "_read").start();
|
|
||||||
new Thread(this::runWrite, this.getClass().getSimpleName() + "_write").start();
|
|
||||||
try {
|
|
||||||
mStartuplatch.await();
|
|
||||||
mState.set(State.RUNNING);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new IllegalStateException("already started");
|
throw new IllegalStateException("already started");
|
||||||
}
|
new Thread(this, this.getClass().getSimpleName()).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stop SerialInputOutputManager threads
|
* stop SerialInputOutputManager thread
|
||||||
*
|
*
|
||||||
* when using readTimeout == 0 (default), additionally use usbSerialPort.close() to
|
* when using readTimeout == 0 (default), additionally use usbSerialPort.close() to
|
||||||
* interrupt blocking read
|
* interrupt blocking read
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public synchronized void stop() {
|
||||||
if(mState.compareAndSet(State.RUNNING, State.STOPPING)) {
|
if (getState() == State.RUNNING) {
|
||||||
synchronized (mWriteBufferLock) {
|
|
||||||
mWriteBufferLock.notifyAll(); // wake up write thread to check the stop condition
|
|
||||||
}
|
|
||||||
Log.i(TAG, "Stop requested");
|
Log.i(TAG, "Stop requested");
|
||||||
|
mState = State.STOPPING;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public State getState() {
|
public synchronized State getState() {
|
||||||
return mState.get();
|
return mState;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if the thread is still running
|
* Continuously services the read and write buffers until {@link #stop()} is
|
||||||
|
* called, or until a driver exception is raised.
|
||||||
*/
|
*/
|
||||||
private boolean isStillRunning() {
|
@Override
|
||||||
State state = mState.get();
|
public void run() {
|
||||||
return ((state == State.RUNNING) || (state == State.STARTING))
|
synchronized (this) {
|
||||||
&& !Thread.currentThread().isInterrupted();
|
if (getState() != State.STOPPED) {
|
||||||
|
throw new IllegalStateException("Already running");
|
||||||
}
|
}
|
||||||
|
mState = State.RUNNING;
|
||||||
/**
|
}
|
||||||
* Notify listener of an error
|
Log.i(TAG, "Running ...");
|
||||||
*
|
|
||||||
* @param e the exception
|
|
||||||
*/
|
|
||||||
private void notifyErrorListener(Throwable e) {
|
|
||||||
Listener listener = getListener();
|
|
||||||
if (listener != null) {
|
|
||||||
try {
|
try {
|
||||||
listener.onRunError(e instanceof Exception ? (Exception) e : new Exception(e));
|
if(mThreadPriority != Process.THREAD_PRIORITY_DEFAULT)
|
||||||
} catch (Throwable t) {
|
|
||||||
Log.w(TAG, "Exception in onRunError: " + t.getMessage(), t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the thread priority
|
|
||||||
*/
|
|
||||||
private void setThreadPriority() {
|
|
||||||
if (mThreadPriority != Process.THREAD_PRIORITY_DEFAULT) {
|
|
||||||
Process.setThreadPriority(mThreadPriority);
|
Process.setThreadPriority(mThreadPriority);
|
||||||
|
while (true) {
|
||||||
|
if (getState() != State.RUNNING) {
|
||||||
|
Log.i(TAG, "Stopping mState=" + getState());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
step();
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
/**
|
Log.w(TAG, "Run ending due to exception: " + e.getMessage(), e);
|
||||||
* Continuously services the read buffers until {@link #stop()} is called, or until a driver exception is
|
final Listener listener = getListener();
|
||||||
* raised.
|
if (listener != null) {
|
||||||
*/
|
listener.onRunError(e);
|
||||||
void runRead() {
|
|
||||||
Log.i(TAG, "runRead running ...");
|
|
||||||
try {
|
|
||||||
setThreadPriority();
|
|
||||||
mStartuplatch.countDown();
|
|
||||||
do {
|
|
||||||
stepRead();
|
|
||||||
} while (isStillRunning());
|
|
||||||
Log.i(TAG, "runRead: Stopping mState=" + getState());
|
|
||||||
} catch (Throwable e) {
|
|
||||||
if (Thread.currentThread().isInterrupted()) {
|
|
||||||
Log.w(TAG, "runRead: interrupted");
|
|
||||||
} else if(mSerialPort.isOpen()) {
|
|
||||||
Log.w(TAG, "runRead ending due to exception: " + e.getMessage(), e);
|
|
||||||
} else {
|
|
||||||
Log.i(TAG, "runRead: Socket closed");
|
|
||||||
}
|
}
|
||||||
notifyErrorListener(e);
|
|
||||||
} finally {
|
} finally {
|
||||||
if (mState.compareAndSet(State.RUNNING, State.STOPPING)) {
|
synchronized (this) {
|
||||||
synchronized (mWriteBufferLock) {
|
mState = State.STOPPED;
|
||||||
mWriteBufferLock.notifyAll(); // wake up write thread to check the stop condition
|
Log.i(TAG, "Stopped");
|
||||||
}
|
|
||||||
} else if (mState.compareAndSet(State.STOPPING, State.STOPPED)) {
|
|
||||||
Log.i(TAG, "runRead: Stopped mState=" + getState());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void step() throws IOException {
|
||||||
* Continuously services the write buffers until {@link #stop()} is called, or until a driver exception is
|
|
||||||
* raised.
|
|
||||||
*/
|
|
||||||
void runWrite() {
|
|
||||||
Log.i(TAG, "runWrite running ...");
|
|
||||||
try {
|
|
||||||
setThreadPriority();
|
|
||||||
mStartuplatch.countDown();
|
|
||||||
do {
|
|
||||||
stepWrite();
|
|
||||||
} while (isStillRunning());
|
|
||||||
Log.i(TAG, "runWrite: Stopping mState=" + getState());
|
|
||||||
} catch (Throwable e) {
|
|
||||||
if (Thread.currentThread().isInterrupted()) {
|
|
||||||
Log.w(TAG, "runWrite: interrupted");
|
|
||||||
} else if(mSerialPort.isOpen()) {
|
|
||||||
Log.w(TAG, "runWrite ending due to exception: " + e.getMessage(), e);
|
|
||||||
} else {
|
|
||||||
Log.i(TAG, "runWrite: Socket closed");
|
|
||||||
}
|
|
||||||
notifyErrorListener(e);
|
|
||||||
} finally {
|
|
||||||
if (!mState.compareAndSet(State.RUNNING, State.STOPPING)) {
|
|
||||||
if (mState.compareAndSet(State.STOPPING, State.STOPPED)) {
|
|
||||||
Log.i(TAG, "runWrite: Stopped mState=" + getState());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void stepRead() throws IOException {
|
|
||||||
// Handle incoming data.
|
// Handle incoming data.
|
||||||
byte[] buffer;
|
byte[] buffer;
|
||||||
synchronized (mReadBufferLock) {
|
synchronized (mReadBufferLock) {
|
||||||
@ -309,26 +234,21 @@ public class SerialInputOutputManager {
|
|||||||
listener.onNewData(data);
|
listener.onNewData(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void stepWrite() throws IOException, InterruptedException {
|
|
||||||
// Handle outgoing data.
|
// Handle outgoing data.
|
||||||
byte[] buffer = null;
|
buffer = null;
|
||||||
synchronized (mWriteBufferLock) {
|
synchronized (mWriteBufferLock) {
|
||||||
int len = mWriteBuffer.position();
|
len = mWriteBuffer.position();
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
buffer = new byte[len];
|
buffer = new byte[len];
|
||||||
mWriteBuffer.rewind();
|
mWriteBuffer.rewind();
|
||||||
mWriteBuffer.get(buffer, 0, len);
|
mWriteBuffer.get(buffer, 0, len);
|
||||||
mWriteBuffer.clear();
|
mWriteBuffer.clear();
|
||||||
mWriteBufferLock.notifyAll(); // Notify writeAsync that there is space in the buffer
|
|
||||||
} else {
|
|
||||||
mWriteBufferLock.wait();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (buffer != null) {
|
if (buffer != null) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "Writing data len=" + buffer.length);
|
Log.d(TAG, "Writing data len=" + len);
|
||||||
}
|
}
|
||||||
mSerialPort.write(buffer, mWriteTimeout);
|
mSerialPort.write(buffer, mWriteTimeout);
|
||||||
}
|
}
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
package com.hoho.android.usbserial.util;
|
|
||||||
|
|
||||||
import android.hardware.usb.UsbDeviceConnection;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class UsbUtils {
|
|
||||||
|
|
||||||
private UsbUtils() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ArrayList<byte[]> getDescriptors(UsbDeviceConnection connection) {
|
|
||||||
ArrayList<byte[]> descriptors = new ArrayList<>();
|
|
||||||
byte[] rawDescriptors = connection.getRawDescriptors();
|
|
||||||
if (rawDescriptors != null) {
|
|
||||||
int pos = 0;
|
|
||||||
while (pos < rawDescriptors.length) {
|
|
||||||
int len = rawDescriptors[pos] & 0xFF;
|
|
||||||
if (len == 0)
|
|
||||||
break;
|
|
||||||
if (pos + len > rawDescriptors.length)
|
|
||||||
len = rawDescriptors.length - pos;
|
|
||||||
byte[] descriptor = new byte[len];
|
|
||||||
System.arraycopy(rawDescriptors, pos, descriptor, 0, len);
|
|
||||||
descriptors.add(descriptor);
|
|
||||||
pos += len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return descriptors;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
package com.hoho.android.usbserial.util;
|
|
||||||
|
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Some devices return XON and XOFF characters inline in read() data.
|
|
||||||
* Other devices return XON / XOFF condition thru getXOFF() method.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class XonXoffFilter {
|
|
||||||
private boolean xon = true;
|
|
||||||
|
|
||||||
public XonXoffFilter() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getXON() {
|
|
||||||
return xon;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter XON/XOFF from read() data and remember
|
|
||||||
*
|
|
||||||
* @param data unfiltered data
|
|
||||||
* @return filtered data
|
|
||||||
*/
|
|
||||||
public byte[] filter(byte[] data) {
|
|
||||||
int found = 0;
|
|
||||||
for (int i=0; i<data.length; i++) {
|
|
||||||
if (data[i] == UsbSerialPort.CHAR_XON || data[i] == UsbSerialPort.CHAR_XOFF)
|
|
||||||
found++;
|
|
||||||
}
|
|
||||||
if(found == 0)
|
|
||||||
return data;
|
|
||||||
byte[] filtered = new byte[data.length - found];
|
|
||||||
for (int i=0, j=0; i<data.length; i++) {
|
|
||||||
if (data[i] == UsbSerialPort.CHAR_XON)
|
|
||||||
xon = true;
|
|
||||||
else if(data[i] == UsbSerialPort.CHAR_XOFF)
|
|
||||||
xon = false;
|
|
||||||
else
|
|
||||||
filtered[j++] = data[i];
|
|
||||||
}
|
|
||||||
return filtered;
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,11 +17,6 @@ public class Log {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int w(String tag, String msg, Throwable tr) {
|
|
||||||
System.out.println("WARN: " + tag + ": " + msg + " / " + tr.getMessage());
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int e(String tag, String msg) {
|
public static int e(String tag, String msg) {
|
||||||
System.out.println("ERROR: " + tag + ": " + msg);
|
System.out.println("ERROR: " + tag + ": " + msg);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
package com.hoho.android.usbserial.driver;
|
package com.hoho.android.usbserial.driver;
|
||||||
|
|
||||||
import static com.hoho.android.usbserial.driver.CdcAcmSerialDriver.USB_SUBCLASS_ACM;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertThrows;
|
import static org.junit.Assert.assertThrows;
|
||||||
import static org.mockito.Mockito.clearInvocations;
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.times;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.hardware.usb.UsbConstants;
|
import android.hardware.usb.UsbConstants;
|
||||||
@ -16,8 +12,6 @@ import android.hardware.usb.UsbDeviceConnection;
|
|||||||
import android.hardware.usb.UsbEndpoint;
|
import android.hardware.usb.UsbEndpoint;
|
||||||
import android.hardware.usb.UsbInterface;
|
import android.hardware.usb.UsbInterface;
|
||||||
|
|
||||||
import com.hoho.android.usbserial.util.HexDump;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -34,37 +28,14 @@ public class CdcAcmSerialDriverTest {
|
|||||||
UsbEndpoint readEndpoint = mock(UsbEndpoint.class);
|
UsbEndpoint readEndpoint = mock(UsbEndpoint.class);
|
||||||
UsbEndpoint writeEndpoint = mock(UsbEndpoint.class);
|
UsbEndpoint writeEndpoint = mock(UsbEndpoint.class);
|
||||||
|
|
||||||
/*
|
|
||||||
* digispark - no IAD
|
|
||||||
* UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=2,mSubclass=2,mProtocol=1,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=131,mAttributes=3,mMaxPacketSize=8,mInterval=255]]
|
|
||||||
* UsbInterface[mId=1,mAlternateSetting=0,mName=null,mClass=10,mSubclass=0,mProtocol=0,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=1,mAttributes=2,mMaxPacketSize=8,mInterval=0]
|
|
||||||
* UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=8,mInterval=0]]
|
|
||||||
*/
|
|
||||||
when(usbDeviceConnection.getRawDescriptors()).thenReturn(HexDump.hexStringToByteArray(
|
|
||||||
"12 01 10 01 02 00 00 08 D0 16 7E 08 00 01 01 02 00 01\n" +
|
|
||||||
"09 02 43 00 02 01 00 80 32\n" +
|
|
||||||
"09 04 00 00 01 02 02 01 00\n" +
|
|
||||||
"05 24 00 10 01\n" +
|
|
||||||
"04 24 02 02\n" +
|
|
||||||
"05 24 06 00 01\n" +
|
|
||||||
"05 24 01 03 01\n" +
|
|
||||||
"07 05 83 03 08 00 FF\n" +
|
|
||||||
"09 04 01 00 02 0A 00 00 00\n" +
|
|
||||||
"07 05 01 02 08 00 00\n" +
|
|
||||||
"07 05 81 02 08 00 00"));
|
|
||||||
when(usbDeviceConnection.claimInterface(controlInterface,true)).thenReturn(true);
|
when(usbDeviceConnection.claimInterface(controlInterface,true)).thenReturn(true);
|
||||||
when(usbDeviceConnection.claimInterface(dataInterface,true)).thenReturn(true);
|
when(usbDeviceConnection.claimInterface(dataInterface,true)).thenReturn(true);
|
||||||
when(usbDevice.getInterfaceCount()).thenReturn(2);
|
when(usbDevice.getInterfaceCount()).thenReturn(2);
|
||||||
when(usbDevice.getInterface(0)).thenReturn(controlInterface);
|
when(usbDevice.getInterface(0)).thenReturn(controlInterface);
|
||||||
when(usbDevice.getInterface(1)).thenReturn(dataInterface);
|
when(usbDevice.getInterface(1)).thenReturn(dataInterface);
|
||||||
when(controlInterface.getId()).thenReturn(0);
|
|
||||||
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
||||||
when(controlInterface.getInterfaceSubclass()).thenReturn(USB_SUBCLASS_ACM);
|
|
||||||
when(controlInterface.getEndpointCount()).thenReturn(1);
|
when(controlInterface.getEndpointCount()).thenReturn(1);
|
||||||
when(controlInterface.getEndpoint(0)).thenReturn(controlEndpoint);
|
when(controlInterface.getEndpoint(0)).thenReturn(controlEndpoint);
|
||||||
when(dataInterface.getId()).thenReturn(1);
|
|
||||||
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
||||||
when(dataInterface.getEndpointCount()).thenReturn(2);
|
when(dataInterface.getEndpointCount()).thenReturn(2);
|
||||||
when(dataInterface.getEndpoint(0)).thenReturn(writeEndpoint);
|
when(dataInterface.getEndpoint(0)).thenReturn(writeEndpoint);
|
||||||
@ -125,7 +96,7 @@ public class CdcAcmSerialDriverTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void multiPortDevice() throws Exception {
|
public void multiPortDevice() throws Exception {
|
||||||
int n = 2;
|
int n = 4;
|
||||||
UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class);
|
UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class);
|
||||||
UsbDevice usbDevice = mock(UsbDevice.class);
|
UsbDevice usbDevice = mock(UsbDevice.class);
|
||||||
UsbInterface[] controlInterfaces = new UsbInterface[n];
|
UsbInterface[] controlInterfaces = new UsbInterface[n];
|
||||||
@ -134,42 +105,6 @@ public class CdcAcmSerialDriverTest {
|
|||||||
UsbEndpoint[] readEndpoints = new UsbEndpoint[n];
|
UsbEndpoint[] readEndpoints = new UsbEndpoint[n];
|
||||||
UsbEndpoint[] writeEndpoints = new UsbEndpoint[n];
|
UsbEndpoint[] writeEndpoints = new UsbEndpoint[n];
|
||||||
|
|
||||||
/*
|
|
||||||
* pi zero - dual port
|
|
||||||
* UsbInterface[mId=0,mAlternateSetting=0,mName=TinyUSB CDC,mClass=2,mSubclass=2,mProtocol=0,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=8,mInterval=16]]
|
|
||||||
* UsbInterface[mId=1,mAlternateSetting=0,mName=null,mClass=10,mSubclass=0,mProtocol=0,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=64,mInterval=0]
|
|
||||||
* UsbEndpoint[mAddress=130,mAttributes=2,mMaxPacketSize=64,mInterval=0]]
|
|
||||||
* UsbInterface[mId=2,mAlternateSetting=0,mName=TinyUSB CDC,mClass=2,mSubclass=2,mProtocol=0,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=131,mAttributes=3,mMaxPacketSize=8,mInterval=16]]
|
|
||||||
* UsbInterface[mId=3,mAlternateSetting=0,mName=null,mClass=10,mSubclass=0,mProtocol=0,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=4,mAttributes=2,mMaxPacketSize=64,mInterval=0]
|
|
||||||
* UsbEndpoint[mAddress=132,mAttributes=2,mMaxPacketSize=64,mInterval=0]]
|
|
||||||
*/
|
|
||||||
when(usbDeviceConnection.getRawDescriptors()).thenReturn(HexDump.hexStringToByteArray(
|
|
||||||
"12 01 00 02 EF 02 01 40 FE CA 02 40 00 01 01 02 03 01\n" +
|
|
||||||
"09 02 8D 00 04 01 00 80 32\n" +
|
|
||||||
"08 0B 00 02 02 02 00 00\n" +
|
|
||||||
"09 04 00 00 01 02 02 00 04\n" +
|
|
||||||
"05 24 00 20 01\n" +
|
|
||||||
"05 24 01 00 01\n" +
|
|
||||||
"04 24 02 02\n" +
|
|
||||||
"05 24 06 00 01\n" +
|
|
||||||
"07 05 81 03 08 00 10\n" +
|
|
||||||
"09 04 01 00 02 0A 00 00 00\n" +
|
|
||||||
"07 05 02 02 40 00 00\n" +
|
|
||||||
"07 05 82 02 40 00 00\n" +
|
|
||||||
"08 0B 02 02 02 02 00 00\n" +
|
|
||||||
"09 04 02 00 01 02 02 00 04\n" +
|
|
||||||
"05 24 00 20 01\n" +
|
|
||||||
"05 24 01 00 03\n" +
|
|
||||||
"04 24 02 02\n" +
|
|
||||||
"05 24 06 02 03\n" +
|
|
||||||
"07 05 83 03 08 00 10\n" +
|
|
||||||
"09 04 03 00 02 0A 00 00 00\n" +
|
|
||||||
"07 05 04 02 40 00 00\n" +
|
|
||||||
"07 05 84 02 40 00 00\n"));
|
|
||||||
when(usbDevice.getInterfaceCount()).thenReturn(2*n);
|
when(usbDevice.getInterfaceCount()).thenReturn(2*n);
|
||||||
for(int i=0; i<n; i++) {
|
for(int i=0; i<n; i++) {
|
||||||
controlInterfaces[i] = mock(UsbInterface.class);
|
controlInterfaces[i] = mock(UsbInterface.class);
|
||||||
@ -179,14 +114,11 @@ public class CdcAcmSerialDriverTest {
|
|||||||
writeEndpoints[i] = mock(UsbEndpoint.class);
|
writeEndpoints[i] = mock(UsbEndpoint.class);
|
||||||
when(usbDeviceConnection.claimInterface(controlInterfaces[i], true)).thenReturn(true);
|
when(usbDeviceConnection.claimInterface(controlInterfaces[i], true)).thenReturn(true);
|
||||||
when(usbDeviceConnection.claimInterface(dataInterfaces[i], true)).thenReturn(true);
|
when(usbDeviceConnection.claimInterface(dataInterfaces[i], true)).thenReturn(true);
|
||||||
when(usbDevice.getInterface(2*i )).thenReturn(controlInterfaces[i]);
|
when(usbDevice.getInterface(2*i+0)).thenReturn(controlInterfaces[i]);
|
||||||
when(usbDevice.getInterface(2*i+1)).thenReturn(dataInterfaces[i]);
|
when(usbDevice.getInterface(2*i+1)).thenReturn(dataInterfaces[i]);
|
||||||
when(controlInterfaces[i].getId()).thenReturn(2*i);
|
|
||||||
when(controlInterfaces[i].getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
when(controlInterfaces[i].getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
||||||
when(controlInterfaces[i].getInterfaceSubclass()).thenReturn(USB_SUBCLASS_ACM);
|
|
||||||
when(controlInterfaces[i].getEndpointCount()).thenReturn(1);
|
when(controlInterfaces[i].getEndpointCount()).thenReturn(1);
|
||||||
when(controlInterfaces[i].getEndpoint(0)).thenReturn(controlEndpoints[i]);
|
when(controlInterfaces[i].getEndpoint(0)).thenReturn(controlEndpoints[i]);
|
||||||
when(dataInterfaces[i].getId()).thenReturn(2*i+1);
|
|
||||||
when(dataInterfaces[i].getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
when(dataInterfaces[i].getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
||||||
when(dataInterfaces[i].getEndpointCount()).thenReturn(2);
|
when(dataInterfaces[i].getEndpointCount()).thenReturn(2);
|
||||||
when(dataInterfaces[i].getEndpoint(0)).thenReturn(writeEndpoints[i]);
|
when(dataInterfaces[i].getEndpoint(0)).thenReturn(writeEndpoints[i]);
|
||||||
@ -198,39 +130,18 @@ public class CdcAcmSerialDriverTest {
|
|||||||
when(writeEndpoints[i].getDirection()).thenReturn(UsbConstants.USB_DIR_OUT);
|
when(writeEndpoints[i].getDirection()).thenReturn(UsbConstants.USB_DIR_OUT);
|
||||||
when(writeEndpoints[i].getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK);
|
when(writeEndpoints[i].getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK);
|
||||||
}
|
}
|
||||||
int i = 1;
|
int i = 2;
|
||||||
CdcAcmSerialDriver driver = new CdcAcmSerialDriver(usbDevice);
|
CdcAcmSerialDriver driver = new CdcAcmSerialDriver(usbDevice);
|
||||||
CdcAcmSerialDriver.CdcAcmSerialPort port = (CdcAcmSerialDriver.CdcAcmSerialPort) driver.getPorts().get(i);
|
CdcAcmSerialDriver.CdcAcmSerialPort port = (CdcAcmSerialDriver.CdcAcmSerialPort) driver.getPorts().get(i);
|
||||||
port.mConnection = usbDeviceConnection;
|
port.mConnection = usbDeviceConnection;
|
||||||
// reset invocations from countPorts()
|
|
||||||
clearInvocations(controlInterfaces[0]);
|
|
||||||
clearInvocations(controlInterfaces[1]);
|
|
||||||
|
|
||||||
port.openInt();
|
port.openInt();
|
||||||
assertEquals(readEndpoints[i], port.mReadEndpoint);
|
assertEquals(readEndpoints[i], port.mReadEndpoint);
|
||||||
assertEquals(writeEndpoints[i], port.mWriteEndpoint);
|
assertEquals(writeEndpoints[i], port.mWriteEndpoint);
|
||||||
verify(controlInterfaces[0], times(0)).getInterfaceClass(); // not openInterface with 'no IAD fallback'
|
|
||||||
verify(controlInterfaces[1], times(2)).getInterfaceClass(); // openInterface with IAD
|
|
||||||
port.closeInt();
|
|
||||||
clearInvocations(controlInterfaces[0]);
|
|
||||||
clearInvocations(controlInterfaces[1]);
|
|
||||||
|
|
||||||
when(usbDeviceConnection.getRawDescriptors()).thenReturn(null);
|
|
||||||
port.openInt();
|
|
||||||
verify(controlInterfaces[0], times(2)).getInterfaceClass(); // openInterface with 'no IAD fallback'
|
|
||||||
verify(controlInterfaces[1], times(2)).getInterfaceClass(); // openInterface with 'no IAD fallback'
|
|
||||||
port.closeInt();
|
|
||||||
clearInvocations(controlInterfaces[0]);
|
|
||||||
clearInvocations(controlInterfaces[1]);
|
|
||||||
|
|
||||||
when(usbDeviceConnection.getRawDescriptors()).thenReturn(HexDump.hexStringToByteArray("01 02 02 82 02")); // truncated descriptor
|
|
||||||
port.openInt();
|
|
||||||
verify(controlInterfaces[0], times(2)).getInterfaceClass(); // openInterface with 'no IAD fallback'
|
|
||||||
verify(controlInterfaces[1], times(2)).getInterfaceClass(); // openInterface with 'no IAD fallback'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void compositeDevice() throws Exception {
|
public void compositeDevice() throws Exception {
|
||||||
|
// mock BBC micro:bit
|
||||||
UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class);
|
UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class);
|
||||||
UsbDevice usbDevice = mock(UsbDevice.class);
|
UsbDevice usbDevice = mock(UsbDevice.class);
|
||||||
UsbInterface massStorageInterface = mock(UsbInterface.class);
|
UsbInterface massStorageInterface = mock(UsbInterface.class);
|
||||||
@ -242,42 +153,6 @@ public class CdcAcmSerialDriverTest {
|
|||||||
UsbEndpoint readEndpoint = mock(UsbEndpoint.class);
|
UsbEndpoint readEndpoint = mock(UsbEndpoint.class);
|
||||||
UsbEndpoint writeEndpoint = mock(UsbEndpoint.class);
|
UsbEndpoint writeEndpoint = mock(UsbEndpoint.class);
|
||||||
|
|
||||||
/*
|
|
||||||
* BBC micro:bit
|
|
||||||
* UsbInterface[mId=0,mAlternateSetting=0,mName=USB_MSC,mClass=8,mSubclass=6,mProtocol=80,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=130,mAttributes=2,mMaxPacketSize=64,mInterval=0]
|
|
||||||
* UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=64,mInterval=0]]
|
|
||||||
* UsbInterface[mId=1,mAlternateSetting=0,mName=mbed Serial Port,mClass=2,mSubclass=2,mProtocol=1,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=131,mAttributes=3,mMaxPacketSize=16,mInterval=32]]
|
|
||||||
* UsbInterface[mId=2,mAlternateSetting=0,mName=mbed Serial Port,mClass=10,mSubclass=0,mProtocol=0,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=4,mAttributes=2,mMaxPacketSize=64,mInterval=0]
|
|
||||||
* UsbEndpoint[mAddress=132,mAttributes=2,mMaxPacketSize=64,mInterval=0]]
|
|
||||||
* UsbInterface[mId=3,mAlternateSetting=0,mName=CMSIS-DAP,mClass=3,mSubclass=0,mProtocol=0,mEndpoints=[
|
|
||||||
* UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=64,mInterval=1]
|
|
||||||
* UsbEndpoint[mAddress=1,mAttributes=3,mMaxPacketSize=64,mInterval=1]]
|
|
||||||
* UsbInterface[mId=4,mAlternateSetting=0,mName=WebUSB: CMSIS-DAP,mClass=255,mSubclass=3,mProtocol=0,mEndpoints=[]
|
|
||||||
*/
|
|
||||||
when(usbDeviceConnection.getRawDescriptors()).thenReturn(HexDump.hexStringToByteArray(
|
|
||||||
"12 01 10 02 EF 02 01 40 28 0D 04 02 00 10 01 02 03 01\n" +
|
|
||||||
"09 02 8B 00 05 01 00 80 FA\n" +
|
|
||||||
"09 04 00 00 02 08 06 50 08\n" +
|
|
||||||
"07 05 82 02 40 00 00\n" +
|
|
||||||
"07 05 02 02 40 00 00\n" +
|
|
||||||
"08 0B 01 02 02 02 01 04\n" +
|
|
||||||
"09 04 01 00 01 02 02 01 04\n" +
|
|
||||||
"05 24 00 10 01\n" +
|
|
||||||
"05 24 01 03 02\n" +
|
|
||||||
"04 24 02 06\n" +
|
|
||||||
"05 24 06 01 02\n" +
|
|
||||||
"07 05 83 03 10 00 20\n" +
|
|
||||||
"09 04 02 00 02 0A 00 00 05\n" +
|
|
||||||
"07 05 04 02 40 00 00\n" +
|
|
||||||
"07 05 84 02 40 00 00\n" +
|
|
||||||
"09 04 03 00 02 03 00 00 06\n" +
|
|
||||||
"09 21 00 01 00 01 22 21 00\n" +
|
|
||||||
"07 05 81 03 40 00 01\n" +
|
|
||||||
"07 05 01 03 40 00 01\n" +
|
|
||||||
"09 04 04 00 00 FF 03 00 07"));
|
|
||||||
when(usbDeviceConnection.claimInterface(controlInterface,true)).thenReturn(true);
|
when(usbDeviceConnection.claimInterface(controlInterface,true)).thenReturn(true);
|
||||||
when(usbDeviceConnection.claimInterface(dataInterface,true)).thenReturn(true);
|
when(usbDeviceConnection.claimInterface(dataInterface,true)).thenReturn(true);
|
||||||
when(usbDevice.getInterfaceCount()).thenReturn(5);
|
when(usbDevice.getInterfaceCount()).thenReturn(5);
|
||||||
@ -286,16 +161,10 @@ public class CdcAcmSerialDriverTest {
|
|||||||
when(usbDevice.getInterface(2)).thenReturn(dataInterface);
|
when(usbDevice.getInterface(2)).thenReturn(dataInterface);
|
||||||
when(usbDevice.getInterface(3)).thenReturn(hidInterface);
|
when(usbDevice.getInterface(3)).thenReturn(hidInterface);
|
||||||
when(usbDevice.getInterface(4)).thenReturn(vendorInterface);
|
when(usbDevice.getInterface(4)).thenReturn(vendorInterface);
|
||||||
when(massStorageInterface.getId()).thenReturn(0);
|
|
||||||
when(massStorageInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_MASS_STORAGE);
|
when(massStorageInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_MASS_STORAGE);
|
||||||
when(controlInterface.getId()).thenReturn(1);
|
|
||||||
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
||||||
when(controlInterface.getInterfaceSubclass()).thenReturn(USB_SUBCLASS_ACM);
|
|
||||||
when(dataInterface.getId()).thenReturn(2);
|
|
||||||
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
||||||
when(hidInterface.getId()).thenReturn(3);
|
|
||||||
when(hidInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_HID);
|
when(hidInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_HID);
|
||||||
when(vendorInterface.getId()).thenReturn(4);
|
|
||||||
when(vendorInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_VENDOR_SPEC);
|
when(vendorInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_VENDOR_SPEC);
|
||||||
|
|
||||||
when(controlInterface.getEndpointCount()).thenReturn(1);
|
when(controlInterface.getEndpointCount()).thenReturn(1);
|
||||||
@ -316,12 +185,9 @@ public class CdcAcmSerialDriverTest {
|
|||||||
port.openInt();
|
port.openInt();
|
||||||
assertEquals(readEndpoint, port.mReadEndpoint);
|
assertEquals(readEndpoint, port.mReadEndpoint);
|
||||||
assertEquals(writeEndpoint, port.mWriteEndpoint);
|
assertEquals(writeEndpoint, port.mWriteEndpoint);
|
||||||
|
|
||||||
ProbeTable probeTable = UsbSerialProber.getDefaultProbeTable();
|
|
||||||
Class<? extends UsbSerialDriver> probeDriver = probeTable.findDriver(usbDevice);
|
|
||||||
assertEquals(driver.getClass(), probeDriver);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void compositeRndisDevice() throws Exception {
|
public void compositeRndisDevice() throws Exception {
|
||||||
UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class);
|
UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class);
|
||||||
@ -334,30 +200,6 @@ public class CdcAcmSerialDriverTest {
|
|||||||
UsbEndpoint readEndpoint = mock(UsbEndpoint.class);
|
UsbEndpoint readEndpoint = mock(UsbEndpoint.class);
|
||||||
UsbEndpoint writeEndpoint = mock(UsbEndpoint.class);
|
UsbEndpoint writeEndpoint = mock(UsbEndpoint.class);
|
||||||
|
|
||||||
// has multiple USB_CLASS_CDC_DATA interfaces => get correct with IAD
|
|
||||||
when(usbDeviceConnection.getRawDescriptors()).thenReturn(HexDump.hexStringToByteArray(
|
|
||||||
"12 01 00 02 EF 02 01 40 FE CA 02 40 00 01 01 02 03 01\n" +
|
|
||||||
"09 02 8D 00 04 01 00 80 32\n" +
|
|
||||||
"08 0B 00 02 E0 01 03 00\n" +
|
|
||||||
"09 04 00 00 01 E0 01 03 04\n" +
|
|
||||||
"05 24 00 10 01\n" +
|
|
||||||
"05 24 01 00 01\n" +
|
|
||||||
"04 24 02 00\n" +
|
|
||||||
"05 24 06 00 01\n" +
|
|
||||||
"07 05 81 03 08 00 01\n" +
|
|
||||||
"09 04 01 00 02 0A 00 00 00\n" +
|
|
||||||
"07 05 82 02 40 00 00\n" +
|
|
||||||
"07 05 02 02 40 00 00\n" +
|
|
||||||
"08 0B 02 02 02 02 00 00\n" +
|
|
||||||
"09 04 02 00 01 02 02 00 04\n" +
|
|
||||||
"05 24 00 20 01\n" +
|
|
||||||
"05 24 01 00 03\n" +
|
|
||||||
"04 24 02 02\n" +
|
|
||||||
"05 24 06 02 03\n" +
|
|
||||||
"07 05 83 03 08 00 10\n" +
|
|
||||||
"09 04 03 00 02 0A 00 00 00\n" +
|
|
||||||
"07 05 04 02 40 00 00\n" +
|
|
||||||
"07 05 84 02 40 00 00"));
|
|
||||||
when(usbDeviceConnection.claimInterface(controlInterface,true)).thenReturn(true);
|
when(usbDeviceConnection.claimInterface(controlInterface,true)).thenReturn(true);
|
||||||
when(usbDeviceConnection.claimInterface(dataInterface,true)).thenReturn(true);
|
when(usbDeviceConnection.claimInterface(dataInterface,true)).thenReturn(true);
|
||||||
when(usbDevice.getInterfaceCount()).thenReturn(4);
|
when(usbDevice.getInterfaceCount()).thenReturn(4);
|
||||||
@ -365,96 +207,11 @@ public class CdcAcmSerialDriverTest {
|
|||||||
when(usbDevice.getInterface(1)).thenReturn(rndisDataInterface);
|
when(usbDevice.getInterface(1)).thenReturn(rndisDataInterface);
|
||||||
when(usbDevice.getInterface(2)).thenReturn(controlInterface);
|
when(usbDevice.getInterface(2)).thenReturn(controlInterface);
|
||||||
when(usbDevice.getInterface(3)).thenReturn(dataInterface);
|
when(usbDevice.getInterface(3)).thenReturn(dataInterface);
|
||||||
when(rndisControlInterface.getId()).thenReturn(0);
|
|
||||||
when(rndisControlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_WIRELESS_CONTROLLER);
|
when(rndisControlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_WIRELESS_CONTROLLER);
|
||||||
when(rndisControlInterface.getInterfaceSubclass()).thenReturn(1);
|
when(rndisControlInterface.getInterfaceSubclass()).thenReturn(1);
|
||||||
when(rndisControlInterface.getInterfaceProtocol()).thenReturn(3);
|
when(rndisControlInterface.getInterfaceProtocol()).thenReturn(3);
|
||||||
when(rndisDataInterface.getId()).thenReturn(1);
|
|
||||||
when(rndisDataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
when(rndisDataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
||||||
when(controlInterface.getId()).thenReturn(2);
|
|
||||||
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
||||||
when(controlInterface.getInterfaceSubclass()).thenReturn(USB_SUBCLASS_ACM);
|
|
||||||
when(dataInterface.getId()).thenReturn(3);
|
|
||||||
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
|
||||||
|
|
||||||
when(controlInterface.getEndpointCount()).thenReturn(1);
|
|
||||||
when(controlInterface.getEndpoint(0)).thenReturn(controlEndpoint);
|
|
||||||
when(dataInterface.getEndpointCount()).thenReturn(2);
|
|
||||||
when(dataInterface.getEndpoint(0)).thenReturn(writeEndpoint);
|
|
||||||
when(dataInterface.getEndpoint(1)).thenReturn(readEndpoint);
|
|
||||||
when(controlEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_IN);
|
|
||||||
when(controlEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_INT);
|
|
||||||
when(readEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_IN);
|
|
||||||
when(readEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK);
|
|
||||||
when(writeEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_OUT);
|
|
||||||
when(writeEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK);
|
|
||||||
|
|
||||||
CdcAcmSerialDriver driver = new CdcAcmSerialDriver(usbDevice);
|
|
||||||
CdcAcmSerialDriver.CdcAcmSerialPort port = (CdcAcmSerialDriver.CdcAcmSerialPort) driver.getPorts().get(0);
|
|
||||||
port.mConnection = usbDeviceConnection;
|
|
||||||
port.openInt();
|
|
||||||
assertEquals(readEndpoint, port.mReadEndpoint);
|
|
||||||
assertEquals(writeEndpoint, port.mWriteEndpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void compositeAlternateSettingDevice() throws Exception {
|
|
||||||
UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class);
|
|
||||||
UsbDevice usbDevice = mock(UsbDevice.class);
|
|
||||||
UsbInterface ethernetControlInterface = mock(UsbInterface.class);
|
|
||||||
UsbInterface ethernetDummyInterface = mock(UsbInterface.class);
|
|
||||||
UsbInterface ethernetDataInterface = mock(UsbInterface.class);
|
|
||||||
UsbInterface controlInterface = mock(UsbInterface.class);
|
|
||||||
UsbInterface dataInterface = mock(UsbInterface.class);
|
|
||||||
UsbEndpoint controlEndpoint = mock(UsbEndpoint.class);
|
|
||||||
UsbEndpoint readEndpoint = mock(UsbEndpoint.class);
|
|
||||||
UsbEndpoint writeEndpoint = mock(UsbEndpoint.class);
|
|
||||||
|
|
||||||
// has multiple USB_CLASS_CDC_DATA interfaces => get correct with IAD
|
|
||||||
when(usbDeviceConnection.getRawDescriptors()).thenReturn(HexDump.hexStringToByteArray(
|
|
||||||
"12 01 00 02 EF 02 01 40 FE CA 02 40 00 01 01 02 03 01\n" +
|
|
||||||
"09 02 9A 00 04 01 00 80 32\n" +
|
|
||||||
"08 0B 00 02 02 06 00 00\n" +
|
|
||||||
"09 04 00 00 01 02 06 00 04\n" +
|
|
||||||
"05 24 00 20 01\n" +
|
|
||||||
"05 24 06 00 01\n" +
|
|
||||||
"0D 24 0F 04 00 00 00 00 DC 05 00 00 00\n" +
|
|
||||||
"07 05 81 03 08 00 01\n" +
|
|
||||||
"09 04 01 00 00 0A 00 00 00\n" +
|
|
||||||
"09 04 01 01 02 0A 00 00 00\n" +
|
|
||||||
"07 05 82 02 40 00 00\n" +
|
|
||||||
"07 05 02 02 40 00 00\n" +
|
|
||||||
"08 0B 02 02 02 02 00 00\n" +
|
|
||||||
"09 04 02 00 01 02 02 00 04\n" +
|
|
||||||
"05 24 00 20 01\n" +
|
|
||||||
"05 24 01 00 03\n" +
|
|
||||||
"04 24 02 02\n" +
|
|
||||||
"05 24 06 02 03\n" +
|
|
||||||
"07 05 83 03 08 00 10\n" +
|
|
||||||
"09 04 03 00 02 0A 00 00 00\n" +
|
|
||||||
"07 05 04 02 40 00 00\n" +
|
|
||||||
"07 05 84 02 40 00 00"));
|
|
||||||
when(usbDeviceConnection.claimInterface(controlInterface,true)).thenReturn(true);
|
|
||||||
when(usbDeviceConnection.claimInterface(dataInterface,true)).thenReturn(true);
|
|
||||||
when(usbDevice.getInterfaceCount()).thenReturn(5);
|
|
||||||
when(usbDevice.getInterface(0)).thenReturn(ethernetControlInterface);
|
|
||||||
when(usbDevice.getInterface(1)).thenReturn(ethernetDummyInterface);
|
|
||||||
when(usbDevice.getInterface(2)).thenReturn(ethernetDataInterface);
|
|
||||||
when(usbDevice.getInterface(3)).thenReturn(controlInterface);
|
|
||||||
when(usbDevice.getInterface(4)).thenReturn(dataInterface);
|
|
||||||
when(ethernetControlInterface.getId()).thenReturn(0);
|
|
||||||
when(ethernetControlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
|
||||||
when(ethernetControlInterface.getInterfaceSubclass()).thenReturn(6);
|
|
||||||
when(ethernetDummyInterface.getId()).thenReturn(1);
|
|
||||||
when(ethernetDummyInterface.getAlternateSetting()).thenReturn(0);
|
|
||||||
when(ethernetDummyInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
|
||||||
when(ethernetDataInterface.getId()).thenReturn(1);
|
|
||||||
when(ethernetDataInterface.getAlternateSetting()).thenReturn(1);
|
|
||||||
when(ethernetDataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
|
||||||
when(controlInterface.getId()).thenReturn(2);
|
|
||||||
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
|
||||||
when(controlInterface.getInterfaceSubclass()).thenReturn(USB_SUBCLASS_ACM);
|
|
||||||
when(dataInterface.getId()).thenReturn(3);
|
|
||||||
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
||||||
|
|
||||||
when(controlInterface.getEndpointCount()).thenReturn(1);
|
when(controlInterface.getEndpointCount()).thenReturn(1);
|
||||||
@ -493,7 +250,6 @@ public class CdcAcmSerialDriverTest {
|
|||||||
when(usbDevice.getInterface(0)).thenReturn(controlInterface);
|
when(usbDevice.getInterface(0)).thenReturn(controlInterface);
|
||||||
when(usbDevice.getInterface(1)).thenReturn(dataInterface);
|
when(usbDevice.getInterface(1)).thenReturn(dataInterface);
|
||||||
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM);
|
||||||
when(controlInterface.getInterfaceSubclass()).thenReturn(USB_SUBCLASS_ACM);
|
|
||||||
when(controlInterface.getEndpointCount()).thenReturn(1);
|
when(controlInterface.getEndpointCount()).thenReturn(1);
|
||||||
when(controlInterface.getEndpoint(0)).thenReturn(controlEndpoint);
|
when(controlInterface.getEndpoint(0)).thenReturn(controlEndpoint);
|
||||||
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA);
|
||||||
@ -510,7 +266,7 @@ public class CdcAcmSerialDriverTest {
|
|||||||
CdcAcmSerialDriver driver = new CdcAcmSerialDriver(usbDevice);
|
CdcAcmSerialDriver driver = new CdcAcmSerialDriver(usbDevice);
|
||||||
CdcAcmSerialDriver.CdcAcmSerialPort port = (CdcAcmSerialDriver.CdcAcmSerialPort) driver.getPorts().get(0);
|
CdcAcmSerialDriver.CdcAcmSerialPort port = (CdcAcmSerialDriver.CdcAcmSerialPort) driver.getPorts().get(0);
|
||||||
port.mConnection = usbDeviceConnection;
|
port.mConnection = usbDeviceConnection;
|
||||||
assertThrows(IOException.class, port::openInt);
|
assertThrows(IOException.class, () ->port.openInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
package com.hoho.android.usbserial.util;
|
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertThrows;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.security.InvalidParameterException;
|
|
||||||
|
|
||||||
public class HexDumpText {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void toByteArray() throws Exception {
|
|
||||||
assertThat(HexDump.toByteArray((byte)0x4a), equalTo(new byte[]{ 0x4A}));
|
|
||||||
assertThat(HexDump.toByteArray((short)0x4a5b), equalTo(new byte[]{ 0x4A, 0x5B}));
|
|
||||||
assertThat(HexDump.toByteArray((int)0x4a5b6c7d), equalTo(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void toHexString() throws Exception {
|
|
||||||
assertEquals("4A", HexDump.toHexString((byte)0x4a));
|
|
||||||
assertEquals("4A 5B", HexDump.toHexString((short)0x4a5b));
|
|
||||||
assertEquals("4A 5B 6C 7D", HexDump.toHexString((int)0x4a5b6c7d));
|
|
||||||
assertEquals("4A 5B 6C 7D", HexDump.toHexString(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}));
|
|
||||||
assertEquals("5B 6C", HexDump.toHexString(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}, 1, 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void dumpHexString() throws Exception {
|
|
||||||
assertEquals("10 31 32 33 34 35 36 37 .1234567\n18 39 .9", HexDump.dumpHexString(new byte[]{ 0x10, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x18, 0x39}));
|
|
||||||
assertEquals("31 32 12", HexDump.dumpHexString(new byte[]{ 0x30, 0x31, 0x32, 0x33}, 1, 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void toByte() throws Exception {
|
|
||||||
assertThat(HexDump.hexStringToByteArray("4a 5B-6c\n7d"), equalTo(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}));
|
|
||||||
assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3 "));
|
|
||||||
assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3z"));
|
|
||||||
assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3Z"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
package com.hoho.android.usbserial.util;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import android.hardware.usb.UsbEndpoint;
|
|
||||||
import android.os.Process;
|
|
||||||
|
|
||||||
import com.hoho.android.usbserial.driver.CommonUsbSerialPort;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class SerialInputOutputManagerTest {
|
|
||||||
|
|
||||||
|
|
||||||
// catch all Throwables in onNewData() and onRunError()
|
|
||||||
@Test
|
|
||||||
public void throwable() throws Exception {
|
|
||||||
|
|
||||||
class ExceptionListener implements SerialInputOutputManager.Listener {
|
|
||||||
public Exception e;
|
|
||||||
@Override public void onNewData(byte[] data) { throw new RuntimeException("exception1"); }
|
|
||||||
@Override public void onRunError(Exception e) { this.e = e; throw new RuntimeException("exception2"); }
|
|
||||||
}
|
|
||||||
class ErrorListener implements SerialInputOutputManager.Listener {
|
|
||||||
public Exception e;
|
|
||||||
@Override public void onNewData(byte[] data) { throw new UnknownError("error1"); }
|
|
||||||
@Override public void onRunError(Exception e) { this.e = e; throw new UnknownError("error2");}
|
|
||||||
}
|
|
||||||
|
|
||||||
UsbEndpoint readEndpoint = mock(UsbEndpoint.class);
|
|
||||||
when(readEndpoint.getMaxPacketSize()).thenReturn(16);
|
|
||||||
CommonUsbSerialPort port = mock(CommonUsbSerialPort.class);
|
|
||||||
when(port.getReadEndpoint()).thenReturn(readEndpoint);
|
|
||||||
when(port.read(new byte[16], 0)).thenReturn(1);
|
|
||||||
when(port.isOpen()).thenReturn(true);
|
|
||||||
SerialInputOutputManager manager = new SerialInputOutputManager(port);
|
|
||||||
manager.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
|
|
||||||
|
|
||||||
ExceptionListener exceptionListener = new ExceptionListener();
|
|
||||||
manager.setListener(exceptionListener);
|
|
||||||
manager.runRead();
|
|
||||||
assertEquals(RuntimeException.class, exceptionListener.e.getClass());
|
|
||||||
assertEquals("exception1", exceptionListener.e.getMessage());
|
|
||||||
|
|
||||||
ErrorListener errorListener = new ErrorListener();
|
|
||||||
manager.setListener(errorListener);
|
|
||||||
manager.runRead();
|
|
||||||
assertEquals(Exception.class, errorListener.e.getClass());
|
|
||||||
assertEquals("java.lang.UnknownError: error1", errorListener.e.getMessage());
|
|
||||||
assertEquals(UnknownError.class, errorListener.e.getCause().getClass());
|
|
||||||
assertEquals("error1", errorListener.e.getCause().getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user