Skip to content

Commit

Permalink
Separate CAN and CANopen bindings, introduce CAN network api.
Browse files Browse the repository at this point in the history
The CAN Network API is fairly close to IP network.
Currently there is a fixed assumpton that each detected CAN interface can host RAW and CANopen traffic.
Depending on further evollution/evaluation it might be ammended to some more fine grained detection.

Signed-off-by: Łukasz Dywicki <[email protected]>
  • Loading branch information
splatch committed Oct 31, 2023
1 parent ad3e60f commit 9c90b70
Show file tree
Hide file tree
Showing 67 changed files with 2,320 additions and 598 deletions.
44 changes: 27 additions & 17 deletions bundles/org.connectorio.addons.binding.can/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,53 @@
<dependencies>
<dependency>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.units</artifactId>
<artifactId>org.connectorio.addons.network.can</artifactId>
</dependency>
<dependency>
<groupId>com.github.hypfvieh</groupId>
<artifactId>dbus-java</artifactId>
<version>3.2.3</version>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.addons.binding.plc4x</artifactId>
</dependency>
<dependency>
<groupId>com.github.hypfvieh</groupId>
<artifactId>java-utils</artifactId>
<version>1.0.6</version>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.units</artifactId>
</dependency>

<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.33</version>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-driver-can</artifactId>
</dependency>

<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-ffi</artifactId>
<version>2.1.15</version>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.config.discovery</artifactId>
</dependency>

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
</dependency>

<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.config.discovery</artifactId>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.addons.feature.plc4x</artifactId>
<classifier>features</classifier>
<type>xml</type>
</dependency>

<dependency>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.addons.binding.test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>

</project>

This file was deleted.

20 changes: 6 additions & 14 deletions bundles/org.connectorio.addons.binding.canopen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<dependencies>
<dependency>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.addons.binding.can</artifactId>
<artifactId>org.connectorio.addons.network.can</artifactId>
</dependency>
<dependency>
<groupId>org.connectorio.addons</groupId>
Expand All @@ -45,16 +45,12 @@
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.units.si</artifactId>
</dependency>

<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-driver-canopen</artifactId>
</dependency>

<dependency>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.addons.binding.test</artifactId>
</dependency>

<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.config.discovery</artifactId>
Expand All @@ -73,21 +69,17 @@
<artifactId>org.connectorio.plc4x.extras.decorator.throttle</artifactId>
</dependency>

<!--
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>framework</artifactId>
<type>kar</type>
</dependency>
-->

<dependency>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.addons.feature.plc4x</artifactId>
<classifier>features</classifier>
<type>xml</type>
</dependency>

<dependency>
<groupId>org.connectorio.addons</groupId>
<artifactId>org.connectorio.addons.binding.test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,98 @@

import java.util.Collections;
import java.util.Set;
import org.connectorio.addons.binding.can.CANInterface;
import org.connectorio.addons.binding.can.CANInterfaceTypes;
import org.connectorio.addons.binding.can.discovery.CANInterfaceDiscoveryDelegate;
import org.connectorio.addons.binding.canopen.internal.CANopenBindingConstants;
import org.connectorio.addons.network.Network;
import org.connectorio.addons.network.NetworkType;
import org.connectorio.addons.network.can.CanNetwork;
import org.connectorio.addons.network.can.CanNetworkInterfaceTypes;
import org.connectorio.addons.network.can.CanNetworkTypes;
import org.connectorio.addons.network.iface.NetworkInterface;
import org.connectorio.addons.network.iface.NetworkInterfaceRegistry;
import org.connectorio.addons.network.iface.NetworkInterfaceStateCallback;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* Basic discovery service which rely on discovery of CAN interfaces.
* Basic discovery service which rely on network API to discover CAN interface.
*/
@Component(service = {DiscoveryService.class, CANInterfaceDiscoveryDelegate.class})
@Component(service = {DiscoveryService.class, NetworkInterfaceStateCallback.class})
public class CoInterfaceDiscoveryDelegate extends AbstractDiscoveryService implements DiscoveryService,
CANInterfaceDiscoveryDelegate {
NetworkInterfaceStateCallback {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(CANopenBindingConstants.SOCKETCAN_BRIDGE_THING_TYPE);
private final NetworkInterfaceRegistry networkInterfaceRegistry;

public CoInterfaceDiscoveryDelegate() throws IllegalArgumentException {
@Activate
public CoInterfaceDiscoveryDelegate(@Reference NetworkInterfaceRegistry networkInterfaceRegistry) throws IllegalArgumentException {
super(SUPPORTED_THING_TYPES, 30, true);
this.networkInterfaceRegistry = networkInterfaceRegistry;
}

@Override
public void interfaceAvailable(CANInterface iface) {
if (CANInterfaceTypes.SOCKET_CAN.equals(iface.getType())) {
final String name = iface.getName();
final DiscoveryResult result = DiscoveryResultBuilder
.create(new ThingUID(CANopenBindingConstants.SOCKETCAN_BRIDGE_THING_TYPE, name))
.withLabel("CANopen interface " + name)
.withProperty("name", name)
.withRepresentationProperty("name")
.build();
protected void startScan() {
for (NetworkInterface networkInterface : networkInterfaceRegistry.getAll()) {
if (isCompatible(networkInterface)) {
discover(networkInterface);
}
}
}

thingDiscovered(result);
@Override
public void networkInterfaceUp(NetworkInterface networkInterface) {
if (isCompatible(networkInterface)) {
discover(networkInterface);
}
}

@Override
protected void startScan() {
public void networkInterfaceDown(NetworkInterface networkInterface) {
for (Network network : networkInterface.getNetworks()) {
if (!(network instanceof CanNetwork)) {
continue;
}

// remove all discovery results which point to networks we just lost
thingRemoved(createThingUID(networkInterface));
}
}

private void discover(NetworkInterface networkInterface) {
for (Network network : networkInterface.getNetworks()) {
if (!(network instanceof CanNetwork)) {
continue;
}

CanNetwork canNetwork = (CanNetwork) network;
NetworkType networkType = canNetwork.getType();
if (CanNetworkTypes.CANOPEN.equals(networkType)) {
String name = networkInterface.getUID().getName();
final DiscoveryResult result = DiscoveryResultBuilder
.create(createThingUID(networkInterface))
.withLabel("CANopen interface " + name)
.withProperty("name", name)
.withRepresentationProperty("name")
.build();

thingDiscovered(result);
}
}
}

private static ThingUID createThingUID(NetworkInterface networkInterface) {
return new ThingUID(CANopenBindingConstants.SOCKETCAN_BRIDGE_THING_TYPE, networkInterface.getUID().getName());
}

private static boolean isCompatible(NetworkInterface networkInterface) {
return CanNetworkInterfaceTypes.SOCKETCAN.equals(networkInterface.getInterfaceType()) ||
CanNetworkInterfaceTypes.VCAN.equals(networkInterface.getInterfaceType());
}

}
Loading

0 comments on commit 9c90b70

Please sign in to comment.