Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[178] Support Discovery of USB dongles (for some Telegesis and one Ember dongle) #179

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions org.openhab.binding.zigbee.ember/.classpath
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java/"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
2 changes: 2 additions & 0 deletions org.openhab.binding.zigbee.ember/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Import-Package: com.zsmartsystems.zigbee,
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.smarthome.config.core,
org.eclipse.smarthome.config.discovery,
org.eclipse.smarthome.config.discovery.usbserial,
org.eclipse.smarthome.core.i18n,
org.eclipse.smarthome.core.library.types,
org.eclipse.smarthome.core.thing,
Expand All @@ -26,6 +27,7 @@ Import-Package: com.zsmartsystems.zigbee,
org.eclipse.smarthome.core.thing.binding.firmware,
org.eclipse.smarthome.core.thing.type,
org.eclipse.smarthome.core.types,
org.junit;version="4.0.0";resolution:=optional,
org.openhab.binding.zigbee,
org.openhab.binding.zigbee.discovery,
org.openhab.binding.zigbee.handler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright (c) 2014-2018 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.zigbee.ember.internal.discovery;

import static java.util.Arrays.asList;
import static org.openhab.binding.zigbee.ZigBeeBindingConstants.CONFIGURATION_BAUD;
import static org.openhab.binding.zigbee.ZigBeeBindingConstants.CONFIGURATION_FLOWCONTROL;
import static org.openhab.binding.zigbee.ZigBeeBindingConstants.CONFIGURATION_PORT;
import static org.openhab.binding.zigbee.ZigBeeBindingConstants.FLOWCONTROL_CONFIG_SOFTWARE_XONXOFF;
import static org.openhab.binding.zigbee.ember.EmberBindingConstants.THING_TYPE_EMBER;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.config.discovery.usbserial.UsbSerialDeviceInformation;
import org.eclipse.smarthome.config.discovery.usbserial.UsbSerialDiscoveryParticipant;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.osgi.service.component.annotations.Component;

