You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/03/22 08:50:04 UTC
[incubator-plc4x] branch master updated: + Refactored value
visualization + Added negative test for crc checksum calculation
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/master by this push:
new 31f061f + Refactored value visualization + Added negative test for crc checksum calculation
31f061f is described below
commit 31f061f64de219b646a4b4c5a9c424484a3fc72b
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Mar 22 09:49:58 2018 +0100
+ Refactored value visualization
+ Added negative test for crc checksum calculation
---
.../apache/plc4x/java/ads/api/util/ByteValue.java | 2 +-
.../java/ads/api/util/UnsignedIntLEByteValue.java | 8 +++++-
.../ads/api/util/UnsignedShortLEByteValue.java | 8 ++++--
.../java/ads/protocol/Payload2SerialProtocol.java | 20 +++++++-------
...load2SerialProtocolExampleConversationTest.java | 31 ++++++++++++++++++++++
5 files changed, 55 insertions(+), 14 deletions(-)
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/ByteValue.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/ByteValue.java
index 602bab0..561aba2 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/ByteValue.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/ByteValue.java
@@ -99,6 +99,6 @@ public class ByteValue implements ByteReadable {
@Override
public String toString() {
// TODO: maybe we could find a way to implement this to string
- return getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()) + "{value=" + value.length + "bytes}";
+ return getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()) + "{bytes=" + value.length + "}";
}
}
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedIntLEByteValue.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedIntLEByteValue.java
index 820d9a3..db477b9 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedIntLEByteValue.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedIntLEByteValue.java
@@ -22,6 +22,9 @@ import io.netty.buffer.ByteBuf;
import java.nio.ByteBuffer;
+import static java.lang.Long.toHexString;
+import static org.apache.commons.lang3.StringUtils.leftPad;
+
public abstract class UnsignedIntLEByteValue extends ByteValue {
public static final int UNSIGNED_INT_LE_NUM_BYTES = 4;
@@ -90,6 +93,9 @@ public abstract class UnsignedIntLEByteValue extends ByteValue {
@Override
public String toString() {
- return String.valueOf(getAsLong());
+ return super.toString() + "{" +
+ "longValue=" + getAsLong() +
+ ",hexValue=0x" + leftPad(toHexString(getAsLong()), UNSIGNED_INT_LE_NUM_BYTES, "0") +
+ "} ";
}
}
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedShortLEByteValue.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedShortLEByteValue.java
index 01fe3df..9a43332 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedShortLEByteValue.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/UnsignedShortLEByteValue.java
@@ -22,6 +22,9 @@ import io.netty.buffer.ByteBuf;
import java.nio.ByteBuffer;
+import static java.lang.Integer.toHexString;
+import static org.apache.commons.lang3.StringUtils.leftPad;
+
public abstract class UnsignedShortLEByteValue extends ByteValue {
public static final int UNSIGNED_SHORT_LE_NUM_BYTES = 2;
@@ -88,8 +91,9 @@ public abstract class UnsignedShortLEByteValue extends ByteValue {
@Override
public String toString() {
- return getClass().getSimpleName() + "{" +
+ return super.toString() + "{" +
"intValue=" + getAsInt() +
- "} " + super.toString();
+ ",hexValue=0x" + leftPad(toHexString(getAsInt()), UNSIGNED_SHORT_LE_NUM_BYTES, "0") +
+ "}";
}
}
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Payload2SerialProtocol.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Payload2SerialProtocol.java
index 6326891..7234f37 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Payload2SerialProtocol.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Payload2SerialProtocol.java
@@ -19,6 +19,8 @@
package org.apache.plc4x.java.ads.protocol;
import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import org.apache.plc4x.java.ads.api.serial.AmsSerialAcknowledgeFrame;
@@ -32,9 +34,6 @@ import org.slf4j.LoggerFactory;
import java.util.List;
-import static java.lang.Integer.toHexString;
-import static org.apache.commons.lang3.StringUtils.leftPad;
-
public class Payload2SerialProtocol extends MessageToMessageCodec<ByteBuf, ByteBuf> {
private static final Logger LOGGER = LoggerFactory.getLogger(Payload2TcpProtocol.class);
@@ -69,6 +68,11 @@ public class Payload2SerialProtocol extends MessageToMessageCodec<ByteBuf, ByteB
case AmsSerialFrame.ID:
AmsSerialFrame amsSerialFrame = AmsSerialFrame.of(magicCookie, transmitterAddress, receiverAddress, fragmentNumber, userDataLength, userData, crc);
LOGGER.debug("Ams Serial Frame received {}", amsSerialFrame);
+ // TODO: check if this is the right way to ack a package.
+ ChannelFuture channelFuture = channelHandlerContext.writeAndFlush(AmsSerialAcknowledgeFrame.of(transmitterAddress, receiverAddress, fragmentNumber));
+ channelFuture.addListener((ChannelFutureListener) future -> {
+ // TODO: we might wait for the ack-frame to be transmitted before we forward the package
+ });
out.add(userData.getByteBuf());
break;
case AmsSerialAcknowledgeFrame.ID:
@@ -80,13 +84,9 @@ public class Payload2SerialProtocol extends MessageToMessageCodec<ByteBuf, ByteB
LOGGER.debug("Ams Serial Reset Frame received {}", amsSerialResetFrame);
break;
}
- int calculatedCrc16 = DigestUtil.calculateCrc16(magicCookie, transmitterAddress, receiverAddress, fragmentNumber, userDataLength, userData);
- if (!crc.equals(CRC.of(calculatedCrc16))) {
- throw new PlcProtocolException("CRC checksum wrong. Got "
- + "0x" + leftPad(toHexString(crc.getAsInt()), 4, "0")
- + " expected "
- + "0x" + leftPad(toHexString(calculatedCrc16), 4, "0")
- );
+ CRC calculatedCrc = CRC.of(DigestUtil.calculateCrc16(magicCookie, transmitterAddress, receiverAddress, fragmentNumber, userDataLength, userData));
+ if (!crc.equals(calculatedCrc)) {
+ throw new PlcProtocolException("CRC checksum wrong. Got " + crc + " expected " + calculatedCrc);
}
if (byteBuf.readableBytes() > 0) {
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Payload2SerialProtocolExampleConversationTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Payload2SerialProtocolExampleConversationTest.java
index 3cf2c38..f273fb0 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Payload2SerialProtocolExampleConversationTest.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Payload2SerialProtocolExampleConversationTest.java
@@ -32,6 +32,7 @@ import org.apache.plc4x.java.ads.api.serial.AmsSerialAcknowledgeFrame;
import org.apache.plc4x.java.ads.api.serial.AmsSerialFrame;
import org.apache.plc4x.java.ads.api.serial.types.FragmentNumber;
import org.apache.plc4x.java.ads.api.serial.types.UserData;
+import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -62,6 +63,36 @@ public class Payload2SerialProtocolExampleConversationTest {
SUT = new Payload2SerialProtocol();
}
+ @Test(expected = PlcProtocolException.class)
+ public void testWrongCrc() throws Exception {
+ int[] exampleRequestInt = {
+ /*Magic Cookie */ 0x01, 0xA5,
+ /*Sender */ 0x00,
+ /*Empfaenger */ 0x00,
+ /*Fragmentnummer */ 0x06,
+ /*Datenlaenge */ 0x2C,
+ /*NetID Empfaenger*/ 0xC0, 0xA8, 0x64, 0xAE, 0x01, 0x01,
+ /*Port Nummer */ 0x21, 0x03,
+ /*NetID Sender */ 0xC0, 0xA8, 0x64, 0x9C, 0x01, 0x01,
+ /*Portnummer */ 0x01, 0x80,
+ /*Kommando lesen */ 0x02, 0x00,
+ /*Status */ 0x04, 0x00,
+ /*Anzahl Datenbyte*/ 0x0C, 0x00, 0x00, 0x00,
+ /*Fehlercode */ 0x00, 0x00, 0x00, 0x00,
+ /*InvokeID */ 0x07, 0x00, 0x00, 0x00,
+ /*Index Gruppe */ 0x05, 0xF0, 0x00, 0x00,
+ /*Index Offset */ 0x04, 0x00, 0x00, 0x9D,
+ /*Anzahl Byte */ 0x02, 0x00, 0x00, 0x00,
+ // This Checksum is flipped to provoke exception
+ /*Checksumme */ 0x28, 0x79,
+ };
+ byte[] exampleRequest = ArrayUtils.toPrimitive(Arrays
+ .stream(exampleRequestInt)
+ .mapToObj(value -> (byte) value)
+ .toArray(Byte[]::new));
+ SUT.decode(channelHandlerContextMock, Unpooled.wrappedBuffer(exampleRequest), new ArrayList<>());
+ }
+
@Test
public void exampleConversation() throws Exception {
// 1. Terminal --> PLC : Request of 2 bytre data
--
To stop receiving notification emails like this one, please contact
sruehl@apache.org.