This is an automated email from the ASF dual-hosted git repository.
ldywicki pushed a commit to branch feature/profinet2
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/feature/profinet2 by this push:
new c166e74 Brief implementation of profinet dcp identification logic.
c166e74 is described below
commit c166e74ee20ce5ecf129214d32eae111ef8ac3ad
Author: Ćukasz Dywicki <[email protected]>
AuthorDate: Mon Apr 19 19:14:10 2021 +0200
Brief implementation of profinet dcp identification logic.
So far broadcast request + replies are covered. Remote answers are mapped
to PlcStruct in absence of better choice.
Switching RawSocketChannelFactory form passive to an active since it can
write down over libpcap.
---
.../rawsocket/RawSocketChannelFactory.java | 2 +-
sandbox/test-java-profinet-driver/pom.xml | 11 --
.../java/profinet/dcp/ProfinetDCPPlcDriver.java | 17 ++++
.../ProfinetDCPDriverContext.java} | 6 +-
.../java/profinet/dcp/field/BaseIdentifyField.java | 52 ++++++++++
.../{ProfinetDcpField.java => IdentifyField.java} | 9 +-
.../profinet/dcp/field/IdentifyRequestField.java | 88 ++++++++++++++++
.../profinet/dcp/field/IdentifyResponseField.java | 63 ++++++++++++
.../java/profinet/dcp/field/ProfinetDcpField.java | 2 +-
.../profinet/dcp/field/ProfinetFieldHandler.java | 8 +-
.../dcp/protocol/ProfinetDCPProtocolLogic.java | 113 ++++++++++++++-------
.../protocol/ProfinetDCPSubscriptionHandle.java | 93 ++++++++++++++++-
.../conversation/Conversation.java} | 14 ++-
.../conversation/IdentifyRequestConversation.java | 74 ++++++++++++++
14 files changed, 490 insertions(+), 62 deletions(-)
diff --git
a/plc4j/transports/raw-socket/src/main/java/org/apache/plc4x/java/transport/rawsocket/RawSocketChannelFactory.java
b/plc4j/transports/raw-socket/src/main/java/org/apache/plc4x/java/transport/rawsocket/RawSocketChannelFactory.java
index bed7748..207f735 100644
---
a/plc4j/transports/raw-socket/src/main/java/org/apache/plc4x/java/transport/rawsocket/RawSocketChannelFactory.java
+++
b/plc4j/transports/raw-socket/src/main/java/org/apache/plc4x/java/transport/rawsocket/RawSocketChannelFactory.java
@@ -53,7 +53,7 @@ public class RawSocketChannelFactory extends
NettyChannelFactory implements HasC
@Override
public boolean isPassive() {
- return true;
+ return false;
}
@Override
diff --git a/sandbox/test-java-profinet-driver/pom.xml
b/sandbox/test-java-profinet-driver/pom.xml
index 93405f3..2a05f6c 100644
--- a/sandbox/test-java-profinet-driver/pom.xml
+++ b/sandbox/test-java-profinet-driver/pom.xml
@@ -70,12 +70,6 @@
<version>1.8.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- <version>1.3.0-alpha5</version>
- <scope>compile</scope>
- </dependency>
<!-- actual dependencies ;-) -->
<dependency>
@@ -135,11 +129,6 @@
<version>0.9.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>org.apache.plc4x</groupId>
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/ProfinetDCPPlcDriver.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/ProfinetDCPPlcDriver.java
index 0e40959..58c8a79 100644
---
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/ProfinetDCPPlcDriver.java
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/ProfinetDCPPlcDriver.java
@@ -22,6 +22,7 @@ import java.util.function.Consumer;
import java.util.function.ToIntFunction;
import org.apache.plc4x.java.api.value.PlcValueHandler;
import org.apache.plc4x.java.profinet.dcp.configuration.ProfinetConfiguration;
+import org.apache.plc4x.java.profinet.dcp.context.ProfinetDCPDriverContext;
import org.apache.plc4x.java.profinet.dcp.field.ProfinetFieldHandler;
import org.apache.plc4x.java.profinet.dcp.protocol.ProfinetDCPProtocolLogic;
import org.apache.plc4x.java.profinet.dcp.readwrite.BaseEthernetFrame;
@@ -77,10 +78,26 @@ public class ProfinetDCPPlcDriver extends
GeneratedDriverBase<BaseEthernetFrame>
}
@Override
+ protected boolean canRead() {
+ return false; // wait for GET requests
+ }
+
+ @Override
+ protected boolean canWrite() {
+ return true;
+ }
+
+ @Override
+ protected boolean canSubscribe() {
+ return true;
+ }
+
+ @Override
protected ProtocolStackConfigurer<BaseEthernetFrame> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(BaseEthernetFrame.class,
BaseEthernetFrameIO.class)
.withProtocol(ProfinetDCPProtocolLogic.class)
.withPacketSizeEstimator(ProfinetPacketEstimator.class)
+ .withDriverContext(ProfinetDCPDriverContext.class)
//.withCorruptPacketRemover(CorruptEthernetFrameRemover.class)
.build();
}
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/context/ProfinetDCPDriverContext.java
similarity index 82%
copy from
sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
copy to
sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/context/ProfinetDCPDriverContext.java
index 98d92d5..9fcd434 100644
---
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/context/ProfinetDCPDriverContext.java
@@ -16,9 +16,9 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.plc4x.java.profinet.dcp.field;
+package org.apache.plc4x.java.profinet.dcp.context;
-import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.spi.context.DriverContext;
-public class ProfinetDcpField implements PlcField {
+public class ProfinetDCPDriverContext implements DriverContext {
}
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/BaseIdentifyField.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/BaseIdentifyField.java
new file mode 100644
index 0000000..37d752f
--- /dev/null
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/BaseIdentifyField.java
@@ -0,0 +1,52 @@
+/*
+ * 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.profinet.dcp.field;
+
+import org.apache.plc4x.java.profinet.dcp.readwrite.types.DCPBlockOption;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Matcher;
+
+/**
+ * Base type for identification fields which allows to parse blocks section
field expression.
+ */
+abstract class BaseIdentifyField extends ProfinetDcpField implements
IdentifyField {
+
+ protected static List<DCPBlockOption> parseBlocks(Matcher matcher) {
+ String requestedBlocks = matcher.group("block");
+
+ List<DCPBlockOption> blocks = new ArrayList<>();
+ if (requestedBlocks == null || requestedBlocks.trim().isEmpty()) {
+ blocks.add(DCPBlockOption.ALL_SELECTOR);
+ } else if (requestedBlocks.contains(",")) {
+ Arrays.stream(requestedBlocks.split(","))
+ .map(String::trim)
+ .map(String::toUpperCase)
+ .map(DCPBlockOption::valueOf)
+ .forEach(blocks::add);
+ } else {
+
blocks.add(DCPBlockOption.valueOf(requestedBlocks.trim().toUpperCase()));
+ }
+ return blocks;
+ }
+
+}
+
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyField.java
similarity index 76%
copy from
sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
copy to
sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyField.java
index 98d92d5..c58ee3f 100644
---
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyField.java
@@ -20,5 +20,12 @@ package org.apache.plc4x.java.profinet.dcp.field;
import org.apache.plc4x.java.api.model.PlcField;
-public class ProfinetDcpField implements PlcField {
+/**
+ * Definition of constants and types related to identification requests or
replies.
+ */
+public interface IdentifyField extends PlcField {
+
+ String BLOCK_PATTERN = "(IP|PROPERTIES|DHCP|CONTROL|ALL|,)+";
+ String BLOCK_ADDRESS_PATTERN = "(?:(?<block>" + BLOCK_PATTERN + "))?";
+
}
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyRequestField.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyRequestField.java
new file mode 100644
index 0000000..80e753a
--- /dev/null
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyRequestField.java
@@ -0,0 +1,88 @@
+/*
+ * 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.profinet.dcp.field;
+
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
+import org.apache.plc4x.java.profinet.dcp.readwrite.types.DCPBlockOption;
+
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Definition of identify field which can accept Profinet DCP block options.
+ *
+ * Supported syntax:
+ * IDENTIFY_BROADCAST:
+ * IDENTIFY_BROADCAST:/responseDelay
+ * IDENTIFY_BROADCAST:BLOCK/responseDelay
+ * IDENTIFY_BROADCAST:IP,DHCP/responseDelay
+ *
+ * Block options are defined as {@link IdentifyField#BLOCK_PATTERN}. If no
block is defined then ALL is assumed.
+ */
+public class IdentifyRequestField extends BaseIdentifyField implements
IdentifyField {
+
+ public static final Pattern ADDRESS_PATTERN =
Pattern.compile("IDENTIFY_BROADCAST:" + BLOCK_ADDRESS_PATTERN
+"(?:\\/(?<responseDelay>\\d+))?");
+
+ private final List<DCPBlockOption> blocks;
+ private final int responseDelay;
+
+ public IdentifyRequestField(List<DCPBlockOption> blocks) {
+ this(blocks, 1000);
+ }
+
+ public IdentifyRequestField(List<DCPBlockOption> blocks, int
responseDelay) {
+ this.blocks = blocks;
+ this.responseDelay = responseDelay;
+ }
+
+ public List<DCPBlockOption> getBlocks() {
+ return blocks;
+ }
+
+ public int getResponseDelay() {
+ return responseDelay;
+ }
+
+ public static boolean matches(String fieldQuery) {
+ return ADDRESS_PATTERN.matcher(fieldQuery).matches();
+ }
+
+ public static Matcher getMatcher(String addressString) throws
PlcInvalidFieldException {
+ Matcher matcher = ADDRESS_PATTERN.matcher(addressString);
+ if (matcher.matches()) {
+ return matcher;
+ }
+
+ throw new PlcInvalidFieldException(addressString, ADDRESS_PATTERN);
+ }
+
+ public static IdentifyRequestField of(String addressString) {
+ Matcher matcher = getMatcher(addressString);
+ List<DCPBlockOption> blocks = parseBlocks(matcher);
+
+ String responseDelayGroup = matcher.group("responseDelay");
+ if (responseDelayGroup != null) {
+ return new IdentifyRequestField(blocks,
Integer.parseInt(responseDelayGroup));
+ }
+
+ return new IdentifyRequestField(blocks);
+ }
+
+}
\ No newline at end of file
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyResponseField.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyResponseField.java
new file mode 100644
index 0000000..a9cb17f
--- /dev/null
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/IdentifyResponseField.java
@@ -0,0 +1,63 @@
+/*
+ * 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.profinet.dcp.field;
+
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
+import org.apache.plc4x.java.profinet.dcp.readwrite.types.DCPBlockOption;
+
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Response field which is used for identify response subscriptions.
+ */
+public class IdentifyResponseField extends BaseIdentifyField implements
IdentifyField {
+
+ public static final Pattern ADDRESS_PATTERN =
Pattern.compile("IDENTIFY_RESPONSE:" + BLOCK_ADDRESS_PATTERN);
+
+ private final List<DCPBlockOption> blocks;
+
+ public IdentifyResponseField(List<DCPBlockOption> blocks) {
+ this.blocks = blocks;
+ }
+
+ public List<DCPBlockOption> getBlocks() {
+ return blocks;
+ }
+
+ public static boolean matches(String fieldQuery) {
+ return ADDRESS_PATTERN.matcher(fieldQuery).matches();
+ }
+
+ public static Matcher getMatcher(String addressString) throws
PlcInvalidFieldException {
+ Matcher matcher = ADDRESS_PATTERN.matcher(addressString);
+ if (matcher.matches()) {
+ return matcher;
+ }
+
+ throw new PlcInvalidFieldException(addressString, ADDRESS_PATTERN);
+ }
+
+ public static IdentifyResponseField of(String addressString) {
+ Matcher matcher = getMatcher(addressString);
+ return new IdentifyResponseField(parseBlocks(matcher));
+ }
+
+}
\ No newline at end of file
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
index 98d92d5..6628b294 100644
---
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
@@ -20,5 +20,5 @@ package org.apache.plc4x.java.profinet.dcp.field;
import org.apache.plc4x.java.api.model.PlcField;
-public class ProfinetDcpField implements PlcField {
+public abstract class ProfinetDcpField implements PlcField {
}
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetFieldHandler.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetFieldHandler.java
index b90b84c..48b9e15 100644
---
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetFieldHandler.java
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetFieldHandler.java
@@ -23,12 +23,18 @@ import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.spi.connection.PlcFieldHandler;
/**
- * TODO implement this.
+ *
*/
public class ProfinetFieldHandler implements PlcFieldHandler {
@Override
public PlcField createField(String fieldQuery) throws
PlcInvalidFieldException {
+ if (IdentifyRequestField.matches(fieldQuery)) {
+ return IdentifyRequestField.of(fieldQuery);
+ }
+ if (IdentifyResponseField.matches(fieldQuery)) {
+ return IdentifyResponseField.of(fieldQuery);
+ }
return null;
}
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/ProfinetDCPProtocolLogic.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/ProfinetDCPProtocolLogic.java
index e383d1c..2583b78 100644
---
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/ProfinetDCPProtocolLogic.java
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/ProfinetDCPProtocolLogic.java
@@ -27,34 +27,36 @@ import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import ch.qos.logback.classic.util.LogbackMDCAdapter;
-import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
-import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
-import org.apache.plc4x.java.api.messages.PlcSubscriptionResponse;
+import org.apache.plc4x.java.api.exceptions.PlcException;
+import org.apache.plc4x.java.api.messages.*;
import org.apache.plc4x.java.api.model.PlcConsumerRegistration;
+import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.api.types.PlcSubscriptionType;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.profinet.dcp.configuration.ProfinetConfiguration;
+import org.apache.plc4x.java.profinet.dcp.field.IdentifyRequestField;
import org.apache.plc4x.java.profinet.dcp.field.ProfinetDcpField;
+import
org.apache.plc4x.java.profinet.dcp.protocol.conversation.IdentifyRequestConversation;
import org.apache.plc4x.java.profinet.dcp.readwrite.*;
import org.apache.plc4x.java.profinet.dcp.readwrite.types.*;
import org.apache.plc4x.java.spi.ConversationContext;
import org.apache.plc4x.java.spi.Plc4xProtocolBase;
import org.apache.plc4x.java.spi.configuration.HasConfiguration;
+import org.apache.plc4x.java.spi.context.DriverContext;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.ReadBuffer;
-import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent;
-import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest;
-import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse;
-import org.apache.plc4x.java.spi.messages.PlcSubscriber;
+import org.apache.plc4x.java.spi.messages.*;
import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
import org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration;
import org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField;
import org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle;
+import org.apache.plc4x.java.spi.transaction.RequestTransactionManager;
import org.apache.plc4x.java.spi.values.PlcNull;
import org.apache.plc4x.java.spi.values.PlcValues;
import org.slf4j.Logger;
@@ -73,8 +75,9 @@ public class ProfinetDCPProtocolLogic extends
Plc4xProtocolBase<BaseEthernetFram
public static final Duration REQUEST_TIMEOUT = Duration.ofMillis(10000);
private final Logger logger =
LoggerFactory.getLogger(ProfinetDCPProtocolLogic.class);
private Map<DefaultPlcConsumerRegistration,
Consumer<PlcSubscriptionEvent>> consumers = new ConcurrentHashMap<>();
+ private RequestTransactionManager tm;
- private AtomicInteger invokeId = new AtomicInteger(0);
+ private AtomicLong invokeId = new AtomicLong(0);
private ProfinetConfiguration configuration;
@Override
@@ -83,26 +86,65 @@ public class ProfinetDCPProtocolLogic extends
Plc4xProtocolBase<BaseEthernetFram
}
@Override
+ public void setDriverContext(DriverContext driverContext) {
+ super.setDriverContext(driverContext);
+
+ this.tm = new RequestTransactionManager();
+ }
+
+ @Override
public void onConnect(ConversationContext<BaseEthernetFrame> context) {
- DCPServiceID serviceId = DCPServiceID.IDENTIFY;
- DCPServiceType serviceType = DCPServiceType.REQUEST;
- long xid = invokeId.incrementAndGet();
- int responseDelay = 1000;
- DCPBlock[] blocks = new DCPBlock[] { new
AllSelector(DCPBlockOption.ALL_SELECTOR)};
-
- DcpIdentRequestPDU requestPDU = new DcpIdentRequestPDU(serviceId,
serviceType, xid, responseDelay, blocks);
- ProfinetFrame profiFrame = new
ProfinetFrame(FrameType.IDENTIFY_MULTICAST_REQUEST, requestPDU);
- EthernetFrame ethernetFrame = new EthernetFrame(PROFINET_BROADCAST,
configuration.getSender(), PN_DCP,
- profiFrame);
- /*TaggedFrame ethernetTaggedFrame = new
TaggedFrame(PROFINET_BROADCAST, configuration.getSender(),
- TypeLAN.VLAN.getValue(),profiFrame,(byte)
0,false,0,TypeLAN.PN_DCP.getValue());
-*/
- // this is broadcast thus reply might come from multiple participants
- context.sendToWire(ethernetFrame);
context.fireConnected();
}
@Override
+ public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest
writeRequest) {
+ CompletableFuture<PlcWriteResponse> response = new
CompletableFuture<>();
+ if (writeRequest.getFieldNames().size() != 1) {
+ response.completeExceptionally(new IllegalArgumentException("You
can write only one field at the time"));
+ return response;
+ }
+
+ PlcField field = writeRequest.getFields().get(0);
+ if (!(field instanceof ProfinetDcpField)) {
+ response.completeExceptionally(new IllegalArgumentException("Only
ProfinetDcpField instances are supported"));
+ return response;
+ }
+
+ if (field instanceof IdentifyRequestField) {
+ writeInternally((DefaultPlcWriteRequest) writeRequest,
(IdentifyRequestField) field, response);
+ return response;
+ }
+
+ response.completeExceptionally(new IllegalArgumentException("Only
IdentifyRequestField instances are supported"));
+ return response;
+ }
+
+ private void writeInternally(DefaultPlcWriteRequest writeRequest,
IdentifyRequestField field, CompletableFuture<PlcWriteResponse> response) {
+ final RequestTransactionManager.RequestTransaction transaction =
tm.startRequest();
+
+ String fieldName = writeRequest.getFieldNames().iterator().next();
+
+ CompletableFuture<PlcResponseCode> callback = new
CompletableFuture<>();
+ callback.whenComplete((code, error) -> {
+ if (error != null) {
+ if (error instanceof PlcException) {
+ response.complete(new
DefaultPlcWriteResponse(writeRequest, Collections.singletonMap(fieldName,
PlcResponseCode.REMOTE_ERROR)));
+ } else {
+ response.complete(new
DefaultPlcWriteResponse(writeRequest, Collections.singletonMap(fieldName,
PlcResponseCode.INTERNAL_ERROR)));
+ }
+ transaction.endRequest();
+ return;
+ }
+ response.complete(new DefaultPlcWriteResponse(writeRequest,
Collections.singletonMap(fieldName, code)));
+ transaction.endRequest();
+ });
+
+ IdentifyRequestConversation conversation = new
IdentifyRequestConversation(this.context, configuration.getSender(),
invokeId::incrementAndGet, field);
+ transaction.submit(() -> conversation.execute(callback));
+ }
+
+ @Override
public void onDisconnect(ConversationContext<BaseEthernetFrame> context) {
context.fireDisconnected();
}
@@ -111,11 +153,13 @@ public class ProfinetDCPProtocolLogic extends
Plc4xProtocolBase<BaseEthernetFram
protected void decode(ConversationContext<BaseEthernetFrame> context,
BaseEthernetFrame msg) throws Exception {
if (msg instanceof TaggedFrame) {
TaggedFrame frame = (TaggedFrame) msg;
- if (frame.getEthernetType() != PN_DCP) {
- logger.trace("Discarding unwanted frame type {}",
frame.getEthernetType());
+ if ((short) frame.getEthernetType() != PN_DCP) {
+ logger.trace("Discarding unwanted tagged frame with type
0x{}", Integer.toHexString(frame.getEthernetType()));
+ return;
}
- } else if (msg.getEtherType() != PN_DCP) {
- logger.trace("Discarding unwanted frame type {}",
msg.getEtherType());
+ } else if ((short) msg.getEtherType() != PN_DCP) {
+ logger.trace("Discarding unwanted ethernet frame type 0x{}",
Integer.toHexString(msg.getEtherType()));
+ return;
}
ProfinetFrame profinetFrame = msg.getPayload();
@@ -129,22 +173,12 @@ public class ProfinetDCPProtocolLogic extends
Plc4xProtocolBase<BaseEthernetFram
if (handle.matches(profinetFrame)) {
logger.trace("Dispatching frame {} to {}", profinetFrame,
handle);
-
- ProfinetDcpField field = handle.getField();
- // todo map actual DCP fields to PlcValues ?
- PlcValue value = PlcValues.of(profinetFrame);
- DefaultPlcSubscriptionEvent event = new
DefaultPlcSubscriptionEvent(
- Instant.now(),
- Collections.singletonMap(
- handle.getName(),
- new ResponseItem<>(PlcResponseCode.OK, value)
- )
- );
- consumer.accept(event);
+ consumer.accept(handle.transform(profinetFrame));
}
}
}
+ /*
if (profinetFrame.getFrameType() == FrameType.IDENTIFY_RESPONSE) {
logger.info("Ident response from Profinet device:");
if (profinetFrame.getFrame() instanceof DcpIdentResponsePDU) {
@@ -165,6 +199,7 @@ public class ProfinetDCPProtocolLogic extends
Plc4xProtocolBase<BaseEthernetFram
logger.error("Unexpected ident response {}",
profinetFrame.getFrame().getClass());
}
}
+ //*/
}
@Override
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/ProfinetDCPSubscriptionHandle.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/ProfinetDCPSubscriptionHandle.java
index 2c3b06e..bdd737e 100644
---
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/ProfinetDCPSubscriptionHandle.java
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/ProfinetDCPSubscriptionHandle.java
@@ -18,13 +18,34 @@
*/
package org.apache.plc4x.java.profinet.dcp.protocol;
+import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.api.value.PlcValue;
+import org.apache.plc4x.java.profinet.dcp.field.IdentifyResponseField;
import org.apache.plc4x.java.profinet.dcp.field.ProfinetDcpField;
-import org.apache.plc4x.java.profinet.dcp.readwrite.ProfinetFrame;
+import org.apache.plc4x.java.profinet.dcp.readwrite.*;
+import org.apache.plc4x.java.profinet.dcp.readwrite.types.DCPBlockOption;
+import org.apache.plc4x.java.profinet.dcp.readwrite.types.FrameType;
+import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent;
import org.apache.plc4x.java.spi.messages.PlcSubscriber;
+import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
import org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle;
+import org.apache.plc4x.java.spi.values.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.time.Instant;
+import java.util.*;
+
+/**
+ * Handle responsible for deciding if received message exchange is matching
and transforming DCP PDU.
+ *
+ * Given that Profinet DCP frames are quite heavy and hard to navigate they
are wrapped into {@link PlcStruct} which is
+ * based on simple map.
+ */
public class ProfinetDCPSubscriptionHandle extends
DefaultPlcSubscriptionHandle {
+ private final Logger logger =
LoggerFactory.getLogger(ProfinetDCPSubscriptionHandle.class);
private final String name;
private final ProfinetDcpField field;
@@ -39,7 +60,24 @@ public class ProfinetDCPSubscriptionHandle extends
DefaultPlcSubscriptionHandle
}
boolean matches(ProfinetFrame pdu) {
- // TODO implement matching logic
+ if (pdu.getFrameType() == FrameType.IDENTIFY_RESPONSE && field
instanceof IdentifyResponseField) {
+ List<DCPBlockOption> fieldBlocks = ((IdentifyResponseField)
field).getBlocks();
+ if (fieldBlocks.contains(DCPBlockOption.ALL_SELECTOR)) {
+ return true;
+ }
+
+ if (pdu.getFrame() instanceof DcpIdentResponsePDU) {
+ DCPBlock[] blocks = ((DcpIdentResponsePDU)
pdu.getFrame()).getBlocks();
+ for (DCPBlock block : blocks) {
+ if (fieldBlocks.contains(block.getBlockType())) {
+ return true;
+ }
+ }
+ }
+ }
+
+ logger.trace("Unmatched profinet identify response {}. Ignoring",
pdu.getFrame());
+ // TODO implement rest of matching logic
return false;
}
@@ -51,4 +89,55 @@ public class ProfinetDCPSubscriptionHandle extends
DefaultPlcSubscriptionHandle
return "ProfinetDCPSubscriptionHandle [service=" + field + "]";
}
+ public PlcSubscriptionEvent transform(ProfinetFrame pdu) {
+ if (pdu.getFrameType() == FrameType.IDENTIFY_RESPONSE) {
+ ProfinetData frame = pdu.getFrame();
+ if (frame instanceof DcpIdentResponsePDU) {
+ DCPBlock[] blocks = ((DcpIdentResponsePDU) frame).getBlocks();
+ Map<String, PlcValue> struct = new LinkedHashMap<>();
+ for (DCPBlock block : blocks) {
+ if (block instanceof IP) {
+ IP ip = (IP) block;
+ struct.put("ip.address",
PlcSTRING.of(addressToString(ip.getIpAddress(), ".")));
+ struct.put("ip.mask",
PlcSTRING.of(addressToString(ip.getSubnetMask(), ".")));
+ struct.put("ip.gateway",
PlcSTRING.of(addressToString(ip.getStandardGateway(), ".")));
+
+ }
+
+ if (block instanceof DeviceProperties) {
+ DeviceProperties wrapper = (DeviceProperties) block;
+ DCPDeviceProperties property = wrapper.getProperties();
+ //System.out.println("Device property " + property + "
" + property.getClass());
+
+ if (property instanceof DeviceOptions) {
+ // todo map it somehow
+ } else if (property instanceof StationType) {
+ struct.put("station.type",
PlcSTRING.of(((StationType) property).getVendorNameForDevice().getText()));
+ } else if (property instanceof StationName) {
+ struct.put("station.name",
PlcSTRING.of(((StationName) property).getName().getText()));
+ } else if (property instanceof DeviceId) {
+ struct.put("device.id", PlcINT.of(((DeviceId)
property).getDeviceId()));
+ } else if (property instanceof DeviceRole) {
+ struct.put("device.role", PlcBYTE.of(((DeviceRole)
property).getRole()));
+ } else if (property instanceof DeviceInstance) {
+ struct.put("device.instance.low",
PlcBYTE.of(((DeviceInstance) property).getInstanceLow()));
+ struct.put("device.instance.high",
PlcBYTE.of(((DeviceInstance) property).getInstanceHigh()));
+ } else {
+ logger.trace("Unsupported device property {}",
property);
+ }
+ }
+ }
+
+ return new DefaultPlcSubscriptionEvent(Instant.now(),
Collections.singletonMap(
+ getName(), new ResponseItem<>(PlcResponseCode.OK, new
PlcStruct(struct)))
+ );
+ }
+ }
+
+ throw new IllegalArgumentException("Frame " + pdu.getFrameType() + "
not supported");
+ }
+
+ private String addressToString(IPv4Address address, String separator) {
+ return address.getOctet1() + separator + address.getOctet2() +
separator + address.getOctet3() + separator + address.getOctet4();
+ }
}
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/conversation/Conversation.java
similarity index 68%
copy from
sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
copy to
sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/conversation/Conversation.java
index 98d92d5..a08188a 100644
---
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/field/ProfinetDcpField.java
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/conversation/Conversation.java
@@ -16,9 +16,17 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.plc4x.java.profinet.dcp.field;
+package org.apache.plc4x.java.profinet.dcp.protocol.conversation;
-import org.apache.plc4x.java.api.model.PlcField;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Definition of conversation which involves one or more request or
request/response packets on the wire.
+ *
+ * @param <T> Type of callback filled by this conversation.
+ */
+public interface Conversation<T> {
+
+ void execute(CompletableFuture<T> callback);
-public class ProfinetDcpField implements PlcField {
}
diff --git
a/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/conversation/IdentifyRequestConversation.java
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/conversation/IdentifyRequestConversation.java
new file mode 100644
index 0000000..9836f25
--- /dev/null
+++
b/sandbox/test-java-profinet-driver/src/main/java/org/apache/plc4x/java/profinet/dcp/protocol/conversation/IdentifyRequestConversation.java
@@ -0,0 +1,74 @@
+/*
+ * 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.profinet.dcp.protocol.conversation;
+
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.profinet.dcp.field.IdentifyRequestField;
+import org.apache.plc4x.java.profinet.dcp.protocol.ProfinetDCPProtocolLogic;
+import org.apache.plc4x.java.profinet.dcp.readwrite.AllSelector;
+import org.apache.plc4x.java.profinet.dcp.readwrite.BaseEthernetFrame;
+import org.apache.plc4x.java.profinet.dcp.readwrite.DCPBlock;
+import org.apache.plc4x.java.profinet.dcp.readwrite.DcpIdentRequestPDU;
+import org.apache.plc4x.java.profinet.dcp.readwrite.EthernetFrame;
+import org.apache.plc4x.java.profinet.dcp.readwrite.MacAddress;
+import org.apache.plc4x.java.profinet.dcp.readwrite.ProfinetFrame;
+import org.apache.plc4x.java.profinet.dcp.readwrite.types.DCPServiceID;
+import org.apache.plc4x.java.profinet.dcp.readwrite.types.DCPServiceType;
+import org.apache.plc4x.java.profinet.dcp.readwrite.types.FrameType;
+import org.apache.plc4x.java.spi.ConversationContext;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+
+/**
+ * Implementation of message exchange needed to cover identify request.
+ *
+ * Since this is broadcast operation overall implementation is quite simple.
+ */
+public class IdentifyRequestConversation implements
Conversation<PlcResponseCode> {
+ private final ConversationContext<BaseEthernetFrame> context;
+ private final MacAddress sender;
+ private final Supplier<Long> xid;
+ private final IdentifyRequestField identifyRequest;
+
+ public IdentifyRequestConversation(ConversationContext<BaseEthernetFrame>
context, MacAddress sender, Supplier<Long> xid, IdentifyRequestField
identifyRequest) {
+ this.context = context;
+ this.sender = sender;
+ this.xid = xid;
+ this.identifyRequest = identifyRequest;
+ }
+
+ public void execute(CompletableFuture<PlcResponseCode> callback) {
+ DCPServiceID serviceId = DCPServiceID.IDENTIFY;
+ DCPServiceType serviceType = DCPServiceType.REQUEST;
+
+ DCPBlock[] blocks =
identifyRequest.getBlocks().stream().map(AllSelector::new).toArray(DCPBlock[]::new);
+
+ DcpIdentRequestPDU requestPDU = new DcpIdentRequestPDU(serviceId,
serviceType, xid.get(), identifyRequest.getResponseDelay(), blocks);
+ ProfinetFrame dcpFrame = new
ProfinetFrame(FrameType.IDENTIFY_MULTICAST_REQUEST, requestPDU);
+ EthernetFrame ethernetFrame = new
EthernetFrame(ProfinetDCPProtocolLogic.PROFINET_BROADCAST, sender,
ProfinetDCPProtocolLogic.PN_DCP, dcpFrame);
+
+ try {
+ context.sendToWire(ethernetFrame);
+ callback.complete(PlcResponseCode.OK);
+ } catch (Exception e) {
+ callback.completeExceptionally(e);
+ }
+ }
+}