This is an automated email from the ASF dual-hosted git repository. cdutz pushed a commit to branch feature/implement-df1-driver in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 817085a1793afd95c06c6eb1ac139bd5e1519886 Author: Christofer Dutz <[email protected]> AuthorDate: Wed Aug 7 16:58:30 2019 +0200 - Updated the current Netty driver prototype to work with the latest code generation updates. --- plc4j/protocols/pom.xml | 2 +- .../java/org/apache/plc4x/java/df1/Df1Field.java | 18 ++++++++++ .../java/df1/connection/SerialDf1Connection.java | 40 ++++++++++++++++++++-- .../plc4x/java/df1/protocol/Df1Protocol.java | 32 ++++++++--------- .../plc4x/java/df1/protocol/Plc4XDf1Protocol.java | 7 ++-- .../apache/plc4x/java/df1/DF1PlcDriverTest.java | 26 +++++++++++--- 6 files changed, 96 insertions(+), 29 deletions(-) diff --git a/plc4j/protocols/pom.xml b/plc4j/protocols/pom.xml index 6f776b7..1543985 100644 --- a/plc4j/protocols/pom.xml +++ b/plc4j/protocols/pom.xml @@ -36,7 +36,7 @@ <modules> <module>driver-bases</module> - <module>ads</module> + <!--module>ads</module--> <module>delta-v</module> <module>ethernet-ip</module> <module>iso-on-tcp</module> diff --git a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/Df1Field.java b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/Df1Field.java index 43c8e4e..7ca0536 100644 --- a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/Df1Field.java +++ b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/Df1Field.java @@ -1,3 +1,21 @@ +/* + * 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.df1; import org.apache.plc4x.java.api.model.PlcField; diff --git a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/connection/SerialDf1Connection.java b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/connection/SerialDf1Connection.java index de8803e..4a07499 100644 --- a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/connection/SerialDf1Connection.java +++ b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/connection/SerialDf1Connection.java @@ -19,23 +19,59 @@ package org.apache.plc4x.java.df1.connection; import io.netty.channel.*; +import org.apache.commons.lang3.StringUtils; import org.apache.plc4x.java.api.messages.PlcReadRequest; import org.apache.plc4x.java.api.messages.PlcReadResponse; import org.apache.plc4x.java.api.messages.PlcWriteRequest; import org.apache.plc4x.java.api.messages.PlcWriteResponse; +import org.apache.plc4x.java.base.connection.ChannelFactory; import org.apache.plc4x.java.base.connection.SerialChannelFactory; import org.apache.plc4x.java.base.events.ConnectedEvent; import org.apache.plc4x.java.base.messages.*; import org.apache.plc4x.java.df1.protocol.Df1Protocol; import org.apache.plc4x.java.df1.protocol.Plc4XDf1Protocol; import org.apache.plc4x.java.df1.util.Df1FieldHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.CompletableFuture; public class SerialDf1Connection extends BaseDf1Connection implements PlcReader, PlcWriter { + private static final Logger logger = LoggerFactory.getLogger(SerialDf1Connection.class); + + private short localAddr; + private short remoteAddr; + public SerialDf1Connection(String comPortName, String params) { - super(new SerialChannelFactory(comPortName)); + this(new SerialChannelFactory(comPortName), params); + } + + public SerialDf1Connection(ChannelFactory channelFactory, String params) { + super(channelFactory, false); + this.localAddr = (short) 0x00; + this.remoteAddr = (short) 0x09; + + // Override some of the settings, if they are asked for. + if (!StringUtils.isEmpty(params)) { + for (String param : params.split("&")) { + String[] paramElements = param.split("="); + String paramName = paramElements[0]; + if (paramElements.length == 2) { + String paramValue = paramElements[1]; + switch (paramName) { + case "local-addr": + this.localAddr = Short.parseShort(paramValue); + break; + case "remote-addr": + this.remoteAddr = Short.parseShort(paramValue); + break; + default: + logger.debug("Unknown parameter {} with value {}", paramName, paramValue); + } + } + } + } } @Override @@ -65,7 +101,7 @@ public class SerialDf1Connection extends BaseDf1Connection implements PlcReader, } } }); - pipeline.addLast(new Df1Protocol()); + pipeline.addLast(new Df1Protocol(localAddr, remoteAddr)); pipeline.addLast(new Plc4XDf1Protocol()); } }; diff --git a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Df1Protocol.java b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Df1Protocol.java index 92ae07d..9818ceb 100644 --- a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Df1Protocol.java +++ b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Df1Protocol.java @@ -23,6 +23,7 @@ import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandlerContext; import org.apache.plc4x.java.api.exceptions.PlcProtocolException; import org.apache.plc4x.java.base.PlcByteToMessageCodec; +import org.apache.plc4x.java.df1.DF1Command; import org.apache.plc4x.java.df1.DF1Symbol; import org.apache.plc4x.java.df1.DF1SymbolMessageFrame; import org.apache.plc4x.java.df1.DF1UnprotectedReadRequest; @@ -36,17 +37,18 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -public class Df1Protocol extends PlcByteToMessageCodec<DF1Symbol> { +public class Df1Protocol extends PlcByteToMessageCodec<DF1Command> { private static final Logger logger = LoggerFactory.getLogger(Df1Protocol.class); + private final short localAddr; + private final short remoteAddr; private final DF1SymbolIO df1SymbolIO; - private Map<Integer, Short> readRequestSizes; - - public Df1Protocol() { + public Df1Protocol(short localAddr, short remoteAddr) { + this.localAddr = localAddr; + this.remoteAddr = remoteAddr; df1SymbolIO = new DF1SymbolIO(); - readRequestSizes = new HashMap<>(); } @Override @@ -54,21 +56,14 @@ public class Df1Protocol extends PlcByteToMessageCodec<DF1Symbol> { } @Override - protected void encode(ChannelHandlerContext ctx, DF1Symbol msg, ByteBuf out) throws Exception { - // Remember the size of the request as we need this to decode the response. - if(msg instanceof DF1SymbolMessageFrame) { - DF1SymbolMessageFrame frame = (DF1SymbolMessageFrame) msg; - if(frame.getCommand() instanceof DF1UnprotectedReadRequest) { - DF1UnprotectedReadRequest unprotectedReadRequest = (DF1UnprotectedReadRequest) frame.getCommand(); - int transactionCounter = unprotectedReadRequest.getTransactionCounter(); - readRequestSizes.put(transactionCounter, unprotectedReadRequest.getSize()); - } - } + protected void encode(ChannelHandlerContext ctx, DF1Command msg, ByteBuf out) throws Exception { + // Create a new df1 frame for transmitting the command + DF1SymbolMessageFrame frame = new DF1SymbolMessageFrame(remoteAddr, localAddr, msg); // Serialize the message // TODO: Create the buffer with the correct size. WriteBuffer writeBuffer = new WriteBuffer(100); - df1SymbolIO.serialize(writeBuffer, msg); + df1SymbolIO.serialize(writeBuffer, frame); byte[] data = writeBuffer.getData(); // Send the serialized data @@ -106,7 +101,7 @@ public class Df1Protocol extends PlcByteToMessageCodec<DF1Symbol> { break; } case (short) 0x41: { - int transactionCounter = in.getUnsignedShort(6); + /*int transactionCounter = in.getUnsignedShort(6); if(!readRequestSizes.containsKey(transactionCounter)) { logger.warn("Unknown transaction counter: {}", transactionCounter); if (logger.isDebugEnabled()) { @@ -119,7 +114,8 @@ public class Df1Protocol extends PlcByteToMessageCodec<DF1Symbol> { size = readRequestSizes.remove(transactionCounter); if(in.readableBytes() < 8 + size) { return; - } + }*/ + // TODO: Let's just assume all is good for now ... break; } } diff --git a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Plc4XDf1Protocol.java b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Plc4XDf1Protocol.java index 98a7a38..d8be305 100644 --- a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Plc4XDf1Protocol.java +++ b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/protocol/Plc4XDf1Protocol.java @@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -public class Plc4XDf1Protocol extends PlcMessageToMessageCodec<DF1Symbol, PlcRequestContainer> { +public class Plc4XDf1Protocol extends PlcMessageToMessageCodec<DF1Command, PlcRequestContainer> { private static final Logger logger = LoggerFactory.getLogger(Plc4XDf1Protocol.class); @@ -48,8 +48,7 @@ public class Plc4XDf1Protocol extends PlcMessageToMessageCodec<DF1Symbol, PlcReq int transactionId = this.transactionId.getAndIncrement(); logger.debug("Creating request for offset {}, with length {} and transaction id {}", address, size, transactionId); // TODO: differentiate commands - DF1SymbolMessageFrameStart frameStart = new DF1SymbolMessageFrameStart((short) 0x09, (short) 0x00, new DF1ReadRequest((short) 0x00, transactionId, address, size)); - out.add(frameStart); + out.add(new DF1UnprotectedReadRequest((short) 0x00, transactionId, address, size)); } } else { throw new IllegalStateException("This should not happen!"); @@ -57,7 +56,7 @@ public class Plc4XDf1Protocol extends PlcMessageToMessageCodec<DF1Symbol, PlcReq } @Override - protected void decode(ChannelHandlerContext ctx, DF1Symbol msg, List<Object> out) throws Exception { + protected void decode(ChannelHandlerContext ctx, DF1Command msg, List<Object> out) throws Exception { System.out.println("Hello"); } diff --git a/sandbox/test-java-df1-driver/src/test/java/org/apache/plc4x/java/df1/DF1PlcDriverTest.java b/sandbox/test-java-df1-driver/src/test/java/org/apache/plc4x/java/df1/DF1PlcDriverTest.java index ae9e470..fa58a5e 100644 --- a/sandbox/test-java-df1-driver/src/test/java/org/apache/plc4x/java/df1/DF1PlcDriverTest.java +++ b/sandbox/test-java-df1-driver/src/test/java/org/apache/plc4x/java/df1/DF1PlcDriverTest.java @@ -1,12 +1,30 @@ +/* + * 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.df1; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import java.util.regex.Matcher; import static org.apache.plc4x.java.df1.DF1PlcDriver.DF1_URI_PATTERN; import static org.apache.plc4x.java.df1.DF1PlcDriver.SERIAL_PATTERN; -import static org.junit.Assert.*; public class DF1PlcDriverTest { @@ -14,13 +32,13 @@ public class DF1PlcDriverTest { public void matchExpression() { Matcher matcher = SERIAL_PATTERN.matcher("serial:///COM4"); - assertTrue(matcher.matches()); + Assertions.assertTrue(matcher.matches()); } @Test public void matchExpression2() { Matcher matcher = DF1_URI_PATTERN.matcher("df1:serial:///COM4"); - assertTrue(matcher.matches()); + Assertions.assertTrue(matcher.matches()); } } \ No newline at end of file
