Skip to content

Commit 1d9ae5b

Browse files
committed
Auto start driver when DVB-T device is connected
1 parent 795c5ac commit 1d9ae5b

File tree

9 files changed

+279
-15
lines changed

9 files changed

+279
-15
lines changed

drivers/src/main/java/info/martinmarinov/drivers/usb/DvbUsbDevice.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ public interface Creator {
6161
* Try to instantiate a {@link DvbDevice} with the provided {@link UsbDevice} instance.
6262
* @param usbDevice a usb device that is attached to the system
6363
* @param context the application context, used for accessing usb system service and obtaining permissions
64+
* @param filter
6465
* @return a {@link DvbDevice} instance to control the device if the current creator supports it
6566
* or null if the {@link UsbDevice} is not supported by the creator.
6667
*/
67-
DvbDevice create(UsbDevice usbDevice, Context context) throws DvbException;
68+
DvbDevice create(UsbDevice usbDevice, Context context, DeviceFilter filter) throws DvbException;
69+
Set<DeviceFilter> getSupportedDevices();
6870
}
6971

7072
private final static String TAG = DvbUsbDevice.class.getSimpleName();

drivers/src/main/java/info/martinmarinov/drivers/usb/DvbUsbDeviceRegistry.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@
2828
import java.util.Collection;
2929
import java.util.List;
3030

31+
import info.martinmarinov.drivers.DeviceFilter;
3132
import info.martinmarinov.drivers.DvbDevice;
3233
import info.martinmarinov.drivers.DvbException;
34+
import info.martinmarinov.drivers.tools.DeviceFilterMatcher;
3335
import info.martinmarinov.drivers.usb.cxusb.CxUsbDvbDeviceCreator;
3436
import info.martinmarinov.drivers.usb.rtl28xx.Rtl2xx2DvbDeviceCreator;
3537

