-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename can binding to canbus, invent bittpl.
BitTpl stands for bit template, a generic purpose binary payload matcher. Signed-off-by: Łukasz Dywicki <[email protected]>
- Loading branch information
Showing
50 changed files
with
1,770 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ng.canbus/src/main/java/org/connectorio/addons/binding/canbus/CANbusBindingConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2023-2023 ConnectorIO sp. z o.o. | ||
* | ||
* This is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* https://www.gnu.org/licenses/gpl-3.0.txt | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Foobar; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
package org.connectorio.addons.binding.canbus; | ||
|
||
|
||
import java.util.Set; | ||
import org.connectorio.addons.binding.BaseBindingConstants; | ||
import org.openhab.core.thing.ThingTypeUID; | ||
|
||
public interface CANbusBindingConstants extends BaseBindingConstants { | ||
|
||
String BINDING_ID = BaseBindingConstants.identifier("canbus"); | ||
|
||
ThingTypeUID SOCKETCAN_BRIDGE_THING_TYPE = new ThingTypeUID(BINDING_ID, "socketcan"); | ||
ThingTypeUID MESSAGE_THING_TYPE = new ThingTypeUID(BINDING_ID, "message"); | ||
Set<ThingTypeUID> SUPPORTED_THINGS = Set.of(SOCKETCAN_BRIDGE_THING_TYPE, MESSAGE_THING_TYPE); | ||
} |
14 changes: 14 additions & 0 deletions
14
...ons.binding.canbus/src/main/java/org/connectorio/addons/binding/canbus/CanConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.connectorio.addons.binding.canbus; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
import java.util.function.Consumer; | ||
|
||
public interface CanConnection { | ||
|
||
CompletableFuture<Void> write(int cob, byte[] data); | ||
|
||
// cancellable subscription | ||
Future<Void> subscribe(int cob, Consumer<byte[]> callback); | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...nbus/src/main/java/org/connectorio/addons/binding/canbus/config/MessageConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.connectorio.addons.binding.canbus.config; | ||
|
||
public class MessageConfiguration { | ||
|
||
int cob; | ||
String template; | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...us/src/main/java/org/connectorio/addons/binding/canbus/config/SocketCANConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.connectorio.addons.binding.canbus.config; | ||
|
||
import org.connectorio.addons.binding.config.Configuration; | ||
|
||
public class SocketCANConfiguration implements Configuration { | ||
|
||
public String name; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
.../src/main/java/org/connectorio/addons/binding/canbus/config/channel/BaseOffsetConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.connectorio.addons.binding.canbus.config.channel; | ||
|
||
public class BaseOffsetConfig implements OffsetConfig { | ||
|
||
public int offset; | ||
|
||
@Override | ||
public int offset() { | ||
return offset; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...src/main/java/org/connectorio/addons/binding/canbus/config/channel/ByteChannelConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.connectorio.addons.binding.canbus.config.channel; | ||
|
||
public class ByteChannelConfig extends BaseOffsetConfig implements LengthConfig { | ||
|
||
public int length; | ||
|
||
@Override | ||
public int length() { | ||
return length; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
.../main/java/org/connectorio/addons/binding/canbus/config/channel/ContactChannelConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.connectorio.addons.binding.canbus.config.channel; | ||
|
||
public class ContactChannelConfig extends BaseOffsetConfig { | ||
|
||
String openValue; | ||
String closedValue; | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...nbus/src/main/java/org/connectorio/addons/binding/canbus/config/channel/LengthConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.connectorio.addons.binding.canbus.config.channel; | ||
|
||
public interface LengthConfig { | ||
|
||
int length(); | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...c/main/java/org/connectorio/addons/binding/canbus/config/channel/NumberChannelConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.connectorio.addons.binding.canbus.config.channel; | ||
|
||
import org.apache.plc4x.java.genericcan.readwrite.GenericCANDataType; | ||
|
||
public class NumberChannelConfig extends BaseOffsetConfig implements LengthConfig { | ||
|
||
public int length; | ||
|
||
public GenericCANDataType type; | ||
|
||
@Override | ||
public int length() { | ||
return length; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...nbus/src/main/java/org/connectorio/addons/binding/canbus/config/channel/OffsetConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.connectorio.addons.binding.canbus.config.channel; | ||
|
||
public interface OffsetConfig { | ||
|
||
int offset(); | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...c/main/java/org/connectorio/addons/binding/canbus/config/channel/SwitchChannelConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.connectorio.addons.binding.canbus.config.channel; | ||
|
||
public class SwitchChannelConfig extends BaseOffsetConfig { | ||
|
||
String onValue; | ||
String offValue; | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...rc/main/java/org/connectorio/addons/binding/canbus/discovery/CANDiscoveryParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.connectorio.addons.binding.canbus.discovery; | ||
|
||
// TBD | ||
public interface CANDiscoveryParticipant { | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...us/src/main/java/org/connectorio/addons/binding/canbus/internal/DefaultCanConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.connectorio.addons.binding.canbus.internal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
import org.apache.plc4x.java.api.PlcConnection; | ||
import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent; | ||
import org.apache.plc4x.java.api.model.PlcConsumerRegistration; | ||
import org.apache.plc4x.java.api.value.PlcValue; | ||
import org.apache.plc4x.java.can.generic.tag.GenericCANTag; | ||
import org.apache.plc4x.java.genericcan.readwrite.GenericCANDataType; | ||
import org.apache.plc4x.java.spi.values.PlcUSINT; | ||
import org.apache.plc4x.java.spi.values.PlcValues; | ||
import org.connectorio.addons.binding.canbus.CanConnection; | ||
|
||
public class DefaultCanConnection implements CanConnection { | ||
|
||
private final PlcConnection connection; | ||
|
||
public DefaultCanConnection(PlcConnection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> write(int cob, byte[] data) { | ||
GenericCANTag tag = new GenericCANTag(cob, GenericCANDataType.UNSIGNED8, 8); | ||
return connection.writeRequestBuilder() | ||
.addTag("data", tag, encode(data)) | ||
.build().execute() | ||
.thenApply(r -> null); | ||
} | ||
|
||
private PlcValue encode(byte[] data) { | ||
List<PlcUSINT> values = new ArrayList<>(); | ||
for (byte b : data) { | ||
values.add(new PlcUSINT(b)); | ||
} | ||
|
||
return PlcValues.of(values); | ||
} | ||
|
||
@Override | ||
public Future<Void> subscribe(int cob, Consumer<byte[]> callback) { | ||
GenericCANTag tag = new GenericCANTag(cob, GenericCANDataType.UNSIGNED8, 8); | ||
|
||
final AtomicReference<PlcConsumerRegistration> registration = new AtomicReference<>(); | ||
CompletableFuture<Void> ret = new CompletableFuture<>() { | ||
@Override | ||
public boolean cancel(boolean mayInterruptIfRunning) { | ||
PlcConsumerRegistration reg = registration.get(); | ||
if (reg != null) { | ||
reg.unregister(); | ||
} | ||
return super.cancel(mayInterruptIfRunning); | ||
} | ||
}; | ||
|
||
connection.subscriptionRequestBuilder() | ||
.addEventTag("data", tag) | ||
.build().execute().thenApply(r -> { | ||
PlcConsumerRegistration reg = r.getSubscriptionHandle("data") | ||
.register(new Consumer<PlcSubscriptionEvent>() { | ||
@Override | ||
public void accept(PlcSubscriptionEvent plcSubscriptionEvent) { | ||
Collection<Short> data = plcSubscriptionEvent.getAllShorts("data"); | ||
callback.accept(decode(new ArrayList<>(data))); | ||
} | ||
}); | ||
registration.set(reg); | ||
return registration; | ||
}); | ||
|
||
return ret; | ||
} | ||
|
||
private byte[] decode(List<Short> data) { | ||
byte[] bytes = new byte[data.size()]; | ||
for (int index = 0; index < bytes.length; index++) { | ||
bytes[index] = data.get(index).byteValue(); | ||
} | ||
return bytes; | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...g/connectorio/addons/binding/canbus/internal/discovery/CANInterfaceDiscoveryDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright (C) 2023-2023 ConnectorIO Sp. z o.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.connectorio.addons.binding.canbus.internal.discovery; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import org.connectorio.addons.binding.canbus.CANbusBindingConstants; | ||
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 network API to discover CAN interface. | ||
*/ | ||
@Component(service = {DiscoveryService.class, NetworkInterfaceStateCallback.class}) | ||
public class CANInterfaceDiscoveryDelegate extends AbstractDiscoveryService implements DiscoveryService, | ||
NetworkInterfaceStateCallback { | ||
|
||
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton( | ||
CANbusBindingConstants.SOCKETCAN_BRIDGE_THING_TYPE); | ||
private final NetworkInterfaceRegistry networkInterfaceRegistry; | ||
|
||
@Activate | ||
public CANInterfaceDiscoveryDelegate(@Reference NetworkInterfaceRegistry networkInterfaceRegistry) throws IllegalArgumentException { | ||
super(SUPPORTED_THING_TYPES, 30, true); | ||
this.networkInterfaceRegistry = networkInterfaceRegistry; | ||
} | ||
|
||
@Override | ||
protected void startScan() { | ||
for (NetworkInterface networkInterface : networkInterfaceRegistry.getAll()) { | ||
if (isCompatible(networkInterface)) { | ||
discover(networkInterface); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void networkInterfaceUp(NetworkInterface networkInterface) { | ||
if (isCompatible(networkInterface)) { | ||
discover(networkInterface); | ||
} | ||
} | ||
|
||
@Override | ||
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.CAN_RAW.equals(networkType)) { | ||
String name = networkInterface.getUID().getName(); | ||
final DiscoveryResult result = DiscoveryResultBuilder | ||
.create(createThingUID(networkInterface)) | ||
.withLabel("CAN interface " + name) | ||
.withProperty("name", name) | ||
.withRepresentationProperty("name") | ||
.build(); | ||
|
||
thingDiscovered(result); | ||
} | ||
} | ||
} | ||
|
||
private static ThingUID createThingUID(NetworkInterface networkInterface) { | ||
return new ThingUID( | ||
CANbusBindingConstants.SOCKETCAN_BRIDGE_THING_TYPE, networkInterface.getUID().getName()); | ||
} | ||
|
||
private static boolean isCompatible(NetworkInterface networkInterface) { | ||
return CanNetworkInterfaceTypes.SOCKETCAN.equals(networkInterface.getInterfaceType()) || | ||
CanNetworkInterfaceTypes.VCAN.equals(networkInterface.getInterfaceType()); | ||
} | ||
|
||
} |
Oops, something went wrong.