/**
* Discovery for ZigBee USB dongles, integrated in Eclipse SmartHome's USB-serial discovery by implementing
* a component of type {@link UsbSerialDiscoveryParticipant}.
* <p/>
* Currently, this {@link UsbSerialDiscoveryParticipant} supports the ZigBee dongle 'BV AV2010/10' from Bitron Video.
*
* @author Henning Sudbrock - initial contribution
*/
@Component(service = UsbSerialDiscoveryParticipant.class)
public class ZigBeeEmberUsbSerialDiscoveryParticipant implements UsbSerialDiscoveryParticipant {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(asList(THING_TYPE_EMBER));

public static final int SILICON_LABS_USB_VENDOR_ID = 0x10c4;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a minor comment - such constants have been documented for the Telegesis dongle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented only those constants for the Telegesis dongle where I found documentation useful (i.e., those constants where there are multiple different dongles which might or might not have the same product ID - having in mind that there are two dongles that look alike but have different product IDs, and two dongles that don't look alike but have the same product ID, which I wanted to clarify using those constants). Here, I don't think that additional documentation is needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

public static final int BITRON_VIDEO_2010_10_PRODUCT_ID = 0x8b34;
private static final int BITRON_VIDEO_2010_10_BAUD_RATE = 57600;
private static final String BITRON_VIDEO_2010_10_DEFAULT_LABEL = "Bitron Video AV2010/10 Ember ZigBee Dongle";

@Override
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
return SUPPORTED_THING_TYPES;
}

@Override
public @Nullable DiscoveryResult createResult(UsbSerialDeviceInformation deviceInformation) {
if (isBitronVideoDongle(deviceInformation)) {
return DiscoveryResultBuilder.create(createBitronVideoDongleThingType(deviceInformation))
.withLabel(createBitronVideoDongleLabel(deviceInformation))
.withRepresentationProperty(CONFIGURATION_PORT)
.withProperty(CONFIGURATION_PORT, deviceInformation.getSerialPort())
.withProperty(CONFIGURATION_BAUD, BITRON_VIDEO_2010_10_BAUD_RATE)
.withProperty(CONFIGURATION_FLOWCONTROL, FLOWCONTROL_CONFIG_SOFTWARE_XONXOFF)
.build();
} else {
return null;
}
}

@Override
public @Nullable ThingUID getThingUID(UsbSerialDeviceInformation deviceInformation) {
if (isBitronVideoDongle(deviceInformation)) {
return createBitronVideoDongleThingType(deviceInformation);
} else {
return null;
}
}

private boolean isBitronVideoDongle(UsbSerialDeviceInformation deviceInformation) {
return deviceInformation.getVendorId() == SILICON_LABS_USB_VENDOR_ID
&& deviceInformation.getProductId() == BITRON_VIDEO_2010_10_PRODUCT_ID;
}

private ThingUID createBitronVideoDongleThingType(UsbSerialDeviceInformation deviceInformation) {
return new ThingUID(THING_TYPE_EMBER, deviceInformation.getSerialNumber());
}

private @Nullable String createBitronVideoDongleLabel(UsbSerialDeviceInformation deviceInformation) {
if (deviceInformation.getProduct() != null && !deviceInformation.getProduct().isEmpty()) {
return deviceInformation.getProduct() + " (ZigBee USB dongle)";
} else {
return BITRON_VIDEO_2010_10_DEFAULT_LABEL;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2014-2018 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.zigbee.ember.internal.discovery;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.openhab.binding.zigbee.ZigBeeBindingConstants.CONFIGURATION_BAUD;
import static org.openhab.binding.zigbee.ZigBeeBindingConstants.CONFIGURATION_PORT;
import static org.openhab.binding.zigbee.ember.EmberBindingConstants.THING_TYPE_EMBER;
import static org.openhab.binding.zigbee.ember.internal.discovery.ZigBeeEmberUsbSerialDiscoveryParticipant.BITRON_VIDEO_2010_10_PRODUCT_ID;
import static org.openhab.binding.zigbee.ember.internal.discovery.ZigBeeEmberUsbSerialDiscoveryParticipant.SILICON_LABS_USB_VENDOR_ID;

import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.usbserial.UsbSerialDeviceInformation;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.junit.Before;
import org.junit.Test;
import org.openhab.binding.zigbee.ember.EmberBindingConstants;
import org.openhab.binding.zigbee.ember.internal.discovery.ZigBeeEmberUsbSerialDiscoveryParticipant;

/**
* Unit tests for the {@link ZigBeeEmberUsbSerialDiscoveryParticipant}.
*/
public class ZigBeeEmberUsbSerialDiscoveryParticipantTest {

private ZigBeeEmberUsbSerialDiscoveryParticipant discoveryParticipant;

@Before
public void setup() {
discoveryParticipant = new ZigBeeEmberUsbSerialDiscoveryParticipant();
}

/**
* If only USB vendor ID or only USB product ID or none of them matches, then no device is discovered.
*/
@Test
public void testNonEmberDongleNotDiscovered() {
assertNull(discoveryParticipant.getThingUID(forUsbDongle(SILICON_LABS_USB_VENDOR_ID, 0x1234)));
assertNull(discoveryParticipant.getThingUID(forUsbDongle(0xabcd, BITRON_VIDEO_2010_10_PRODUCT_ID)));
assertNull(discoveryParticipant.getThingUID(forUsbDongle(0xabcd, 0x1234)));

assertNull(discoveryParticipant.createResult(forUsbDongle(SILICON_LABS_USB_VENDOR_ID, 0x1234)));
assertNull(discoveryParticipant.createResult(forUsbDongle(0xabcd, BITRON_VIDEO_2010_10_PRODUCT_ID)));
assertNull(discoveryParticipant.createResult(forUsbDongle(0xabcd, 0x1234)));
}

/**
* For matching USB vendor and product ID, a suitable thingUID is returned.
*/
@Test
public void testEmberDongleDiscoveredThingUID() {
ThingUID thingUID = discoveryParticipant.getThingUID(
forUsbDongle(SILICON_LABS_USB_VENDOR_ID, BITRON_VIDEO_2010_10_PRODUCT_ID, "serial", "/dev/ttyUSB0"));

assertNotNull(thingUID);
assertEquals(thingUID, new ThingUID(THING_TYPE_EMBER, "serial"));
}

/**
* For matching USB vendor and product ID, a suitable discovery result is returned.
*/
@Test
public void testEmberDongleDiscoveredDiscoveryResult() {
DiscoveryResult discoveryResult = discoveryParticipant.createResult(
forUsbDongle(SILICON_LABS_USB_VENDOR_ID, BITRON_VIDEO_2010_10_PRODUCT_ID, "serial", "/dev/ttyUSB0"));

assertNotNull(discoveryResult);
assertEquals(discoveryResult.getThingUID(), new ThingUID(THING_TYPE_EMBER, "serial"));
assertNotNull(discoveryResult.getLabel());
assertEquals(discoveryResult.getRepresentationProperty(), CONFIGURATION_PORT);
assertEquals(discoveryResult.getProperties().get(CONFIGURATION_PORT), "/dev/ttyUSB0");
assertNotNull(discoveryResult.getProperties().get(CONFIGURATION_BAUD));
}

private UsbSerialDeviceInformation forUsbDongle(int vendorId, int productId, String serial, String device) {
return new UsbSerialDeviceInformation(vendorId, productId, serial, null, null, 0, null, device);
}

private UsbSerialDeviceInformation forUsbDongle(int vendorId, int productId) {
return new UsbSerialDeviceInformation(vendorId, productId, null, null, null, 0, null, "/dev/ttyUSB0");
}

}
5 changes: 3 additions & 2 deletions org.openhab.binding.zigbee.telegesis/.classpath
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java/"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
2 changes: 2 additions & 0 deletions org.openhab.binding.zigbee.telegesis/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Import-Package: com.zsmartsystems.zigbee,
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.smarthome.config.core,
org.eclipse.smarthome.config.discovery,
org.eclipse.smarthome.config.discovery.usbserial,
org.eclipse.smarthome.core.i18n,
org.eclipse.smarthome.core.library.types,
org.eclipse.smarthome.core.thing,
Expand All @@ -27,6 +28,7 @@ Import-Package: com.zsmartsystems.zigbee,
org.eclipse.smarthome.core.thing.binding.firmware,
org.eclipse.smarthome.core.thing.type,
org.eclipse.smarthome.core.types,
org.junit;version="4.0.0";resolution:=optional,
org.openhab.binding.zigbee,
org.openhab.binding.zigbee.discovery,
org.openhab.binding.zigbee.handler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright (c) 2014-2018 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.zigbee.telegesis.internal.discovery;

import static java.util.Arrays.asList;
import static org.openhab.binding.zigbee.ZigBeeBindingConstants.CONFIGURATION_BAUD;
import static org.openhab.binding.zigbee.ZigBeeBindingConstants.CONFIGURATION_PORT;
import static org.openhab.binding.zigbee.telegesis.TelegesisBindingConstants.THING_TYPE_TELEGESIS;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.config.discovery.usbserial.UsbSerialDeviceInformation;
import org.eclipse.smarthome.config.discovery.usbserial.UsbSerialDiscoveryParticipant;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.osgi.service.component.annotations.Component;

/**
* Discovery for ZigBee USB dongles, integrated in Eclipse SmartHome's USB-serial discovery by implementing
* a component of type {@link UsbSerialDiscoveryParticipant}.
* <p/>
* Currently, this {@link UsbSerialDiscoveryParticipant} supports three USB dongles built around
* a SiLabs EM357 ZigBee chip and a CP210x USB to UART bridge.
*
* @author Henning Sudbrock - initial contribution
*/
@Component(service = UsbSerialDiscoveryParticipant.class)
public class ZigBeeTelegesisUsbSerialDiscoveryParticipant implements UsbSerialDiscoveryParticipant {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(asList(THING_TYPE_TELEGESIS));

public static final int SILICON_LABS_USB_VENDOR_ID = 0x10c4;

/**
* For the {@link ZigBeeTelegesisUsbSerialDiscoveryParticipant#SILICON_LABS_USB_VENDOR_ID}, this USB product ID
* is used for the Telegesis ZigBee dongle model ETRX3USB+8M, as well as for one version of the QIVICON ZigBee stick
* (which is also produced by Telegesis).
*/
public static final int QIVICON_DONGLE_V1_AND_TELEGESIS_USB_PRODUCT_ID = 0x8293;

/**
* For the {@link ZigBeeTelegesisUsbSerialDiscoveryParticipant#SILICON_LABS_USB_VENDOR_ID}, this USB product ID
* is used for one version of the QIVICON ZigBee stick.
*/
public static final int QIVICON_DONGLE_V2_USB_PRODUCT_ID = 0x89fb;

private static final int QIVICON_DONGLE_BAUD_RATE = 19200;

private static final String QIVICON_DONGLE_DEFAULT_LABEL = "Telegesis ZigBee Dongle";

@Override
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
return SUPPORTED_THING_TYPES;
}

@Override
public @Nullable DiscoveryResult createResult(UsbSerialDeviceInformation deviceInformation) {
if (isQiviconTelegesisDongle(deviceInformation)) {
return DiscoveryResultBuilder.create(createQiviconTelegesisDongleThingType(deviceInformation))
.withLabel(createQiviconTelegesisDongleLabel(deviceInformation))
.withRepresentationProperty(CONFIGURATION_PORT)
.withProperty(CONFIGURATION_PORT, deviceInformation.getSerialPort())
.withProperty(CONFIGURATION_BAUD, QIVICON_DONGLE_BAUD_RATE).build();
} else {
return null;
}
}

@Override
public @Nullable ThingUID getThingUID(UsbSerialDeviceInformation deviceInformation) {
if (isQiviconTelegesisDongle(deviceInformation)) {
return createQiviconTelegesisDongleThingType(deviceInformation);
} else {
return null;
}
}

private boolean isQiviconTelegesisDongle(UsbSerialDeviceInformation deviceInformation) {
return deviceInformation.getVendorId() == SILICON_LABS_USB_VENDOR_ID
&& (deviceInformation.getProductId() == QIVICON_DONGLE_V1_AND_TELEGESIS_USB_PRODUCT_ID
|| deviceInformation.getProductId() == QIVICON_DONGLE_V2_USB_PRODUCT_ID);
}

private ThingUID createQiviconTelegesisDongleThingType(UsbSerialDeviceInformation deviceInformation) {
return new ThingUID(THING_TYPE_TELEGESIS, deviceInformation.getSerialNumber());
}

private @Nullable String createQiviconTelegesisDongleLabel(UsbSerialDeviceInformation deviceInformation) {
if (deviceInformation.getProduct() != null && !deviceInformation.getProduct().isEmpty()) {
return deviceInformation.getProduct() + " (ZigBee USB dongle)";
} else {
return QIVICON_DONGLE_DEFAULT_LABEL;
}
}
}
Loading