Skip to content

Commit e44bea2

Browse files
committed
Merge branch 'feature/additional_usb_filter_support' into develop
2 parents 20caed4 + 1113632 commit e44bea2

10 files changed

Lines changed: 225 additions & 5 deletions

File tree

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
<attribute name="maven.pomderived" value="true"/>
2323
</attributes>
2424
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
2526
<classpathentry kind="output" path="target/classes"/>
2627
</classpath>

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
target
2+
tmp

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@
7979
<version>20090211</version>
8080
<scope>provided</scope>
8181
</dependency>
82+
<dependency>
83+
<groupId>com.pivotallabs</groupId>
84+
<artifactId>robolectric</artifactId>
85+
<version>1.1</version>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.mockito</groupId>
90+
<artifactId>mockito-core</artifactId>
91+
<version>1.9.5</version>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.apache.httpcomponents</groupId>
96+
<artifactId>httpclient</artifactId>
97+
<version>4.2.5</version>
98+
<scope>test</scope>
99+
</dependency>
82100
</dependencies>
83101
<build>
84102
<finalName>AndroidSerial</finalName>

src/main/java/com/yourinventit/processing/android/serial/UsbSerialCommunicator.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
import processing.core.PApplet;
1616
import android.content.Context;
1717
import android.hardware.usb.UsbDevice;
18+
import android.hardware.usb.UsbDeviceConnection;
1819
import android.hardware.usb.UsbManager;
1920

