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/16 18:44:24 UTC

[incubator-plc4x] branch master updated: fixed DigestUtil under the assumption the crc16 is LittleEndian

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 df13820  fixed DigestUtil under the assumption the crc16 is LittleEndian
df13820 is described below

commit df1382063fbb78f6e4b02a2a998a17601c294eca
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Mar 16 19:44:20 2018 +0100

    fixed DigestUtil under the assumption the crc16 is LittleEndian
---
 .../plc4x/java/ads/protocol/util/DigestUtil.java   |  6 ++---
 ...load2SerialProtocolExampleConversationTest.java | 31 +++++++++++-----------
 2 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
index 79e0117..6305a31 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
@@ -48,12 +48,12 @@ public class DigestUtil {
             currentCrcValue = crc16.update(currentCrcValue, byteReadable.getBytes());
         }
         short finalCrc = crc16.finalCRC16(currentCrcValue);
-        return Short.toUnsignedInt(finalCrc);
+        return Short.toUnsignedInt(Short.reverseBytes(finalCrc));
     }
 
     public static int calculateCrc16(byte[] bytes) {
-        short crc = (short) crc16.calculateCRC(bytes);
-        return Short.toUnsignedInt(crc);
+        short finalCrc = (short) crc16.calculateCRC(bytes);
+        return Short.toUnsignedInt(Short.reverseBytes(finalCrc));
     }
 
 }
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 2eeb1d4..3cf2c38 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
@@ -18,6 +18,8 @@
  */
 package org.apache.plc4x.java.ads.protocol;
 
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.plc4x.java.ads.api.commands.AdsReadRequest;
 import org.apache.plc4x.java.ads.api.commands.AdsReadResponse;
@@ -30,9 +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.ads.api.util.ByteValue;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ErrorCollector;
@@ -41,6 +41,8 @@ import java.util.ArrayList;
 import java.util.Arrays;
 
 import static org.apache.plc4x.java.ads.util.Assert.byteArrayEqualsTo;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
 
 /**
  * @see <a href="https://infosys.beckhoff.com/content/1033/tcadsamsserialspec/html/tcamssericalspec,0xsample.htm?id=60692407917020132">example</a>
@@ -52,12 +54,14 @@ public class Payload2SerialProtocolExampleConversationTest {
 
     private Payload2SerialProtocol SUT;
 
+    private ChannelHandlerContext channelHandlerContextMock;
+
     @Before
     public void setUp() throws Exception {
+        channelHandlerContextMock = mock(ChannelHandlerContext.class, RETURNS_DEEP_STUBS);
         SUT = new Payload2SerialProtocol();
     }
 
-    @Ignore("Either the crc calculation is wrong or the supplied crc are wrong")
     @Test
     public void exampleConversation() throws Exception {
         // 1. Terminal --> PLC : Request of 2 bytre data
@@ -79,7 +83,7 @@ public class Payload2SerialProtocolExampleConversationTest {
             /*Index Gruppe    */    0x05, 0xF0, 0x00, 0x00,
             /*Index Offset    */    0x04, 0x00, 0x00, 0x9D,
             /*Anzahl Byte     */    0x02, 0x00, 0x00, 0x00,
-            /*Checksumme      */    -24, -110,
+            /*Checksumme      */    0x82, 0x97,
         };
         byte[] exampleRequest = ArrayUtils.toPrimitive(Arrays
             .stream(exampleRequestInt)
@@ -96,10 +100,8 @@ public class Payload2SerialProtocolExampleConversationTest {
             Length.of(0x2)
         );
         AmsSerialFrame amsSerialFrame = AmsSerialFrame.of(FragmentNumber.of((byte) 0x06), UserData.of(amsPacket.getBytes()));
-        System.out.println(amsSerialFrame.dump());
-        System.err.println(ByteValue.of(exampleRequest).dump());
-        errorCollector.checkThat(amsSerialFrame.getBytes(), byteArrayEqualsTo(exampleRequest));
-        SUT.encode(null, amsPacket.getByteBuf(), new ArrayList<>());
+        errorCollector.checkThat("example request not same", amsSerialFrame.getBytes(), byteArrayEqualsTo(exampleRequest));
+        SUT.encode(channelHandlerContextMock, amsPacket.getByteBuf(), new ArrayList<>());
 
         // PLC --> Terminal : Acknowledge:
         int[] exampleResponsAckInt = {
@@ -116,10 +118,9 @@ public class Payload2SerialProtocolExampleConversationTest {
             .toArray(Byte[]::new));
 
         AmsSerialAcknowledgeFrame amsSerialAcknowledgeFrame = AmsSerialAcknowledgeFrame.of(amsSerialFrame.getTransmitterAddress(), amsSerialFrame.getReceiverAddress(), amsSerialFrame.getFragmentNumber());
-        System.out.println(amsSerialAcknowledgeFrame.dump());
-        System.err.println(ByteValue.of(exampleAckResponse).dump());
-        errorCollector.checkThat(amsSerialAcknowledgeFrame.getBytes(), byteArrayEqualsTo(exampleAckResponse));
-        SUT.decode(null, amsSerialAcknowledgeFrame.getByteBuf(), new ArrayList<>());
+        errorCollector.checkThat("ack response not same", amsSerialAcknowledgeFrame.getBytes(), byteArrayEqualsTo(exampleAckResponse));
+        SUT.decode(channelHandlerContextMock, Unpooled.wrappedBuffer(exampleAckResponse), new ArrayList<>());
+        SUT.decode(channelHandlerContextMock, amsSerialAcknowledgeFrame.getByteBuf(), new ArrayList<>());
 
         // PLC sends data:
         int[] exampleResponseInt = {
@@ -156,8 +157,8 @@ public class Payload2SerialProtocolExampleConversationTest {
             Data.of((byte) 0xAF, (byte) 0x27)
         );
         AmsSerialFrame amsResponseSerialFrame = AmsSerialFrame.of(FragmentNumber.of((byte) 0xEC), UserData.of(amsResponsePacket.getBytes()));
-        System.out.println(amsResponseSerialFrame.dump());
-        System.err.println(ByteValue.of(exampleResponse).dump());
-        errorCollector.checkThat(amsResponseSerialFrame.getBytes(), byteArrayEqualsTo(exampleResponse));
+        errorCollector.checkThat("read response not same", amsResponseSerialFrame.getBytes(), byteArrayEqualsTo(exampleResponse));
+        SUT.decode(channelHandlerContextMock, Unpooled.wrappedBuffer(exampleResponse), new ArrayList<>());
+        SUT.decode(channelHandlerContextMock, amsResponseSerialFrame.getByteBuf(), new ArrayList<>());
     }
 }

-- 
To stop receiving notification emails like this one, please contact
sruehl@apache.org.