3638
public class DvbUsbDeviceRegistry {
3739

38-
private static DvbUsbDevice.Creator[] AVAILABLE_DRIVERS = new DvbUsbDevice.Creator[] {
40+
public static DvbUsbDevice.Creator[] AVAILABLE_DRIVERS = new DvbUsbDevice.Creator[] {
3941
new Rtl2xx2DvbDeviceCreator(),
4042
new CxUsbDvbDeviceCreator()
4143
};
@@ -50,8 +52,13 @@ public class DvbUsbDeviceRegistry {
5052
*/
5153
private static DvbDevice getDvbUsbDeviceFor(UsbDevice usbDevice, Context context) throws DvbException {
5254
for (DvbUsbDevice.Creator c : AVAILABLE_DRIVERS) {
53-
DvbDevice dvbDevice = c.create(usbDevice, context);
54-
if (dvbDevice != null) return dvbDevice;
55+
DeviceFilterMatcher deviceFilterMatcher = new DeviceFilterMatcher(c.getSupportedDevices());
56+
DeviceFilter filter = deviceFilterMatcher.getFilter(usbDevice);
57+
58+
if (filter != null) {
59+
DvbDevice dvbDevice = c.create(usbDevice, context, filter);
60+
if (dvbDevice != null) return dvbDevice;
61+
}
5562
}
5663
return null;
5764
}

drivers/src/main/java/info/martinmarinov/drivers/usb/cxusb/CxUsbDvbDeviceCreator.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,25 @@
2323
import android.content.Context;
2424
import android.hardware.usb.UsbDevice;
2525

26+
import java.util.Set;
27+
28+
import info.martinmarinov.drivers.DeviceFilter;
2629
import info.martinmarinov.drivers.DvbException;
2730
import info.martinmarinov.drivers.usb.DvbUsbDevice;
2831

32+
import static info.martinmarinov.drivers.tools.SetUtils.setOf;
2933
import static info.martinmarinov.drivers.usb.cxusb.MygicaT230.MYGICA_T230;
3034

3135
public class CxUsbDvbDeviceCreator implements DvbUsbDevice.Creator {
36+
private final static Set<DeviceFilter> CXUSB_DEVICES = setOf(MYGICA_T230);
37+
38+
@Override
39+
public Set<DeviceFilter> getSupportedDevices() {
40+
return CXUSB_DEVICES;
41+
}
42+
3243
@Override
33-
public DvbUsbDevice create(UsbDevice usbDevice, Context context) throws DvbException {
34-
if (MYGICA_T230.matches(usbDevice)) {
35-
return new MygicaT230(usbDevice, context);
36-
}
37-
return null;
44+
public DvbUsbDevice create(UsbDevice usbDevice, Context context, DeviceFilter filter) throws DvbException {
45+
return new MygicaT230(usbDevice, context);
3846
}
3947
}

drivers/src/main/java/info/martinmarinov/drivers/usb/rtl28xx/Rtl2xx2DvbDeviceCreator.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import info.martinmarinov.drivers.DvbException;
3030
import info.martinmarinov.drivers.usb.DvbUsbDevice;
3131
import info.martinmarinov.drivers.usb.DvbUsbIds;
32-
import info.martinmarinov.drivers.tools.DeviceFilterMatcher;
3332

3433
import static info.martinmarinov.drivers.tools.SetUtils.setOf;
3534

@@ -70,13 +69,13 @@ public class Rtl2xx2DvbDeviceCreator implements DvbUsbDevice.Creator {
7069
new DeviceFilter(0x5654, 0xca42, "GoTView MasterHD 3")
7170
);
7271

73-
private final static DeviceFilterMatcher SUPPORTED_DEVICES = new DeviceFilterMatcher(RTL2832_DEVICES);
74-
7572
@Override
76-
public DvbUsbDevice create(UsbDevice usbDevice, Context context) throws DvbException {
77-
DeviceFilter filter = SUPPORTED_DEVICES.getFilter(usbDevice);
78-
if (filter == null) return null; // not supported device found
73+
public Set<DeviceFilter> getSupportedDevices() {
74+
return RTL2832_DEVICES;
75+
}
7976

77+
@Override
78+
public DvbUsbDevice create(UsbDevice usbDevice, Context context, DeviceFilter filter) throws DvbException {
8079
return new Rtl2832DvbDevice(usbDevice, context, filter);
8180
}
8281
}

dvbservice/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ model {
4747
testCompile 'org.powermock:powermock:1.6.5'
4848
testCompile 'org.powermock:powermock-module-junit4:1.6.5'
4949
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
50+
androidTestCompile 'com.android.support:support-annotations:25.3.1'
51+
androidTestCompile 'com.android.support.test:runner:0.5'
5052
}
5153
}
5254
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* This is an Android user space port of DVB-T Linux kernel modules.
3+
*
4+
* Copyright (C) 2017 Martin Marinov <martintzvetomirov at gmail com>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
package info.martinmarinov.dvbservice;
22+
23+
import android.content.res.Resources;
24+
import android.content.res.XmlResourceParser;
25+
import android.support.test.filters.MediumTest;
26+
import android.support.test.runner.AndroidJUnit4;
27+
import android.util.AttributeSet;
28+
import android.util.Xml;
29+
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.xmlpull.v1.XmlPullParser;
33+
34+
import java.util.HashSet;
35+
import java.util.Set;
36+
37+
import info.martinmarinov.drivers.DeviceFilter;
38+
import info.martinmarinov.drivers.usb.DvbUsbDevice;
39+
import info.martinmarinov.drivers.usb.DvbUsbDeviceRegistry;
40+
41+
import static android.support.test.InstrumentationRegistry.getTargetContext;
42+
import static org.hamcrest.MatcherAssert.assertThat;
43+
import static org.hamcrest.core.IsEqual.equalTo;
44+
45+
@RunWith(AndroidJUnit4.class)
46+
@MediumTest
47+
public class DeviceFilterXmlEquivalenceTester {
48+
49+
@Test
50+
public void allDevicesAreInXml() {
51+
Set<DeviceFilter> devicesInXml = getDeviceData(getTargetContext().getResources(), R.xml.device_filter);
52+
Set<DeviceFilter> supportedDecvices = getDevicesInApp();
53+
54+
if (!supportedDecvices.equals(devicesInXml)) {
55+
System.out.println("Devices needed to be added to the xml");
56+
57+
for (DeviceFilter supportedDevice : supportedDecvices) {
58+
if (!devicesInXml.contains(supportedDevice)) {
59+
System.out.printf(" <usb-device vendor-id=\"0x%x\" product-id=\"0x%x\"/> <!-- %s -->\n",
60+
supportedDevice.getVendorId(), supportedDevice.getProductId(), supportedDevice.getName()
61+
);
62+
}
63+
}
64+
65+
System.out.println("Devices in xml but not supported");
66+
67+
for (DeviceFilter deviceInXml : devicesInXml) {
68+
if (!supportedDecvices.contains(deviceInXml)) {
69+
System.out.printf("vendor-id=0x%x, product-id=0x%x\n",
70+
deviceInXml.getVendorId(), deviceInXml.getProductId()
71+
);
72+
}
73+
}
74+
75+
}
76+
77+
assertThat(supportedDecvices, equalTo(devicesInXml));
78+
}
79+
80+
private static HashSet<DeviceFilter> getDevicesInApp() {
81+
HashSet<DeviceFilter> supportedDevices = new HashSet<>();
82+
for (DvbUsbDevice.Creator d : DvbUsbDeviceRegistry.AVAILABLE_DRIVERS) {
83+
supportedDevices.addAll(d.getSupportedDevices());
84+
}
85+
return supportedDevices;
86+
}
87+
88+
private static Set<DeviceFilter> getDeviceData(Resources resources, int xmlResourceId) {
89+
Set<DeviceFilter> ans = new HashSet<>();
90+
try {
91+
XmlResourceParser xml = resources.getXml(xmlResourceId);
92+
93+
xml.next();
94+
int eventType;
95+
while ((eventType = xml.getEventType()) != XmlPullParser.END_DOCUMENT) {
96+
97+
switch (eventType) {
98+
case XmlPullParser.START_TAG:
99+
if (xml.getName().equals("usb-device")) {
100+
AttributeSet as = Xml.asAttributeSet(xml);
101+
Integer vendorId = parseInt( as.getAttributeValue(null, "vendor-id"));
102+
Integer productId = parseInt( as.getAttributeValue(null, "product-id"));
103+
ans.add(new DeviceFilter(vendorId, productId, null));
104+
}
105+
break;
106+
}
107+
xml.next();
108+
}
109+
} catch (Exception e) {
110+
throw new RuntimeException(e);
111+
}
112+
return ans;
113+
}
114+
115+
private static Integer parseInt(String number) {
116+
if (number.startsWith("0x")) {
117+
return Integer.valueOf( number.substring(2), 16);
118+
} else {
119+
return Integer.valueOf( number, 10);
120+
}
121+
}
122+
}

dvbservice/src/main/AndroidManifest.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@
4646
<service
4747
android:name=".DvbService"
4848
android:exported="false"/>
49+
<activity
50+
android:name="info.martinmarinov.dvbservice.UsbDelegate"
51+
android:theme = "@android:style/Theme.Translucent.NoTitleBar"
52+
android:exported="true" >
53+
<intent-filter>
54+
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
55+
</intent-filter>
56+
57+
<meta-data
58+
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
59+
android:resource="@xml/device_filter" />
60+
</activity>
4961
</application>
5062

5163
</manifest>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This is an Android user space port of DVB-T Linux kernel modules.
3+
*
4+
* Copyright (C) 2017 Martin Marinov <martintzvetomirov at gmail com>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
package info.martinmarinov.dvbservice;
22+
23+
import android.app.Activity;
24+
import android.content.ActivityNotFoundException;
25+
import android.content.Intent;
26+
import android.hardware.usb.UsbDevice;
27+
import android.hardware.usb.UsbManager;
28+
import android.os.Bundle;
29+
import android.util.Log;
30+
31+
public class UsbDelegate extends Activity {
32+
private static final String TAG = UsbDelegate.class.getSimpleName();
33+
private static final String ACTION_DVB_DEVICE_ATTACHED = "info.martinmarinov.dvbdriver.DVB_DEVICE_ATTACHED";
34+
35+
@Override
36+
public void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
39+
Intent intent = getIntent();
40+
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
41+
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
42+
Log.d(TAG, "USB DVB-T attached: " + usbDevice.getDeviceName());
43+
Intent newIntent = new Intent(ACTION_DVB_DEVICE_ATTACHED);
44+
newIntent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);
45+
try {
46+
startActivity(newIntent);
47+
} catch (ActivityNotFoundException e) {
48+
Log.d(TAG, "No activity found for DVB-T handling");
49+
}
50+
}
51+
52+
finish();
53+
}
54+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
~ This is an Android user space port of DVB-T Linux kernel modules.
5+
~
6+
~ Copyright (C) 2017 Martin Marinov <martintzvetomirov at gmail com>
7+
~
8+
~ This program is free software; you can redistribute it and/or modify
9+
~ it under the terms of the GNU General Public License as published by
10+
~ the Free Software Foundation; either version 2 of the License, or
11+
~ (at your option) any later version.
12+
~
13+
~ This program is distributed in the hope that it will be useful,
14+
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
~ GNU General Public License for more details.
17+
~
18+
~ You should have received a copy of the GNU General Public License
19+
~ along with this program; if not, write to the Free Software
20+
~ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
-->
22+
23+
<resources>
24+
25+
<usb-device vendor-id="0x0bda" product-id="0x2832"/> <!-- Realtek RTL2832U reference design -->
26+
<usb-device vendor-id="0x0413" product-id="0x6f0f"/> <!-- Leadtek Winfast DTV Dongle Mini D -->
27+
<usb-device vendor-id="0x1b80" product-id="0xd394"/> <!-- MaxMedia HU394-T -->
28+
<usb-device vendor-id="0x1f4d" product-id="0xb803"/> <!-- G-Tek Electronics Group Lifeview LV5TDLX DVB-T -->
29+
<usb-device vendor-id="0x1b80" product-id="0xd39d"/> <!-- Sveon STV20 -->
30+
<usb-device vendor-id="0x1f4d" product-id="0xa803"/> <!-- Crypto ReDi PC 50 A -->
31+
<usb-device vendor-id="0x0413" product-id="0x6f12"/> <!-- Leadtek WinFast DTV2000DS Plus -->
32+
<usb-device vendor-id="0x0ccd" product-id="0x00d7"/> <!-- TerraTec Cinergy T Stick+ -->
33+
<usb-device vendor-id="0x0413" product-id="0x6a03"/> <!-- Leadtek WinFast DTV Dongle mini -->
34+
<usb-device vendor-id="0x0ccd" product-id="0x00d3"/> <!-- TerraTec Cinergy T Stick RC (Rev. 3) -->
35+
<usb-device vendor-id="0x1f4d" product-id="0xc803"/> <!-- Trekstor DVB-T Stick Terres 2.0 -->
36+
<usb-device vendor-id="0x1b80" product-id="0xd3a8"/> <!-- ASUS My Cinema-U3100Mini Plus V2 -->
37+
<usb-device vendor-id="0x1b80" product-id="0xd3b0"/> <!-- Sveon STV21 -->
38+
<usb-device vendor-id="0x1b80" product-id="0xd3a4"/> <!-- TURBO-X Pure TV Tuner DTT-2000 -->
39+
<usb-device vendor-id="0x0572" product-id="0xc688"/> <!-- Mygica T230 DVB-T/T2/C -->
40+
<usb-device vendor-id="0x0ccd" product-id="0x00b3"/> <!-- TerraTec NOXON DAB Stick -->
41+
<usb-device vendor-id="0x0bda" product-id="0x2838"/> <!-- Realtek RTL2832U reference design -->
42+
<usb-device vendor-id="0x1b80" product-id="0xd393"/> <!-- GIGABYTE U7300 -->
43+
<usb-device vendor-id="0x185b" product-id="0x0650"/> <!-- Compro VideoMate U650F -->
44+
<usb-device vendor-id="0x0458" product-id="0x707f"/> <!-- Genius TVGo DVB-T03 -->
45+
<usb-device vendor-id="0x0ccd" product-id="0x00b4"/> <!-- TerraTec NOXON DAB Stick (rev 3) -->
46+
<usb-device vendor-id="0x1d19" product-id="0x1101"/> <!-- Dexatek DK DVB-T Dongle -->
47+
<usb-device vendor-id="0x1d19" product-id="0x1104"/> <!-- MSI DIGIVOX Micro HD -->
48+
<usb-device vendor-id="0x1b80" product-id="0xd395"/> <!-- Peak DVB-T USB -->
49+
<usb-device vendor-id="0x0ccd" product-id="0x00a9"/> <!-- TerraTec Cinergy T Stick Black -->
50+
<usb-device vendor-id="0x15f4" product-id="0x0131"/> <!-- Astrometa DVB-T2 -->
51+
<usb-device vendor-id="0x1d19" product-id="0x1102"/> <!-- Dexatek DK mini DVB-T Dongle -->
52+
<usb-device vendor-id="0x5654" product-id="0xca42"/> <!-- GoTView MasterHD 3 -->
53+
<usb-device vendor-id="0x0413" product-id="0x6680"/> <!-- DigitalNow Quad DVB-T Receiver -->
54+
<usb-device vendor-id="0x0ccd" product-id="0x00e0"/> <!-- TerraTec NOXON DAB Stick (rev 2) -->
55+
<usb-device vendor-id="0x1b80" product-id="0xd3af"/> <!-- Sveon STV27 -->
56+
<usb-device vendor-id="0x185b" product-id="0x0620"/> <!-- Compro VideoMate U620F -->
57+
58+
</resources>

0 commit comments

Comments
 (0)