21+
import com.hoho.android.usbserial.driver.CdcAcmSerialDriver;
22+
import com.hoho.android.usbserial.driver.UsbId;
2023
import com.hoho.android.usbserial.driver.UsbSerialDriver;
2124
import com.hoho.android.usbserial.driver.UsbSerialProber;
2225
import com.yourinventit.processing.android.serial.SerialInputOutputManager.Listener;
@@ -27,7 +30,8 @@
2730
* @author dbaba@yourinventit.com
2831
*
2932
*/
30-
class UsbSerialCommunicator extends AbstractAndroidSerialCommunicator implements Listener {
33+
class UsbSerialCommunicator extends AbstractAndroidSerialCommunicator implements
34+
Listener {
3135

3236
/**
3337
* {@link Logger}
@@ -77,8 +81,7 @@ protected UsbSerialDriver findUsbSerialDriver(String deviceName) {
7781
for (final UsbSerialProber prober : UsbSerialProber.values()) {
7882
for (final UsbDevice usbDevice : usbManager.getDeviceList()
7983
.values()) {
80-
final UsbSerialDriver driver = prober.getDevice(usbManager,
81-
usbDevice);
84+
final UsbSerialDriver driver = getDevice(prober, usbDevice);
8285
if (driver != null
8386
&& deviceName
8487
.equals(driver.getDevice().getDeviceName())) {
@@ -224,8 +227,7 @@ public String[] list() {
224227
for (final UsbSerialProber prober : UsbSerialProber.values()) {
225228
for (final UsbDevice usbDevice : usbManager.getDeviceList()
226229
.values()) {
227-
final UsbSerialDriver driver = prober.getDevice(usbManager,
228-
usbDevice);
230+
final UsbSerialDriver driver = getDevice(prober, usbDevice);
229231
if (driver != null) {
230232
names.add(driver.getDevice().getDeviceName());
231233
}
@@ -263,4 +265,39 @@ public void write(int what) {
263265
public void write(String what) {
264266
write(what.getBytes());
265267
}
268+
269+
/**
270+
* Returns a new {@link UsbSerialDriver} instance
271+
*
272+
* @param prober
273+
* @param usbDevice
274+
* @return
275+
*/
276+
protected UsbSerialDriver getDevice(UsbSerialProber prober,
277+
UsbDevice usbDevice) {
278+
UsbSerialDriver driver = prober.getDevice(usbManager, usbDevice);
279+
if (driver == null) {
280+
switch (prober) {
281+
case CDC_ACM_SERIAL:
282+
// Issue #1
283+
// https://github.com/inventit/processing-android-serial/issues/1
284+
// For supporting other Arduino gadgets than what the usb serial
285+
// driver expects shown in the constant UsbId class:
286+
// https://code.google.com/p/usb-serial-for-android/source/browse/UsbSerialLibrary/src/com/hoho/android/usbserial/driver/UsbId.java#39
287+
if (usbDevice.getVendorId() == UsbId.VENDOR_ARDUINO) {
288+
final UsbDeviceConnection connection = usbManager
289+
.openDevice(usbDevice);
290+
if (connection == null) {
291+
return null;
292+
}
293+
driver = new CdcAcmSerialDriver(usbDevice, connection);
294+
}
295+
break;
296+
297+
default:
298+
// do nothing for now
299+
}
300+
}
301+
return driver;
302+
}
266303
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2013 InventIt Inc.
3+
*/
4+
package com.yourinventit.dmc.api.moat.android;
5+
6+
import java.io.File;
7+
8+
import org.junit.runners.model.InitializationError;
9+
10+
import com.xtremelabs.robolectric.RobolectricConfig;
11+
import com.xtremelabs.robolectric.RobolectricTestRunner;
12+
13+
/**
14+
*
15+
* @author dbaba@yourinventit.com
16+
*
17+
*/
18+
public class MoatRobolectricTestRunner extends RobolectricTestRunner {
19+
20+
private static final File PROJECT_FILE;
21+
22+
static {
23+
System.out.println( System.getenv().get( "ANDROID_HOME" ));
24+
PROJECT_FILE = new File(
25+
"src/test/resources/robolectric/AndroidManifest.xml");
26+
}
27+
28+
/**
29+
* @param testClass
30+
* @throws InitializationError
31+
*/
32+
public MoatRobolectricTestRunner(Class<?> testClass)
33+
throws InitializationError {
34+
super(testClass, new RobolectricConfig(PROJECT_FILE.getParentFile()));
35+
}
36+
37+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (C) 2013 InventIt Inc.
3+
*/
4+
package com.yourinventit.processing.android.serial;
5+
6+
import static org.mockito.Mockito.*;
7+
import static junit.framework.Assert.*;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
13+
import processing.core.PApplet;
14+
import android.content.Context;
15+
import android.hardware.usb.UsbDevice;
16+
import android.hardware.usb.UsbDeviceConnection;
17+
import android.hardware.usb.UsbManager;
18+
19+
import com.hoho.android.usbserial.driver.UsbId;
20+
import com.hoho.android.usbserial.driver.UsbSerialDriver;
21+
import com.hoho.android.usbserial.driver.UsbSerialProber;
22+
import com.yourinventit.dmc.api.moat.android.MoatRobolectricTestRunner;
23+
24+
/**
25+
*
26+
* @author dbaba@yourinventit.com
27+
*
28+
*/
29+
@RunWith(MoatRobolectricTestRunner.class)
30+
public class UsbSerialCommunicatorTest {
31+
32+
private UsbSerialCommunicator communicator;
33+
34+
private final PApplet pApplet = mock(PApplet.class);
35+
private final UsbDevice usbDevice = mock(UsbDevice.class);
36+
private final UsbManager usbManager = mock(UsbManager.class);
37+
private final UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class);
38+
39+
@Before
40+
public void setUp() throws Exception {
41+
when(pApplet.getApplicationContext()).thenReturn(pApplet);
42+
when(pApplet.getSystemService(Context.USB_SERVICE)).thenReturn(
43+
usbManager);
44+
communicator = new UsbSerialCommunicator(pApplet);
45+
}
46+
47+
/**
48+
* Regression Test
49+
*/
50+
@Test
51+
public void test_getDevice_ok1() {
52+
when(usbDevice.getVendorId()).thenReturn(UsbId.VENDOR_ARDUINO);
53+
when(usbDevice.getProductId()).thenReturn(UsbId.ARDUINO_LEONARDO);
54+
when(usbManager.openDevice(usbDevice)).thenReturn(usbDeviceConnection);
55+
final UsbSerialDriver driver = communicator.getDevice(
56+
UsbSerialProber.CDC_ACM_SERIAL, usbDevice);
57+
assertNotNull(driver);
58+
}
59+
60+
/**
61+
* Arduino Due Test
62+
*/
63+
@Test
64+
public void test_getDevice_ok2() {
65+
when(usbDevice.getVendorId()).thenReturn(UsbId.VENDOR_ARDUINO);
66+
// http://www.devtal.de/wiki/Benutzer:Rdiez/ArduinoDue
67+
// 0x003e for Arduino Due
68+
when(usbDevice.getProductId()).thenReturn(0x003e);
69+
when(usbManager.openDevice(usbDevice)).thenReturn(usbDeviceConnection);
70+
final UsbSerialDriver driver = communicator.getDevice(
71+
UsbSerialProber.CDC_ACM_SERIAL, usbDevice);
72+
assertNotNull(driver);
73+
}
74+
75+
/**
76+
* Ensure not working with other vendor ID than Arduino's.
77+
*/
78+
@Test
79+
public void test_getDevice_null() {
80+
when(usbDevice.getVendorId()).thenReturn(UsbId.VENDOR_FTDI);
81+
when(usbManager.openDevice(usbDevice)).thenReturn(usbDeviceConnection);
82+
final UsbSerialDriver driver = communicator.getDevice(
83+
UsbSerialProber.CDC_ACM_SERIAL, usbDevice);
84+
assertNull(driver);
85+
}
86+
}

src/test/java/test/R.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test;
2+
/* AUTO-GENERATED FILE. DO NOT MODIFY.
3+
*
4+
* This class was automatically generated by the
5+
* aapt tool from the resource data it found. It
6+
* should not be modified by hand.
7+
*/
8+
9+
10+
11+
public final class R {
12+
public static final class attr {
13+
}
14+
}

src/test/resources/.gitkeep

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="test"
4+
android:versionCode="1"
5+
android:versionName="1.0">
6+
<uses-feature android:name="android.hardware.usb.host" />
7+
<uses-sdk android:minSdkVersion="10" />
8+
<application android:label=""
9+
android:icon="@drawable/icon">
10+
<activity android:name="">
11+
<intent-filter>
12+
<action android:name="android.intent.action.MAIN" />
13+
<category android:name="android.intent.category.LAUNCHER" />
14+
</intent-filter>
15+
<intent-filter>
16+
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
17+
</intent-filter>
18+
<meta-data
19+
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
20+
android:resource="@xml/device_filter" />
21+
</activity>
22+
</application>
23+
</manifest>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
</resources>

0 commit comments

Comments
 (0)