You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2019/08/27 12:51:16 UTC
[plc4x] branch develop updated: - First Working version of an
AB-ETH PLC4X driver that is able to communicate with a real device.
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 6c9ce3c - First Working version of an AB-ETH PLC4X driver that is able to communicate with a real device.
6c9ce3c is described below
commit 6c9ce3ccfad45438bb85dc2c52abcdaa82f032ac
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Aug 27 14:51:07 2019 +0200
- First Working version of an AB-ETH PLC4X driver that is able to communicate with a real device.
---
plc4j/examples/hello-world-plc4x/pom.xml | 7 +++
.../plc4x/java/examples/helloplc4x/HelloPlc4x.java | 2 +
.../main/resources/protocols/abeth/ab-eth.mspec | 1 -
.../java/abeth/connection/AbEthFieldHandler.java | 36 +++++++++++++
.../java/abeth/connection/AbEthPlcConnection.java | 17 ++++--
.../apache/plc4x/java/abeth/model/AbEthField.java | 8 ++-
.../plc4x/java/abeth/model/types/FileType.java | 1 +
.../java/abeth/protocol/Plc4xAbEthProtocol.java | 62 ++++++++++++++++++++--
8 files changed, 123 insertions(+), 11 deletions(-)
diff --git a/plc4j/examples/hello-world-plc4x/pom.xml b/plc4j/examples/hello-world-plc4x/pom.xml
index 988a075..53a20c3 100644
--- a/plc4j/examples/hello-world-plc4x/pom.xml
+++ b/plc4j/examples/hello-world-plc4x/pom.xml
@@ -55,6 +55,12 @@
<version>0.5.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.plc4x.sandbox</groupId>
+ <artifactId>test-java-ab-eth-driver</artifactId>
+ <version>0.5.0-SNAPSHOT</version>
+ <scope>runtime</scope>
+ </dependency>
<dependency>
<groupId>commons-cli</groupId>
@@ -85,6 +91,7 @@
<usedDependencies combine.children="append">
<usedDependency>org.apache.plc4x:plc4j-driver-s7</usedDependency>
<usedDependency>org.apache.plc4x:plc4j-driver-simulated</usedDependency>
+ <usedDependency>org.apache.plc4x.sandbox:test-java-ab-eth-driver</usedDependency>
<usedDependency>org.slf4j:log4j-over-slf4j</usedDependency>
</usedDependencies>
</configuration>
diff --git a/plc4j/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java b/plc4j/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
index 9a236ae..dd04d78 100644
--- a/plc4j/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
+++ b/plc4j/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
@@ -83,6 +83,8 @@ public class HelloPlc4x {
logger.error("An error occurred: " + throwable.getMessage(), throwable);
}
});
+ } catch (Exception e) {
+ e.printStackTrace();
}
}
diff --git a/protocols/ab-eth/src/main/resources/protocols/abeth/ab-eth.mspec b/protocols/ab-eth/src/main/resources/protocols/abeth/ab-eth.mspec
index a279bdb..851a072 100644
--- a/protocols/ab-eth/src/main/resources/protocols/abeth/ab-eth.mspec
+++ b/protocols/ab-eth/src/main/resources/protocols/abeth/ab-eth.mspec
@@ -64,7 +64,6 @@
[typeSwitch 'commandCode'
['0x4F' DF1CommandResponseMessageProtectedTypedLogicalRead [uint 8 'status']
[array uint 8 'data' length 'payloadLength - 8']
- [optional uint 8 'extendedStatus' 'status != 0']
]
]
]
diff --git a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/connection/AbEthFieldHandler.java b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/connection/AbEthFieldHandler.java
new file mode 100644
index 0000000..913736a
--- /dev/null
+++ b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/connection/AbEthFieldHandler.java
@@ -0,0 +1,36 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you 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.
+*/
+package org.apache.plc4x.java.abeth.connection;
+
+import org.apache.plc4x.java.abeth.model.AbEthField;
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
+import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.base.connection.DefaultPlcFieldHandler;
+
+public class AbEthFieldHandler extends DefaultPlcFieldHandler {
+
+ @Override
+ public PlcField createField(String fieldQuery) throws PlcInvalidFieldException {
+ if (AbEthField.matches(fieldQuery)) {
+ return AbEthField.of(fieldQuery);
+ }
+ throw new PlcInvalidFieldException(fieldQuery);
+ }
+
+}
diff --git a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/connection/AbEthPlcConnection.java b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/connection/AbEthPlcConnection.java
index 7c0228c..594f837 100644
--- a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/connection/AbEthPlcConnection.java
+++ b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/connection/AbEthPlcConnection.java
@@ -30,11 +30,9 @@ import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.base.connection.ChannelFactory;
import org.apache.plc4x.java.base.connection.NettyPlcConnection;
import org.apache.plc4x.java.base.connection.TcpSocketChannelFactory;
+import org.apache.plc4x.java.base.events.ConnectEvent;
import org.apache.plc4x.java.base.events.ConnectedEvent;
-import org.apache.plc4x.java.base.messages.InternalPlcReadRequest;
-import org.apache.plc4x.java.base.messages.InternalPlcReadResponse;
-import org.apache.plc4x.java.base.messages.PlcReader;
-import org.apache.plc4x.java.base.messages.PlcRequestContainer;
+import org.apache.plc4x.java.base.messages.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -73,6 +71,12 @@ public class AbEthPlcConnection extends NettyPlcConnection implements PlcReader
}
@Override
+ protected void sendChannelCreatedEvent() {
+ // Send an event to the pipeline telling the Protocol filters what's going on.
+ channel.pipeline().fireUserEventTriggered(new ConnectEvent());
+ }
+
+ @Override
public PlcField prepareField(String fieldQuery) throws PlcInvalidFieldException {
return AbEthField.of(fieldQuery);
}
@@ -106,6 +110,11 @@ public class AbEthPlcConnection extends NettyPlcConnection implements PlcReader
}
@Override
+ public PlcReadRequest.Builder readRequestBuilder() {
+ return new DefaultPlcReadRequest.Builder(this, new AbEthFieldHandler());
+ }
+
+ @Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
InternalPlcReadRequest internalReadRequest = checkInternal(readRequest, InternalPlcReadRequest.class);
CompletableFuture<InternalPlcReadResponse> future = new CompletableFuture<>();
diff --git a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/model/AbEthField.java b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/model/AbEthField.java
index 49143d3..82d7844 100644
--- a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/model/AbEthField.java
+++ b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/model/AbEthField.java
@@ -28,7 +28,7 @@ import java.util.regex.Pattern;
public class AbEthField implements PlcField {
private static final Pattern ADDRESS_PATTERN =
- Pattern.compile("^N(?<fileNumber>\\d{1,7})\\:(?<elementNumber>\\d{1,7})/(?<subElementNumber>\\d{1,7}):(?<dataType>[a-zA-Z_]+)(\\[(?<size>\\d+)])?");
+ Pattern.compile("^N(?<fileNumber>\\d{1,7}):(?<elementNumber>\\d{1,7})/(?<subElementNumber>\\d{1,7}):(?<dataType>[a-zA-Z_]+)(\\[(?<size>\\d+)])?");
private static final String FILE_NUMBER = "fileNumber";
private static final String ELEMENT_NUMBER = "elementNumber";
@@ -70,13 +70,17 @@ public class AbEthField implements PlcField {
return subElementNumber;
}
+ public static boolean matches(String fieldString) {
+ return ADDRESS_PATTERN.matcher(fieldString).matches();
+ }
+
public static AbEthField of(String fieldString) {
Matcher matcher = ADDRESS_PATTERN.matcher(fieldString);
if(matcher.matches()) {
short fileNumber = Short.parseShort(matcher.group(FILE_NUMBER));
short elementNumber = Short.parseShort(matcher.group(ELEMENT_NUMBER));
short subElementNumber = Short.parseShort(matcher.group(SUB_ELEMENT_NUMBER));
- FileType fileType = FileType.valueOf(Short.parseShort(matcher.group(DATA_TYPE)));
+ FileType fileType = FileType.valueOf(matcher.group(DATA_TYPE).toUpperCase());
short byteSize = Short.parseShort(matcher.group(SIZE));
return new AbEthField(byteSize, fileNumber, fileType,elementNumber, subElementNumber);
}
diff --git a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/model/types/FileType.java b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/model/types/FileType.java
index 3c3df85..ebc02ef 100644
--- a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/model/types/FileType.java
+++ b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/model/types/FileType.java
@@ -23,6 +23,7 @@ import java.util.Map;
public enum FileType {
+ HURZ((short) 0x64),
STATUS((short) 0x84),
BIT((short) 0x85),
TIMER((short) 0x86),
diff --git a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/Plc4xAbEthProtocol.java b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/Plc4xAbEthProtocol.java
index 7e90dfd..6c3965d 100644
--- a/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/Plc4xAbEthProtocol.java
+++ b/sandbox/test-java-ab-eth-driver/src/main/java/org/apache/plc4x/java/abeth/protocol/Plc4xAbEthProtocol.java
@@ -18,11 +18,15 @@ under the License.
*/
package org.apache.plc4x.java.abeth.protocol;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
+import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.plc4x.java.abeth.*;
import org.apache.plc4x.java.abeth.model.AbEthField;
import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcRequest;
import org.apache.plc4x.java.api.messages.PlcResponse;
@@ -35,13 +39,18 @@ import org.apache.plc4x.java.base.messages.DefaultPlcReadResponse;
import org.apache.plc4x.java.base.messages.InternalPlcReadRequest;
import org.apache.plc4x.java.base.messages.PlcRequestContainer;
import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
+import org.apache.plc4x.java.base.messages.items.DefaultShortFieldItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
public class Plc4xAbEthProtocol extends PlcMessageToMessageCodec<CIPEncapsulationPacket, PlcRequestContainer> {
@@ -145,11 +154,56 @@ public class Plc4xAbEthProtocol extends PlcMessageToMessageCodec<CIPEncapsulatio
}
private PlcResponse decodeReadResponse(
- CIPEncapsulationReadResponse cipResponse, PlcRequestContainer requestContainer) {
+ CIPEncapsulationReadResponse plcReadResponse, PlcRequestContainer requestContainer) {
+
+ InternalPlcReadRequest plcReadRequest = (InternalPlcReadRequest) requestContainer.getRequest();
+
+ Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> values = new HashMap<>();
+ for (String fieldName : plcReadRequest.getFieldNames()) {
+ AbEthField field = (AbEthField) plcReadRequest.getField(fieldName);
+ PlcResponseCode responseCode = decodeResponseCode(plcReadResponse.getResponse().getStatus());
+
+ BaseDefaultFieldItem fieldItem = null;
+ if (responseCode == PlcResponseCode.OK) {
+ try {
+ switch (field.getFileType()) {
+ case HURZ:
+ if(plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
+ DF1CommandResponseMessageProtectedTypedLogicalRead df1PTLR = (DF1CommandResponseMessageProtectedTypedLogicalRead) plcReadResponse.getResponse();
+ short[] data = df1PTLR.getData();
+ Short[] convData = new Short[data.length];
+ for(int i = 0; i < data.length; i++) {
+ convData[i] = data[i];
+ }
+ fieldItem = new DefaultShortFieldItem(convData);
+ }
+ break;
+ default:
+ logger.warn("Problem during decoding of field {}: Decoding of file type not implemented; " +
+ "FieldInformation: {}", fieldName, field);
+ }
+ }
+ catch (Exception e) {
+ logger.warn("Some other error occurred casting field {}, FieldInformation: {}",fieldName, field,e);
+ }
+ }
+ Pair<PlcResponseCode, BaseDefaultFieldItem> result = new ImmutablePair<>(responseCode, fieldItem);
+ values.put(fieldName, result);
+ }
- InternalPlcReadRequest readRequest = (InternalPlcReadRequest) requestContainer.getRequest();
+ return new DefaultPlcReadResponse(plcReadRequest, values);
+ }
- Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> fields = new HashMap<>();
- return new DefaultPlcReadResponse(readRequest, fields);
+ private PlcResponseCode decodeResponseCode(short status) {
+ if(status == 0) {
+ return PlcResponseCode.OK;
+ }
+ return PlcResponseCode.NOT_FOUND;
+ }
+
+ private BaseDefaultFieldItem decodeReadResponseUnsignedByteField(AbEthField field, ByteBuf data) {
+ Short[] shorts = null;//readAllValues(Short.class, field, i -> data.readUnsignedByte());
+ return new DefaultShortFieldItem(shorts);
}
+
}